< >
Home » ROS与C++入门教程 » ROS与C++入门教程-actionlib-启动action服务器和客户端并带有数据节点

ROS与C++入门教程-actionlib-启动action服务器和客户端并带有数据节点

ROS与C++入门教程-actionlib-启动action服务器和客户端并带有数据节点

说明:

  • 介绍如何启动action服务器和客户端并带有数据节点

编写数据节点

  • 创建文件,actionlib_tutorials/scripts/gen_numbers.py,参考代码
cd ~/catkin_ws/src/actionlib_tutorials/scripts/
touch gen_numbers.py
vim gen_numbers.py
  • 内容如下:
#!/usr/bin/env python

import rospy
from std_msgs.msg import Float32
import random
def gen_number():
    pub = rospy.Publisher('random_number', Float32)
    rospy.init_node('random_number_generator', log_level=rospy.INFO)
    rospy.loginfo("Generating random numbers")

    while not rospy.is_shutdown():
        pub.publish(Float32(random.normalvariate(5, 1)))
        rospy.sleep(0.05)

if __name__ == '__main__':
  try:
    gen_number()
  except Exception, e:
    print "done"
  • 上面的代码生成以5为中心,标准偏差为1的正态分布的随机数,并在/random_number主题上发布数字。
  • 设置执行权限
chmod +x gen_numbers.py

启动数据节点

  • 新终端
$ roscore
  • 启动数据节点
$ rosrun actionlib_tutorials gen_numbers.py 
  • 显示:
Generating random numbers
  • 查看Action Feedback:
$ rostopic echo /averaging/feedback
  • 结果:
---
header: 
  seq: 1
  stamp: 1251489509536852000
  frame_id: 
status: 
  goal_id: 
    stamp: 1251489509511553000
    id: 1251489509.511553000
  status: 1
  text: 
feedback: 
  sample: 1
  data: 3.96250081062
  mean: 3.96250081062
  std_dev: 0.000687940046191
---
header: 
  seq: 2
  stamp: 1251489509588828000
  frame_id: 
status: 
  goal_id: 
    stamp: 1251489509511553000
    id: 1251489509.511553000
  status: 1
  text: 
feedback: 
  sample: 2
  data: 5.16988706589
  mean: 4.56619405746
  std_dev: 0.60369348526
---
  • 查看Action Result
$ rostopic echo /averaging/result
  • 结果:
---
header: 
  seq: 1
  stamp: 1251489786993936000
  frame_id: 
status: 
  goal_id: 
    stamp: 1251489781746524000
    id: 1251489781.746524000
  status: 4
  text: 
result: 
  mean: 4.99936008453
  std_dev: 1.10789334774
  • Action Node Graph 图形节点
$ rosrun rqt_graph rqt_graph &
  • 结果:

请输入图片描述

启动服务器客户端

  • 新终端,启动服务器:
$ rosrun actionlib_tutorials averaging_server
  • 显示:
[ INFO] 1251489514.736936000: /averaging: Aborted
  • 新终端,启动客户端:
$ rosrun actionlib_tutorials averaging_client
  • 显示:
[ INFO] 1251489514.737339000: Action finished: ABORTED

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ROS与C++入门教程