回调函数未被调用

时间:2018-03-09 22:41:34

标签: python callback ros

我正在实施以下代码:

class NavigationServer:

  def __init__(self, name):
    self._action_name = name
    self._as = actionlib.SimpleActionServer(self._action_name, 
    rodain_get_directions.msg.NavigateAction, 
    execute_cb=self.execute_cb, auto_start=False)
    self._as.start()

   def execute_cb(self, goal):
     rospy.loginfo("starting callback")
     self._as.set_succeeded()

这是我的客户方:

def __goal_action_callback(data):

    rospy.loginfo(RobotMoving.get_name() + " " + "I heard {}".format(data))

    ac = SimpleActionClient("nav_server", NavigateAction)

    rospy.loginfo("goalCallback: Waiting for action server to start.")
    ac.wait_for_server()

    rospy.loginfo("goalCallback: Navigation service found.")

    goal = NavigateGoal()

    goal.target_pos = data

    ac.send_goal_and_wait(goal)

    if ac.get_state() == GoalStatus.SUCCEEDED:
        rospy.loginfo("Navigate action is completed!")
    else:
        rospy.loginfo("Navigate action failed!")

由于某些原因,我的callback函数未被调用,并且消息未被写入日志文件中。知道为什么会这样吗? 客户端工作正常。    SimpleActionServer是用于初始化服务器的ROS方法。

编辑1:修复了回调预期参数

编辑2:添加主要功能

if __name__ == "__main__":
  rospy.init_node("nav_server")

  rospy.loginfo("Starting navigation_server node")

  server = NavigationServer(rospy.get_name())
  rospy.spin()

编辑3:添加__goal_action_callback调用和客户端日志文件

def subscribe_to_topic(self, topic_name, topic_type):
    rospy.Subscriber(topic_name, topic_type, RobotMoving.__goal_action_callback) 

 uav.subscribe_to_topic("/directions", Point)

 [INFO] [1520695340.703649, 0.000000]: Waiting for service enable_motors
 [INFO] [1520695342.820296, 0.107000]: Turning on motors....
 [INFO] [1520695342.824292, 0.107000]: Navigation Client: Motors on
 [INFO] [1520695343.868810, 1.033000]: Navigation Client I heard x: 
 1.0
 y: 5.0
 z: 3.0
 [INFO] [1520695343.929997, 1.085000]: goalCallback: Waiting for 
 action server to start.
 [INFO] [1520695351.322910, 7.894000]: goalCallback: Navigation 
 service found.
 [INFO] [1520695351.323404, 7.894000]: Navigate action failed: 0

1 个答案:

答案 0 :(得分:0)

execute_cb的构造函数的SimpleActionServer参数定义了一个带有目标参数的回调函数。回调的documentation不是很好,但查看source code将显示传递的参数。

要通知客户端成功,您需要将服务设置为成功。在这种情况下,允许结果作为可选参数传递。

在回调实现中添加参数并通知成功应该可以解决问题:

def execute_cb(self, goal):
    rospy.loginfo("starting callback")
    self.__as.set_succeeded()