< >
Home » ROS与Python入门教程 » ROS与Python入门教程-actionlib-开发数据节点

ROS与Python入门教程-actionlib-开发数据节点

ROS与Python入门教程-actionlib-开发数据节点

说明

  • 介绍需要数据节点进行交互的action的服务器和客户端,然后可视化节点图

开发数据节点

  • 访问代码库
  • 新建learning_actionlib/scripts/gen_numbers.py
$ roscd learning_actionlib
$ touch scripts/gen_numbers.py
$ chmod +x scripts/gen_numbers.py
$ rosed scripts/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"

启动节点:

  • 新终端打开:
$ roscore
  • 新终端打开:
$ rosrun learning_actionlib gen_numbers.py 
  • 效果:
Generating random numbers

查看action反馈

  • 例子里,这个action服务端和客户端都是使用c++编写,直接下载库的代码包,编译
  • 新终端打开
$ 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结果

  • 命令:
$ 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节点图

  • 命令:
$ rosrun rqt_graph rqt_graph &
  • 效果:

请输入图片描述

启动服务端和客户端

  • 新终端打开服务端,命令:
$ rosrun learning_actionlib averaging_server
  • 新终端打开服务端,命令:
 $ rosrun learning_actionlib averaging_client

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

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


标签: ros与python入门教程