ROS Topic Test Cases
Prerequisites
Section titled “Prerequisites”ROS master-slave communication has been configured (robot dog as master, local computer as slave).
The operation steps in this article use the perception host as an example. The navigation host configuration is similar.
Preparation
Section titled “Preparation”Connect to Robot Dog WiFi
Section titled “Connect to Robot Dog WiFi”Connect the development host to the robot dog’s rear Ethernet port via cable, or connect via WiFi:
- WiFi name: X30****** (check the robot dog serial number)
- WiFi password: 12345678

Log in to Robot Dog Terminal
Section titled “Log in to Robot Dog Terminal”ssh ysc@192.168.1.105Password is ' (English single quote)

Test Connectivity Between Local and Robot Dog Terminals
Section titled “Test Connectivity Between Local and Robot Dog Terminals”Get the local terminal IP:

Ping from the robot dog terminal to the local terminal, and from the local terminal to the robot dog terminal:
ping 192.168.1.123 # Enter in robot dog terminal, replace with your local terminal IPping 192.168.1.105 # Enter in local terminalIf both terminals receive data, connectivity is confirmed.


Hostname Configuration
Section titled “Hostname Configuration”Modify Slave .bashrc File
Section titled “Modify Slave .bashrc File”export HOSTNAME=szc # Replace with your local terminal nameexport ROS_IP=192.168.1.123 # Replace with your local terminal IPexport ROS_MASTER_URI=http://192.168.1.105:11311 # Robot dog IPAfter modification, run source ~/.bashrc.

Modify Slave hosts File (usually only needs to be done once)
Section titled “Modify Slave hosts File (usually only needs to be done once)”sudo vi /etc/hostsAdd the following (note: use tab between IP and hostname, not spaces):
192.168.1.123 szc # Replace with your local terminal IP and name192.168.1.105 ysc
Modify Master .bashrc File
Section titled “Modify Master .bashrc File”export ROS_HOSTNAME=yscexport ROS_IP=192.168.1.105export ROS_MASTER_URI=http://192.168.1.105:11311After modification, run source ~/.bashrc.

Verify output:
http://192.168.1.105:11311http://192.168.1.105:11311
Modify Master hosts File
Section titled “Modify Master hosts File”sudo vi /etc/hostsAdd the following:
192.168.1.105 ysc192.168.1.123 szc # Replace with your local terminal IP and name
Master-Slave Communication Test
Section titled “Master-Slave Communication Test”On the local terminal, execute:
rostopic listIf a list of topic names is displayed:

Test with the /battery/current topic:
rostopic echo /battery/currentIf topic messages are received, master-slave communication is configured correctly.

Scenario 1: Built-in ROS Topic, No Custom Definition Needed
Section titled “Scenario 1: Built-in ROS Topic, No Custom Definition Needed”Using the /imu/data topic as an example, which starts publishing immediately after boot.
Subscriber Node Source Code
Section titled “Subscriber Node Source Code”#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 Configuration
Section titled “CMakeLists.txt Configuration”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 # Replace with your local terminal IPexport ROS_MASTER_URI=http://192.168.1.105:11311source devel/setup.bashrosrun imu_driver imu_subscriberScenario 2: Custom Topic Message
Section titled “Scenario 2: Custom Topic Message”Using the /battery_state topic as an example, with message type message_transformer_cpp/BatteryData being a custom message type.
Custom Message File Source (BatteryData.msg)
Section titled “Custom Message File Source (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_temperatureSubscriber Node Source Code
Section titled “Subscriber Node Source Code”#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 Configuration
Section titled “CMakeLists.txt Configuration”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 Configuration
Section titled “package.xml Configuration”<build_depend>message_generation</build_depend><exec_depend>message_runtime</exec_depend>Scenario 3: Dependency on External ROS Package
Section titled “Scenario 3: Dependency on External ROS Package”The Livox-ros-driver2 driver package must be installed and configured first. Refer to Livox Driver Installation Tutorial.
Problem: rostopic echo /livox/lidar only outputs point cloud array data, without standard message headers and LiDAR frame metadata.
Solution: The Livox-ros-driver2 driver package must be installed on the local terminal to correctly parse the custom message type of the /livox/lidar topic.