Skip to content

Commit 621744f

Browse files
committed
[python_package_that_uses_the_services] Switching from AmazingQuote to AddPoints.
1 parent 45cd34d commit 621744f

2 files changed

Lines changed: 33 additions & 73 deletions

File tree

ros2_tutorial_workspace/src/python_package_that_uses_the_services/python_package_that_uses_the_services/what_is_the_point_service_client_node.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
MIT LICENSE
33
4-
Copyright (C) 2023 Murilo Marques Marinho (www.murilomarinho.info)
4+
Copyright (C) 2023-2025 Murilo Marques Marinho (www.murilomarinho.info)
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -28,18 +28,18 @@
2828
from rclpy.task import Future
2929
from rclpy.node import Node
3030

31-
from package_with_interfaces.srv import WhatIsThePoint
31+
from package_with_interfaces.srv import AddPoints
3232

3333

34-
class WhatIsThePointServiceClientNode(Node):
35-
"""A ROS2 Node with a Service Client for WhatIsThePoint."""
34+
class AddPointsServiceClientNode(Node):
35+
"""A ROS2 Node with a Service Client for AddPoints."""
3636

3737
def __init__(self):
38-
super().__init__('what_is_the_point_service_client')
38+
super().__init__('add_points_service_client')
3939

4040
self.service_client = self.create_client(
41-
srv_type=WhatIsThePoint,
42-
srv_name='/what_is_the_point')
41+
srv_type=AddPoints,
42+
srv_name='/add_points')
4343

4444
while not self.service_client.wait_for_service(timeout_sec=1.0):
4545
self.get_logger().info(f'service {self.service_client.srv_name} not available, waiting...')
@@ -54,19 +54,13 @@ def __init__(self):
5454
def timer_callback(self):
5555
"""Method that is periodically called by the timer."""
5656

57-
request = WhatIsThePoint.Request()
58-
if random.uniform(0, 1) < 0.5:
59-
request.quote.quote = "I wonder about the Ultimate Question of Life, the Universe, and Everything."
60-
request.quote.philosopher_name = "Creators of Deep Thought"
61-
request.quote.id = 1979
62-
else:
63-
request.quote.quote = """[...] your living... it is always potatoes. I dream of potatoes."""
64-
request.quote.philosopher_name = "a young Maltese potato farmer"
65-
request.quote.id = 2013
57+
request = AddPoints.Request()
58+
request.a = random.uniform(0, 1000)
59+
request.b = random.uniform(0, 1000)
6660

6761
if self.future is not None and not self.future.done():
6862
self.future.cancel() # Cancel the future. The callback will be called with Future.result == None.
69-
self.get_logger().info("Service Future canceled. The Node took too long to process the service call."
63+
self.get_logger().warn("Service Future canceled. The Node took too long to process the service call."
7064
"Is the Service Server still alive?")
7165
self.future = self.service_client.call_async(request)
7266
self.future.add_done_callback(self.process_response)
@@ -75,15 +69,9 @@ def process_response(self, future: Future):
7569
"""Callback for the future, that will be called when it is done"""
7670
response = future.result()
7771
if response is not None:
78-
self.get_logger().info(dedent(f"""
79-
We have thus received the point of our quote.
80-
81-
{(response.point.x, response.point.y, response.point.z)}
82-
"""))
72+
self.get_logger().info(f"The result was {(response.result.x, response.result.y, response.result.z)}")
8373
else:
84-
self.get_logger().info(dedent("""
85-
The response was None. :(
86-
"""))
74+
self.get_logger().info("The response was None.")
8775

8876

8977
def main(args=None):
@@ -95,9 +83,9 @@ def main(args=None):
9583
try:
9684
rclpy.init(args=args)
9785

98-
what_is_the_point_service_client_node = WhatIsThePointServiceClientNode()
86+
add_points_service_client_node = AddPointsServiceClientNode()
9987

100-
rclpy.spin(what_is_the_point_service_client_node)
88+
rclpy.spin(add_points_service_client_node)
10189
except KeyboardInterrupt:
10290
pass
10391
except Exception as e:

ros2_tutorial_workspace/src/python_package_that_uses_the_services/python_package_that_uses_the_services/what_is_the_point_service_server_node.py

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
MIT LICENSE
33
4-
Copyright (C) 2023 Murilo Marques Marinho (www.murilomarinho.info)
4+
Copyright (C) 2023-25 Murilo Marques Marinho (www.murilomarinho.info)
55
66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal
@@ -26,61 +26,33 @@
2626

2727
import rclpy
2828
from rclpy.node import Node
29-
from package_with_interfaces.srv import WhatIsThePoint
29+
from package_with_interfaces.srv import AddPoints
3030

3131

32-
class WhatIsThePointServiceServerNode(Node):
33-
"""A ROS2 Node with a Service Server for WhatIsThePoint."""
32+
class AddPointsServiceServerNode(Node):
33+
"""A ROS2 Node with a Service Server for AddPoints."""
3434

3535
def __init__(self):
3636
super().__init__('what_is_the_point_service_server')
3737

3838
self.service_server = self.create_service(
39-
srv_type=WhatIsThePoint,
40-
srv_name='/what_is_the_point',
41-
callback=self.what_is_the_point_service_callback)
39+
srv_type=AddPoints,
40+
srv_name='/add_points',
41+
callback=self.add_points_service_callback)
4242

