ADXL355 Embedded-hal driver crate

栏目: IT技术 · 发布时间: 4年前

内容简介:AThe

ADXL355 Embedded-hal driver crate

A Rust driver crate for the ADXL355 digital accelerometer. This driver accepts an embedded-hal SPI interface and a GPIO OutputPin (for the chip select). It implements the Accelerometer Trait so you can easily obtain both raw sensor data and scaled-to- Standard-gravity sensor data.

The ADXL355 is one of the best three axis digital accelerometers that I know of. It has a noise floor of only 25 µg/√Hz . For example, the very popular MPU9250 Inertial Measurement Unit (IMU) specifies 300 µg/√Hz for its noise density. One order of magnitude higher.

Of course the ADXL355 is more expensive, but I think it hits a nice sweet spot between the low end IMUs and very high end and high cost MEMS accelerometers (with noise densities of around 7 µg/√Hz ).

For this blogpost we make use of this breakout board .

It has a 2x6 pin header. We hacked it by shorting pin 1 to pin 8 so we could use a 10 strand flatcable that we had laying around. That means we lose the INT1 pin, but we’re not using it yet anyway.

ADXL355 Embedded-hal driver crate

Using the driver

The first step is to add the crate in your Cargo.toml . At the time of writing the latest version is 0.2.0 .

# Cargo.toml

[dependencies]
adxl355 = "0.2.0"

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]

Minimum viable example

To quickly get started with default settings you’ll only need to import

use adxl355::{Adxl355, Accelerometer};

Adxl355 is used to create the driver instance, and Accelerometer needs to be use d to get the trait in scope so we can use its API.

Let’s create the default driver and read a sample and send it over serial.

let mut accelerometer = Adxl355::default(spi, cs).unwrap();

// send start command
accelerometer.start();

let accel = accelerometer.accel_norm().unwrap();
writeln!(tx, "accel [g]: x={:.3}, y={:.3}, z={:.3}", accel.x, accel.y, accel.z).unwrap();

Custom configuration and data_ready interrupt

It is unlikely that the default settings match exactly with your project requirements. In the next example we will configure the sample rate, the measurement range and utilise the ADXL355’s data ready signal to trigger reading the data registers.

Sample Rate

Since we’ll be looking at the serial data with our slow human eyes, we will select the slowest possible sample rate. We need to import the ODR_LPF enum. It is called ODR_LPF because it will configure both the O utput D ata R ate and the L ow P ass F ilter. Lower data rate means more filtering and less noise. According to the datasheet the slowest datarate is 3.906 Hz . So we’ll get about 4 samples a second.

Measurement Range

For the measurement range we can choose between ±2g , ±4g and ±8g . If you have ever worked with accelerometers, you probably noticed that you always measure the gravity constant as an offset in the direct of the gravity. That means that we actually have to subtract 1g to get the effective range.

Let’s go for the ±4g mode since ±2g is default.

Use the customized config object

We apply these settings by creating a config object and changing its properties.

use adxl355::{Adxl355, Config as ADXLConfig, ODR_LPF, Range, Accelerometer};

// ...

let mut cfg = ADXLConfig::new();
cfg.odr(ODR_LPF::ODR_3_906_Hz)
   .range(Range::_4G);

let mut accelerometer = Adxl355::new(spi, cs, &cfg).unwrap();

External interrupt

To use the data ready interrupt we need to configure the gpio pin connected to this external signal. How to do this differs per microcontroller and also per HAL crate. For the STM32F103 it is done as follows:

let mut drdy = gpioa.pa0.into_floating_input(&mut gpioa.crl);

drdy.make_interrupt_source(&mut afio);
drdy.trigger_on_edge(&dp.EXTI, Edge::RISING);
drdy.enable_interrupt(&dp.EXTI);

Then we poll the interrupt bit in the main loop and read the data when ready

loop {
	  // Read accel data if data_ready interrupt bit is set
    if drdy.check_interrupt() {

        let norm = accelerometer.accel_norm().unwrap();
        writeln!(tx, "accel [g]: x={:.3}, y={:.3}, z={:.3}", accel.x, accel.y, accel.z).unwrap();

        // if we don't clear this bit, the ISR would trigger indefinitely
        drdy.clear_interrupt_pending_bit();
    }
}

More examples

ADXL355 Embedded-hal driver crate
ADXL355 with blackpill and black magic probe
ADXL355 Embedded-hal driver crate
ADXL355 with blackpill and black magic probe

For the complete code and more examples please see the example directory of the crate repository.

See the README for building and running instructions.


以上所述就是小编给大家介绍的《ADXL355 Embedded-hal driver crate》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Usability for the Web

Usability for the Web

Tom Brinck、Darren Gergle、Scott D. Wood / Morgan Kaufmann / 2001-10-15 / USD 65.95

Every stage in the design of a new web site is an opportunity to meet or miss deadlines and budgetary goals. Every stage is an opportunity to boost or undercut the site's usability. Thi......一起来看看 《Usability for the Web》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具