惯性测量单元在无人机和机器人中是不可忽却的一部分,它帮助设备识别方向,变得更加智能。本文将使用Arduino开发板通过I2C方式连接BMI088惯性测量单元模块,实现获取加速度值和角速度值。
YBX-BMI088模块 YBX-BMI088(I2C)模块是博根工作室推出的扩展板,基于博世传感器的BMI088芯片。该传感器是一款六轴传感器模块,内部带有一个16位的加速度传感器模块和一个16位的陀螺仪模块。 该模块加速度传感器的I2C地址是0x19,陀螺仪的I2C地址是0x69。
硬件连接 Arduino开发板通过I2C连接到BMI088模块,以下是硬件连接原理图:
BMI088模块的VCC连接到Arduino开发板的5V,GND连接到Arduino的GND。BMI088模块的SDA和SCL引脚分别连接到Arduino的I2C引脚(A4和A5)。硬件连接完成后如下图所示:
安装库文件 在进行编程之前,我们需要安装Bolder Flight Systems的BMI088库,可以在Github上面下载,下载链接:https://github.com/bolderflight/BMI088。也可以在库管理器中搜索BMI088,找到该库文件,然后进行安装。
代码 本文使用的是库文件中是Basic_I2C示例,代码如下: - #include "BMI088.h"
- /* accel object */
- Bmi088Accel accel(Wire,0x19);
- /* gyro object */
- Bmi088Gyro gyro(Wire,0x69);
- void setup()
- {
- int status;
- /* USB Serial to print data */
- Serial.begin(115200);
- while(!Serial) {}
- /* start the sensors */
- status = accel.begin();
- if (status < 0) {
- Serial.println("Accel Initialization Error");
- Serial.println(status);
- while (1) {}
- }
- status = gyro.begin();
- if (status < 0) {
- Serial.println("Gyro Initialization Error");
- Serial.println(status);
- while (1) {}
- }
- }
- void loop()
- {
- /* read the accel */
- accel.readSensor();
- /* read the gyro */
- gyro.readSensor();
- /* print the data */
- Serial.print(accel.getAccelX_mss());
- Serial.print("\t");
- Serial.print(accel.getAccelY_mss());
- Serial.print("\t");
- Serial.print(accel.getAccelZ_mss());
- Serial.print("\t");
- Serial.print(gyro.getGyroX_rads());
- Serial.print("\t");
- Serial.print(gyro.getGyroY_rads());
- Serial.print("\t");
- Serial.print(gyro.getGyroZ_rads());
- Serial.print("\t");
- Serial.print(accel.getTemperature_C());
- Serial.print("\n");
- /* delay to help with printing */
- delay(20);
- }
复制代码编译并将代码上传直Arudino开发板。连接开发板至计算机。
运行测试结果 打开串口监视器,查看Arduino开发板的输出结果。旋转模块,并观察输出数值的变化。
|