ROS2机器感知:相机、雷达、IMU

🕒 2025-09-18 📁 ROS系统 👤 laumy 🔥 199 热度

相机

相机安装

sudo apt install ros-jazzy-usb-cam

可视化

run usb_cam usb_cam_node_exe --ros-args -p video_device:=/dev/video2

ros2 run rqt_image_vew rqt_image_view

上面的-p指定具体的摄像头,可以用v4l2-ctl –list-devices列出。

参数

查看当前相机的发布话题

laumy@ThinkBook-14-G7-IAH:~/dev_ws$ ros2 topic info -v /image_raw
Type: sensor_msgs/msg/Image

Publisher count: 1

Node name: usb_cam
Node namespace: /
Topic type: sensor_msgs/msg/Image
Topic type hash: RIHS01_d31d41a9a4c4bc8eae9be757b0beed306564f7526c88ea6a4588fb9582527d47
Endpoint type: PUBLISHER
GID: 01.0f.e7.13.47.a9.02.80.00.00.00.00.00.00.18.03
QoS profile:
  Reliability: RELIABLE
  History (Depth): UNKNOWN
  Durability: VOLATILE
  Lifespan: Infinite
  Deadline: Infinite
  Liveliness: AUTOMATIC
  Liveliness lease duration: Infinite

Subscription count: 1

Node name: rqt_gui_cpp_node_43605
Node namespace: /
Topic type: sensor_msgs/msg/Image
Topic type hash: RIHS01_d31d41a9a4c4bc8eae9be757b0beed306564f7526c88ea6a4588fb9582527d47
Endpoint type: SUBSCRIPTION
GID: 01.0f.e7.13.55.aa.5f.b0.00.00.00.00.00.00.14.04
QoS profile:
  Reliability: BEST_EFFORT
  History (Depth): UNKNOWN
  Durability: VOLATILE
  Lifespan: Infinite
  Deadline: Infinite
  Liveliness: AUTOMATIC
  Liveliness lease duration: Infinite

查看话题下的消息类型

laumy@ThinkBook-14-G7-IAH:~/dev_ws$ ros2 interface show sensor_msgs/msg/Image 
# This message contains an uncompressed image
# (0, 0) is at top-left corner of image

std_msgs/Header header # Header timestamp should be acquisition time of image
    builtin_interfaces/Time stamp
        int32 sec
        uint32 nanosec
    string frame_id
                             # Header frame_id should be optical frame of camera
                             # origin of frame should be optical center of cameara
                             # +x should point to the right in the image
                             # +y should point down in the image
                             # +z should point into to plane of the image
                             # If the frame_id here and the frame_id of the CameraInfo
                             # message associated with the image conflict
                             # the behavior is undefined

uint32 height                # image height, that is, number of rows
uint32 width                 # image width, that is, number of columns

# The legal values for encoding are in file include/sensor_msgs/image_encodings.hpp
# If you want to standardize a new string format, join
# ros-users@lists.ros.org and send an email proposing a new encoding.

string encoding       # Encoding of pixels -- channel meaning, ordering, size
                      # taken from the list of strings in include/sensor_msgs/image_encodings.hpp

uint8 is_bigendian    # is this data bigendian?
uint32 step           # Full row length in bytes
uint8[] data          # actual matrix data, size is (step * rows)

三维相机

雷达

常见的雷达类型,单线激光雷达、多线激光雷达。

消息格式

可用下面命令查看雷达的消息类型

ros2 interface show sensor_msgs/msg/LaserScan 

打印如下:

# Single scan from a planar laser range-finder
#
# If you have another ranging device with different behavior (e.g. a sonar
# array), please find or create a different message, since applications
# will make fairly laser-specific assumptions about this data

std_msgs/Header header # timestamp in the header is the acquisition time of
    builtin_interfaces/Time stamp
        int32 sec
        uint32 nanosec
    string frame_id
                             # the first ray in the scan.
                             #
                             # in frame frame_id, angles are measured around
                             # the positive Z axis (counterclockwise, if Z is up)
                             # with zero angle being forward along the x axis

