-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathsample_infield_correction.py
More file actions
executable file
·270 lines (234 loc) · 11.2 KB
/
sample_infield_correction.py
File metadata and controls
executable file
·270 lines (234 loc) · 11.2 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#!/usr/bin/env python
from datetime import datetime
import enum
import sys
from rcl_interfaces.msg import ParameterDescriptor
import rclpy
from rclpy.client import Client
from rclpy.executors import ExternalShutdownException
from rclpy.node import Node
from std_srvs.srv import Trigger
from zivid_interfaces.srv import (
InfieldCorrectionCapture,
InfieldCorrectionCompute,
InfieldCorrectionRead,
)
class InfieldCorrectionOperation(enum.Enum):
Verify = 'verify'
Correct = 'correct'
CorrectAndWrite = 'correct_and_write'
Read = 'read'
Reset = 'reset'
def capture_status_to_string(response):
status_map = {
InfieldCorrectionCapture.Response.STATUS_NOT_SET: 'STATUS_NOT_SET',
InfieldCorrectionCapture.Response.STATUS_OK: 'STATUS_OK',
InfieldCorrectionCapture.Response.STATUS_DETECTION_FAILED: 'STATUS_DETECTION_FAILED',
InfieldCorrectionCapture.Response.STATUS_INVALID_CAPTURE_METHOD: 'STATUS_INVALID_CAPTURE_METHOD', # noqa: E501
InfieldCorrectionCapture.Response.STATUS_INVALID_ALIGNMENT: 'STATUS_INVALID_ALIGNMENT',
}
if response.status in status_map:
return status_map[response.status]
raise RuntimeError(f'Invalid status: {response.status}')
def declare_and_get_parameter_enum(node, name, enum_type):
name_value_map = {op.value: op for op in enum_type}
value = (
node.declare_parameter(name, '', ParameterDescriptor(read_only=True))
.get_parameter_value()
.string_value
)
if value not in name_value_map:
valid_values = ', '.join(list(name_value_map.keys()))
raise RuntimeError(
f"Invalid value for parameter '{name}': '{value}'. Expected one of: {valid_values}."
)
node.get_logger().info(f'Got parameter {name}: {value}')
return name_value_map[value]
class Sample(Node):
def __init__(self):
super().__init__('sample_infield_correction_py')
def _request_trigger_and_print_response(self, client):
future = client.call_async(Trigger.Request())
rclpy.spin_until_future_complete(self, future)
if not future.done():
raise RuntimeError(f'Failed to call the service {client.srv_name}')
result = future.result()
self.get_logger().info(f'Trigger results ({client.srv_name}):')
self.get_logger().info(f' Success: {result.success}')
message_str = f'"""{result.message}"""' if result.message else ''
self.get_logger().info(f' Message: {message_str}')
return result.success
def _request_capture_and_print_response(self, client):
future = client.call_async(InfieldCorrectionCapture.Request())
rclpy.spin_until_future_complete(self, future)
if not future.done():
raise RuntimeError('Failed to call the infield correction capture service')
result = future.result()
self.get_logger().info(f'Trigger results ({client.srv_name}):')
self.get_logger().info(f' Success: {result.success}')
message_str = f'"""{result.message}"""' if result.message else ''
self.get_logger().info(f' Message: {message_str}')
self.get_logger().info(f' Status: {capture_status_to_string(result)}')
self.get_logger().info(f' Number of captures: {result.number_of_captures}')
return result.success
def _request_infield_correction_compute_and_print_response(
self, client: Client, name: str
):
future = client.call_async(InfieldCorrectionCompute.Request())
rclpy.spin_until_future_complete(self, future)
if not future.done():
raise RuntimeError(f'Failed to call the infield correction {name} service')
result = future.result()
self.get_logger().info(
f'Infield correction compute results ({client.srv_name}):'
)
self.get_logger().info(f' Success: {result.success}')
self.get_logger().info(f' Number of captures: {result.number_of_captures}')
self.get_logger().info(' Trueness errors:')
for i, error in enumerate(result.trueness_errors):
self.get_logger().info(f' - Capture {i + 1}: {error * 100.0} %')
self.get_logger().info(
f' Average trueness error: {result.average_trueness_error * 100.0} %'
)
self.get_logger().info(
f' Maximum trueness error: {result.maximum_trueness_error * 100.0} %'
)
self.get_logger().info(
f' Dimension accuracy: {result.dimension_accuracy * 100.0} %'
)
self.get_logger().info(f' Z min: {result.z_min} m')
self.get_logger().info(f' Z max: {result.z_max} m')
message_str = f'"""{result.message}"""' if result.message else ''
self.get_logger().info(f' Message: {message_str}')
return result.success
def _request_read_and_print_response(self, client: Client):
future = client.call_async(InfieldCorrectionRead.Request())
rclpy.spin_until_future_complete(self, future)
if not future.done():
raise RuntimeError('Failed to call the infield correction read service')
result = future.result()
time_str = datetime.fromtimestamp(
result.camera_correction_timestamp.sec
).strftime('%c')
self.get_logger().info(f'Trigger results ({client.srv_name}):')
self.get_logger().info(f' Success: {result.success}')
message_str = f'"""{result.message}"""' if result.message else ''
self.get_logger().info(f' Message: {message_str}')
self.get_logger().info(
f' Has camera correction: {result.has_camera_correction}'
)
self.get_logger().info(
f' Camera correction timestamp: {result.camera_correction_timestamp.sec} ({time_str})'
)
return result.success
def run_operation(self, operation: InfieldCorrectionOperation):
if operation == InfieldCorrectionOperation.Verify:
self.get_logger().info('--- Starting infield correction: Verify ---')
start_client = self.create_client(Trigger, 'infield_correction/start')
while not start_client.wait_for_service(timeout_sec=3.0):
self.get_logger().info(
f'Service {start_client.srv_name} not available, waiting...'
)
capture_client = self.create_client(
InfieldCorrectionCapture, 'infield_correction/capture'
)
compute_client = self.create_client(
InfieldCorrectionCompute, 'infield_correction/compute'
)
if not self._request_trigger_and_print_response(start_client):
raise RuntimeError('Could not start infield correction, aborting.')
if not self._request_capture_and_print_response(capture_client):
raise RuntimeError(
'Could not perform an infield correction capture, aborting.'
)
self._request_infield_correction_compute_and_print_response(
compute_client, 'compute'
)
elif operation in [
InfieldCorrectionOperation.Correct,
InfieldCorrectionOperation.CorrectAndWrite,
]:
self.get_logger().info('--- Starting infield correction ---')
start_client = self.create_client(Trigger, 'infield_correction/start')
while not start_client.wait_for_service(timeout_sec=3.0):
self.get_logger().info(
f'Service {start_client.srv_name} not available, waiting...'
)
if not self._request_trigger_and_print_response(start_client):
raise RuntimeError('Could not start infield correction, aborting.')
capture_client = self.create_client(
InfieldCorrectionCapture, 'infield_correction/capture'
)
compute_client = self.create_client(
InfieldCorrectionCompute, 'infield_correction/compute'
)
wait_seconds = 5
num_successful_captures_target = 3
self.get_logger().info(
f'--- Starting captures, will proceed until {num_successful_captures_target} '
'captures have completed with a detected calibration board ---'
)
sleep_before_capture = False
num_successful_captures = 0
while num_successful_captures < num_successful_captures_target:
if sleep_before_capture:
self.get_logger().info(
f'--- Waiting for {wait_seconds} seconds'
'before taking the next capture ---'
)
rclpy.spin_until_future_complete(
self, rclpy.task.Future(), None, wait_seconds
)
else:
sleep_before_capture = True
success = self._request_capture_and_print_response(capture_client)
if success:
num_successful_captures += 1
self._request_infield_correction_compute_and_print_response(
compute_client, 'compute'
)
self.get_logger().info('--- Captures complete ---')
if operation == InfieldCorrectionOperation.CorrectAndWrite:
self.get_logger().info('--- Writing correction results to camera ---')
compute_and_write_client = self.create_client(
InfieldCorrectionCompute, 'infield_correction/compute_and_write'
)
self._request_infield_correction_compute_and_print_response(
compute_and_write_client, 'compute and write'
)
elif operation == InfieldCorrectionOperation.Read:
self.get_logger().info('--- Starting infield correction: Read ---')
read_client = self.create_client(
InfieldCorrectionRead, 'infield_correction/read'
)
while not read_client.wait_for_service(timeout_sec=3.0):
self.get_logger().info(
f'Service {read_client.srv_name} not available, waiting...'
)
self._request_read_and_print_response(read_client)
elif operation == InfieldCorrectionOperation.Reset:
self.get_logger().info('--- Starting infield correction: Reset ---')
reset_client = self.create_client(Trigger, 'infield_correction/reset')
while not reset_client.wait_for_service(timeout_sec=3.0):
self.get_logger().info(
f'Service {reset_client.srv_name} not available, waiting...'
)
self._request_trigger_and_print_response(reset_client)
else:
raise RuntimeError(f'Unknown operation: {operation}')
def main(args=None):
rclpy.init(args=args)
try:
sample = Sample()
operation = declare_and_get_parameter_enum(
sample, 'operation', InfieldCorrectionOperation
)
sample.run_operation(operation)
sample.get_logger().info('Spinning node.. Press Ctrl+C to abort.')
rclpy.spin(sample)
except KeyboardInterrupt:
pass
except ExternalShutdownException:
sys.exit(1)
if __name__ == '__main__':
main()