srv host and client implemented successfully
This commit is contained in:
parent
2d7675fc93
commit
183e08e853
@ -1,10 +1,3 @@
|
||||
# Header data, eg timestamp
|
||||
# Problem: "header__struct.hpp: No such file or directory"
|
||||
#Header header
|
||||
|
||||
# Sensor Data coming back from an array of atmospheric sensors
|
||||
float32[] temperature
|
||||
float32[] pressure
|
||||
float32[] humidity
|
||||
string command
|
||||
---
|
||||
bool success
|
||||
|
@ -28,8 +28,8 @@ from rclpy.node import Node
|
||||
|
||||
# Definition of Parent Classes
|
||||
#******************************************************************************#
|
||||
|
||||
|
||||
# TOPICS
|
||||
#******************************************************************************#
|
||||
|
||||
class MinimalPublisher(Node):
|
||||
|
||||
@ -77,7 +77,6 @@ class MinimalPublisher(Node):
|
||||
|
||||
#******************************************************************************#
|
||||
|
||||
|
||||
class MinimalSubscriber(Node):
|
||||
|
||||
def __init__(self, NODE_NAME, TOPIC_NAME, MSG_TYPE, NUM_MSGS):
|
||||
@ -105,9 +104,12 @@ class MinimalSubscriber(Node):
|
||||
return self.msg
|
||||
|
||||
|
||||
#******************************************************************************#
|
||||
|
||||
|
||||
#******************************************************************************#
|
||||
# SERVICE
|
||||
#******************************************************************************#
|
||||
|
||||
class MinimalServiceProvider(Node):
|
||||
def __init__(self, NODE_NAME, SRV_NAME, SRV_TYPE):
|
||||
self.NODE_NAME = NODE_NAME
|
||||
@ -126,20 +128,25 @@ class MinimalServiceProvider(Node):
|
||||
* request.<name>
|
||||
* response.<name>
|
||||
"""
|
||||
self.get_logger().info("Service Call received")
|
||||
|
||||
# Add callback code here:
|
||||
# What does Service do?
|
||||
# ....
|
||||
|
||||
|
||||
#self.get_logger().info('Incoming request\na: %d b: %d c: %d' % (request.a, request.b, request.c))
|
||||
# Abstraction layer:
|
||||
response = self.user_defined(request, response) # Function defined below
|
||||
|
||||
return response
|
||||
|
||||
|
||||
# Insert your callback code here
|
||||
def user_defined(self, request, response):
|
||||
# Write user defined code here
|
||||
# Adapt to fit your service type
|
||||
response.success = True
|
||||
return response
|
||||
|
||||
|
||||
#******************************************************************************#
|
||||
|
||||
|
||||
class MinimalServiceClient(Node):
|
||||
class MinimalServiceClientAsync(Node):
|
||||
def __init__(self, NODE_NAME, SRV_NAME, SRV_TYPE):
|
||||
self.NODE_NAME = NODE_NAME
|
||||
self.SRV_NAME = SRV_NAME
|
||||
@ -154,11 +161,20 @@ class MinimalServiceClient(Node):
|
||||
def send_request(self, request):
|
||||
"""
|
||||
Feed request of type "SRV_TYPE.Request()"
|
||||
Access variable using
|
||||
* request = SRV_TYPE.Request()
|
||||
* request.<var_name> = ....
|
||||
"""
|
||||
self.request = request
|
||||
self.future = self.srv_client.call_async(self.request)
|
||||
|
||||
def check_if_service_completed(self):
|
||||
def check_if_service_complete(self):
|
||||
""" Checks if service call was answered by host.
|
||||
|
||||
Returns tuple [done, response]:
|
||||
* done: Bool (service call complete)
|
||||
* response: msg class from srv type
|
||||
"""
|
||||
if self.future.done():
|
||||
try:
|
||||
response = self.future.result()
|
||||
@ -166,10 +182,9 @@ class MinimalServiceClient(Node):
|
||||
self.get_logger().info(
|
||||
'Service call failed %r' % (e,))
|
||||
else:
|
||||
pass
|
||||
# Adapt to fit service type
|
||||
#self.get_logger().info(
|
||||
'Result of add_three_ints: for %d + %d + %d = %d' %
|
||||
(minimal_client.req.a, minimal_client.req.b, minimal_client.req.c, response.sum))
|
||||
return response
|
||||
# self.get_logger().info('Result of add_three_ints: for %d + %d + %d = %d' %(minimal_client.req.a, minimal_client.req.b, minimal_client.req.c, response.sum))
|
||||
return self.future.done(), response
|
||||
else:
|
||||
return None
|
||||
return self.future.done(), None
|
||||
|
39
src/pubsub/pubsub/service_client.py
Normal file
39
src/pubsub/pubsub/service_client.py
Normal file
@ -0,0 +1,39 @@
|
||||
import rclpy
|
||||
from rclpy.node import Node
|
||||
|
||||
# Service Type Files
|
||||
from custom_interfaces.srv import CustomSrv1
|
||||
|
||||
# Service Minimal Host and Client
|
||||
import pubsub.pubsub_library_v3 as lib
|
||||
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
|
||||
# Create Service Client
|
||||
minimal_client = lib.MinimalServiceClientAsync(NODE_NAME="srv_client_node", SRV_NAME="test_srv", SRV_TYPE=CustomSrv1)
|
||||
|
||||
# Create Service Request
|
||||
srv_request = CustomSrv1.Request()
|
||||
srv_request.command = "Hallo"
|
||||
minimal_client.send_request(srv_request) # send request to service provider
|
||||
|
||||
while rclpy.ok():
|
||||
try:
|
||||
rclpy.spin_once(minimal_client, timeout_sec=0.1)
|
||||
|
||||
done, response = minimal_client.check_if_service_complete()
|
||||
if done:
|
||||
print(response.success)
|
||||
else:
|
||||
print("Service not complete")
|
||||
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
print("\n\nShutting down...")
|
||||
minimal_client.destroy_node()
|
||||
rclpy.shutdown()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
27
src/pubsub/pubsub/service_host.py
Normal file
27
src/pubsub/pubsub/service_host.py
Normal file
@ -0,0 +1,27 @@
|
||||
import rclpy
|
||||
from rclpy.node import Node
|
||||
|
||||
# Service Type Files
|
||||
from custom_interfaces.srv import CustomSrv1
|
||||
|
||||
# Service Minimal Host and Client
|
||||
import pubsub.pubsub_library_v3 as lib
|
||||
|
||||
|
||||
def main(args=None):
|
||||
rclpy.init(args=args)
|
||||
|
||||
minimal_service = lib.MinimalServiceProvider(NODE_NAME="Service_Host_Test", SRV_NAME="test_srv", SRV_TYPE=CustomSrv1)
|
||||
|
||||
while rclpy.ok():
|
||||
try:
|
||||
rclpy.spin_once(minimal_service, timeout_sec=0.1)
|
||||
|
||||
except (KeyboardInterrupt, SystemExit):
|
||||
print("\n\nShutting down...")
|
||||
minimal_service.destroy_node()
|
||||
rclpy.shutdown()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
@ -21,7 +21,9 @@ setup(
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'talker = pubsub.talker:main',
|
||||
'listener = pubsub.listener:main'
|
||||
'listener = pubsub.listener:main',
|
||||
'srvhost = pubsub.service_host:main',
|
||||
'srvclient = pubsub.service_client:main'
|
||||
],
|
||||
},
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user