笔者因研究课题涉及ROS开发,学习了古月居出品的ROS入门21讲,为巩固话题通信(用于不断更新、少逻辑处理的数据处理场景)知识,本文将ROS的发布者、订阅者和话题消息三讲内容进行总结。
(1)创建功能包;
cd ~/catkin_ws/src
catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim
(2)创建发布者cpp文件;
在新建的功能包下src中打开新终端,并键入:gedit velocity_publisher.cpp,输入以下代码并保存。
/**
* 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist
*/
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "velocity_publisher");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);
// 设置循环的频率
ros::Rate loop_rate(10);
int count = 0;
while (ros::ok())
{
// 初始化geometry_msgs::Twist类型的消息
geometry_msgs::Twist vel_msg;
vel_msg.linear.x = 0.5;
vel_msg.angular.z = 0.2;
// 发布消息
turtle_vel_pub.publish(vel_msg);
ROS_INFO("Publish turtle velocity command[%0.2f m/s, %0.2f rad/s]",vel_msg.linear.x, vel_msg.angular.z);
// 按照循环频率延时
loop_rate.sleep();
}
return 0;
}
(3)在CMakeLists.txt添加编译规则;
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})
cd ~/catkin_ws
catkin_make
或
catkin_make_isolated
source devel/setup.bash
或
source devel_isolated/setup.bash
roscore
②新建终端,运行海龟结点;
rosrun turtlesim turtlesim_node
//每新打开一个终端,需要重新定义源
source devel/setup.bash
或
source devel_isolated/setup.bash
rosrun learning_topic velocity_publisher
(1)在发布者功能包下创建订阅者cpp文件;
在新建的功能包下src中,终端键入:gedit pose_subscriber.cpp,输入以下代码并保存。
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* 该例程将订阅/turtle1/pose话题,消息类型turtlesim::Pose
*/
#include <ros/ros.h>
#include "turtlesim/Pose.h"
// 接收到订阅的消息后,会进入消息回调函数
void poseCallback(const turtlesim::Pose::ConstPtr& msg)
{
// 将接收到的消息打印出来
ROS_INFO("Turtle pose: x:%0.6f, y:%0.6f", msg->x, msg->y);
}
int main(int argc, char **argv)
{
// 初始化ROS节点
ros::init(argc, argv, "pose_subscriber");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Subscriber,订阅名为/turtle1/pose的topic,注册回调函数poseCallback
ros::Subscriber pose_sub = n.subscribe("/turtle1/pose", 10, poseCallback);
// 循环等待回调函数
ros::spin();
return 0;
}
add_executable(pose_subscriber src/pose_subscriber.cpp)
target_link_libraries(pose_subscriber ${catkin_LIBRARIES})
cd ~/catkin_ws
catkin_make
或
catkin_make_isolated
source devel/setup.bash
或
source devel_isolated/setup.bash
roscore
rosrun turtlesim turtlesim_node
启动发布者后,海龟将进行匀速圆周运动。
③新建终端,运行订阅者;
//每新打开一个终端,需要重新定义源
source devel/setup.bash
或
source devel_isolated/setup.bash
rosrun learning_topic pose_subscriber
④新建终端,运行海龟键盘动态结点。
rosrun turtlesim turtle_teleop_key
在该终端下,可通过键盘方向键手动控制海龟移动。
(1)创建.msg文件;
在~/catkin_ws/src/learning_topic目录下,新建msg目录,并进入该目录,在该目录下打开终端,键入:gedit Person.msg,输入以下内容,并保存。
string name
uint8 sex
uint8 age
string x
string y
uint8 unkonwn = 0
uint8 male = 1
uint8 female = 2
<build_depend>message_generation</build_depend>
<exec_depend>message_runtime</exec_depend>
//在find_package下,添加message_generation
//在合适位置添加以下内容
add_message_files(FILES Person.msg)
generate_messages(DEPENDENCIES std_msgs)
//在catkin_package,添加message_runtime
(1)创建测试消息的发布者;
在~/catkin_ws/src/learning_topic/src目录下打开终端,键入:gedit person_publisher.cpp,输入以下代码,并保存。
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* 该例程将发布/person_info话题,自定义消息类型learning_topic::Person
*/
#include <ros/ros.h>
#include "learning_topic/Person.h"
int main(int argc, char **argv)
{
// ROS节点初始化
ros::init(argc, argv, "person_publisher");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Publisher,发布名为/person_info的topic,消息类型为learning_topic::Person,队列长度10
ros::Publisher person_info_pub = n.advertise<learning_topic::Person>("/person_info", 10);
// 设置循环的频率
ros::Rate loop_rate(1);
int count = 0;
while (ros::ok())
{
// 初始化learning_topic::Person类型的消息
learning_topic::Person person_msg;
person_msg.name = "Tom";
person_msg.age = 18;
person_msg.sex = learning_topic::Person::male;
// 发布消息
person_info_pub.publish(person_msg);
ROS_INFO("Publish Person Info: name:%s age:%d sex:%d",
person_msg.name.c_str(), person_msg.age, person_msg.sex);
// 按照循环频率延时
loop_rate.sleep();
}
return 0;
}
(2)创建测试消息的订阅者;
在~/catkin_ws/src/learning_topic/src目录下打开终端,键入:gedit person_subscriber.cpp,输入以下代码,并保存。
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************/
/**
* 该例程将订阅/person_info话题,自定义消息类型learning_topic::Person
*/
#include <ros/ros.h>
#include "learning_topic/Person.h"
// 接收到订阅的消息后,会进入消息回调函数
void personInfoCallback(const learning_topic::Person::ConstPtr& msg)
{
// 将接收到的消息打印出来
ROS_INFO("Subcribe Person Info: name:%s age:%d sex:%d",
msg->name.c_str(), msg->age, msg->sex);
}
int main(int argc, char **argv)
{
// 初始化ROS节点
ros::init(argc, argv, "person_subscriber");
// 创建节点句柄
ros::NodeHandle n;
// 创建一个Subscriber,订阅名为/person_info的topic,注册回调函数personInfoCallback
ros::Subscriber person_info_sub = n.subscribe("/person_info", 10, personInfoCallback);
// 循环等待回调函数
ros::spin();
return 0;
}
(3)在CMakeLists.txt添加编译规则;
add_executable(person_publisher src/person_publisher.cpp)
target_link_libraries(person_publisher ${catkin_LIBRARIES})
add_dependencies(person_publisher ${PROJECT_NAME}_generate_messages_cpp)
add_executable(person_subscriber src/person_subscriber.cpp)
target_link_libraries(person_subscriber ${catkin_LIBRARIES})
add_dependencies(person_subscriber ${PROJECT_NAME}_generate_messages_cpp)
cd ~/catkin_ws
catkin_make
或
catkin_make_isolated
source devel/setup.bash
或
source devel_isolated/setup.bash
roscore
//新建终端,然后再执行以下代码
cd ~/catkin_ws
source devel/setup.bash
或
source devel_isolated/setup.bash
rosrun learning_topic person_publisher
//新建终端,然后再执行以下代码
cd ~/catkin_ws
source devel/setup.bash
或
source devel_isolated/setup.bash
rosrun learning_topic person_subscriber
参考资料:
[1] 古月居GYH. 【古月居】古月·ROS入门21讲 | 一学就会的ROS机器人入门教程; 2019-07-16 [accessed 2023-04-09].
[2] ~Old. ROS——话题通信; 2022-03-18 [accessed 2023-04-09].
[3] Maccy37. OS查询话题具体内容常用指令; 2020-09-04 [accessed 2023-04-10].
[4] 李徕徕. ROS(4)--------------------ros::Rate loop_rate( ) & loop_rate.sleep()&ros_spin()&ros_spinOnce(); 2020-07-29 [accessed 2023-04-10].
[5] 小萌虫. ROS21讲; 2021-08-13 [accessed 2023-04-10].