publishing ROS topic from the execution callback of ROS Action

asked 2019-05-22 07:59:07 -0500

SkandaNR gravatar image

Hello,

I am building a state machine node(ROS2 Action_client) that interact with Planner node(ROS2 Action_server). In the execution callback of the planner node I need to publish a topic (from the same node). Is it possible to publish a topic while action is running?

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 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()

    # The default SingleThreadedExecutor causes the action server to process goals sequentially
    rclpy.spin(minimal_action_server)

    minimal_action_server.destroy()
    rclpy.shutdown()

with above code I am getting following error,

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
edit retag flag offensive close merge delete

Comments

You have created a publisher for Bool type messages and you are trying to publish a string with it.

You should also create your publisher within the class __int__ method so it's specific to that instance, but that wouldn't cause this error.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-05-22 10:21:51 -0500 )edit