commit 58370b79b3a1d174e9b21b03449702b782223fed Author: Julian Gaal Date: Mon Apr 8 10:11:36 2019 +0200 first commit diff --git a/.cargo/config b/.cargo/config new file mode 100644 index 0000000..112515e --- /dev/null +++ b/.cargo/config @@ -0,0 +1,2 @@ +[target.arm-unknown-linux-gnueabihf] +linker = "arm-linux-gnueabihf-gcc" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f88dba --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/target +**/*.rs.bk +Cargo.lock \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cfadaa1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "mpu6050" +version = "0.1.0" +authors = ["Julian Gaal "] +edition = "2018" + +[dependencies] +i2cdev = "0.4.1" +embedded-hal = "0.2.2" +linux-embedded-hal = "0.2.2" diff --git a/README.md b/README.md new file mode 100644 index 0000000..971d973 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# mpu6050 diff --git a/src/.lib.rs.swp b/src/.lib.rs.swp new file mode 100644 index 0000000..9775e8c Binary files /dev/null and b/src/.lib.rs.swp differ diff --git a/src/bin/.linux.rs.swp b/src/bin/.linux.rs.swp new file mode 100644 index 0000000..b0fb9dc Binary files /dev/null and b/src/bin/.linux.rs.swp differ diff --git a/src/bin/linux.rs b/src/bin/linux.rs new file mode 100644 index 0000000..cf941dd --- /dev/null +++ b/src/bin/linux.rs @@ -0,0 +1,14 @@ +use mpu6050::*; +use linux_embedded_hal::{I2cdev, Delay}; +use i2cdev::linux::LinuxI2CError; + +fn main() -> Result<(), Error> { + let i2c = I2cdev::new("/dev/i2c-1") + .map_err(Error::I2c)?; + + let delay = Delay; + + let mut mpu = Mpu6050::new(i2c, delay); + mpu.init()?; + Ok(()) +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..ef7c44e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,109 @@ +//#![no_std] + +///! Mpu6050 sensor driver. +///! Datasheet: +use embedded_hal::{ + blocking::delay::DelayMs, + blocking::i2c::{Read, Write, WriteRead}, +}; + +const Mpu6050_SLAVE_ADDR: u8 = 0x68; +const Mpu6050_WHOAMI: u8 = 0x75; + +/// High Bytle Register Gyro x orientation +const Mpu6050_GYRO_REGX_H: u8 = 0x43; +/// High Bytle Register Gyro y orientation +const Mpu6050_GYRO_REGY_H: u8 = 0x45; +/// High Bytle Register Gyro z orientation +const Mpu6050_GYRO_REGZ_H: u8 = 0x47; + +/// High Byte Register Acc roll +const Mpu6050_ACC_REGX_H: u8 = 0x3b; +/// High Byte Register Acc pitch +const Mpu6050_ACC_REGY_H: u8 = 0x3d; +/// High Byte Register Acc yaw +const Mpu6050_ACC_REGZ_H: u8 = 0x3f; + +/// Register to control chip waking from sleep, enabling sensors, default: sleep +const POWER_MGMT_1: u8 = 0x6b; +/// Internal clock +const POWER_MGMT_2: u8 = 0x6c; + +/// Gyro Sensitivity +const FS_SEL: (f64, f64, f64, f64) = (131., 65.5, 32.8, 16.4); +/// Accelerometer Sensitivity +const AFS_SEL: (f64, f64, f64, f64) = (16384., 8192., 4096., 2048.); + +/// All possible errors in this crate +#[derive(Debug)] +pub enum Error { + /// I2C bus error + I2c(E), + + /// Invalid chip ID was read + InvalidChipId(u8), +} + +pub struct Rotation { + x: T, + y: T, + z: T, +} + +pub struct Mpu6050 { + i2c: I, + delay: D +} + +impl Mpu6050 +where + I: Write + WriteRead, + D: DelayMs, +{ + pub fn new(i2c: I, delay: D) -> Self { + Mpu6050 { + i2c, + delay, + } + } + + pub fn init(&mut self) -> Result<(), Error> { + self.write(POWER_MGMT_1, 0)?; + + self.verify()?; + + self.delay.delay_ms(100u8); + Ok(()) + } + + pub fn verify(&mut self) -> Result<(), Error> { + let address = self.read(Mpu6050_WHOAMI)?; + if address != Mpu6050_SLAVE_ADDR { + return Err(Error::InvalidChipId(address)); + } + + println!("address {}", address); + Ok(()) + } + + pub fn gyro_data(&mut self) -> Result, Error> { + Ok(Rotation { x: 1., y: 2., z: 3. }) + } + + pub fn write(&mut self, address: u8, byte: u8) -> Result<(), Error> { + self.i2c.write(Mpu6050_SLAVE_ADDR, &[address, byte]) + .map_err(Error::I2c)?; + + self.delay.delay_ms(10u8); + Ok(()) + } + + pub fn read(&mut self, address: u8) -> Result> { + let mut buffer = [0]; + self.i2c.write_read(Mpu6050_SLAVE_ADDR, &[address], &mut buffer) + .map_err(Error::I2c)?; + + println!("GOT {:?}", &buffer); + Ok(buffer[0]) + } +}