文章只是個人學習過程中學習筆記,主要參考ROS教程12.
[ROS](01)創建ROS工作空間
[ROS](02)創建&編譯ROS軟件包Package
在 [ROS](06)ROS通信 —— 話題(Topic)通信 中通過鍵盤
和rostopic pub [turtle1/cmd_vel]
命令控制Turtlesim
turtle in motion.
This chapter passesPythonTo write the motion control node of the turtle,通過指定的話題,Also let the turtle continue to do a circular motion.
啟動roscore、turtlesim_node,然後通過ROSCommands to get topics and messages,and the type of message.
創建turtle_publisher
(發布者)節點,The node will continuously broadcast messages.
在beginner_tutorials
Create one under the packagescripts
directory to store the ones we createdPython腳本文件:turtle_publisher.py
,並scripts
Execute file permissions in the directory: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.0line speed and 1.8angular velocity of movement
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
軟件包根目錄下.Just add at the end of the filecatkin_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>
Actually this example is very simple,不需要編譯,直接執行python文件就行.But let's get used to it,After each code creation and modification,就catkin_make編譯一下,即使是PythonNodes must use it too.This is to ensure automatic generation for created messages and servicesPython代碼.
if you have startedroscore、turtlesim_node,Then please skip this step,Otherwise, please execute the following commands in a new terminal first:
roscore
rosrun turtlesim turtlesim_node
Finally start the publisher node in a new terminalturtle_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. ︎