1
Fork 0

roll pitch estimation accelerometer alone

This commit is contained in:
Julian Gaal 2019-04-15 22:14:45 +02:00
parent 8b29b9f2ac
commit 1f76432fa1
4 changed files with 17 additions and 9 deletions

View file

@ -8,3 +8,4 @@ edition = "2018"
i2cdev = "0.4.1" i2cdev = "0.4.1"
embedded-hal = "0.2.2" embedded-hal = "0.2.2"
linux-embedded-hal = "0.2.2" linux-embedded-hal = "0.2.2"
libm = "0.1.2"

View file

@ -13,6 +13,9 @@ cross-compile with `cargo build --bin main --target=arm-unknown-linux-gnueabihf`
- [ ] basic feature support as described [here](https://github.com/Tijndagamer/mpu6050/blob/master/mpu6050/mpu6050.py) - [ ] basic feature support as described [here](https://github.com/Tijndagamer/mpu6050/blob/master/mpu6050/mpu6050.py)
- [x] read gyro data - [x] read gyro data
- [x] read acc data - [x] read acc data
- [x] software calibration
- [x] roll, pitch estimation accelerometer only
- [ ] complementary filter for roll, pitch estimate
- [ ] read temp data - [ ] read temp data
- [ ] timer/clock control with PWR_MGMT_2 - [ ] timer/clock control with PWR_MGMT_2
- [ ] internal clock, register 108 `POWER_MGMT_2`, [will cycle between sleep mode and waking up to take a single sample of data from active sensors at a rate determined by LP_WAKE_CTRL](https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf) (page 41-43) - [ ] internal clock, register 108 `POWER_MGMT_2`, [will cycle between sleep mode and waking up to take a single sample of data from active sensors at a rate determined by LP_WAKE_CTRL](https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf) (page 41-43)

View file

@ -10,10 +10,10 @@ fn main() -> Result<(), Error<LinuxI2CError>> {
let mut mpu = Mpu6050::new(i2c, delay); let mut mpu = Mpu6050::new(i2c, delay);
mpu.init()?; mpu.init()?;
mpu.soft_calibrate(200)?; //mpu.soft_calibrate(200)?;
loop { loop {
match mpu.get_gyro() { match mpu.get_acc_angles() {
Ok(r) => { Ok(r) => {
println!("{:?}", r); println!("{:?}", r);
}, },

View file

@ -1,15 +1,17 @@
#![no_std] #![no_std]
pub mod constants;
///! Mpu6050 sensor driver. ///! Mpu6050 sensor driver.
///! Datasheet: ///! Datasheet:
pub mod constants;
use crate::constants::*;
use libm as m;
use embedded_hal::{ use embedded_hal::{
blocking::delay::DelayMs, blocking::delay::DelayMs,
blocking::i2c::{Read, Write, WriteRead}, blocking::i2c::{Read, Write, WriteRead},
}; };
use crate::constants::*;
#[derive(Default)] #[derive(Default)]
struct Bias { struct Bias {
@ -85,7 +87,7 @@ where
} }
} }
pub fn soft_calibrate(&mut self, steps: u8) -> Result<(), Error<E>> { pub fn soft_calib(&mut self, steps: u8) -> Result<(), Error<E>> {
let mut bias = Bias::default(); let mut bias = Bias::default();
for _ in 0..steps+1 { for _ in 0..steps+1 {
@ -118,10 +120,12 @@ where
Ok(()) Ok(())
} }
/// Implements complementary filter for roll/pitch
/// NOTE: yaw will always point up, sensor has no magnetometer to allow fusion /// NOTE: yaw will always point up, sensor has no magnetometer to allow fusion
pub fn rpy(&mut self) -> Result<(f32, f32, f32), Error<E>> { pub fn get_acc_angles(&mut self) -> Result<(f32, f32), Error<E>> {
Ok((0.0, 0.0, 0.0)) let (ax, ay, az) = self.get_acc()?;
let roll: f32 = m::atan2f(ay, m::sqrtf(m::powf(ax, 2.) + m::powf(az, 2.)));
let pitch: f32 = m::atan2f(-ax, m::sqrtf(m::powf(ay, 2.) + m::powf(az, 2.)));
Ok((roll, pitch))
} }
// TODO work on removing unnecessary type conversion // TODO work on removing unnecessary type conversion