程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[ROS] (08) ROS communication - topic (Topic) communication programming (Python)

編輯:Python

文章只是個人學習過程中學習筆記,主要參考ROS教程12.


[ROS](01)創建ROS工作空間

[ROS](02)創建&編譯ROS軟件包Package

目錄

  • 1、介紹
  • 2、Get topics and messages
  • 3、編寫發布者節點
    • 3.1 Python 代碼
    • 3.2 配置CMakeLists.txt
    • 3.3 配置package.xml
    • 3.4 編譯
  • 4、測試代碼

1、介紹

在 [ROS](06)ROS通信 —— 話題(Topic)通信 中通過鍵盤rostopic pub [turtle1/cmd_vel]命令控制Turtlesimturtle in motion.
This chapter passesPythonTo write the motion control node of the turtle,通過指定的話題,Also let the turtle continue to do a circular motion.

圖1-1 turtle_publisher 演示結果

2、Get topics and messages

啟動roscore、turtlesim_node,然後通過ROSCommands to get topics and messages,and the type of message.

圖2-1 通過ROSCommands to get topics and messages

3、編寫發布者節點

創建turtle_publisher(發布者)節點,The node will continuously broadcast messages.

3.1 Python 代碼

beginner_tutorialsCreate one under the packagescriptsdirectory to store the ones we createdPython腳本文件:turtle_publisher.py,並scriptsExecute 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

3.2 配置CMakeLists.txt

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}
)

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="[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>

3.4 編譯

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代碼.

4、測試代碼

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所示.


  1. ROS.otg. ROS教程[EB/OL]. 2020-12-22[2022-7-5].
    http://wiki.ros.org/cn/ROS/Tutorials. ︎

  2. .ROS.org. 編寫簡單的發布者和訂閱者(Python)[EB/OL]. 2020-12-25[2022-07-30]. https://wiki.ros.org/cn/ROS/Tutorials/WritingPublisherSubscriber%28python%29. ︎


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved