一,定义msg消息
$ roscd beginner_tutorials
$ mkdir msg
$ cd msg
$ touch Num.msg
$ rosed beginner_tutorials Num.msg
int64 num
rosed 你的功能包名-—》【beginner_tutorials】 package.xml
,增加依赖,message_generation
message_runtime
rosed 你的功能包名--》【beginner_tutorials】 CMakeLists.txt
,增加依赖,# Do not just add this line to your CMakeLists.txt, modify the existing line
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
message_generation
)
add_message_files(
FILES
Num.msg
)
generate_messages(
DEPENDENCIES
std_msgs
)
catkin_package(
# INCLUDE_DIRS include
# LIBRARIES beginner_tutorials
CATKIN_DEPENDS roscpp rospy std_msgs message_runtime
# DEPENDS system_lib
)
$ cd ~/catkin_ws
$ catkin_make
$ rosmsg show beginner_tutorials/Num
int64 num
编写发布器的步骤
$ roscd beginner_tutorials
$ mkdir scripts
$ cd scripts
https://github.com/ros/ros_tutorials/blob/kinetic-devel/rospy_tutorials/001_talker_listener/talker.py
$ touch talker.py #生成文件
$ chmod +x talker.py #设置可执行
$ rosed beginner_tutorials talker.py #自己输入代码
内容如下:
#!/usr/bin/env python
#coding=utf-8
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 = "超哥 好帅啊 %s" % rospy.get_time()
rospy.loginfo(hello_str)
pub.publish(hello_str)
rate.sleep()
if __name__ == '__main__':
try:
talker()
except rospy.ROSInterruptException:
pass
代码:#!/usr/bin/env python
分析:指定通过python解释代码
代码:import rospy
分析:导入rospy包,rospy是ROS的python客户端。参考rospy API接口
代码:from std_msgs.msg import String
分析:
msg = String()
msg.data = str
String(data=str)
参考python的标准函数库
代码:def talker()
分析:定义talker函数,参考如何定义函数
代码:pub = rospy.Publisher('chatter', String, queue_size=10)
分析:定义发布的主题名称chatter, 消息类型String,实质是std_msgs.msg.String, 设置队列条目个数.参考rospy.Publisher API
代码:rospy.init_node('talker', anonymous=True)
分析:
代码:rate = rospy.Rate(10) # 10hz
分析:设置发布的频率,单位是每秒次数,这是每秒10次的频率发布主题,参考rospy.Rate API
代码:rospy.is_shutdown()
分析:用于检测程序是否退出,是否按Ctrl-C 或其他
代码:rospy.loginfo
分析:在屏幕输出日志信息,写入到rosout节点,也可以通过rqt_console来查看。参考rospy.loginfo API
代码:pub.publish(hello_str)
分析:发布信息到主题,参考pub.publish API
代码:rate.sleep()
分析:睡眠一定持续时间,如果参数为负数,睡眠会立即返回。参考 sleep api
$ cd ~/catkin_ws
$ catkin_make
$ roscore
$rosrun beginner_tutorials talker.py
[INFO] [WallTime: 1526964840.801095] 超哥 好帅啊 1526964840.8
[INFO] [WallTime: 1526964840.901098] 超哥 好帅啊 1526964840.9
[INFO] [WallTime: 1526964841.001053] 超哥 好帅啊 1526964841.0
[INFO] [WallTime: 1526964841.101093] 超哥 好帅啊 1526964841.1
[INFO] [WallTime: 1526964841.201097] 超哥 好帅啊 1526964841.2
$ rostopic echo /chatter
data: 超哥 好帅啊 1478444433.95
---
data: 超哥 好帅啊 1478444434.45
---
编写订阅器的步骤
https://github.com/ros/ros_tutorials/blob/kinetic-devel/rospy_tutorials/001_talker_listener/listener.py
$ roscd beginner_tutorials
$ cd scripts
$ touch listener.py
$ chmod +x listener.py
$ rosed beginner_tutorials listener.py
#!/usr/bin/env python
#coding=utf-8
import rospy
from std_msgs.msg import String
def callback(data):
rospy.loginfo(rospy.get_caller_id() + '我觉得 %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.
#对上面注释翻译
#在ROS中,节点是唯一命名的。 如果两个节点相同
#名称被启动,前一个被启动。该
#anonymous = True标志意味着rospy会选择一个独特的
#我们的'侦听器'节点的名称,以便多个侦听器可以
#同时运行。
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()
代码:rospy.init_node('listener', anonymous=True)
分析:
代码: rospy.Subscriber("chatter", String, callback)
分析:
代码:rospy.spin()
分析:保持节点运行,直到节点关闭。不像roscpp,rospy.spin不影响订阅的回调函数,因为回调函数有自己的线程。
$ cd ~/catkin_ws
$ catkin_make
$ roscore
$rosrun beginner_tutorials listener.py
[INFO] [WallTime: 1526964838.601590] /listener_1299_1526964825697我觉得 超哥 好帅啊 1526964838.6
[INFO] [WallTime: 1526964838.701610] /listener_1299_1526964825697我觉得 超哥 好帅啊 1526964838.7
[INFO] [WallTime: 1526964838.801621] /listener_1299_1526964825697我觉得 超哥 好帅啊 1526964838.8
[INFO] [WallTime: 1526964838.901650] /listener_1299_1526964825697我觉得 超哥 好帅啊 1526964838.9
[INFO] [WallTime: 1526964839.001606] /listener_1299_1526964825697我觉得 超哥 好帅啊 1526964839.0
[INFO] [WallTime: 1526964839.101618] /listener_1299_1526964825697我觉得 超哥 好帅啊 1526964839.1
$ rostopic pub -r 10 /chatter std_msgs/String "test"
$ rostopic echo /chatter
data: test
---
data: test
---
data: test
---
data: test
---
data: test
---
data: test