-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Behaviors to Interact with ROS Services (#215)
- Loading branch information
1 parent
ba572a7
commit 1729e28
Showing
11 changed files
with
775 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ | |
|
||
from . import actions | ||
from . import dock | ||
from . import services | ||
from . import set_bool |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# License: BSD | ||
# https://raw.githubusercontent.com/splintered-reality/py_trees_ros_tutorials/devel/LICENSE | ||
# | ||
############################################################################## | ||
# Documentation | ||
############################################################################## | ||
|
||
""" | ||
Service server templates. | ||
""" | ||
|
||
############################################################################## | ||
# Imports | ||
############################################################################## | ||
|
||
import time | ||
|
||
import rclpy | ||
|
||
############################################################################## | ||
# Service Server | ||
############################################################################## | ||
# | ||
# References: | ||
# | ||
# service client : https://github.com/ros2/rclpy/blob/rolling/rclpy/rclpy/client.py | ||
# service server : https://github.com/ros2/rclpy/blob/rolling/rclpy/rclpy/service.py | ||
# service examples : https://github.com/ros2/examples/tree/rolling/rclpy/services | ||
|
||
class GenericServer(object): | ||
""" | ||
Generic service server that can be used for testing. | ||
Args: | ||
node_name (:obj:`str`): name of the node | ||
service_name (:obj:`str`): name of the service | ||
service_type (:obj:`type`): type of the service | ||
sleep_time (:obj:`float`): time to sleep before returning a response | ||
callback (:obj:`Optional[callable]`): callback to execute when the service is called | ||
""" | ||
def __init__(self, | ||
node_name, | ||
service_name, | ||
service_type, | ||
sleep_time=1.0, | ||
callback=None): | ||
self.node = rclpy.create_node(node_name) | ||
|
||
self.sleep_time = sleep_time | ||
self.callback = callback | ||
|
||
# Create the service | ||
self.server = self.node.create_service( | ||
service_type, | ||
service_name, | ||
self.execute_callback | ||
) | ||
|
||
def execute_callback(self, request, response): | ||
""" | ||
Execute the callback and populate the response. | ||
""" | ||
if self.callback is not None: | ||
return self.callback(request, response) | ||
|
||
# Do nothing | ||
time.sleep(self.sleep_time) | ||
return response | ||
|
||
def shutdown(self): | ||
""" | ||
Cleanup | ||
""" | ||
self.server.destroy() | ||
self.node.destroy_node() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding: utf-8 -*- | ||
# | ||
# License: BSD | ||
# https://raw.githubusercontent.com/splintered-reality/py_trees_ros/devel/LICENSE | ||
# | ||
############################################################################## | ||
# Documentation | ||
############################################################################## | ||
|
||
""" | ||
Mocks a SetBool service | ||
""" | ||
|
||
|
||
############################################################################## | ||
# Imports | ||
############################################################################## | ||
|
||
import time | ||
|
||
from std_srvs.srv import SetBool | ||
|
||
from . import services | ||
|
||
############################################################################## | ||
# Class | ||
############################################################################## | ||
|
||
|
||
class SetBoolServer(services.GenericServer): | ||
""" | ||
Simple server that docks if the goal is true, undocks otherwise. | ||
""" | ||
SUCCESS_MESSAGE = "Succeeded in setting bool to True" | ||
FAILURE_MESSAGE = "Failed to set bool to False" | ||
|
||
def __init__(self, sleep_time=1.0): | ||
super().__init__( | ||
node_name="set_bool_server", | ||
service_name="set_bool", | ||
service_type=SetBool, | ||
sleep_time=sleep_time, | ||
callback=self.callback, | ||
) | ||
|
||
def callback(self, request, response): | ||
time.sleep(self.sleep_time) | ||
if request.data: | ||
response.success = True | ||
response.message = SetBoolServer.SUCCESS_MESSAGE | ||
else: | ||
response.success = False | ||
response.message = SetBoolServer.FAILURE_MESSAGE | ||
return response |
Oops, something went wrong.