-
Notifications
You must be signed in to change notification settings - Fork 0
/
NodeClient.py
68 lines (48 loc) · 1.84 KB
/
NodeClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import rclpy
from rclpy.node import Node
import rclpy.time
from rclpy.executors import MultiThreadedExecutor
from rclpy.callback_groups import ReentrantCallbackGroup
from std_srvs.srv import Trigger
import asyncio
from threading import Thread
import time
class NodeClient(Node):
def __init__(self):
super().__init__('node_client', start_parameter_services=True)
self.cb_group = ReentrantCallbackGroup()
self.max = 0
self.total = 0
for x in range(10):
thread = Thread(target=self.threaded_function, args=(x, ))
thread.start()
self.get_logger().info(f"Initialized {self.get_name()}")
def threaded_function(self, index):
while True:
client = self.create_client(Trigger, 'node_server/Service')
try:
start_time = time.time()
client.wait_for_service()
client.service_is_ready()
response: Trigger.Response = client.call(Trigger.Request())
self.total += 1
elapsed_time = time.time() - start_time
if elapsed_time * 1000 > self.max:
self.max = elapsed_time * 1000
self.get_logger().warning(
f"Service response recieved in {elapsed_time * 1000:.2f} ms Max {self.max:.2f} Total {self.total}")
if response.success is False:
self.get_logger().error(response.message)
finally:
self.destroy_client(client)
def main(args=None):
rclpy.init(args=args)
minimal_client = NodeClient()
executor = MultiThreadedExecutor()
executor.add_node(minimal_client)
executor.spin()
minimal_client.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())