From 1ee669a768794442aadccc8fcece8f162bd0fb8d Mon Sep 17 00:00:00 2001 From: Tbm20Pro Date: Wed, 29 Jan 2025 17:00:10 -0600 Subject: [PATCH 1/4] added laser sensor and intake_coral function --- src/constants.rs | 2 ++ src/subsystems/indexer.rs | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/constants.rs b/src/constants.rs index 8748c87..34c258a 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -24,6 +24,7 @@ pub mod robotmap { pub mod indexer { pub const MOTOR: i32 = 12; + pub const DISTANCE: i32 =0; } pub mod climber { @@ -70,3 +71,4 @@ pub mod elevator { pub const L3: f64 = 0.0; pub const L4: f64 = 39.7; } + diff --git a/src/subsystems/indexer.rs b/src/subsystems/indexer.rs index 67b3aa6..10202c9 100644 --- a/src/subsystems/indexer.rs +++ b/src/subsystems/indexer.rs @@ -1,8 +1,10 @@ use crate::constants::robotmap; use frcrs::ctre::{ControlMode, Talon}; +use frcrs::laser_can::LaserCan; pub struct Indexer { motor: Talon, + laser_can: LaserCan, } impl Default for Indexer { @@ -15,13 +17,20 @@ impl Indexer { pub fn new() -> Self { let motor = Talon::new(robotmap::indexer::MOTOR, None); - Self { motor } + Self { motor, laser_can } } pub fn set_speed(&self, speed: f64) { self.motor.set(ControlMode::Percent, speed); } + pub async fn intake_coral(&self) { + while self.laser_can.get_measurement() > robotmap::indexer::DISTANCE { + self.motor.set(ControlMode::Percent, 1.0); //may be -1 + } + self.motor.stop(); + } + pub fn stop(&self) { self.motor.stop(); } From 691ba5fc43da10e0de4a0c6c75454ee68c00b6be Mon Sep 17 00:00:00 2001 From: Tbm20Pro Date: Fri, 31 Jan 2025 16:40:49 -0600 Subject: [PATCH 2/4] did cargo fmt --- src/constants.rs | 3 +-- src/subsystems/indexer.rs | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 34c258a..1098dba 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -24,7 +24,7 @@ pub mod robotmap { pub mod indexer { pub const MOTOR: i32 = 12; - pub const DISTANCE: i32 =0; + pub const DISTANCE: i32 = 0; } pub mod climber { @@ -71,4 +71,3 @@ pub mod elevator { pub const L3: f64 = 0.0; pub const L4: f64 = 39.7; } - diff --git a/src/subsystems/indexer.rs b/src/subsystems/indexer.rs index 10202c9..523610e 100644 --- a/src/subsystems/indexer.rs +++ b/src/subsystems/indexer.rs @@ -26,7 +26,7 @@ impl Indexer { pub async fn intake_coral(&self) { while self.laser_can.get_measurement() > robotmap::indexer::DISTANCE { - self.motor.set(ControlMode::Percent, 1.0); //may be -1 + self.motor.set(ControlMode::Percent, 1.0); //may be -1 need to test } self.motor.stop(); } From 16da24330f4ff7d09c5ef2553b87ba0ea093d6cf Mon Sep 17 00:00:00 2001 From: Nolan Peterson Date: Sat, 1 Feb 2025 18:53:18 -0700 Subject: [PATCH 3/4] added hz limiter to laser can intake function --- src/subsystems/indexer.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/subsystems/indexer.rs b/src/subsystems/indexer.rs index 523610e..7493e54 100644 --- a/src/subsystems/indexer.rs +++ b/src/subsystems/indexer.rs @@ -1,6 +1,8 @@ +use std::time::Duration; use crate::constants::robotmap; use frcrs::ctre::{ControlMode, Talon}; use frcrs::laser_can::LaserCan; +use tokio::time::{Instant, sleep}; pub struct Indexer { motor: Talon, @@ -25,8 +27,16 @@ impl Indexer { } pub async fn intake_coral(&self) { + let mut last_loop = std::time::Instant::now(); + while self.laser_can.get_measurement() > robotmap::indexer::DISTANCE { self.motor.set(ControlMode::Percent, 1.0); //may be -1 need to test + + // Cap at 250 hz + let elapsed = last_loop.elapsed().as_secs_f64(); + let left = (1. / 250. - elapsed).max(0.); + sleep(Duration::from_secs_f64(left)).await; + last_loop = std::time::Instant::now(); } self.motor.stop(); } From 623890e5b81b0f191e14876d80b1990ca3d2e721 Mon Sep 17 00:00:00 2001 From: Nolan Peterson Date: Sat, 1 Feb 2025 18:54:36 -0700 Subject: [PATCH 4/4] cargo fmt --- src/subsystems/indexer.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/subsystems/indexer.rs b/src/subsystems/indexer.rs index 7493e54..474e817 100644 --- a/src/subsystems/indexer.rs +++ b/src/subsystems/indexer.rs @@ -1,8 +1,9 @@ -use std::time::Duration; use crate::constants::robotmap; use frcrs::ctre::{ControlMode, Talon}; use frcrs::laser_can::LaserCan; -use tokio::time::{Instant, sleep}; +use std::time::Duration; +use std::time::Instant; +use tokio::time::sleep; pub struct Indexer { motor: Talon, @@ -27,7 +28,7 @@ impl Indexer { } pub async fn intake_coral(&self) { - let mut last_loop = std::time::Instant::now(); + let mut last_loop = Instant::now(); while self.laser_can.get_measurement() > robotmap::indexer::DISTANCE { self.motor.set(ControlMode::Percent, 1.0); //may be -1 need to test @@ -36,7 +37,7 @@ impl Indexer { let elapsed = last_loop.elapsed().as_secs_f64(); let left = (1. / 250. - elapsed).max(0.); sleep(Duration::from_secs_f64(left)).await; - last_loop = std::time::Instant::now(); + last_loop = Instant::now(); } self.motor.stop(); }