文章只是個人學習過程中學習筆記,主要參考ROS教程12。
[ROS](01)創建ROS工作空間
[ROS](02)創建&編譯ROS軟件包Package
在 [ROS](06)ROS通信 —— 話題(Topic)通信 中通過鍵盤
和rostopic pub [turtle1/cmd_vel]
命令控制Turtlesim
的烏龜運動了。
這章通過Python來編寫烏龜的運動控制節點,通過指定的話題,同樣讓烏龜持續做圓周運動。
啟動roscore、turtlesim_node,然後通過ROS命令獲取話題和消息,以及消息的類型。
創建turtle_publisher
(發布者)節點,該節點將不斷廣播消息。
在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
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}
)
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="[email protected]">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>
其實這個例子很簡單,不需要編譯,直接執行python文件就行。但是我們還是養成習慣吧,每次創建和修改代碼後,就catkin_make編譯一下,即使是Python節點也必須使用它。這是為了確保能為創建的消息和服務自動生成Python代碼。
如果你已啟動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. ︎