Note: This tutorial assumes that you have completed the previous tutorials: 编写一个行为客户端线程.
(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

使用其他节点运行行为服务器和客户端

Description: 本教程涵盖了使用其他节点运行一个平均服务器和客户端,然后可视化查看输出通道和节点图。

Tutorial Level: INTERMEDIATE

  Show EOL distros: 

编写数据节点

在开始运行服务器和客户端之前,需要创建一个数据节点。使用你最喜欢的编辑器创建actionlib_tutorials/scripts/gen_numbers.py,使用如下内容填充:

   1 #!/usr/bin/env python
   2 import roslib; roslib.load_manifest('actionlib_tutorials')
   3 import rospy
   4 from std_msgs.msg import Float32
   5 import random
   6 def gen_number():
   7     pub = rospy.Publisher('random_number', Float32)
   8     rospy.init_node('random_number_generator', log_level=rospy.INFO)
   9     rospy.loginfo("Generating random numbers")
  10 
  11     while not rospy.is_shutdown():
  12         pub.publish(Float32(random.normalvariate(5, 1)))
  13         rospy.sleep(0.05)
  14 
  15 if __name__ == '__main__':
  16   try:
  17     gen_number()
  18   except Exception, e:
  19     print "done"

   1 #!/usr/bin/env python
   2 
   3 import rospy
   4 from std_msgs.msg import Float32
   5 import random
   6 def gen_number():
   7     pub = rospy.Publisher('random_number', Float32)
   8     rospy.init_node('random_number_generator', log_level=rospy.INFO)
   9     rospy.loginfo("Generating random numbers")
  10 
  11     while not rospy.is_shutdown():
  12         pub.publish(Float32(random.normalvariate(5, 1)))
  13         rospy.sleep(0.05)
  14 
  15 if __name__ == '__main__':
  16   try:
  17     gen_number()
  18   except Exception, e:
  19     print "done"

以上代码使用一个正态分布生成随机5个数字,并且标准差为1,然后发布数据到/random_number话题。

不要忘记编译节点可运行:

  • chmod +x gen_numbers.py

开启数据节点

在一个新终端,运行roscore:

$ roscore

现在在一个新终端运行数据节点:

rosrun actionlib_tutorials gen_numbers.py 

你将会看到:

  • Generating random numbers

查看行为反馈

在一个新终端,rostopic查看反馈通道,并查看从行为服务器传来的反馈:

$ 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
    ---

查看行为结果

在一个新终端,rostopic查看反馈通道,查看从行为服务器传来的反馈:

$ 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

查看行为节点图

另外你可以查看节点:

$ rxgraph &

$ rosrun rqt_graph rqt_graph &

averaging_client_server.png

运行客户端和服务器

在一个新终端运行行为服务器:

$ rosrun actionlib_tutorials averaging_server

当行为完成后,会打印输出信息到平台,有可能是成功或崩溃,取决于随机数采样。

  • [ INFO] 1251489514.736936000: /averaging: Aborted

在新的终端运行行为客户端:

  • $ rosrun actionlib_tutorials averaging_client

当客户端接收到目标完成通知时,会打印行为结果信息到屏幕上:

  • [ INFO] 1251489514.737339000: Action finished: ABORTED

Wiki: cn/actionlib_tutorials/Tutorials/RunningServerAndClientWithNodes (last edited 2017-03-19 07:21:09 by Playfish)