12次发布
0.4.0 | 2024年2月27日 |
---|---|
0.3.4 |
|
0.3.3 | 2021年8月26日 |
0.3.2 | 2020年12月11日 |
0.1.5 | 2019年2月10日 |
#64 在 嵌入式开发
66 每月下载量
275KB
900 行
Bosch Sensortec BNO055嵌入式-hal驱动程序

这是什么?
这是一个用于Bosch的绝对姿态传感器BNO055的embedded-hal驱动程序。
它与设备无关,并使用嵌入式-hal的Write
/WriteRead
(用于I2C)和Delay
特质进行其操作。
使用并重新导出mint的Quaternion进行四元数读取,EulerAngles进行欧拉角和Vector3进行传感器读取。
功能标志
std
默认情况下,此crate与no_std
兼容。但是,您可以通过启用std
功能标志来启用std
功能。目前这仅添加了std::error::Error
特质实现,用于Error
类型。
serde
serde
标志为BNO055Calibration
添加了Serialize
/ Deserialize
实现。
注意:serde
本身与no_std
兼容,但是并非所有序列化器都是(例如,serde-json
不是,但serde-json-core
是),因此请确保您没有意外地启用了serde
的std
功能(请参阅此处以获取完整说明)。
用法
-
在
Cargo.toml
中添加依赖项cargo add bno055
-
实例化并初始化设备
// ... declare and configure your I2c and Delay implementations ... // let i2c = ...; // let delay = ...; // Init BNO055 IMU let imu = bno055::Bno055::new(i2c); imu.init(&mut delay)?; // Enable 9-degrees-of-freedom sensor fusion mode with fast magnetometer calibration imu.set_mode(bno055::BNO055OperationMode::NDOF)?; Ok(imu)
-
读取姿态数据、四元数或欧拉角(横滚、俯仰、偏航/航向)
let quat: mint::Quaternion<f32> = imu.quaternion()?; // or: let euler: mint::EulerAngles<f32, ()> = imu.euler_angles()?;
由于BNO055固件错误,不应依赖欧拉角的读取。我建议坚持使用四元数读取,并在需要时将其转换为欧拉角。
详细信息及示例
设备校准
为首次校准设备传感器
use bno055::{BNO055Calibration, BNO055OperationMode, BNO055_CALIB_SIZE};
let bno055 = ...;
// Enter NDOF (absolute orientation) sensor fusion mode which is also performing
// a regular sensors calibration
bno055.set_mode(BNO055OperationMode::NDOF)?;
// Wait for device to auto-calibrate.
// Please perform steps necessary for auto-calibration to kick in.
// Required steps are described in Datasheet section 3.11
while !bno055.is_fully_calibrated() {}
let calib = bno055.calibration_profile()?;
// Save calibration profile in NVRAM
mcu.nvram_write(BNO055_CALIB_ADDR, calib.as_bytes(), BNO055_CALIB_SIZE)?;
加载先前保存的校准配置文件
use bno055::{BNO055Calibration, BNO055OperationMode, BNO055_CALIB_SIZE};
let bno055 = ...;
// Read saved calibration profile from MCUs NVRAM
let mut buf = [0u8; BNO055_CALIB_SIZE];
mcu.nvram_read(BNO055_CALIB_ADDR, &mut buf, BNO055_CALIB_SIZE)?;
// Apply calibration profile
let calib = BNO055Calibration::from_buf(buf);
bno055.set_calibration_profile(calib)?;
轴映射以适应您的安装
BNO055允许更改默认轴以符合实际物理设备方向,从而提供将BNO055芯片放置在PCB上以适应设计人员,并在软件中将芯片的轴与物理轴匹配的可能性。
use bno055::{AxisRemap, BNO055AxisConfig};
// ...
// Build remap configuration example with X and Y axes swapped:
let remap = AxisRemap::builder()
.swap_x_with(BNO055AxisConfig::AXIS_AS_Y)
.build()
.expect("Failed to build axis remap config");
bno055.set_axis_remap(remap)?;
请注意,AxisRemap
构建器(以及芯片本身)不允许构建无效状态,即当一个轴与其他多个轴交换时。例如,同时将轴X
与Y
和Z
交换是不允许的
AxisRemap::builder()
.swap_x_with(BNO055AxisConfig::AXIS_AS_Y)
.swap_x_with(BNO055AxisConfig::AXIS_AS_Z)
.build()
.unwrap(); // <- panics, .build() returned Err
更改轴符号
也可以翻转芯片的任一轴的符号。
翻转X和Y轴的示例
bno055
.set_axis_sign(BNO055AxisSign::X_NEGATIVE | bno055::BNO055AxisSign::Y_NEGATIVE)
.expect("Unable to communicate");
使用外部32k晶体振荡器
为了更好的性能,建议连接并使用外部32k石英晶体振荡器。
您可以通过调用set_external_crystal
来启用或禁用其使用。
bno055
.set_external_crystal(true)
.expect("Failed to set to external crystal");
使用替代I2C地址
BNO055允许通过设置COM3
引脚为LOW
将I2C地址从默认的0x29
更改为替代的0x28
。
要连接到具有替代地址的设备,可以通过调用with_alternative_address()
来启用其使用。
// use default 0x29 address
let mut bno = bno055::Bno055::new(i2c, delay);
// or:
// use 0x28 address
let mut bno = bno055::Bno055::new(i2c, delay).with_alternative_address();
更改BNO055电源模式
use bno055::{Bno055, BNO055PowerMode};
// Normal mode
bno055.set_power_mode(BNO055PowerMode::NORMAL)?;
// Low-power mode (only accelerometer being awake)
bno055.set_power_mode(BNO055PowerMode::LOW_POWER)?;
// Suspend mode (all sensors and controller are sleeping)
bno055.set_power_mode(BNO055PowerMode::SUSPEND)?;
读取芯片温度
默认情况下,温度以摄氏度指定。
let temp: i8 = bno055.temperature()?;
状态
已完成和测试的内容以及尚未完成的内容
- 传感器初始化
- 设备模式设置
- 设备状态读取
- 校准状态读取
- 外部晶体选择
- 轴映射
- 轴符号设置
- 校准数据读取
- 校准数据设置
- 替代I2C地址
- 考虑寄存器页面
- 方向数据读取
- 四元数
- 欧拉角
- 原始传感器数据读取
- 原始加速度计数据读取
- 原始陀螺仪数据读取
- 原始磁力计数据读取
- 线性加速度数据读取
- 重力向量数据读取
- 温度读取
- 按传感器配置(当不在融合模式时)
- 单位选择
- 中断
许可:MIT。
欢迎贡献!
依赖关系
~1–1.7MB
~35K SLoC