-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathminimal_mqtt_client.py
More file actions
executable file
·695 lines (591 loc) · 24.3 KB
/
Copy pathminimal_mqtt_client.py
File metadata and controls
executable file
·695 lines (591 loc) · 24.3 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
# Copyright 2024 RealSense, Inc. All Rights Reserved.
#
# 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.
"""Main module for interacting with the MQTT client."""
import json
import random
import time
import numpy as np
from paho.mqtt import client as paho_mqtt_client
class DemoMQTTClient:
"""
Class that acts as Demo MQTT Client.
This class provides methods for initializing the MQTT client, handling
MQTT connections, publishing messages, and interacting with MQTT topics.
Attributes:
mqtt_broker_ip: The IP address of the MQTT broker.
mqtt_broker_port: The port number of the MQTT broker.
mqtt_client: The MQTT client instance.
"""
def __init__(self, mqtt_broker_ip, mqtt_broker_port):
"""
Initialize the Demo MQTT client.
Args:
mqtt_broker_ip: The IP address of the MQTT broker.
mqtt_broker_port: The port number of the MQTT broker.
"""
self.mqtt_broker_ip = mqtt_broker_ip
self.mqtt_broker_port = mqtt_broker_port
# Generate a Client ID with a random user-id suffix.
mqtt_client_id = f'mqtt-client-user-{random.randint(0, 1000)}'
self.mqtt_client = paho_mqtt_client.Client(
paho_mqtt_client.CallbackAPIVersion.VERSION1, mqtt_client_id)
# client.username_pw_set(username, password)
self.mqtt_client.on_connect = self.on_connect
self.mqtt_client.connect(mqtt_broker_ip, mqtt_broker_port)
self.tc_done = False
def on_connect(self, client, userdata, flags, rc):
"""
Handle the MQTT connection event.
Args:
client: The MQTT client instance.
userdata: The user data associated with the connection.
flags: The connection flags.
rc: The result code of the connection attempt.
"""
del client, userdata, flags # delete unused params
if rc == 0:
print(f'Connected to MQTT Broker on'
f' ip:{ self.mqtt_broker_ip} port:{self.mqtt_broker_port}')
else:
print(f'Could not Connect to MQTT Broker on'
f' ip:{ self.mqtt_broker_ip} port:{self.mqtt_broker_port}'
f' return_code: {rc}')
def publish(self, msg, topic):
"""
Publish a message to the specified MQTT topic.
Args:
msg: The message to publish.
topic: The MQTT topic to publish to.
"""
msg_count = 1
while True:
time.sleep(1)
result = self.mqtt_client.publish(topic, msg, qos=2)
# result: [0, 1]
status = result[0]
if status == 0:
print(f'Send {msg} to topic {topic}')
else:
print(f'Failed to send message to topic {topic}')
msg_count += 1
if msg_count > 1:
break
def on_message(self, client, userdata, msg):
"""
Handle the MQTT message event.
Args:
client: The MQTT client instance.
userdata: The user data associated with the message.
msg: The received MQTT message.
"""
del client, userdata # delete unused params
content = msg.payload.decode('utf-8')
if msg.topic == 'get_frame_response':
print('got image... \
not printing it since its raw data is huge...')
else:
if msg.topic == 'triggered_calibration_response':
tc_json = json.loads(content)
if tc_json['progress'] == 100.0:
self.tc_done = True
print(f'Received {content} to topic {msg.topic}')
self.locked = False;
def start_client(self):
"""Start the MQTT client."""
self.mqtt_client.loop_start()
self.mqtt_client.subscribe('enumerate_devices_response')
self.mqtt_client.subscribe('get_transformation_response')
self.mqtt_client.subscribe('send_hw_reset_response')
self.mqtt_client.subscribe('send_hwm_command_response')
self.mqtt_client.subscribe('get_device_info_response')
self.mqtt_client.subscribe('get_parameter_response')
self.mqtt_client.subscribe('set_parameter_response')
self.mqtt_client.subscribe('get_frame_response')
self.mqtt_client.subscribe('get_safety_preset_response')
self.mqtt_client.subscribe('set_safety_preset_response')
self.mqtt_client.subscribe('get_safety_interface_config_response')
self.mqtt_client.subscribe('set_safety_interface_config_response')
self.mqtt_client.subscribe('get_calib_config_response')
self.mqtt_client.subscribe('set_calib_config_response')
self.mqtt_client.subscribe('get_application_config_response')
self.mqtt_client.subscribe('set_application_config_response')
self.mqtt_client.subscribe('triggered_calibration_response')
self.mqtt_client.on_message = self.on_message
def stop_client(self):
"""Stop the MQTT client."""
self.mqtt_client.loop_stop()
def enumerate_devices(self, camera_namespace_prefix, camera_name_prefix):
"""
Send a request to enumerate devices.
Args:
camera_namespace_prefix: The prefix of the camera namespace.
camera_name_prefix: The prefix of the camera name.
"""
request_dict = {
'camera_namespace_prefix': camera_namespace_prefix,
'camera_name_prefix': camera_name_prefix
}
j = json.dumps(request_dict)
self.publish(j, 'enumerate_devices_request')
def get_transformation(self, source, destination):
"""
Send a request to find the ROS2 transformation from source frame to destination frame
Args:
source: source frame id.
destination: destination frame id
"""
request_dict = {
'source': source,
'destination': destination
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'get_transformation_request')
while self.locked:
pass
def send_hw_reset_request(self, camera_namespace, camera_name):
"""
Send a request to reset the device
Args:
camera_namespace
camera_name
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'send_hw_reset_request')
while self.locked:
pass
def send_hwm_command(self, camera_namespace, camera_name, opcode,
param1 = 0, param2 = 0, param3 = 0, param4 = 0,
data = []):
"""
Send a hwm command request
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'opcode' : opcode,
'param1' : param1,
'param2' : param2,
'param3' : param3,
'param4' : param4,
'data': np.array(data).tolist()
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'send_hwm_command_request')
while self.locked:
pass
def get_device_info(self, camera_namespace, camera_name):
"""
Send a request to get device info.
Args:
camera_namespace: The namespace of the camera.
camera_name: The name of the camera.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'get_device_info_request')
while self.locked:
pass
def set_param(self, camera_namespace, camera_name,
parameter_name, parameter_value, parameter_type):
"""
Send a request to set a parameter.
Args:
camera_namespace: The namespace of the camera.
camera_name: The name of the camera.
parameter_name: The name of the parameter to set.
parameter_value: The value to set for the parameter.
parameter_type: The type of the parameter.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'parameter_name': parameter_name,
'parameter_value': parameter_value,
'parameter_type': parameter_type
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'set_param_request')
while self.locked:
pass
def get_param(self, camera_namespace, camera_name, parameter_name):
"""
Send a request to get a parameter.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
parameter_name (str): The name of the parameter to get.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'parameter_name': parameter_name,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'get_param_request')
while self.locked:
pass
def get_frame(self, camera_namespace, camera_name, stream_name):
"""
Send a request to get a frame.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
stream_name (str): The name of the stream.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'stream_name': stream_name,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'get_frame_request')
while self.locked:
pass
def get_safety_preset(self, camera_namespace, camera_name, index):
"""
Send a request to get a safety preset.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
index (int): The index of the safety preset.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'index': index,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'get_safety_preset_request')
while self.locked:
pass
def set_safety_preset(self, camera_namespace, camera_name, sp, index):
"""
Send a request to set a safety preset.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
sp (str): The safety preset.
index (int): The index of the safety preset.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'safety_preset': sp,
'index': str(index),
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'set_safety_preset_request')
while self.locked:
pass
def get_safety_interface_config(self, camera_namespace, camera_name, index=2):
"""
Send a request to get a safety inteface config.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'calib_location': index
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'get_safety_interface_config_request')
while self.locked:
pass
def set_safety_interface_config(self, camera_namespace, camera_name, sic):
"""
Send a request to set a safety interface config.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
sic (str): The safety interface config.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'safety_interface_config': sic,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'set_safety_interface_config_request')
while self.locked:
pass
def get_calib_config(self, camera_namespace, camera_name):
"""
Send a request to get a calib config.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'get_calib_config_request')
while self.locked:
pass
def set_calib_config(self, camera_namespace, camera_name, calib_config):
"""
Send a request to set a calib config.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
calib_config (str): The calib config.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'calib_config': calib_config,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'set_calib_config_request')
while self.locked:
pass
def get_application_config(self, camera_namespace, camera_name):
"""
Send a request to get an application config.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'get_application_config_request')
while self.locked:
pass
def set_application_config(self, camera_namespace, camera_name, application_config):
"""
Send a request to set an application config.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
application_config (str): The application config.
"""
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'application_config': application_config,
}
j = json.dumps(request_dict)
self.locked = True
self.publish(j, 'set_application_config_request')
while self.locked:
pass
def triggered_calibration(self, camera_namespace, camera_name):
"""
Run triggered calibration action.
Args:
camera_namespace (str): The namespace of the camera.
camera_name (str): The name of the camera.
"""
self.tc_done = False
request_dict = {
'camera_namespace': camera_namespace,
'camera_name': camera_name,
'json': 'calib run'
}
j = json.dumps(request_dict)
self.publish(j, 'triggered_calibration_request')
if __name__ == '__main__':
MQTT_BROKER_IP = 'localhost'
MQTT_BROKER_PORT = 1883
jsons_dir = '../../realsense2_camera/examples/d500_tables/'
demo_mqtt_client = DemoMQTTClient(MQTT_BROKER_IP, MQTT_BROKER_PORT)
demo_mqtt_client.start_client()
CAMERA_NAMESPACE_PREFIX = 'robot'
CAMERA_NAME_PREFIX = 'c_'
# enumerate devices
demo_mqtt_client.enumerate_devices(CAMERA_NAMESPACE_PREFIX,
CAMERA_NAME_PREFIX)
# choose specific camera
CAMERA_NAMESPACE = 'robot1'
CAMERA_NAME = 'c_353322320702'
# reset the device
demo_mqtt_client.send_hw_reset_request(CAMERA_NAMESPACE,
CAMERA_NAME)
#needs time to reset
import time
time.sleep(8)
# get device info
demo_mqtt_client.get_device_info(CAMERA_NAMESPACE,
CAMERA_NAME)
# get ROS2 transformation from 'c_353322320702_link' to 'c_353322320702_color_frame'
demo_mqtt_client.get_transformation('c_353322320702_link', 'c_353322320702_color_frame')
# send raw HWM command like GVD
demo_mqtt_client.send_hwm_command(camera_namespace= CAMERA_NAMESPACE,
camera_name = CAMERA_NAME,
opcode = 0x10)
# switch to service mode
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'safety_camera.safety_mode',
'2',
'int')
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'rgb_camera.exposure',
'6012',
'int')
demo_mqtt_client.get_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'rgb_camera.exposure')
demo_mqtt_client.get_frame(CAMERA_NAMESPACE,
CAMERA_NAME,
'color')
###################################################################
################ SAFETY PRESET GET/SET EXAMPLE ####################
demo_mqtt_client.get_safety_preset(CAMERA_NAMESPACE,
CAMERA_NAME,
1)
safety_preset_file = open(jsons_dir + 'safety_preset_example.json',
mode='r',
encoding='utf-8')
safety_preset_json = json.load(safety_preset_file)
SP_ESCAPED = str(safety_preset_json).replace('"', '\"')
SP_ESCAPED = str(safety_preset_json).replace("'", '\"')
demo_mqtt_client.set_safety_preset(CAMERA_NAMESPACE,
CAMERA_NAME,
SP_ESCAPED,
61)
###################################################################
########### SAFETY INTERFACE CONFIG GET/SET EXAMPLE ###############
demo_mqtt_client.get_safety_interface_config(CAMERA_NAMESPACE,
CAMERA_NAME)
safety_interface_config_file = open(jsons_dir + 'safety_interface_config_example.json',
mode='r',
encoding='utf-8')
safety_interface_config_json = json.load(safety_interface_config_file)
SIC_ESCAPED = str(safety_interface_config_json).replace('"', '\"')
SIC_ESCAPED = str(safety_interface_config_json).replace("'", '\"')
demo_mqtt_client.set_safety_interface_config(CAMERA_NAMESPACE,
CAMERA_NAME,
SIC_ESCAPED)
###################################################################
############## CALIB CONFIG GET/SET EXAMPLE #######################
demo_mqtt_client.get_calib_config(CAMERA_NAMESPACE,
CAMERA_NAME)
calib_config_file = open(jsons_dir + 'calib_config_example.json',
mode='r',
encoding='utf-8')
calib_config_json = json.load(calib_config_file)
CALIB_CONFIG_ESCAPED = str(calib_config_json).replace('"', '\"')
CALIB_CONFIG_ESCAPED = str(calib_config_json).replace("'", '\"')
demo_mqtt_client.set_calib_config(CAMERA_NAMESPACE,
CAMERA_NAME,
CALIB_CONFIG_ESCAPED)
##################################################################
########## APPLICATION CONFIG GET/SET EXAMPLE ####################
demo_mqtt_client.get_application_config(CAMERA_NAMESPACE,
CAMERA_NAME)
application_config_file = open(jsons_dir + 'application_config_example.json',
mode='r',
encoding='utf-8')
application_config_json = json.load(application_config_file)
APPLICATION_CONFIG_ESCAPED = str(application_config_json).replace('"', '\"')
APPLICATION_CONFIG_ESCAPED = str(application_config_json).replace("'", '\"')
demo_mqtt_client.set_application_config(CAMERA_NAMESPACE,
CAMERA_NAME,
APPLICATION_CONFIG_ESCAPED)
###################################################################
############## TRIGGERED CALIBRATION EXAMPLE ######################
# setup params for triggered calibration
# we are already in service mode, no need to switch
# switch to visual preset #1
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'depth_module.visual_preset',
'1',
'int')
# enable emitter
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'depth_module.emitter_enabled',
'true',
'bool')
# enable auto exposure
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'depth_module.enable_auto_exposure',
'true',
'bool')
# turn off depth streaming
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'enable_depth',
'false',
'bool')
# turn off infra1 streaming
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'enable_infra1',
'false',
'bool')
# turn off infra2 streaming
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'enable_infra2',
'false',
'bool')
# turn off safety streaming
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'enable_safety',
'false',
'bool')
# turn off occupancy streaming
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'enable_occupancy',
'false',
'bool')
# turn off lpcl streaming
demo_mqtt_client.set_param(CAMERA_NAMESPACE,
CAMERA_NAME,
'enable_labeled_point_cloud',
'false',
'bool')
# call the triggered calibration method
demo_mqtt_client.triggered_calibration(CAMERA_NAMESPACE,
CAMERA_NAME)
# check if TC is done, otherwise sleep for 2 seconds
while not demo_mqtt_client.tc_done:
time.sleep(2)
###################################################################
demo_mqtt_client.stop_client()
print(f'MQTT example completed, stopping the client and exiting')