diff --git a/pod-operation/src/components/gyro.rs b/pod-operation/src/components/gyro.rs index 725af89d..48d5440b 100644 --- a/pod-operation/src/components/gyro.rs +++ b/pod-operation/src/components/gyro.rs @@ -1,19 +1,27 @@ use mpu6050::Mpu6050; use rppal::i2c::I2c; -pub struct Mpu6050Sensor { - mpu6050: Mpu6050, +pub struct Gyroscope { + mpu6050: Mpu6050, } -impl Mpu6050Sensor { - pub fn new() -> Self { - let i2c = I2c::new().unwrap(); - let mpu6050 = Mpu6050::new(i2c); - Mpu6050Sensor { mpu6050 } - } +pub struct Orientation { + pub pitch: f32, + pub roll: f32, +} + +impl Gyroscope { + pub fn new() -> Self { + let i2c = I2c::new().unwrap(); + let mpu6050 = Mpu6050::new(i2c); + Gyroscope { mpu6050 } + } - pub fn read_accel_gyro(&mut self) -> (f32, f32) { - let acc = self.mpu6050.get_acc_angles().unwrap(); - (acc[0], acc[1]) - } + pub fn read_orientation(&mut self) -> Orientation { + let acc = self.mpu6050.get_acc_angles().unwrap(); + Orientation { + pitch: acc[0], + roll: acc[1], + } + } } diff --git a/pod-operation/src/demo.rs b/pod-operation/src/demo.rs index 2392c086..d16f5e4d 100644 --- a/pod-operation/src/demo.rs +++ b/pod-operation/src/demo.rs @@ -3,44 +3,56 @@ use tracing::info; use crate::components::lim_temperature::LimTemperature; use crate::components::pressure_transducer::PressureTransducer; use crate::components::signal_light::SignalLight; +use crate::components::gyro::Gyroscope; pub async fn blink(mut signal_light: SignalLight) { - let mut i = 0; - - info!("Starting blink demo."); - loop { - tokio::time::sleep(std::time::Duration::from_millis(500)).await; - if i % 4 == 0 { - signal_light.enable(); - } else if i % 4 == 1 { - signal_light.disable(); - } - - i += 1; - } + let mut i = 0; + + info!("Starting blink demo."); + loop { + tokio::time::sleep(std::time::Duration::from_millis(500)).await; + if i % 4 == 0 { + signal_light.enable(); + } else if i % 4 == 1 { + signal_light.disable(); + } + + i += 1; + } } pub async fn read_pressure_transducer(mut pressure_transducer: PressureTransducer) { - info!("Starting pressure transducer demo."); + info!("Starting pressure transducer demo."); - loop { - tokio::time::sleep(std::time::Duration::new(1, 0)).await; - println!("{:?}", pressure_transducer.read()); - } + loop { + tokio::time::sleep(std::time::Duration::new(1, 0)).await; + println!("{:?}", pressure_transducer.read()); + } } pub async fn read_ads1015(mut lim_temperature: LimTemperature) { - info!("Starting ADS1015 Demo."); - - let mut i = 0; - loop { - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - println!("{:?}", lim_temperature.read_pins()); - i += 1; - if i > 1000 { - break; - } - } - - lim_temperature.cleanup(); + info!("Starting ADS1015 Demo."); + + let mut i = 0; + loop { + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + println!("{:?}", lim_temperature.read_pins()); + i += 1; + if i > 1000 { + break; + } + } + + lim_temperature.cleanup(); +} + +pub async fn read_gyroscope(mut gyroscope: Gyroscope) { + info!("Starting Gyroscope Demo."); + tokio::spawn(async move { + loop { + let orientation = gyroscope.read_orientation(); + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + println!("Pitch: {:?}, Roll: {:?}", orientation.pitch, orientation.roll); + } + }); } diff --git a/pod-operation/src/main.rs b/pod-operation/src/main.rs index 60a35a77..d459f4f4 100644 --- a/pod-operation/src/main.rs +++ b/pod-operation/src/main.rs @@ -7,48 +7,41 @@ mod components; mod demo; mod state_machine; -use crate::components::gyro::Mpu6050Sensor; +use crate::components::gyro::Gyroscope; use crate::components::lim_temperature::LimTemperature; use crate::components::pressure_transducer::PressureTransducer; use crate::components::signal_light::SignalLight; use crate::state_machine::StateMachine; + #[tokio::main] async fn main() -> Result<(), Box> { - tracing::subscriber::set_global_default(FmtSubscriber::default())?; + tracing::subscriber::set_global_default(FmtSubscriber::default())?; - let (layer, io) = SocketIo::new_layer(); + let (layer, io) = SocketIo::new_layer(); - // let signal_light = SignalLight::new(); - // tokio::spawn(demo::blink(signal_light)); + let signal_light = SignalLight::new(); + tokio::spawn(demo::blink(signal_light)); - // let pressure_transducer = PressureTransducer::new(0x40); - // tokio::spawn(demo::read_pressure_transducer(pressure_transducer)); + let pressure_transducer = PressureTransducer::new(0x40); + tokio::spawn(demo::read_pressure_transducer(pressure_transducer)); - // let ads1015 = LimTemperature::new(ads1x1x::SlaveAddr::Default); - // tokio::spawn(demo::read_ads1015(ads1015)); + let ads1015 = LimTemperature::new(ads1x1x::SlaveAddr::Default); + tokio::spawn(demo::read_ads1015(ads1015)); - // tokio::spawn(async { - // let mut state_machine = StateMachine::new(io); - // state_machine.run().await; - // }); + tokio::spawn(async { + let mut state_machine = StateMachine::new(io); + state_machine.run().await; + }); - let mut mpu6050_sensor = Mpu6050Sensor::new(); - tokio::spawn(async move { - loop { - let (pitch, roll) = mpu6050_sensor.read_accel_gyro(); - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - println!("[{:?} {:?}]", pitch, roll); - } - }); - let app = axum::Router::new().layer(layer); + let app = axum::Router::new().layer(layer); - info!("Starting server on port 5000"); + info!("Starting server on port 5000"); - let server = Server::bind(&"127.0.0.1:5000".parse().unwrap()).serve(app.into_make_service()); + let server = Server::bind(&"127.0.0.1:5000".parse().unwrap()).serve(app.into_make_service()); - if let Err(e) = server.await { - error!("server error: {}", e); - } + if let Err(e) = server.await { + error!("server error: {}", e); + } - Ok(()) + Ok(()) }