ROS-发布者Publisher的编程实现


在前面的章节,我们学了通过按键、命令行的形式控制小海龟。本节我们学习通过编程实现小海龟的控制。

一、话题模型

如下图所示:
Subcriber是海龟仿真器;
Publisher是速度值的发布者,我们本节通过程序去实现;
发布者发布一个Twist消息,Twist包括角速度和线速度;
Twist通过一个叫/turtle1/cmd_vel的数据管道总线,传送到Subscrider。
在这里插入图片描述

二、建立功能包

我们把上一讲的test_pkg功能包删除,我们重新建立一个。
geometry_msgs是Twist消息的功能包;
turtlesim也是一个被依赖的功能包;

$ cd ~/catkin_ws/src
$ catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim

三、创建发布者代码

整体思路:
在这里插入图片描述

3.1cpp代码实现

我们把如下的这个古月提供的代码,直接拷贝到linux中的learning_topic/src中,即可。
在这里插入图片描述
此文件代码如下:

/**
 * 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist
 */
 
#include <ros/ros.h>
#include <geometry_msgs/Twist.h>

int main(int argc, char **argv)
{
    
    
	// ROS节点初始化,velocity_publisher是节点的名字
	ros::init(argc, argv, "velocity_publisher");

	// 创建节点句柄,节点句柄用来管理节点资源。
	ros::NodeHandle n;

	// 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
	//  /turtle1/cmd_vel和订阅者的接受话题名是一致的。
	ros::Publisher turtle_vel_pub = n.advertise<geometry_msgs::Twist>("/turtle1/cmd_vel", 10);

	// 设置循环的频率。类似于我们在前面的章节的命令行控制的-r 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;//角速度

	    // 发布消息;ROS_INFO是日志输出,输出到客户端
		turtle_vel_pub.publish(vel_msg);//把vel_msg发布出去
		ROS_INFO("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", 
				vel_msg.linear.x, vel_msg.angular.z);

	    // 按照循环频率延时。我们在上面的ros::Rate loop_rate(10);语句已经设置了。
	    loop_rate.sleep();
	}

	return 0;
}

3.2python代码实现

如果我们不用cpp而是用python,代码如下:
在实现流(即原理)上,python和cpp是一样的。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twist

import rospy
from geometry_msgs.msg import Twist

def velocity_publisher():
	# ROS节点初始化
    rospy.init_node('velocity_publisher', anonymous=True)

	# 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10
    turtle_vel_pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10)

	#设置循环的频率
    rate = rospy.Rate(10) 

    while not rospy.is_shutdown():
		# 初始化geometry_msgs::Twist类型的消息
        vel_msg = Twist()
        vel_msg.linear.x = 0.5
        vel_msg.angular.z = 0.2

		# 发布消息
        turtle_vel_pub.publish(vel_msg)
    	rospy.loginfo("Publsh turtle velocity command[%0.2f m/s, %0.2f rad/s]", 
				vel_msg.linear.x, vel_msg.angular.z)

		# 按照循环频率延时
        rate.sleep()

if __name__ == '__main__':
    try:
        velocity_publisher()
    except rospy.ROSInterruptException:
        pass

需要我们注意的一点是:python要有“允许作为程序执行文件”。
在这里插入图片描述

四、配置发布者代码编译规则

打开CMakeLists.txt文件,在

###########
## Build ##
###########
......
......

中加入如下语句:
第一个语句意为把src/velocity_publisher.cpp编译为velocity_publisher可执行文件
第二个语句意为把velocity_publisher可执行文件和ROS相关的一些库做链接。即C++的接口通过target_link_libraries去和velocity_publisher做链接。

add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${
    
    catkin_LIBRARIES})

如下图所示:
在这里插入图片描述

五、编译并运行发布者

$ cd ~/catkin_ws
$ catkin_make
$ source devel/setup.bash
$ roscore
$ rosrun turtlesim turtlesim_node
$ rosrun learning_topic velocity_publisher

结果如下:
在这里插入图片描述

这里补充一下,现在所运行的这个程序velocity_publisher在catkin_ws/devel/lib/learn_topic目录下面。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45519751/article/details/112619979