ROS 话题测试案例
已完成ROS主从机通信配置(以机器狗为主机,本地计算机为从机)
本文内容操作步骤以感知主机为例,导航主机的配置方法与此类似。
连接机器狗WiFi
Section titled “连接机器狗WiFi”使用网线将开发主机连接到机器狗尾部网口,或通过 WiFi 连接机器狗:
- WiFi 名称:X30******(请查看机器狗序列号)
- WiFi 密码为:12345678

登录机器狗终端
Section titled “登录机器狗终端”ssh ysc@192.168.1.105密码是 '(英文单引号)

测试本地终端与机器狗终端连通性
Section titled “测试本地终端与机器狗终端连通性”得到本地终端的IP:

在机器狗终端 ping 本地终端,在本地终端 ping 机器狗终端:
ping 192.168.1.123 #机器狗终端输入,注意替换为你自己本地终端的IPping 192.168.1.105 #本地终端输入若两个终端均有数据返回,则表示连通。


从机 .bashrc 文件修改
Section titled “从机 .bashrc 文件修改”export HOSTNAME=szc #替换为本地终端的名字export ROS_IP=192.168.1.123 #替换为本地终端的IPexport ROS_MASTER_URI=http://192.168.1.105:11311 #机器狗的IP改完之后 source ~/.bashrc。

从机 hosts 文件修改(一般仅需修改一次)
Section titled “从机 hosts 文件修改(一般仅需修改一次)”sudo vi /etc/hosts增加以下内容(注意IP和hostname之间是tab不是空格):
192.168.1.123 szc #替换为本地终端的IP和名字192.168.1.105 ysc
主机 .bashrc 文件修改
Section titled “主机 .bashrc 文件修改”export ROS_HOSTNAME=yscexport ROS_IP=192.168.1.105export ROS_MASTER_URI=http://192.168.1.105:11311改完之后 source ~/.bashrc。

验证输出:
http://192.168.1.105:11311http://192.168.1.105:11311
主机 hosts 文件修改
Section titled “主机 hosts 文件修改”sudo vi /etc/hosts增加以下内容:
192.168.1.105 ysc192.168.1.123 szc #替换为你自己本地终端的IP和名字
主从机通讯测试
Section titled “主从机通讯测试”在本地终端执行:
rostopic list若能列出一系列话题名:

再以 /battery/current 话题为例测试:
rostopic echo /battery/current若可接收到话题消息,表示主从机通讯配置成功。

