From 929d70a63626945e9498be4a54ab9ae8408efe6b Mon Sep 17 00:00:00 2001 From: juliangaal Date: Wed, 24 Apr 2019 16:02:40 +0200 Subject: [PATCH] adapt recorder and record averaged values --- viz/viz/src/bin/avg_recorder.rs | 64 +++++++++++++++++++++++++++++++++ viz/viz/src/bin/recorder.rs | 10 +++--- 2 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 viz/viz/src/bin/avg_recorder.rs diff --git a/viz/viz/src/bin/avg_recorder.rs b/viz/viz/src/bin/avg_recorder.rs new file mode 100644 index 0000000..7fa0905 --- /dev/null +++ b/viz/viz/src/bin/avg_recorder.rs @@ -0,0 +1,64 @@ +use mpu6050::*; +use linux_embedded_hal::{I2cdev, Delay}; +use i2cdev::linux::LinuxI2CError; +use std::io::prelude::*; +use std::fs::File; +use std::path::Path; +use std::error::Error; + +fn new_file(name: &str) -> File { + let path = Path::new(name); + let display = path.display(); + let _file = match File::create(&path) { + Err(why) => panic!("couldn't create {}: {}", + display, + why.description()), + Ok(file) => return file, + }; +} + +fn write_x_to(file: &mut File, content: String) { + match file.write_all(content.as_bytes()) { + Err(why) => { + println!("couldn't write to file: {}", why.description()); + }, + Ok(_) => {}, + } +} + +fn main() -> Result<(), Mpu6050Error> { + let i2c = I2cdev::new("/dev/i2c-1") + .map_err(Mpu6050Error::I2c)?; + + let delay = Delay; + + let mut mpu = Mpu6050::new(i2c, delay); + mpu.init()?; + mpu.soft_calib(Steps(200))?; + mpu.calc_variance(Steps(200))?; + + println!("Calculated variance: {:?}", mpu.get_variance().unwrap()); + + let mut acc_file = new_file("acc_data_avg.txt"); + let mut gyro_file = new_file("gyro_data_avg.txt"); + let mut temp_file = new_file("temp_data_avg.txt"); + let mut angles_file = new_file("angles_data_avg.txt"); + + loop { + // get roll and pitch estimate + let acc = mpu.get_acc_angles_avg(Steps(5))?; + write_x_to(&mut angles_file, format!("{},{}\n", acc.roll, acc.pitch)); + + // get temp + let temp = mpu.get_temp_avg(Steps(5))?; + write_x_to(&mut temp_file, format!("{}\n", temp)); + + // get gyro data, scaled with sensitivity + let gyro = mpu.get_gyro_avg(Steps(5))?; + write_x_to(&mut gyro_file, format!("{},{},{}\n", gyro.x, gyro.y, gyro.z)); + + // get accelerometer data, scaled with sensitivity + let acc = mpu.get_acc_avg(Steps(5))?; + write_x_to(&mut acc_file, format!("{},{},{}\n", acc.x, acc.y, acc.z)); + } +} diff --git a/viz/viz/src/bin/recorder.rs b/viz/viz/src/bin/recorder.rs index f2ebf27..99cf8d9 100644 --- a/viz/viz/src/bin/recorder.rs +++ b/viz/viz/src/bin/recorder.rs @@ -34,8 +34,8 @@ fn main() -> Result<(), Mpu6050Error> { let mut mpu = Mpu6050::new(i2c, delay); mpu.init()?; - mpu.soft_calib(200)?; - mpu.calc_variance(200)?; + mpu.soft_calib(Steps(200))?; + mpu.calc_variance(Steps(200))?; println!("Calculated variance: {:?}", mpu.get_variance().unwrap()); @@ -47,7 +47,7 @@ fn main() -> Result<(), Mpu6050Error> { loop { // get roll and pitch estimate let acc = mpu.get_acc_angles()?; - write_x_to(&mut angles_file, format!("{},{}\n", acc.0, acc.1)); + write_x_to(&mut angles_file, format!("{},{}\n", acc.roll, acc.pitch)); // get temp let temp = mpu.get_temp()?; @@ -55,10 +55,10 @@ fn main() -> Result<(), Mpu6050Error> { // get gyro data, scaled with sensitivity let gyro = mpu.get_gyro()?; - write_x_to(&mut gyro_file, format!("{},{},{}\n", gyro.0, gyro.1, gyro.2)); + write_x_to(&mut gyro_file, format!("{},{},{}\n", gyro.x, gyro.y, gyro.z)); // get accelerometer data, scaled with sensitivity let acc = mpu.get_acc()?; - write_x_to(&mut acc_file, format!("{},{},{}\n", acc.0, acc.1, acc.2)); + write_x_to(&mut acc_file, format!("{},{},{}\n", acc.x, acc.y, acc.z)); } }