网站建设 洪塔,福田网站制作比较好的,成全视频免费高清观看在线动漫,购物商城网站开发公司Python为ROS编写一个简单的发布者和订阅者1.创建工作空间1.1建立文件夹hello_rospy,再在该目录下建立子目录src,并创建工作空间mkdir -p ~/hello_rospy/srccd ~/hello_rospy/srccatkin_init_workspace1.2 编译cd ~/hello_rospy/catkin_make1.3设置运行环境echo source ~/…Python为ROS编写一个简单的发布者和订阅者1.创建工作空间1.1建立文件夹hello_rospy,再在该目录下建立子目录src,并创建工作空间mkdir -p ~/hello_rospy/srccd ~/hello_rospy/srccatkin_init_workspace1.2 编译cd ~/hello_rospy/catkin_make1.3设置运行环境echo source ~/hello_rospy/devel/setup.bash ~/.bashrc1.4 检查运行环境(可以省略)echo #ROS_PACKAGE_PATH如果成功的话输出内容应该包含本空间的” …/devel/setup.bash文件。2.创建beginner_tutorials包在src目录下创建beginner_tutorials包cd ~/hello_rospy/srccatkin_create_pkg beginner_tutorials std_msgs rospy roscpp3.编写发布者程序和订阅者程序在beginner_tutorials 文件夹下面创建scripts文件夹并在scripts文件夹下新建talker.py和listener.py两个文件roscd beginner_tutorials/mkdir scriptscd scripts在scripts目录下新建talker.py文件写入如下内容#!/usr/bin/env python# license removed for brevityimport rospyfrom std_msgs.msg import Stringdef talker():pub rospy.Publisher(chatter, String, queue_size10)rospy.init_node(talker, anonymousTrue)rate rospy.Rate(10) # 10hzwhile 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文件写入如下内容#!/usr/bin/env pythonimport rospyfrom std_msgs.msg import Stringdef 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# anonymousTrue flag means that rospy will choose a unique# name for our listener node so that multiple listeners can# run simultaneously.rospy.init_node(listener, anonymousTrue)rospy.Subscriber(chatter, String, callback)# spin() simply keeps python from exiting until this node is stoppedrospy.spin()if __name__ __main__:listener()更改文件权限为可执行文件chmod x talker.pychmod x listener.py4. 编译(Python编写可以省略)修改Cmakelist.txt为如下如果是按照上面编写的Cmakelist.txt中的内容步需要修改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 packagesfind_package(catkin REQUIRED COMPONENTSroscpprospystd_msgs)catkin_package()回到工作空间运行,catkin_make,编译catkin_make5. 运行cd #回到梗目录roscore #启动ROSrosrun beginner_tutorials talker.pyrosrun beginner_tutorials listener.py运行结果