ADXL355 Embedded-hal driver crate

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

内容简介: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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

创业无畏

创业无畏

彼得· 戴曼迪斯、史蒂芬· 科特勒 / 贾拥民 / 浙江人民出版社 / 2015-8 / 69.90元

 您是否有最大胆的商业梦想?您是否想把一个好主意快速转化为一家市值几百亿甚至几千亿元的公司?《创业无畏》不仅分享了成功创业家的真知灼见,更为我们绘制了一幅激情创业的行动路线图!  创业缺人手怎么办?如何解决钱的问题?把握指数型大众工具,互联网就是你车间,你的仓库。拥有好的创意,自然有人把钱“白白地送给你用”。当你大海捞针的时候,激励性大奖赛会让针自己跑到你的眼前来!  掌握指数级......一起来看看 《创业无畏》 这本书的介绍吧!

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具