-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rclpy minimal services #140
Changes from 16 commits
d5186a7
13a133b
532098f
a328773
1dd0719
4468432
5528842
4234eb6
abf6146
2c00b58
d1ac262
495f6f4
eeec2d3
bc2a262
4eddd82
247edc9
ef99053
d6cb134
3dc5064
5306074
f8ea98a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Minimal service client cookbook recipes | ||
|
||
This package contains a few strategies to create service clients. | ||
The `client` recipe shows how to request data from a service with a blocking call. | ||
The `client_async` recipe shows how to request data from a service with a non blocking call. The user has to check if a response has been received in the main loop | ||
The `client_async_member_function` recipe is analog to `client_async` but sends the request inside a MinimalClient class |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2016 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import time | ||
|
||
import rclpy | ||
|
||
from example_interfaces.srv import AddTwoInts | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args) | ||
node = rclpy.create_node('minimal_client') | ||
cli = node.create_client(AddTwoInts, 'add_two_ints') | ||
|
||
req = AddTwoInts.Request() | ||
req.a = 41 | ||
req.b = 1 | ||
# wait for connection to be established | ||
# (no wait for service in Python yet) | ||
time.sleep(1) | ||
|
||
cli.call(req) | ||
# when calling wait for future | ||
# spin should not be called in the main loop | ||
cli.wait_for_future() | ||
print('Result of add_two_ints: for %d + %d = %d' % ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: wrap directly after parenthesis, e.g.:
Same below. |
||
req.a, | ||
req.b, | ||
cli.response.sum)) | ||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Copyright 2016 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import time | ||
|
||
import rclpy | ||
|
||
from example_interfaces.srv import AddTwoInts | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args) | ||
|
||
node = rclpy.create_node('minimal_client_async') | ||
|
||
cli = node.create_client(AddTwoInts, 'add_two_ints') | ||
|
||
req = AddTwoInts.Request() | ||
req.a = 41 | ||
req.b = 1 | ||
# wait for connection to be established | ||
# (no wait for service in Python yet) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO here as well |
||
time.sleep(1) | ||
cli.call(req) | ||
while rclpy.ok(): | ||
if cli.response is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this is not final API I would suggest to put a big todo above this line (and similar lines below). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about this TODO message? 5306074 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
print('Result of add_two_ints: for %d + %d = %d' % ( | ||
req.a, | ||
req.b, | ||
cli.response.sum)) | ||
break | ||
rclpy.spin_once(node) | ||
|
||
rclpy.shutdown() | ||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Copyright 2016 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import time | ||
|
||
import rclpy | ||
|
||
from example_interfaces.srv import AddTwoInts | ||
|
||
|
||
class MinimalClientAsync: | ||
def __init__(self, node): | ||
self.cli = node.create_client(AddTwoInts, 'add_two_ints') | ||
# wait for connection to be established | ||
# (no wait for service in Python yet) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO |
||
time.sleep(1) | ||
self.req = AddTwoInts.Request() | ||
|
||
def send_request(self): | ||
self.req.a = 41 | ||
self.req.b = 1 | ||
self.cli.call(self.req) | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args) | ||
|
||
node = rclpy.create_node('minimal_client_async') | ||
|
||
minimal_client = MinimalClientAsync(node) | ||
minimal_client.send_request() | ||
|
||
while rclpy.ok(): | ||
if minimal_client.cli.response is not None: | ||
print('Result of add_two_ints: for %d + %d = %d' % ( | ||
minimal_client.req.a, | ||
minimal_client.req.b, | ||
minimal_client.cli.response.sum)) | ||
break | ||
rclpy.spin_once(node) | ||
|
||
rclpy.shutdown() | ||
|
||
if __name__ == '__main__': | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0"?> | ||
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> | ||
<package format="2"> | ||
<name>examples_rclpy_minimal_client</name> | ||
<version>0.0.0</version> | ||
<description>Examples of minimal service clients using rclpy.</description> | ||
|
||
<maintainer email="mikael@osrfoundation.org">Mikael Arguedas</maintainer> | ||
<license>Apache License 2.0</license> | ||
|
||
<exec_depend>rclpy</exec_depend> | ||
<exec_depend>example_interfaces</exec_depend> | ||
<exec_depend>std_msgs</exec_depend> | ||
|
||
<!-- These test dependencies are optional | ||
Their purpose is to make sure that the code passes the linters --> | ||
<test_depend>ament_copyright</test_depend> | ||
<test_depend>ament_pep257</test_depend> | ||
<test_depend>ament_pep8</test_depend> | ||
<test_depend>ament_pyflakes</test_depend> | ||
|
||
<export> | ||
<build_type>ament_python</build_type> | ||
</export> | ||
</package> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from setuptools import setup | ||
|
||
setup( | ||
name='examples_rclpy_minimal_client', | ||
version='0.0.0', | ||
packages=[], | ||
py_modules=[ | ||
'client', | ||
'client_async', | ||
'client_async_member_function'], | ||
install_requires=['setuptools'], | ||
author='Mikael Arguedas', | ||
author_email='Mikael@osrfoundation.org', | ||
maintainer='Mikael Arguedas', | ||
maintainer_email='mikael@osrfoundation.org', | ||
keywords=['ROS'], | ||
classifiers=[ | ||
'Intended Audience :: Developers', | ||
'License :: OSI Approved :: Apache Software License', | ||
'Programming Language :: Python', | ||
'Topic :: Software Development', | ||
], | ||
description='Examples of minimal service clients using rclpy.', | ||
license='Apache License, Version 2.0', | ||
test_suite='test', | ||
entry_points={ | ||
'console_scripts': [ | ||
'examples_rclpy_minimal_client = client:main', | ||
'examples_rclpy_minimal_client_async = client_async:main', | ||
'examples_rclpy_minimal_client_async_member_function =' | ||
' client_async_member_function:main', | ||
], | ||
}, | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2015 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from ament_copyright.main import main | ||
|
||
|
||
def test_copyright(): | ||
rc = main(argv=['.']) | ||
assert rc == 0, 'Found errors' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2015 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from ament_pep257.main import main | ||
|
||
|
||
def test_pep257(): | ||
rc = main(argv=['.']) | ||
assert rc == 0, 'Found code style errors / warnings' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2015 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from ament_pep8.main import main | ||
|
||
|
||
def test_pep8(): | ||
rc = main(argv=['.']) | ||
assert rc == 0, 'Found code style errors / warnings' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2015 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from ament_pyflakes.main import main | ||
|
||
|
||
def test_pyflakes(): | ||
rc = main(argv=['.']) | ||
assert rc == 0, 'Found errors' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Minimal service server cookbook recipes | ||
|
||
This package contains a few strategies to create service servers. | ||
The `service` recipe shows how to define a service server in an analog way to ROS 1 and rospy | ||
The `service_member_function` recipe creates a MinimalService class that processes the incoming requests |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0"?> | ||
<?xml-model href="http://download.ros.org/schema/package_format2.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> | ||
<package format="2"> | ||
<name>examples_rclpy_minimal_service</name> | ||
<version>0.0.0</version> | ||
<description>Examples of minimal service servers using rclpy.</description> | ||
|
||
<maintainer email="mikael@osrfoundation.org">Mikael Arguedas</maintainer> | ||
<license>Apache License 2.0</license> | ||
|
||
<exec_depend>rclpy</exec_depend> | ||
<exec_depend>example_interfaces</exec_depend> | ||
<exec_depend>std_msgs</exec_depend> | ||
|
||
<!-- These test dependencies are optional | ||
Their purpose is to make sure that the code passes the linters --> | ||
<test_depend>ament_copyright</test_depend> | ||
<test_depend>ament_pep257</test_depend> | ||
<test_depend>ament_pep8</test_depend> | ||
<test_depend>ament_pyflakes</test_depend> | ||
|
||
<export> | ||
<build_type>ament_python</build_type> | ||
</export> | ||
</package> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Copyright 2016 Open Source Robotics Foundation, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import rclpy | ||
|
||
from example_interfaces.srv import AddTwoInts | ||
|
||
|
||
def add_two_ints_callback(request, response): | ||
response.sum = request.a + request.b | ||
print('Incoming request\na: %d b:%d' % (request.a, request.b)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitpick: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done d6cb134 |
||
|
||
return response | ||
|
||
|
||
def main(args=None): | ||
rclpy.init(args) | ||
|
||
node = rclpy.create_node('add_two_ints_server') | ||
|
||
srv = node.create_service(AddTwoInts, 'add_two_ints', add_two_ints_callback) | ||
while rclpy.ok(): | ||
rclpy.spin_once(node) | ||
|
||
# Destroy the service attached to the node explicitly | ||
# (optional - otherwise it will be done automatically | ||
# when the garbage collector destroys the node object) | ||
node.destroy_service(srv) | ||
rclpy.shutdown() | ||
|
||
if __name__ == '__main__': | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would recommend a TODO here then, so we remember to replace this with wait for service later. Or if there is a ticket to add wait for service in Python, you could link to this line of this file (at a specific commit) as a place to update after it is added. You might want to do both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done, ticketed here