场景一:ROS自带话题,无需自定义
Section titled “场景一:ROS自带话题,无需自定义”以 /imu/data 话题为例,该话题为开机后即有消息发布。
订阅者节点文件源码
Section titled “订阅者节点文件源码”#include "ros/ros.h"#include "sensor_msgs/Imu.h"
int g_count = 0;
void imuCallback(const sensor_msgs::Imu::ConstPtr& msg){ g_count++; ROS_INFO("Subscribed imu data, cycle : %d ", g_count);
ROS_INFO("header: "); ROS_INFO(" seq: %d", msg->header.seq); ROS_INFO(" stamp: "); ROS_INFO(" secs: %d", msg->header.stamp.sec); ROS_INFO(" nsecs: %d", msg->header.stamp.nsec); ROS_INFO(" frame_id: \"%s\"", msg->header.frame_id.c_str());
ROS_INFO("orientation: "); ROS_INFO(" x: %f", msg->orientation.x); ROS_INFO(" y: %f", msg->orientation.y); ROS_INFO(" z: %f", msg->orientation.z); ROS_INFO(" w: %f", msg->orientation.w);
ROS_INFO("angular_velocity: "); ROS_INFO(" x: %f", msg->angular_velocity.x); ROS_INFO(" y: %f", msg->angular_velocity.y); ROS_INFO(" z: %f", msg->angular_velocity.z);
ROS_INFO("linear_acceleration: "); ROS_INFO(" x: %f", msg->linear_acceleration.x); ROS_INFO(" y: %f", msg->linear_acceleration.y); ROS_INFO(" z: %f", msg->linear_acceleration.z);
ROS_INFO("---");}
int main(int argc, char **argv){ ros::init(argc, argv, "imu_subscriber"); ros::NodeHandle nh; ros::Subscriber sub = nh.subscribe("/imu/data", 10, imuCallback); ros::spin(); return 0;}CMakeLists.txt 配置
Section titled “CMakeLists.txt 配置”find_package(catkin REQUIRED COMPONENTS roscpp rospy sensor_msgs)
catkin_package( CATKIN_DEPENDS roscpp rospy sensor_msgs)
add_executable(imu_subscriber src/imu_subscriber.cpp)target_link_libraries(imu_subscriber ${catkin_LIBRARIES})
cd ~/jy_cog/transfercatkin_makeexport ROS_IP=192.168.1.123 #替换为本地终端的IPexport ROS_MASTER_URI=http://192.168.1.105:11311source devel/setup.bashrosrun imu_driver imu_subscriber场景二:自定义话题消息
Section titled “场景二:自定义话题消息”以话题 /battery_state 为例,消息类型 message_transformer_cpp/BatteryData 是自定义消息类型。
自定义消息文件源码(BatteryData.msg)
Section titled “自定义消息文件源码(BatteryData.msg)”uint16 voltageint16 currentuint16 remaining_capacityuint16 nominal_capacityuint16 cyclesuint16 production_dateuint16 balanced_lowuint16 balanced_highuint16 protected_stateuint8 software_versionuint8 battery_leveluint8 mos_stateuint8 battery_quantityuint8 battery_ntcfloat32[] battery_temperature订阅者节点文件源码
Section titled “订阅者节点文件源码”#include "ros/ros.h"#include "battery_state/BatteryData.h"
int g_count = 0;
void batteryCallback(const battery_state::BatteryData::ConstPtr& msg){ g_count++;
ROS_INFO("voltage: %d", msg->voltage); ROS_INFO("current: %d", msg->current); ROS_INFO("remaining_capacity: %d", msg->remaining_capacity); ROS_INFO("nominal_capacity: %d", msg->nominal_capacity); ROS_INFO("cycles: %d", msg->cycles); ROS_INFO("production_date: %d", msg->production_date); ROS_INFO("balanced_low: %d", msg->balanced_low); ROS_INFO("balanced_high: %d", msg->balanced_high); ROS_INFO("protected_state: %d", msg->protected_state); ROS_INFO("software_version: %d", msg->software_version); ROS_INFO("battery_level: %d", msg->battery_level); ROS_INFO("mos_state: %d", msg->mos_state); ROS_INFO("battery_quantity: %d", msg->battery_quantity); ROS_INFO("battery_ntc: %d", msg->battery_ntc);
ROS_INFO("battery_temperature: ["); for (size_t i = 0; i < msg->battery_temperature.size(); ++i) { if (i == msg->battery_temperature.size() - 1) { ROS_INFO(" %f", msg->battery_temperature[i]); } else { ROS_INFO(" %f,", msg->battery_temperature[i]); } } ROS_INFO("]"); ROS_INFO("---");}
int main(int argc, char **argv){ ros::init(argc, argv, "battery_subscriber"); ros::NodeHandle nh; ros::Subscriber sub = nh.subscribe("/battery_state", 10, batteryCallback); ros::spin(); return 0;}CMakeLists.txt 配置
Section titled “CMakeLists.txt 配置”find_package(catkin REQUIRED COMPONENTS geometry_msgs roscpp rospy std_msgs message_generation)
add_message_files(FILES BatteryData.msg)generate_messages(DEPENDENCIES std_msgs)
catkin_package( CATKIN_DEPENDS roscpp rospy sensor_msgs message_runtime)
add_executable(battery_subscriber src/battery_subscriber.cpp)target_link_libraries(battery_subscriber ${catkin_LIBRARIES})add_dependencies(battery_subscriber ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})package.xml 配置
Section titled “package.xml 配置”<build_depend>message_generation</build_depend><exec_depend>message_runtime</exec_depend>场景三:依赖外部ROS功能包
Section titled “场景三:依赖外部ROS功能包”需提前完成 Livox-ros-driver2 驱动包的安装和环境配置,具体参考 Livox驱动安装教程。
问题: rostopic echo /livox/lidar 输出时仅有点云数组相关数据,不含标准消息头和激光雷达帧元数据。
解决方案: 在本地终端需要先完成 Livox-ros-driver2 驱动包的安装,才能在本地终端正确解析 /livox/lidar 话题的自定义消息类型。