Merge branch 'master' of github.com:juliangaal/mpu6050 into viz
This commit is contained in:
commit
e6814e616e
4 changed files with 83 additions and 71 deletions
41
README.md
41
README.md
|
@ -1,4 +1,5 @@
|
||||||
# MPU 6050 Rust Driver <img align="right" width="250" height="190" src="https://www.invensense.com/wp-content/uploads/2015/01/rp-mpu-6500.png">
|
# MPU 6050 Rust Driver
|
||||||
|
<img align="right" width="250" height="190" src="https://www.invensense.com/wp-content/uploads/2015/01/rp-mpu-6500.png">
|
||||||
|
|
||||||
Platform agnostic driver for [MPU 6050 6-axis IMU](https://www.invensense.com/products/motion-tracking/6-axis/mpu-6500/) using [`embedded_hal`](https://github.com/rust-embedded/embedded-hal).
|
Platform agnostic driver for [MPU 6050 6-axis IMU](https://www.invensense.com/products/motion-tracking/6-axis/mpu-6500/) using [`embedded_hal`](https://github.com/rust-embedded/embedded-hal).
|
||||||
|
|
||||||
|
@ -15,7 +16,7 @@ use linux_embedded_hal::{I2cdev, Delay};
|
||||||
use i2cdev::linux::LinuxI2CError;
|
use i2cdev::linux::LinuxI2CError;
|
||||||
|
|
||||||
fn main() -> Result<(), Error<LinuxI2CError>> {
|
fn main() -> Result<(), Error<LinuxI2CError>> {
|
||||||
let i2c = I2cdev::new("/dev/i2c-1") // or privide your owm on different platforms
|
let i2c = I2cdev::new("/dev/i2c-1")
|
||||||
.map_err(Error::I2c)?;
|
.map_err(Error::I2c)?;
|
||||||
|
|
||||||
let delay = Delay;
|
let delay = Delay;
|
||||||
|
@ -25,40 +26,26 @@ fn main() -> Result<(), Error<LinuxI2CError>> {
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// get roll and pitch estimate
|
// get roll and pitch estimate
|
||||||
match mpu.get_acc_angles() {
|
let acc = mpu.get_acc_angles()?;
|
||||||
Ok(r) => {
|
println!("r/p: {:?}", acc);
|
||||||
println!("r/p: {:?}", r);
|
|
||||||
},
|
|
||||||
Err(_) => {} ,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get temp
|
// get temp
|
||||||
match mpu.get_temp() {
|
let temp = mpu.get_temp()?;
|
||||||
Ok(r) => {
|
println!("temp: {}c", temp);
|
||||||
println!("temp: {}c", r);
|
|
||||||
},
|
|
||||||
Err(_) => {} ,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get gyro data, scaled with sensitivity
|
// get gyro data, scaled with sensitivity
|
||||||
match mpu.get_gyro() {
|
let gyro = mpu.get_gyro()?;
|
||||||
Ok(r) => {
|
println!("gyro: {:?}", gyro);
|
||||||
println!("gyro: {:?}", r);
|
|
||||||
},
|
|
||||||
Err(_) => {} ,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get accelerometer data, scaled with sensitivity
|
// get accelerometer data, scaled with sensitivity
|
||||||
match mpu.get_acc() {
|
let acc = mpu.get_acc()?;
|
||||||
Ok(r) => {
|
println!("acc: {:?}", acc);
|
||||||
println!("acc: {:?}", r);
|
|
||||||
},
|
|
||||||
Err(_) => {} ,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
#### Compile linux example (Rapsberry Pi 3B)
|
*Note*: this example uses API of version published on crates.io, not local master branch.
|
||||||
|
|
||||||
|
#### Compile linux example (Raspberry Pi 3B)
|
||||||
files [here](https://github.com/juliangaal/mpu6050/blob/master/example/)
|
files [here](https://github.com/juliangaal/mpu6050/blob/master/example/)
|
||||||
|
|
||||||
Requirements:
|
Requirements:
|
||||||
|
|
|
@ -10,39 +10,22 @@ 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_calib(200)?;
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// get roll and pitch estimate
|
// get roll and pitch estimate
|
||||||
match mpu.get_acc_angles() {
|
let acc = mpu.get_acc_angles()?;
|
||||||
Ok(r) => {
|
println!("r/p: {:?}", acc);
|
||||||
println!("r/p: {:?}", r);
|
|
||||||
},
|
|
||||||
Err(_) => {} ,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get temp
|
// get temp
|
||||||
match mpu.get_temp() {
|
let temp = mpu.get_temp()?;
|
||||||
Ok(r) => {
|
println!("temp: {}c", temp);
|
||||||
println!("temp: {}c", r);
|
|
||||||
},
|
|
||||||
Err(_) => {} ,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get gyro data, scaled with sensitivity
|
// get gyro data, scaled with sensitivity
|
||||||
match mpu.get_gyro() {
|
let gyro = mpu.get_gyro()?;
|
||||||
Ok(r) => {
|
println!("gyro: {:?}", gyro);
|
||||||
println!("gyro: {:?}", r);
|
|
||||||
},
|
|
||||||
Err(_) => {} ,
|
|
||||||
}
|
|
||||||
|
|
||||||
// get accelerometer data, scaled with sensitivity
|
// get accelerometer data, scaled with sensitivity
|
||||||
match mpu.get_acc() {
|
let acc = mpu.get_acc()?;
|
||||||
Ok(r) => {
|
println!("acc: {:?}", acc);
|
||||||
println!("acc: {:?}", r);
|
|
||||||
},
|
|
||||||
Err(_) => {} ,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
38
src/lib.rs
38
src/lib.rs
|
@ -1,12 +1,12 @@
|
||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
pub mod constants;
|
pub mod registers;
|
||||||
|
|
||||||
///! Mpu6050 sensor driver.
|
///! Mpu6050 sensor driver.
|
||||||
///! Register sheet: https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf
|
///! Register sheet: https://www.invensense.com/wp-content/uploads/2015/02/MPU-6000-Register-Map1.pdf
|
||||||
///! Data sheet: https://www.invensense.com/wp-content/uploads/2015/02/MPU-6500-Datasheet2.pdf
|
///! Data sheet: https://www.invensense.com/wp-content/uploads/2015/02/MPU-6500-Datasheet2.pdf
|
||||||
|
|
||||||
use crate::constants::*;
|
use crate::registers::*;
|
||||||
use libm::{powf, atan2f, sqrtf};
|
use libm::{powf, atan2f, sqrtf};
|
||||||
use embedded_hal::{
|
use embedded_hal::{
|
||||||
blocking::delay::DelayMs,
|
blocking::delay::DelayMs,
|
||||||
|
@ -96,7 +96,7 @@ pub enum GyroRange {
|
||||||
|
|
||||||
/// All possible errors in this crate
|
/// All possible errors in this crate
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error<E> {
|
pub enum Mpu6050Error<E> {
|
||||||
/// I2C bus error
|
/// I2C bus error
|
||||||
I2c(E),
|
I2c(E),
|
||||||
|
|
||||||
|
@ -142,7 +142,7 @@ where
|
||||||
|
|
||||||
/// Performs software calibration with steps number of readings.
|
/// Performs software calibration with steps number of readings.
|
||||||
/// Readings must be made with MPU6050 in resting position
|
/// Readings must be made with MPU6050 in resting position
|
||||||
pub fn soft_calib(&mut self, steps: u8) -> Result<(), Error<E>> {
|
pub fn soft_calib(&mut self, steps: u8) -> Result<(), Mpu6050Error<E>> {
|
||||||
let mut bias = Bias::default();
|
let mut bias = Bias::default();
|
||||||
|
|
||||||
for _ in 0..steps+1 {
|
for _ in 0..steps+1 {
|
||||||
|
@ -156,31 +156,31 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wakes MPU6050 with all sensors enabled (default)
|
/// Wakes MPU6050 with all sensors enabled (default)
|
||||||
pub fn wake(&mut self) -> Result<(), Error<E>> {
|
pub fn wake(&mut self) -> Result<(), Mpu6050Error<E>> {
|
||||||
self.write_u8(POWER_MGMT_1, 0)?;
|
self.write_u8(POWER_MGMT_1, 0)?;
|
||||||
self.delay.delay_ms(100u8);
|
self.delay.delay_ms(100u8);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Init wakes MPU6050 and verifies register addr, e.g. in i2c
|
/// Init wakes MPU6050 and verifies register addr, e.g. in i2c
|
||||||
pub fn init(&mut self) -> Result<(), Error<E>> {
|
pub fn init(&mut self) -> Result<(), Mpu6050Error<E>> {
|
||||||
self.wake()?;
|
self.wake()?;
|
||||||
self.verify()?;
|
self.verify()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Verifies device to address 0x68 with WHOAMI Register
|
/// Verifies device to address 0x68 with WHOAMI Register
|
||||||
pub fn verify(&mut self) -> Result<(), Error<E>> {
|
pub fn verify(&mut self) -> Result<(), Mpu6050Error<E>> {
|
||||||
let address = self.read_u8(WHOAMI)?;
|
let address = self.read_u8(WHOAMI)?;
|
||||||
if address != SLAVE_ADDR {
|
if address != SLAVE_ADDR {
|
||||||
return Err(Error::InvalidChipId(address));
|
return Err(Mpu6050Error::InvalidChipId(address));
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Roll and pitch estimation from raw accelerometer readings
|
/// Roll and pitch estimation from raw accelerometer readings
|
||||||
/// NOTE: no yaw! no magnetometer present on MPU6050
|
/// NOTE: no yaw! no magnetometer present on MPU6050
|
||||||
pub fn get_acc_angles(&mut self) -> Result<(f32, f32), Error<E>> {
|
pub fn get_acc_angles(&mut self) -> Result<(f32, f32), Mpu6050Error<E>> {
|
||||||
let (ax, ay, az) = self.get_acc()?;
|
let (ax, ay, az) = self.get_acc()?;
|
||||||
let roll: f32 = atan2f(ay, sqrtf(powf(ax, 2.) + powf(az, 2.)));
|
let roll: f32 = atan2f(ay, sqrtf(powf(ax, 2.) + powf(az, 2.)));
|
||||||
let pitch: f32 = atan2f(-ax, sqrtf(powf(ay, 2.) + powf(az, 2.)));
|
let pitch: f32 = atan2f(-ax, sqrtf(powf(ay, 2.) + powf(az, 2.)));
|
||||||
|
@ -202,7 +202,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads rotation (gyro/acc) from specified register
|
/// Reads rotation (gyro/acc) from specified register
|
||||||
fn read_rot(&mut self, reg: u8) -> Result<(f32, f32, f32), Error<E>> {
|
fn read_rot(&mut self, reg: u8) -> Result<(f32, f32, f32), Mpu6050Error<E>> {
|
||||||
let mut buf: [u8; 6] = [0; 6];
|
let mut buf: [u8; 6] = [0; 6];
|
||||||
self.read_bytes(reg, &mut buf)?;
|
self.read_bytes(reg, &mut buf)?;
|
||||||
|
|
||||||
|
@ -214,7 +214,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Accelerometer readings in m/s^2
|
/// Accelerometer readings in m/s^2
|
||||||
pub fn get_acc(&mut self) -> Result<(f32, f32, f32), Error<E>> {
|
pub fn get_acc(&mut self) -> Result<(f32, f32, f32), Mpu6050Error<E>> {
|
||||||
let (mut ax, mut ay, mut az) = self.read_rot(ACC_REGX_H)?;
|
let (mut ax, mut ay, mut az) = self.read_rot(ACC_REGX_H)?;
|
||||||
|
|
||||||
ax /= self.acc_sensitivity;
|
ax /= self.acc_sensitivity;
|
||||||
|
@ -231,7 +231,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gyro readings in rad/s
|
/// Gyro readings in rad/s
|
||||||
pub fn get_gyro(&mut self) -> Result<(f32, f32, f32), Error<E>> {
|
pub fn get_gyro(&mut self) -> Result<(f32, f32, f32), Mpu6050Error<E>> {
|
||||||
let (mut gx, mut gy, mut gz) = self.read_rot(GYRO_REGX_H)?;
|
let (mut gx, mut gy, mut gz) = self.read_rot(GYRO_REGX_H)?;
|
||||||
|
|
||||||
gx *= PI / (180.0 * self.gyro_sensitivity);
|
gx *= PI / (180.0 * self.gyro_sensitivity);
|
||||||
|
@ -248,7 +248,7 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Temp in degrees celcius
|
/// Temp in degrees celcius
|
||||||
pub fn get_temp(&mut self) -> Result<f32, Error<E>> {
|
pub fn get_temp(&mut self) -> Result<f32, Mpu6050Error<E>> {
|
||||||
let mut buf: [u8; 2] = [0; 2];
|
let mut buf: [u8; 2] = [0; 2];
|
||||||
self.read_bytes(TEMP_OUT_H, &mut buf)?;
|
self.read_bytes(TEMP_OUT_H, &mut buf)?;
|
||||||
let raw_temp = self.read_word_2c(&buf[0..2]) as f32;
|
let raw_temp = self.read_word_2c(&buf[0..2]) as f32;
|
||||||
|
@ -257,25 +257,25 @@ where
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes byte to register
|
/// Writes byte to register
|
||||||
pub fn write_u8(&mut self, reg: u8, byte: u8) -> Result<(), Error<E>> {
|
pub fn write_u8(&mut self, reg: u8, byte: u8) -> Result<(), Mpu6050Error<E>> {
|
||||||
self.i2c.write(SLAVE_ADDR, &[reg, byte])
|
self.i2c.write(SLAVE_ADDR, &[reg, byte])
|
||||||
.map_err(Error::I2c)?;
|
.map_err(Mpu6050Error::I2c)?;
|
||||||
self.delay.delay_ms(10u8);
|
self.delay.delay_ms(10u8);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads byte from register
|
/// Reads byte from register
|
||||||
pub fn read_u8(&mut self, reg: u8) -> Result<u8, Error<E>> {
|
pub fn read_u8(&mut self, reg: u8) -> Result<u8, Mpu6050Error<E>> {
|
||||||
let mut byte: [u8; 1] = [0; 1];
|
let mut byte: [u8; 1] = [0; 1];
|
||||||
self.i2c.write_read(SLAVE_ADDR, &[reg], &mut byte)
|
self.i2c.write_read(SLAVE_ADDR, &[reg], &mut byte)
|
||||||
.map_err(Error::I2c)?;
|
.map_err(Mpu6050Error::I2c)?;
|
||||||
Ok(byte[0])
|
Ok(byte[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads series of bytes into buf from specified reg
|
/// Reads series of bytes into buf from specified reg
|
||||||
pub fn read_bytes(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Error<E>> {
|
pub fn read_bytes(&mut self, reg: u8, buf: &mut [u8]) -> Result<(), Mpu6050Error<E>> {
|
||||||
self.i2c.write_read(SLAVE_ADDR, &[reg], buf)
|
self.i2c.write_read(SLAVE_ADDR, &[reg], buf)
|
||||||
.map_err(Error::I2c)?;
|
.map_err(Mpu6050Error::I2c)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
42
src/registers.rs
Normal file
42
src/registers.rs
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
//! All constants used in the driver, mostly register addresses
|
||||||
|
|
||||||
|
/// Slave address of Mpu6050
|
||||||
|
pub const SLAVE_ADDR: u8 = 0x68;
|
||||||
|
/// Internal register to check slave addr
|
||||||
|
pub const WHOAMI: u8 = 0x75;
|
||||||
|
|
||||||
|
/// High Bytle Register Gyro x orientation
|
||||||
|
pub const GYRO_REGX_H: u8 = 0x43;
|
||||||
|
/// High Bytle Register Gyro y orientation
|
||||||
|
pub const GYRO_REGY_H: u8 = 0x45;
|
||||||
|
/// High Bytle Register Gyro z orientation
|
||||||
|
pub const GYRO_REGZ_H: u8 = 0x47;
|
||||||
|
|
||||||
|
/// High Byte Register Calc roll
|
||||||
|
pub const ACC_REGX_H: u8 = 0x3b;
|
||||||
|
/// High Byte Register Calc pitch
|
||||||
|
pub const ACC_REGY_H: u8 = 0x3d;
|
||||||
|
/// High Byte Register Calc yaw
|
||||||
|
pub const ACC_REGZ_H: u8 = 0x3f;
|
||||||
|
|
||||||
|
/// High Byte Register Temperature
|
||||||
|
pub const TEMP_OUT_H: u8 = 0x41;
|
||||||
|
|
||||||
|
/// Register to control chip waking from sleep, enabling sensors, default: sleep
|
||||||
|
pub const POWER_MGMT_1: u8 = 0x6b;
|
||||||
|
/// Internal clock
|
||||||
|
pub const POWER_MGMT_2: u8 = 0x6c;
|
||||||
|
|
||||||
|
/// Gyro Sensitivity
|
||||||
|
pub const FS_SEL: (f32, f32, f32, f32) = (131., 65.5, 32.8, 16.4);
|
||||||
|
/// Calcelerometer Sensitivity
|
||||||
|
pub const AFS_SEL: (f32, f32, f32, f32) = (16384., 8192., 4096., 2048.);
|
||||||
|
|
||||||
|
/// Accelerometer config register
|
||||||
|
pub const ACCEL_CONFIG: u8 = 0x1c;
|
||||||
|
|
||||||
|
/// gyro config register
|
||||||
|
pub const GYRO_CONFIG: u8 = 0x1b;
|
||||||
|
|
||||||
|
/// pi
|
||||||
|
pub const PI: f32 = 3.14159;
|
Loading…
Reference in a new issue