目录
- 如何实现一个发布者
- C++
- 创建功能包
- 编写程序
- 配置CMakeLists.txt
- 编译并运行发布者
- python
- 创建并编写脚本
- 运行
平台:华硕 Thinker Edge R 瑞芯微 RK3399Pro
固件版本:Tinker_Edge_R-Debian-Stretch-V1.0.4-20200615
记录自【古月居】古月·ROS入门21讲 | 一学就会的ROS机器人入门教程 —— 古月居GYH P10 10.发布者Publisher的编程实现
如何实现一个发布者
- 初始化ROS节点;
- 向ROS Master注册节点信息,包括发布的话题名和话题中的消息类型;
- 创建消息数据;
- 按照一定频率循环发布消息。
C++
创建功能包
cd ~/catkin_workspace/src
catkin_create_pkg learning_topic roscpp rospy std_msgs geometry_msgs turtlesim
编写程序
nano ~/catkin_workspace/src/learning_topic/src/velocity_publisher.cpp
/***********************************************************************
Copyright 2020 GuYueHome (www.guyuehome.com).
***********************************************************************//*** 该例程将发布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,队列长度10ros::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;
}
配置CMakeLists.txt
nano ~/catkin_workspace/src/learning_topic/CMakeLists.txt
在Build内,Install前加入
add_executable(velocity_publisher src/velocity_publisher.cpp)
target_link_libraries(velocity_publisher ${catkin_LIBRARIES})
设置需要编译的代码和生成的可执行文件;
设置链接库;
编译并运行发布者
cd ~/catkin_workspace
catkin_make# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
roscore
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun turtlesim turtlesim_node
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_topic velocity_publisher# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rqt_graph
python
创建并编写脚本
cd ~/catkin_workspace/src/learning_topic
mkdir scripts
cd ~/catkin_workspace/src/learning_topic/scripts
nano velocity_publisher.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-########################################################################
#### Copyright 2020 GuYueHome (www.guyuehome.com). ###
######################################################################### 该例程将发布turtle1/cmd_vel话题,消息类型geometry_msgs::Twistimport rospy
from geometry_msgs.msg import Twistdef velocity_publisher():# ROS节点初始化rospy.init_node('velocity_publisher', anonymous=True)# 创建一个Publisher,发布名为/turtle1/cmd_vel的topic,消息类型为geometry_msgs::Twist,队列长度10turtle_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.5vel_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
运行
sudo chmod +x ~/catkin_workspace/src/learning_topic/scripts/velocity_publisher.py# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
roscore
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun turtlesim turtlesim_node
# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rosrun learning_topic velocity_publisher.py# 新建一个终端
source ~/catkin_workspace/devel/setup.bash
rqt_graph