Skip to content
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

Merged
merged 21 commits into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rclpy/services/minimal_client/README.md
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
44 changes: 44 additions & 0 deletions rclpy/services/minimal_client/client.py
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)
Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, ticketed here

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' % (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: wrap directly after parenthesis, e.g.:

print(
    'Result of add_two_ints: for %d + %d = %d' %
    (req.a, req.b, cli.response.sum))

Same below.

req.a,
req.b,
cli.response.sum))

if __name__ == '__main__':
main()
48 changes: 48 additions & 0 deletions rclpy/services/minimal_client/client_async.py
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)
Copy link
Member

Choose a reason for hiding this comment

The 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:
Copy link
Member

Choose a reason for hiding this comment

The 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).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about this TODO message? 5306074

Copy link
Member

Choose a reason for hiding this comment

The 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()
56 changes: 56 additions & 0 deletions rclpy/services/minimal_client/client_async_member_function.py
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)
Copy link
Member

Choose a reason for hiding this comment

The 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()
25 changes: 25 additions & 0 deletions rclpy/services/minimal_client/package.xml
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>
34 changes: 34 additions & 0 deletions rclpy/services/minimal_client/setup.py
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',
],
},
)
20 changes: 20 additions & 0 deletions rclpy/services/minimal_client/test/test_copyright.py
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'
20 changes: 20 additions & 0 deletions rclpy/services/minimal_client/test/test_pep257.py
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'
20 changes: 20 additions & 0 deletions rclpy/services/minimal_client/test/test_pep8.py
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'
20 changes: 20 additions & 0 deletions rclpy/services/minimal_client/test/test_pyflakes.py
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'
5 changes: 5 additions & 0 deletions rclpy/services/minimal_service/README.md
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
25 changes: 25 additions & 0 deletions rclpy/services/minimal_service/package.xml
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>
43 changes: 43 additions & 0 deletions rclpy/services/minimal_service/service.py
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))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: b: %d

Copy link
Member Author

Choose a reason for hiding this comment

The 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()
Loading