Skip to content

Commit

Permalink
Add call to gyro demo function
Browse files Browse the repository at this point in the history
  • Loading branch information
samderanova committed May 18, 2024
1 parent 2cdf0ec commit a0fbb11
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 49 deletions.
30 changes: 15 additions & 15 deletions pod-operation/src/components/gyro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ use mpu6050::Mpu6050;
use rppal::i2c::I2c;

pub struct Gyroscope {
mpu6050: Mpu6050<I2c>,
mpu6050: Mpu6050<I2c>,
}

pub struct Orientation {
pub pitch: f32,
pub roll: f32,
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 new() -> Self {
let i2c = I2c::new().unwrap();
let mpu6050 = Mpu6050::new(i2c);
Gyroscope { mpu6050 }
}

pub fn read_orientation(&mut self) -> Orientation {
let acc = self.mpu6050.get_acc_angles().unwrap();
Orientation {
pitch: acc[0],
roll: acc[1],
}
}
pub fn read_orientation(&mut self) -> Orientation {
let acc = self.mpu6050.get_acc_angles().unwrap();
Orientation {
pitch: acc[0],
roll: acc[1],
}
}
}
53 changes: 28 additions & 25 deletions pod-operation/src/demo.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use tracing::info;

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::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;
Expand All @@ -31,7 +31,7 @@ pub async fn read_pressure_transducer(mut pressure_transducer: PressureTransduce
}

pub async fn read_ads1015(mut lim_temperature: LimTemperature) {
info!("Starting ADS1015 Demo.");
info!("Starting ADS1015 Demo.");

let mut i = 0;
loop {
Expand All @@ -43,16 +43,19 @@ pub async fn read_ads1015(mut lim_temperature: LimTemperature) {
}
}

lim_temperature.cleanup();
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);
}
});
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
);
}
});
}
21 changes: 12 additions & 9 deletions pod-operation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use crate::state_machine::StateMachine;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
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));
Expand All @@ -33,20 +33,23 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let ads1015 = LimTemperature::new(ads1x1x::SlaveAddr::Default);
tokio::spawn(demo::read_ads1015(ads1015));

let gyro: Gyroscope = Gyroscope::new();
tokio::spawn(demo::read_gyroscope(gyro));

tokio::spawn(async {
let mut state_machine = StateMachine::new(io);
state_machine.run().await;
});

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(())
}

0 comments on commit a0fbb11

Please sign in to comment.