From 8311075e1aab0879ecca1873a57191e086939b9b Mon Sep 17 00:00:00 2001 From: rishi-j123 Date: Wed, 29 Jan 2025 17:58:42 -0600 Subject: [PATCH 1/4] LED Subsystem --- src/constants.rs | 5 +++++ src/subsystems/mod.rs | 1 + 2 files changed, 6 insertions(+) diff --git a/src/constants.rs b/src/constants.rs index 8748c87..4dd9cc1 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -30,6 +30,11 @@ pub mod robotmap { pub const RAISE: i32 = 0; pub const GRAB: i32 = 1; } + + pub mod led { + pub const PORT: i32 = 1; + pub const COUNT: i32 = 0; + } } // TODO: get 2025 field dimensions diff --git a/src/subsystems/mod.rs b/src/subsystems/mod.rs index 4062420..bd649fb 100644 --- a/src/subsystems/mod.rs +++ b/src/subsystems/mod.rs @@ -3,6 +3,7 @@ mod drivetrain; mod elevator; mod indexer; mod vision; +mod led; pub use climber::*; pub use drivetrain::*; From 1729a6a1349ecce90e3b0f6b0634de5432e1ff39 Mon Sep 17 00:00:00 2001 From: rishi-j123 Date: Thu, 30 Jan 2025 14:23:01 -0600 Subject: [PATCH 2/4] led subsystem --- src/subsystems/led.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/subsystems/mod.rs | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 src/subsystems/led.rs diff --git a/src/subsystems/led.rs b/src/subsystems/led.rs new file mode 100644 index 0000000..efbb988 --- /dev/null +++ b/src/subsystems/led.rs @@ -0,0 +1,42 @@ +use crate::constants::robotmap; +use ::frcrs::led::Led; + +pub struct led { + led: Led, +} + +impl led { + pub fn init() -> Self { + let led = Led::new(robotmap::led::PORT, robotmap::led::COUNT); + Self { led } + } + + pub fn set(&self, idx: i32, r: i32, g: i32, b: i32) { + self.led.set_rgb(idx, r, g, b); + } + + pub fn flush(&self) { + self.led.flush(); + } + + //level = what branch level robot is at, set led strip to some fraction of total depending on branch + pub async fn elevator_led_update(&self, level: i32) { + self.led.flush().await; + + match level { + 1 => self.solid_height(1), + 2 => self.solid_height(2), + 3 => self.solid_height(3), + 4 => self.solid_height(4), + _ => println!("level {} not supported", level), + } + } + + fn solid_height(&self, level: i32) { + for i in { robotmap::led::COUNT } * { level / 4 } { + self.led.set_rgb(i, 0, 255, 0); + } + } + //want to make fn that will adjust ledstrip according to reefside limelight detections + //pub fn limelight_led_update(&self, ) {} +} diff --git a/src/subsystems/mod.rs b/src/subsystems/mod.rs index bd649fb..f69dbdf 100644 --- a/src/subsystems/mod.rs +++ b/src/subsystems/mod.rs @@ -2,8 +2,8 @@ mod climber; mod drivetrain; mod elevator; mod indexer; -mod vision; mod led; +mod vision; pub use climber::*; pub use drivetrain::*; From 428e45dfb0857706666904b1f72326496f1e6482 Mon Sep 17 00:00:00 2001 From: Nolan Peterson Date: Sat, 1 Feb 2025 18:43:15 -0700 Subject: [PATCH 3/4] updated led framework --- src/subsystems/led.rs | 70 ++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/src/subsystems/led.rs b/src/subsystems/led.rs index efbb988..cc7f095 100644 --- a/src/subsystems/led.rs +++ b/src/subsystems/led.rs @@ -1,42 +1,64 @@ +use std::cell::RefCell; +use std::rc::Rc; +use std::sync::{Arc, Mutex}; +use std::time::Duration; use crate::constants::robotmap; use ::frcrs::led::Led; +use tokio::task::{AbortHandle, spawn_local}; +use tokio::time::sleep; -pub struct led { +pub enum LedStatus { + Disabled +} + +#[derive(Clone)] +pub struct LedSubsystem { led: Led, + handle: Option, } -impl led { +impl LedSubsystem { pub fn init() -> Self { let led = Led::new(robotmap::led::PORT, robotmap::led::COUNT); - Self { led } - } - pub fn set(&self, idx: i32, r: i32, g: i32, b: i32) { - self.led.set_rgb(idx, r, g, b); + Self { + led, + handle: None + } } - pub fn flush(&self) { - self.led.flush(); - } + pub fn set_state(&mut self, state: LedStatus) { + if let Some(handle) = self.handle.take() { + handle.abort(); + } - //level = what branch level robot is at, set led strip to some fraction of total depending on branch - pub async fn elevator_led_update(&self, level: i32) { - self.led.flush().await; + let mut led_clone = self.led.clone(); - match level { - 1 => self.solid_height(1), - 2 => self.solid_height(2), - 3 => self.solid_height(3), - 4 => self.solid_height(4), - _ => println!("level {} not supported", level), - } + let future = match state { + LedStatus::Disabled => { + async move { + LedSubsystem::disabled(&mut led_clone).await; + } + } + }; + + let handle = spawn_local(future).abort_handle(); + + self.handle = Some(handle); } - fn solid_height(&self, level: i32) { - for i in { robotmap::led::COUNT } * { level / 4 } { - self.led.set_rgb(i, 0, 255, 0); + async fn disabled(led: &mut Led) { + loop { + for i in (0..=robotmap::led::COUNT).step_by(2) { + led.set_rgb(i, 255, 0, 0); + } + + for i in (1..=robotmap::led::COUNT).step_by(2) { + led.set_rgb(i, 0, 0, 0); + } + + led.set_data(); + sleep(Duration::from_secs_f64(0.5)).await; } } - //want to make fn that will adjust ledstrip according to reefside limelight detections - //pub fn limelight_led_update(&self, ) {} } From a6326522657958d3656037f8e1442eba863d773c Mon Sep 17 00:00:00 2001 From: Nolan Peterson Date: Sat, 1 Feb 2025 18:44:51 -0700 Subject: [PATCH 4/4] added current state tracking --- src/subsystems/led.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/subsystems/led.rs b/src/subsystems/led.rs index cc7f095..ba0bb96 100644 --- a/src/subsystems/led.rs +++ b/src/subsystems/led.rs @@ -1,4 +1,5 @@ use std::cell::RefCell; +use std::cmp::PartialEq; use std::rc::Rc; use std::sync::{Arc, Mutex}; use std::time::Duration; @@ -7,6 +8,7 @@ use ::frcrs::led::Led; use tokio::task::{AbortHandle, spawn_local}; use tokio::time::sleep; +#[derive(Clone, PartialEq)] pub enum LedStatus { Disabled } @@ -15,6 +17,7 @@ pub enum LedStatus { pub struct LedSubsystem { led: Led, handle: Option, + current_state: LedStatus, } impl LedSubsystem { @@ -23,11 +26,18 @@ impl LedSubsystem { Self { led, - handle: None + handle: None, + current_state: LedStatus::Disabled, } } pub fn set_state(&mut self, state: LedStatus) { + if state == self.current_state { + return; + } else { + self.current_state = state; + } + if let Some(handle) = self.handle.take() { handle.abort(); }