Crate lsm303agr[][src]

Expand description

This is a platform agnostic Rust driver for the LSM303AGR ultra-compact high-performance eCompass module: ultra-low-power 3D accelerometer and 3D magnetometer using the embedded-hal traits.

This driver allows you to:

The devices

The LSM303AGR is an inertial measurement unit (IMU) consisting of a state-of-the-art 3-axis, low-g accelerometer and a low power 3-axis gyroscope. It has been designed for low power, high precision 6-axis and 9-axis applications in mobile phones, tablets, wearable devices, remote controls, game controllers, head-mounted devices and toys.

The LSM303AGR is available in a compact 14-pin 2.5 × 3.0 × 0.83 mm3 LGA package. When accelerometer and gyroscope are in full operation mode, power consumption is typically 925 μA, enabling always-on applications in battery driven devices.

Further Bosch Sensortec sensors, e.g. geomagnetic (BMM150) can be connected as slave via a secondary I2C interface. In this configuration, the LSM303AGR controls the data acquisition of the external sensor and the synchronized data of all sensors is stored the register data and can be additionally stored in the built-in FIFO.

Besides the flexible primary interface (I2C or SPI) that is used to connect to the host, LSM303AGR provides an additional secondary interface. This secondary interface can be used in SPI mode for OIS (optical image stabilization) applications in conjunction with camera modules, or in advanced gaming use cases.

Documents: Datasheet - Application note

Usage examples (see also examples folder)

To use this driver, import this crate and an embedded_hal implementation, then instantiate the appropriate device.

Please find additional examples using hardware in this repository: driver-examples

Connect through I2C, initialize and take some measurements

use linux_embedded_hal::I2cdev;
use lsm303agr::{AccelOutputDataRate, Lsm303agr};

let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Lsm303agr::new_with_i2c(dev);
sensor.init().unwrap();
sensor.set_accel_odr(AccelOutputDataRate::Hz10).unwrap();
loop {
    if sensor.accel_status().unwrap().xyz_new_data {
        let data = sensor.accel_data().unwrap();
        println!("Acceleration: x {} y {} z {}", data.x, data.y, data.z);
    }
}

Connect through SPI, initialize and take some measurements

use linux_embedded_hal::{Spidev, Pin};
use lsm303agr::{AccelOutputDataRate, Lsm303agr};

let dev = Spidev::open("/dev/spidev0.0").unwrap();
let accel_cs = Pin::new(17);
let mag_cs = Pin::new(27);
let mut sensor = Lsm303agr::new_with_spi(dev, accel_cs, mag_cs);
sensor.init().unwrap();
sensor.set_accel_odr(AccelOutputDataRate::Hz10).unwrap();
loop {
    if sensor.accel_status().unwrap().xyz_new_data {
        let data = sensor.accel_data().unwrap();
        println!("Acceleration: x {} y {} z {}", data.x, data.y, data.z);
    }
}

Modules

I2C/SPI interfaces

Device operation modes

Structs

LSM303AGR device driver

Measurement

All possible errors in this crate

Data status

Unscaled measurement

Enums

Accelerometer mode

Accelerometer output data rate

Accelerometer scaling factor

All possible errors in this crate

Magnetometer output data rate