使用ROS2读取IMU传感器数据

创建ROS2工作区

mkdir -p ~/ros2_ws/src
cd ~/ros2_ws/src

创建 ROS 2 工作区是为了把源码、构建产物和安装产物有序隔离与管理,便于开发、编译、调试、依赖管理与版本控制——这大大提高开发效率和可维护性。

创建包

创建包的命令

ros2 pkg create --build-type ament_python bno085_ros --dependencies rclpy sensor_msgs std_msgs

节点文件

在包的指定位置ros2_ws/src/bno085_ros/bno085_ros/创建一个以py结尾的节点文件 引用的库

import traceback
import threading
from smbus2 import SMBus, i2c_msg

import rclpy
from rclpy.node import Node
from sensor_msgs.msg import Imu

import adafruit_bno08x as adafruit_bno_mod
from adafruit_bno08x.i2c import BNO08X_I2C

接口封装

class SMBusWrapper:
    def __init__(self, busnum):

    def try_lock(self):

    def unlock(self):

    def writeto(self, address, buffer, *, start=0, end=None):

    def readinto(self, address, buf, *, start=0, end=None):

    def writeto_then_readinto(self, address, out_buffer, in_buffer,
                              *, out_start=0, out_end=None, in_start=0, in_end=None):

    def readfrom_into(self, address, buf, *, start=0, end=None):
 

    def readfrom(self, address, nbytes):

    def close(self):

定义ROS2的节点

class BNONode(Node):
    def __init__(self, bus_num=5, address=0x4B, topic_name='imu', rate_hz=50.0, frame_id='imu_link'):

    def _enable_reports_explicit(self):
        # 从模块取常量(你之前列出的常量存在)

    def timer_callback(self):

    def destroy_node(self):

构建并运行

colcon build
source install/setup.bash
ros2 run bno085_ros bno_node

查看数据

命令

ros2 topic echo /imu --once

结果

header:
stamp:
sec: 1772884282
nanosec: 744415747
frame_id: imu_link
orientation:
x: -0.230224609375
y: 0.00592041015625
z: 0.97265625
w: -0.02923583984375
orientation_covariance:

-1.0
-1.0
-1.0
-1.0
-1.0
-1.0
-1.0
-1.0
-1.0
angular_velocity:
x: -0.001953125
y: 0.0
z: 0.001953125
angular_velocity_covariance:
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
linear_acceleration:
x: 0.0
y: -0.02734375
z: 0.01171875
linear_acceleration_covariance:
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

本文章使用limfx的vscode插件快速发布