赞
踩
ROS 版本是
- PARAMETERS
- * /rosdistro: melodic
- * /rosversion: 1.14.12
utuntu 系统版本 18.04
1.工作空间是存放工程开发的相关文件的文件夹
src:代码空间
build:编译空间
devel:开发空间
install:安装空间
2.创作工作空间指令
- $mkdir -p ~/catkin_ws/src
- $cd ~/catkin_ws/src
- $catkin_init_workspace
编译工作空间(注意编译工作空间一定要在工作空间的根目录下)
- $ cd ~/catkin_ws/
- $ catkin_make
设置环境变量
$ source devel/setup.bash
检查环境变量
$ echo $ROS_PACKAGE_PATH
3 创建功能包
创建功能包指令
- $ cd ~/catkin_ws/src
- $ catkin_create_pkg test_pkg std_msgs rospy roscpp
编译功能包
- $ cd ~/catkin_ws
- $ catkin_make
- $ source ~/catkin_ws/devel/setup.bash
4. 创建Topic的订阅发布方法
创建源码文件夹
$ mkdir ~/catkin_ws/src/test_pkg/scripts
创建publisher的python文件
- $ cd ~/catkin_ws/src/test_pkg/scripts
- $ vim talker.py
talker.py内容
- #!/usr/bin/env python
-
- 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
创建订阅的python文件
- $ cd ~/catkin_ws/src/test_pkg/scripts
- $ vim listener.py
listener.py的内容
- #!/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
- # name 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()
python不需要编译CMakeList.txt文件
直接运行roscore,以及listener.py,talker.py
运行ros程序
$ roscore
打开新的Terminal,运行 talker
- $ cd ~/catkin_ws/src/test_pkg/scripts
- $ python talker.py
打开新的Terminal,运行 listener
- $ cd ~/catkin_ws/src/test_pkg/scripts
- $ python listener.py
运行结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。