ros如何编译python文件_Python为ROS编写一个简单的发布者和订阅者
Python為ROS編寫(xiě)一個(gè)簡(jiǎn)單的發(fā)布者和訂閱者
1.創(chuàng)建工作空間
1.1建立文件夾hello_rospy,再在該目錄下建立子目錄src,并創(chuàng)建工作空間
mkdir -p ~/hello_rospy/src
cd ~/hello_rospy/src
catkin_init_workspace
1.2 編譯
cd ~/hello_rospy/
catkin_make
1.3設(shè)置運(yùn)行環(huán)境
echo "source ~/hello_rospy/devel/setup.bash" >> ~/.bashrc
1.4 檢查運(yùn)行環(huán)境(可以省略)
echo #ROS_PACKAGE_PATH
如果成功的話(huà),輸出內(nèi)容應(yīng)該包含,本空間的” …/devel/setup.bash"文件。
2.創(chuàng)建beginner_tutorials包
在src目錄下創(chuàng)建beginner_tutorials包
cd ~/hello_rospy/src
catkin_create_pkg beginner_tutorials std_msgs rospy roscpp
3.編寫(xiě)發(fā)布者程序和訂閱者程序
在beginner_tutorials 文件夾下面創(chuàng)建scripts文件夾,并在scripts文件夾下新建talker.py和listener.py兩個(gè)文件
roscd beginner_tutorials/
mkdir scripts
cd scripts
在scripts目錄下新建talker.py文件,寫(xiě)入如下內(nèi)容:
#!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String
def talker():
pub = rospy.Publisher('chatter', String, queue_size=10)
rospy.init_node('talker', anonymous=True)
rate = rospy.Rate(10) # 10hz
while not rospy.is_shutdown():
hello_str = "hello world %s" % rospy.get_time()
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
在scripts目錄下新建listener.py文件,寫(xiě)入如下內(nèi)容:
#!/usr/bin/env python
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
def listener():
# In ROS, nodes are uniquely named. If two nodes with the same
# node are launched, the previous one is kicked off. The
# anonymous=True flag means that rospy will choose a unique
# name for our 'listener' node so that multiple listeners can
# run simultaneously.
rospy.init_node('listener', anonymous=True)
rospy.Subscriber("chatter", String, callback)
# spin() simply keeps python from exiting until this node is stopped
rospy.spin()
if __name__ == '__main__':
listener()
更改文件權(quán)限為可執(zhí)行文件
chmod +x talker.py
chmod +x listener.py
4. 編譯
(Python編寫(xiě)可以省略)修改Cmakelist.txt為如下:
如果是按照上面編寫(xiě)的,Cmakelist.txt中的內(nèi)容步需要修改
cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)
## Find catkin macros and libraries
## if COMPONENTS list like find_package(catkin REQUIRED COMPONENTS xyz)
## is used, also find other catkin packages
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
catkin_package()
回到工作空間運(yùn)行,catkin_make,編譯:
catkin_make
5. 運(yùn)行
cd #回到梗目錄
roscore #啟動(dòng)ROS
rosrun beginner_tutorials talker.py
rosrun beginner_tutorials listener.py
運(yùn)行結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的ros如何编译python文件_Python为ROS编写一个简单的发布者和订阅者的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 京东方a是什么板块股票
- 下一篇: 银行卡黑卡是什么概念