float32 angle_min            # start angle of the scan [rad]
float32 angle_max            # end angle of the scan [rad]
float32 angle_increment      # angular distance between measurements [rad]

float32 time_increment       # time between measurements [seconds] - if your scanner
                             # is moving, this will be used in interpolating position
                             # of 3d points
float32 scan_time            # time between scans [seconds]

float32 range_min            # minimum range value [m]
float32 range_max            # maximum range value [m]

float32[] ranges             # range data [m]
                             # (Note: values < range_min or > range_max should be discarded)
float32[] intensities        # intensity data [device-specific units].  If your
                             # device does not provide intensities, please leave
                             # the array empty.

开箱示例

开箱即用的示例

#安装
sudo apt install ros-jazzy-turtlebot3* -y

#启动Gazebo仿真
export TURTLEBOT3_MODEL=burger
ros2 launch turtlebot3_gazebo turtlebot3_world.launch.py

#打开RViz2
ros2 launch turtlebot3_bringup rviz2.launch.py

如果要让车动起来的话

export TURTLEBOT3_MODEL=burger
ros2 run turtlebot3_teleop teleop_keyboard

键盘的 W A S D 控制机器人移动,RViz2 里 /scan 会随着车动而更新。

IMU

在 ROS2 里,IMU 指的是 惯性测量单元(Inertial Measurement Unit),是一类传感器的统称。它在机器人系统中非常常见,比如无人车、无人机、移动机器人几乎都需要 IMU。

IMU包含加速度计、陀螺仪、磁力计(可选)。

消息格式

IMU的数据格式如下:

laumy@ThinkBook-14-G7-IAH:~/dev_ws$ ros2 interface show sensor_msgs/msg/Imu 
# This is a message to hold data from an IMU (Inertial Measurement Unit)
#
# Accelerations should be in m/s^2 (not in g's), and rotational velocity should be in rad/sec
#
# If the covariance of the measurement is known, it should be filled in (if all you know is the
# variance of each measurement, e.g. from the datasheet, just put those along the diagonal)
# A covariance matrix of all zeros will be interpreted as "covariance unknown", and to use the
# data a covariance will have to be assumed or gotten from some other source
#
# If you have no estimate for one of the data elements (e.g. your IMU doesn't produce an
# orientation estimate), please set element 0 of the associated covariance matrix to -1
# If you are interpreting this message, please check for a value of -1 in the first element of each
# covariance matrix, and disregard the associated estimate.

std_msgs/Header header
    builtin_interfaces/Time stamp
        int32 sec
        uint32 nanosec
    string frame_id

geometry_msgs/Quaternion orientation #姿态,x,y,z的角度,用 四元数 表示姿态(避免欧拉角万向节锁)。一般看这个就行了。
    float64 x 0
    float64 y 0
    float64 z 0
    float64 w 1
float64[9] orientation_covariance # Row major about x, y, z axes

geometry_msgs/Vector3 angular_velocity #角速度,绕 x/y/z 轴的角速度
    float64 x
    float64 y
    float64 z
float64[9] angular_velocity_covariance # Row major about x, y, z axes

geometry_msgs/Vector3 linear_acceleration #线性加速度,三个分量:沿 x/y/z 轴的加速度
    float64 x
    float64 y
    float64 z
float64[9] linear_acceleration_covariance # Row major x, y z

开箱示例

下面是一个开箱即用的示例。Gazebo 里已经有 IMU 传感器插件,可以直接加载。运行下面命令:

ros2 launch ros_gz_sim_demos imu.launch.py

如果要用rviz2来显示的话,需要安装imu插件

sudo apt install ros-jazzy-rviz-imu-plugin

可以在仿真器中选择工具Translate(十字箭头图标)、Rotate(圆弧箭头图标)调整位置和角度,可以看到右边RViz中的也会跟着变化。

也可以使用下面命令来实时打印

ros2 topic echo /imu

发表你的看法

\t