This wrap uses the spin of an executor in a new thread. As a result, the calls of services and actions can be used synchronously. Besides, this node also includes predefined action servers and one client to make its use more user-friendly to developers.
This example shows how to create a node. To block the node, join_spin is used, which blocks the main thread till the thread of the spin ends.
fromsimple_nodeimportNodeimportrclpyclassMyNode(Node):
def__init__(self):
super().__init__("my_node")
defmain():
rclpy.init()
node=MyNode()
node.join_spin()
rclpy.shutdown()
if__name__=="__main__":
main()The following example shows how to create an action server that treats only one goal at the same time. If a new goal is received, the server aborts the current goal and starts treating the new one.
classMyNode(Node):
def__init__(self):
super().__init__("my_node")
self.__action_server=self.create_action_server(ActionType,
"action_name",
self.__execute_cb)
def__execute_server(self, goal_handle):
result=ActionType.Result()
succeed=self.do_something(goal_handle.request)
ifself.__action_server.is_canceled():
self.__action_server.wait_for_canceling()
goal_handle.canceled()
else:
ifsucceed:
goal_handle.succeed()
else:
goal_handle.abort()
returnresultThe following example shows how to create an action client.
classMyNode(Node):
def__init__(self):
super().__init__("my_node")
self.__action_client=self.create_action_client(
ActionType, "action_name")
goal=ActionType.Goal()
self.__action_client.wait_for_server()
self.__action_client.send_goal(goal)
self.__action_client.wait_for_result()
self.__action_client.is_succeeded()