ROS actionlib start

This commit is contained in:
winterhalderp 2021-04-29 16:35:24 +02:00
parent e2aa178cac
commit ed2f2c95c7

View File

@ -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 "<service_type_name>.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