首页 > 解决方案 > 从 ROS Action 的执行回调中发布 ROS 主题

问题描述

我正在构建一个与 Planner 节点(ROS2 Action_server)交互的状态机节点(ROS2 Action_client)。在计划节点的执行回调中,我需要发布一个主题(来自同一个节点)。是否可以在操作运行时发布主题?

class PlanningProblemServer(Node):
def __init__(self): super().__init__('planning_problem_server')
self._action_server = ActionServer(self, PlanAid, 'planning_problem_sm',   
                                                   execute_callback=self.execute_callback, 
                                                   goal_callback=self.goal_callback, 
                                                   cancel_callback=self.cancel_callback)
self._publisher = self.create_publisher(Bool, 'Planning_problem_data')

def goal_callback(self, goal_request): #Some code
def cancel_callback(self, goal_handle): #Some code

def execute_callback(self, goal_handle):
    feedback_msg.feedback = "loading planning problem...!"
    goal_handle.publish_feedback(feedback_msg)
    msg = String()
    msg.data = "abc"
    self._publisher.publish(msg)
    goal_handle.set_succeeded()
    return result

def main(args=None):
    rclpy.init(args=args)
    minimal_action_server = PlanningProblemServer()
    rclpy.spin(minimal_action_server)
    minimal_action_server.destroy()
    rclpy.shutdown()

if __name__ == '__main__': main()

使用上面的代码,我收到以下错误,

Traceback (most recent call last):
File "/home/developer/ros2_ws/ros2/src/planning_problem/planning_prob_pkg/planning_prob_node.py", line 102, in <module>
if __name__ == '__main__': main()
File "/home/developer/ros2_ws/ros2/src/planning_problem/planning_prob_pkg/planning_prob_node.py", line 96, in main
rclpy.spin(minimal_action_server)
File "/opt/ros/crystal/lib/python3.6/site-packages/rclpy/__init__.py", line 119, in spin
executor.spin_once()
File "/opt/ros/crystal/lib/python3.6/site-packages/rclpy/executors.py", line 572, in spin_once
raise handler.exception()
File "/opt/ros/crystal/lib/python3.6/site-packages/rclpy/task.py", line 206, in __call__
self._handler.send(None)
File "/opt/ros/crystal/lib/python3.6/site-packages/rclpy/action/server.py", line 323, in _execute_goal
execute_result = await await_or_execute(execute_callback, goal_handle)
File "/opt/ros/crystal/lib/python3.6/site-packages/rclpy/executors.py", line 92, in await_or_execute
return callback(*args)
File "/home/developer/ros2_ws/ros2/src/planning_problem/planning_prob_pkg/planning_prob_node.py", line 59, in execute_callback
self._publisher.publish(msg)
File "/opt/ros/crystal/lib/python3.6/site-packages/rclpy/publisher.py", line 28, in publish
_rclpy.rclpy_publish(self.publisher_handle, msg)
ValueError: PyCapsule_GetPointer called with invalid PyCapsule object

标签: ros

解决方案


我对ros python接口不是很好

但乍一看,您是否将发布者创建为 bool 类型并将其作为字符串发送?


推荐阅读