通过程序实现发布海龟的速度指令,让海龟运动。
这个系统中,通过ROS Master来管理所有节点,其中主要节点有二,一者为订阅者节点,turtlesim海龟仿真器,一者为发布节点Turtle Velocity用于发布海龟的速度。要求通过程序实现该发布者节点,并且发布message(数据结构为Twist),然后通过一个名为/turtle1/cmd_vel的话题总线,把数据传输给subscriber,使其完成订阅。
首先创建功能包,如上一个笔记。其包含诸多依赖,除了之前提到过的外,还有包含了Twist消息类型的geometry_msgs和turtlesim。
catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim
如何实现一个发布者
- 初始化ROS节点;
- 向ROS Master注册节点信息,包括发布的话题名和话题中的消息类型。说的很恐怖的样子,其实类比C++就是针对节点,话题和消息分别创建类对象,用于后续程序的调用;
- 创建消息数据,其实就是类C++中给类对象的成员数据或者struct的成员赋值;
- 按照一定频率循环发布消息,在循环中加一个延时函数用于控制发布消息的频率。
/*
This program will publish through the topic: /turtle/cmd_vel, message type: geometry::Twist
*/
#include<ros/ros.h>
#include<geometry_msgs/Twist.h>
int main(int argc, char **argv)
{
//ROS NODE INIT
ros::init(argc, argv, "velocity_publisher");
//Build node handle
ros::NodeHandle n;
//Build a publisher, publish a topic named /turtle/cmd_vel, message type is geometry_msgs::Twist, length of queue: 10
ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel",10);
//Set the frequency of loop, send 10 times per second
ros::Rate loop_rate(10);
int count = 0;
while(ros::ok())
{
//Initiate the message typed geometry::Twist
geometry_msgs::Twist vel_msg;
vel_msg.linear.x = 0.5;
vel_msg.angular.z = 0.2;
//Publish the message
turtle_vel_pub.publish(vel_msg);
ROS_INFO("Publish turtle velocity command[%0.2f m/s, %0.2f m/s]",vel_msg.linear.x, vel_msg.angular.z);
//delay as the set of loop frequency
loop_rate.sleep();
}
return 0;
}
其中ros::ok()在循环判断中类似while(1),ROS_INFO类似于cout或者printf(""),loop_rate.sleep()用于循环延时控制。
将这两句加入CMakeList.txt文件Build部分的末尾。
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})
随后用make指令编译
设置路径,但是该方法每次都要重新设置路径,如果希望一劳永逸,可以在主文件夹下Ctrl+H显示隐藏文件.bashrc,编辑在底部加入路径。
source devel/setup.bash
自此,程序从编写到发布结束,可以在任意的路径进行rosrun命令,运行刚刚编写的程序。
roscore
rosrun turtlesim turtlesim_node
rosrun learning_topic velocity_publisher
龟龟