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()
andnew_with_spi()
. - Initialize the device. See:
init()
. - Accelerometer:
- Read measured acceleration. See:
acceleration()
. - Get accelerometer status. See:
accel_status()
. - Set accelerometer mode and output data rate. See:
set_accel_mode_and_odr()
. - Set accelerometer scale. See:
set_accel_scale()
. - Get accelerometer ID. See:
accelerometer_id()
. - Get temperature sensor status. See:
temperature_status()
. - Read measured temperature. See:
temperature()
. - Configure FIFO. See:
acc_set_fifo_mode()
. - Enable/disable interrupts. See:
acc_enable_interrupt()
.
- Read measured acceleration. See:
- Magnetometer:
- Get the magnetometer status. See:
mag_status()
. - Change into continuous/one-shot mode. See:
into_mag_continuous()
. - Read measured magnetic field. See:
magnetic_field()
. - Set magnetometer mode and output data rate. See:
set_mag_mode_and_odr()
. - Get magnetometer ID. See:
magnetometer_id()
. - Enable/disable magnetometer built in offset cancellation. See:
enable_mag_offset_cancellation()
. - Enable/disable magnetometer low-pass filter. See:
mag_enable_low_pass_filter()
.
- Get the magnetometer status. See:
§The devices
The LSM303AGR is an ultralow-power high-performance system-in-package featuring a 3-axis digital linear acceleration sensor and a 3-axis digital magnetic sensor.
The LSM303AGR has linear acceleration full scales of ±2g/±4g/±8g/±16g and a magnetic field dynamic range of ±50 gauss. The LSM303AGR includes an I²C serial bus interface that supports standard, fast mode, fast mode plus, and high-speed (100 kHz, 400 kHz, 1 MHz, and 3.4 MHz) and an SPI serial standard interface.
The system can be configured to generate an interrupt signal for free-fall, motion detection, and magnetic field detection. The magnetic and accelerometer blocks can be enabled or put into power-down mode separately.
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::{Delay, I2cdev};
use lsm303agr::{AccelMode, AccelOutputDataRate, Lsm303agr};
let dev = I2cdev::new("/dev/i2c-1").unwrap();
let mut sensor = Lsm303agr::new_with_i2c(dev);
sensor.init().unwrap();
sensor.set_accel_mode_and_odr(&mut Delay, AccelMode::Normal, AccelOutputDataRate::Hz10).unwrap();
loop {
if sensor.accel_status().unwrap().xyz_new_data() {
let data = sensor.acceleration().unwrap();
println!("Acceleration: x {} y {} z {}", data.x_mg(), data.y_mg(), data.z_mg());
}
}
§Connect through SPI, initialize and take some measurements
use linux_embedded_hal::{Delay, SpidevDevice};
use lsm303agr::{AccelMode, AccelOutputDataRate, Lsm303agr};
let accel_dev = SpidevDevice::open("/dev/spidev0.0").unwrap();
let mag_dev = SpidevDevice::open("/dev/spidev0.1").unwrap();
let mut sensor = Lsm303agr::new_with_spi(accel_dev, mag_dev);
sensor.init().unwrap();
sensor.set_accel_mode_and_odr(&mut Delay, AccelMode::Normal, AccelOutputDataRate::Hz10).unwrap();
loop {
if sensor.accel_status().unwrap().xyz_new_data() {
let data = sensor.acceleration().unwrap();
println!("Acceleration: x {} y {} z {}", data.x_mg(), data.y_mg(), data.z_mg());
}
}
Modules§
- I2C/SPI interfaces
- Device operation modes
Structs§
- An acceleration measurement.
- An Accelerometer ID.
- LSM303AGR device driver
- A magnetic field measurement.
- A Magnetometer ID.
- All possible errors in this crate
- Data status
- A temperature measurement.
- Temperature sensor status
Enums§
- Accelerometer mode
- Accelerometer output data rate
- Accelerometer scaling factor
- All possible errors in this crate
- A FIFO mode.
- An interrupt.
- Magnetometer mode
- Magnetometer output data rate