1、介绍
在 [ROS](06)ROS通信 —— 话题(Topic)通信 中通过键盘
和rostopic pub [turtle1/cmd_vel]
命令控制Turtlesim
的乌龟运动了。
这章通过Python来编写乌龟的运动控制节点,通过指定的话题,同样让乌龟持续做圆周运动。
2、获取话题与消息
启动roscore、turtlesim_node,然后通过ROS命令获取话题和消息,以及消息的类型。
3、编写发布者节点
创建turtle_publisher
(发布者)节点,该节点将不断广播消息。
3.1 Python 代码
在beginner_tutorials
软件包下创建创建一个scripts
目录来存放我们创建的Python脚本文件:turtle_publisher.py
,并scripts
目录下在执行文件权限:chmod +x turtle_publisher.py
。
#!/usr/bin/env python
# encoding: utf-8
import rospy
from geometry_msgs.msg import Twist
def turtle_publisher():
# 初始化ROS节点
rospy.init_node("turtle_publisher",anonymous=True)
# 创建发布者对象
pub = rospy.Publisher('turtle1/cmd_vel', Twist, queue_size=1000)
# 创建一个Rate对象rate,10Hz
rate = rospy.Rate(10)
# 要发布的消息: 让乌龟以2.0的线速度和1.8的角速度移动
msg = Twist()
msg.linear.x = 2.0
msg.linear.y = 0.0
msg.linear.z = 0.0
msg.angular.x = 0.0
msg.angular.y = 0.0
msg.angular.z = 1.8
# 循环
while not rospy.is_shutdown():
pub.publish(msg)
rate.sleep()
if __name__ == '__main__':
try:
turtle_publisher()
except rospy.ROSInterruptException:
pass
3.2 配置CMakeLists.txt
CMakeLists.txt
文件在beginner_tutorials
软件包根目录下。只需要在文件的末尾添加catkin_install_python(…)即可,这样可以确保正确安装了python脚本,并使用了正确的python解释器。
cmake_minimum_required(VERSION 3.0.2)
project(beginner_tutorials)
## Find catkin macros and libraries
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
catkin_package(
)
## Specify additional locations of header files
include_directories(
# include
${catkin_INCLUDE_DIRS}
)
add_executable(turtle_publisher src/turtle_publisher.cpp)
target_link_libraries(turtle_publisher ${catkin_LIBRARIES})
# 安装python可执行脚本
catkin_install_python(PROGRAMS
scripts/turtle_publisher.py
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
)
3.3 配置package.xml
package.xml
文件在beginner_tutorials
软件包根目录下。
<?xml version="1.0"?>
<package format="2">
<name>beginner_tutorials</name>
<version>0.0.0</version>
<description>The beginner_tutorials package</description>
<maintainer email="fly@todo.todo">fly</maintainer>
<license>BSD</license>
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<!-- The export tag contains other, unspecified, tags -->
<export>
<!-- Other tools can request additional information be placed here -->
</export>
</package>
3.4 编译
其实这个例子很简单,不需要编译,直接执行python文件就行。但是我们还是养成习惯吧,每次创建和修改代码后,就catkin_make编译一下,即使是Python节点也必须使用它。这是为了确保能为创建的消息和服务自动生成Python代码。
4、测试代码
如果你已启动roscore、turtlesim_node,那么请略过此步骤,否则请先分别在新的终端执行以下命令:
roscore
rosrun turtlesim turtlesim_node
最后在新的终端里启动发布者节点turtle_publisher.py
:
rosrun beginner_tutorials turtle_publisher.py
运行的结果如图1-1所示。
ROS.otg. ROS教程[EB/OL]. 2020-12-22[2022-7-5].
http://wiki.ros.org/cn/ROS/Tutorials. ↩︎.ROS.org. 编写简单的发布者和订阅者(Python)[EB/OL]. 2020-12-25[2022-07-30]. https://wiki.ros.org/cn/ROS/Tutorials/WritingPublisherSubscriber%28python%29. ↩︎