From 7b55f1e325e5ada3d57519576e45433aef7d737a Mon Sep 17 00:00:00 2001 From: Christopher Herr Date: Wed, 18 Jan 2023 16:13:42 +0100 Subject: [PATCH] revert 3d0fb1ef73108093a4f8fac4e13e26e08c257f23 revert revert ed2f2c95c75d3fb8373d66c6c0775a5468b9737a revert ROS actionlib start --- src/pubsub/pubsub/pubsub_library_v3.py | 102 +++++++++++++++++++++++-- 1 file changed, 94 insertions(+), 8 deletions(-) diff --git a/src/pubsub/pubsub/pubsub_library_v3.py b/src/pubsub/pubsub/pubsub_library_v3.py index 9c1d07a..0aff1eb 100644 --- a/src/pubsub/pubsub/pubsub_library_v3.py +++ b/src/pubsub/pubsub/pubsub_library_v3.py @@ -16,19 +16,25 @@ import rclpy from rclpy.node import Node +from rclpy.action import ActionServer + +# How to use, please refer to: +# Topic: +# - topic_talker.py +# - topic_listener.py +# Service: +# - service_host.py +# - service_client.py +# Action: +# - action_server.py +# - action_client.py -# How to use: -# from pubsub_library import MinimalPublisher -# from pubsub_library import MinimalSubscriber -# minimal_publisher = MinimalPublisher(NODE_NAME='minimal_pub', TOPIC_NAME='user_controller', MSG_TYPE=Usercontroller, MSG_PERIOD=0.5) -# minimal_subscriber = MinimalSubscriber(NODE_NAME='minimal_sub', TOPIC_NAME='epos_feedback', MSG_TYPE=Eposreturn) -# See --> talker.py, listener.py # Definition of Parent Classes #******************************************************************************# -# TOPICS +# TOPIC #******************************************************************************# class MinimalPublisher(Node): @@ -175,7 +181,6 @@ class MinimalServiceProvider(Node): response.success = True # Change this to fit your service type return response - #******************************************************************************# class MinimalServiceClientAsync(Node): @@ -224,3 +229,84 @@ class MinimalServiceClientAsync(Node): return self.future.done(), response # response is of type ".Response()" else: return self.future.done(), None + + + + +#******************************************************************************# +# ACTIONS +#******************************************************************************# + +class MinimalActionServer(Node): + """ Minimal Action Server Class + Inputs: + * NODE_NAME: string + * ACT_NAME: string + * ACT_TYPE: action type class + * act_callback_input: action callback function + + Return: + * + """ + + def __init__(self, NODE_NAME, ACT_NAME, ACT_TYPE, act_callback_input=None): + self.NODE_NAME = NODE_NAME + self.ACT_NAME = ACT_NAME + self.ACT_TYPE = ACT_TYPE + # Check if user passed callback function + if act_callback_input == None: + self.execute_callback= self.std_act_callback + else: + self.execute_callback = act_callback_input + + # Init above laying class "Node" (super class) + super().__init__(self.NODE_NAME) + print("\tStarting Action Server:\t%s"%(self.ACT_NAME)) + self._action_server = ActionServer(self, self.ACT_TYPE, self.ACT_NAME, self.execute_callback) + + + # standard action callback + def std_act_callback(self, goal_handle): + """ Standard Action Callback Method + + Replace this method by inputting a custom callback method using 'act_callback_input'. + This method MUST return a variable of the type 'action result', + eg. result = ACTION_TYPE.Result() + """ + self.get_logger().info('Executing goal...') + ################# + # Action Method # + # here # + ################# + result = self.ACT_TYPE.Result() + return result + +#******************************************************************************# + +class MinimalActionClient(Node): + """ Minimal Action Client + Inputs: + * NODE_NAME: string + * ACT_NAME: string + * ACT_TYPE: action type class + * act_callback_input: action callback function + + Return: + * + """ + + def __init__(self, NODE_NAME, ACT_NAME, ACT_TYPE, act_callback_input=None): + self.NODE_NAME = NODE_NAME + self.ACT_NAME = ACT_NAME + self.ACT_TYPE = ACT_TYPE + + # Init above laying class "Node" (super class) + super().__init__(self.NODE_NAME) + print("\tStarting Action Client for:\t%s"%(self.ACT_NAME)) + self._action_client = ActionClient(self, self.ACT_TYPE, self.ACT_NAME) + + def send_goal(self, input): + return + + def abort_goal(self): + return \ No newline at end of file