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:
- Connect through I2C or SPI. See:
new_with_i2c()
. - Initialize the device. See:
init()
. - Accelerometer:
- Read accelerometer data. See:
accel_data()
. - Read accelerometer data unscaled. See:
accel_data()
. - Get accelerometer status. See:
accel_status()
. - Set accelerometer output data rate. See:
set_accel_odr()
. - Set accelerometer mode. See:
set_accel_mode()
. - Set accelerometer scale. See:
set_accel_scale()
. - Get accelerometer ID. See:
accelerometer_id()
.
- Read accelerometer data. See:
- Magnetometer:
- Get the magnetometer status. See:
mag_status()
. - Change into continuous/one-shot mode. See:
into_mag_continuous()
. - Read magnetometer data. See:
mag_data()
. - Read magnetometer data unscaled. See:
mag_data()
. - Set magnetometer output data rate. See:
set_mag_odr()
. - Get magnetometer ID. See:
magnetometer_id()
.
- Get the magnetometer status. See:
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
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