一种跨平台模块化软件通讯机制
参考
roscore
启动 ROS Master、Parameter Server 和 rosout
hello ros
-
初始化工作空间
Bash 1 2 3 4
mkdir -p ~/hello_ros/src cd ~/hello_ros catkin_make source devel/setup.bash
src
是必须的
-
创建 ros 包
Bash 1 2 3 4 5 6
cd ~/hello_ros/src catkin_create_pkg hello std_msgs roscpp cd .. catkin_make source devel/setup.bash cd ~/hello_ros/src/hello
- 创建一个
hello
的 ros 包,并指定依赖
- 创建一个
-
发布者
Bash 1
vim publisher_node.cpp
C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
#include "ros/ros.h" #include "std_msgs/String.h" int main(int argc, char **argv) { ros::init(argc, argv, "simple_publisher"); ros::NodeHandle nh; // 创建发布者对象 ros::Publisher pub = nh.advertise<std_msgs::String>("chatter", 1000); ros::Rate loop_rate(10); // 10 Hz int count = 0; while (ros::ok()) { std_msgs::String msg; msg.data = "Hello, ROS! " + std::to_string(count); ROS_INFO("%s", msg.data.c_str()); // 发布消息 pub.publish(msg); ros::spinOnce(); loop_rate.sleep(); ++count; } return 0; }
-
订阅者
Bash 1
vim subscriber_node.cpp
C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#include "ros/ros.h" #include "std_msgs/String.h" // 回调函数,处理接收到的消息 void chatterCallback(const std_msgs::String::ConstPtr& msg) { ROS_INFO("I heard: [%s]", msg->data.c_str()); } int main(int argc, char **argv) { ros::init(argc, argv, "simple_subscriber"); ros::NodeHandle nh; // 创建订阅者对象 ros::Subscriber sub = nh.subscribe("chatter", 1000, chatterCallback); // 进入循环,等待回调 ros::spin(); return 0; }
-
编译
Bash 1
vim CMakeLists.txt
Text Only 1 2 3 4 5
add_executable(publisher_node publisher_node.cpp) target_link_libraries(publisher_node ${catkin_LIBRARIES}) add_executable(subscriber_node subscriber_node.cpp) target_link_libraries(subscriber_node ${catkin_LIBRARIES})
Bash 1 2 3
cd ~/hello_ros catkin_make source devel/setup.bash
-
运行
Bash 1 2 3
roscore rosrun hello publisher_node rosrun hello subscriber_node