4343
self.service_server_call_count: int = 0
4444

45-
def what_is_the_point_service_callback(self,
46-
request: WhatIsThePoint.Request,
47-
response: WhatIsThePoint.Response
48-
) -> WhatIsThePoint.Response:
49-
"""Analyses an AmazingQuote and returns what is the point.
50-
If the quote contains 'life', it returns a point whose sum of coordinates is 42.
51-
Otherwise, it returns a random point whose sum of coordinates is not 42.
45+
def add_points_service_callback(self,
46+
request: AddPoints.Request,
47+
response: AddPoints.Response
48+
) -> AddPoints.Response:
49+
"""
50+
Adds the two points `a` and `b` in the request and returns the `result`.
5251
"""
5352

54-
# Generate the x,y,z of the point
55-
if "life" in request.quote.quote.lower():
56-
x: float = random.uniform(0, 42)
57-
y: float = random.uniform(0, 42 - x)
58-
z: float = 42 - (x + y)
59-
else:
60-
x: float = random.uniform(0, 100)
61-
y: float = random.uniform(0, 100)
62-
z: float = random.uniform(0, 100)
63-
if x + y + z == 42: # So you’re telling me there’s a chance? Yes!
64-
x = x + 1 # Not anymore :(
65-
66-
# Assign to the response
67-
response.point.x = x
68-
response.point.y = y
69-
response.point.z = z
70-
71-
# Increase the call count
72-
self.service_server_call_count = self.service_server_call_count + 1
73-
74-
self.get_logger().info(dedent(f"""
75-
This is the call number {self.service_server_call_count} to this Service Server.
76-
The analysis of the AmazingQuote below is complete.
77-
78-
{request.quote.quote}
79-
80-
-- {request.quote.philosopher_name}
81-
82-
The point has been sent back to the client.
83-
"""))
53+
response.result.x = request.a.x + request.b.x
54+
response.result.y = request.a.y + request.b.y
55+
response.result.z = request.a.z + request.b.z
8456

8557
return response
8658

@@ -94,9 +66,9 @@ def main(args=None):
9466
try:
9567
rclpy.init(args=args)
9668

97-
what_is_the_point_service_server_node = WhatIsThePointServiceServerNode()
69+
add_points_service_server_node = AddPointsServiceServerNode()
9870

99-
rclpy.spin(what_is_the_point_service_server_node)
71+
rclpy.spin(add_points_service_server_node)
10072
except KeyboardInterrupt:
10173
pass
10274
except Exception as e:

0 commit comments

Comments
 (0)