-
Notifications
You must be signed in to change notification settings - Fork 418
Expand file tree
/
Copy pathRos.js
More file actions
933 lines (911 loc) · 28.9 KB
/
Ros.js
File metadata and controls
933 lines (911 loc) · 28.9 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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
/**
* @fileOverview
* @author Brandon Alexander - baalexander@gmail.com
*/
import socketAdapter from './SocketAdapter.js';
import Topic from './Topic.js';
import Service from './Service.js';
import Param from './Param.js';
import TFClient from '../tf/TFClient.js';
import ActionClient from '../actionlib/ActionClient.js';
import SimpleActionServer from '../actionlib/SimpleActionServer.js';
import { EventEmitter } from 'eventemitter3';
/**
* Manages connection to the server and all interactions with ROS.
*
* Emits the following events:
* * 'error' - There was an error with ROS.
* * 'connection' - Connected to the WebSocket server.
* * 'close' - Disconnected to the WebSocket server.
* * <topicName> - A message came from rosbridge with the given topic name.
* * <serviceID> - A service response came from rosbridge with the given ID.
*/
export default class Ros extends EventEmitter {
/** @type {WebSocket | import("ws").WebSocket | null} */
socket = null;
idCounter = 0;
isConnected = false;
groovyCompatibility = true;
/**
* @param {Object} [options]
* @param {string} [options.url] - The WebSocket URL for rosbridge. Can be specified later with `connect`.
* @param {boolean} [options.groovyCompatibility=true] - Don't use interfaces that changed after the last groovy release or rosbridge_suite and related tools.
* @param {'websocket'|RTCPeerConnection} [options.transportLibrary='websocket'] - 'websocket', or an RTCPeerConnection instance controlling how the connection is created in `connect`.
* @param {Object} [options.transportOptions={}] - The options to use when creating a connection. Currently only used if `transportLibrary` is RTCPeerConnection.
*/
constructor(options) {
super();
options = options || {};
this.transportLibrary = options.transportLibrary || 'websocket';
this.transportOptions = options.transportOptions || {};
this.groovyCompatibility = options.groovyCompatibility ?? true;
// begin by checking if a URL was given
if (options.url) {
this.connect(options.url);
}
}
/**
* Connect to the specified WebSocket.
*
* @param {string} url - WebSocket URL or RTCDataChannel label for rosbridge.
*/
connect(url) {
if (this.transportLibrary.constructor.name === 'RTCPeerConnection') {
this.socket = Object.assign(
// @ts-expect-error -- this is kinda wild. `this.transportLibrary` can either be a string or an RTCDataChannel. This needs fixing.
this.transportLibrary.createDataChannel(url, this.transportOptions),
socketAdapter(this)
);
} else if (this.transportLibrary === 'websocket') {
// browsers, Deno, and Bun support WebSockets natively
if (typeof WebSocket === 'function') {
if (!this.socket || this.socket.readyState === WebSocket.CLOSED) {
const sock = new WebSocket(url);
sock.binaryType = 'arraybuffer';
this.socket = Object.assign(sock, socketAdapter(this));
}
} else {
// if in Node.js, import ws to replace WebSocket API
import('ws').then((ws) => {
if (!this.socket || this.socket.readyState === ws.WebSocket.CLOSED) {
const sock = new ws.WebSocket(url);
sock.binaryType = 'arraybuffer'
this.socket = Object.assign(sock, socketAdapter(this));
}
})
}
} else {
throw 'Unknown transportLibrary: ' + this.transportLibrary.toString();
}
}
/**
* Disconnect from the WebSocket server.
*/
close() {
if (this.socket) {
this.socket.close();
}
}
/**
* Send an authorization request to the server.
*
* @param {string} mac - MAC (hash) string given by the trusted source.
* @param {string} client - IP of the client.
* @param {string} dest - IP of the destination.
* @param {string} rand - Random string given by the trusted source.
* @param {Object} t - Time of the authorization request.
* @param {string} level - User level as a string given by the client.
* @param {Object} end - End time of the client's session.
*/
authenticate(mac, client, dest, rand, t, level, end) {
// create the request
var auth = {
op: 'auth',
mac: mac,
client: client,
dest: dest,
rand: rand,
t: t,
level: level,
end: end
};
// send the request
this.callOnConnection(auth);
}
/**
* Send an encoded message over the WebSocket.
*
* @param {Object} messageEncoded - The encoded message to be sent.
*/
sendEncodedMessage(messageEncoded) {
if (!this.isConnected) {
this.once('connection', () => {
if (this.socket !== null) {
this.socket.send(messageEncoded);
}
});
} else {
if (this.socket !== null) {
this.socket.send(messageEncoded);
}
}
}
/**
* Send the message over the WebSocket, but queue the message up if not yet
* connected.
*
* @param {Object} message - The message to be sent.
*/
callOnConnection(message) {
if (this.transportOptions.encoder) {
this.transportOptions.encoder(message, this.sendEncodedMessage);
} else {
this.sendEncodedMessage(JSON.stringify(message));
}
}
/**
* Send a set_level request to the server.
*
* @param {string} level - Status level (none, error, warning, info).
* @param {number} [id] - Operation ID to change status level on.
*/
setStatusLevel(level, id) {
var levelMsg = {
op: 'set_level',
level: level,
id: id
};
this.callOnConnection(levelMsg);
}
/**
* @callback getActionServersCallback
* @param {string[]} actionservers - Array of action server names.
*/
/**
* @callback getActionServersFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve a list of action servers in ROS as an array of string.
*
* @param {getActionServersCallback} callback - Function with the following params:
* @param {getActionServersFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getActionServers(callback, failedCallback) {
/** @satisfies {Service<any, any>} */
var getActionServers = new Service({
ros: this,
name: 'rosapi/action_servers',
serviceType: 'rosapi/GetActionServers'
});
var request = {};
if (typeof failedCallback === 'function') {
getActionServers.callService(
request,
function (result) {
callback(result.action_servers);
},
function (message) {
failedCallback(message);
}
);
} else {
getActionServers.callService(request, function (result) {
callback(result.action_servers);
});
}
}
/**
* @callback getTopicsCallback
* @param {Object} result - The result object with the following params:
* @param {string[]} result.topics - Array of topic names.
* @param {string[]} result.types - Array of message type names.
*/
/**
* @callback getTopicsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve a list of topics in ROS as an array.
*
* @param {getTopicsCallback} callback - Function with the following params:
* @param {getTopicsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getTopics(callback, failedCallback) {
var topicsClient = new Service({
ros: this,
name: 'rosapi/topics',
serviceType: 'rosapi/Topics'
});
var request = {};
if (typeof failedCallback === 'function') {
topicsClient.callService(
request,
function (result) {
callback(result);
},
function (message) {
failedCallback(message);
}
);
} else {
topicsClient.callService(request, function (result) {
callback(result);
});
}
}
/**
* @callback getTopicsForTypeCallback
* @param {string[]} topics - Array of topic names.
*/
/**
* @callback getTopicsForTypeFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve a list of topics in ROS as an array of a specific type.
*
* @param {string} topicType - The topic type to find.
* @param {getTopicsForTypeCallback} callback - Function with the following params:
* @param {getTopicsForTypeFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getTopicsForType(topicType, callback, failedCallback) {
var topicsForTypeClient = new Service({
ros: this,
name: 'rosapi/topics_for_type',
serviceType: 'rosapi/TopicsForType'
});
var request = {
type: topicType
};
if (typeof failedCallback === 'function') {
topicsForTypeClient.callService(
request,
function (result) {
callback(result.topics);
},
function (message) {
failedCallback(message);
}
);
} else {
topicsForTypeClient.callService(request, function (result) {
callback(result.topics);
});
}
}
/**
* @callback getServicesCallback
* @param {string[]} services - Array of service names.
*/
/**
* @callback getServicesFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve a list of active service names in ROS.
*
* @param {getServicesCallback} callback - Function with the following params:
* @param {getServicesFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getServices(callback, failedCallback) {
var servicesClient = new Service({
ros: this,
name: 'rosapi/services',
serviceType: 'rosapi/Services'
});
var request = {};
if (typeof failedCallback === 'function') {
servicesClient.callService(
request,
function (result) {
callback(result.services);
},
function (message) {
failedCallback(message);
}
);
} else {
servicesClient.callService(request, function (result) {
callback(result.services);
});
}
}
/**
* @callback getServicesForTypeCallback
* @param {string[]} topics - Array of service names.
*/
/**
* @callback getServicesForTypeFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve a list of services in ROS as an array as specific type.
*
* @param {string} serviceType - The service type to find.
* @param {getServicesForTypeCallback} callback - Function with the following params:
* @param {getServicesForTypeFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getServicesForType(serviceType, callback, failedCallback) {
var servicesForTypeClient = new Service({
ros: this,
name: 'rosapi/services_for_type',
serviceType: 'rosapi/ServicesForType'
});
var request = {
type: serviceType
};
if (typeof failedCallback === 'function') {
servicesForTypeClient.callService(
request,
function (result) {
callback(result.services);
},
function (message) {
failedCallback(message);
}
);
} else {
servicesForTypeClient.callService(request, function (result) {
callback(result.services);
});
}
}
/**
* @callback getServiceRequestDetailsCallback
* @param {Object} result - The result object with the following params:
* @param {string[]} result.typedefs - An array containing the details of the service request.
*/
/**
* @callback getServiceRequestDetailsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve the details of a ROS service request.
*
* @param {string} type - The type of the service.
* @param {getServiceRequestDetailsCallback} callback - Function with the following params:
* @param {getServiceRequestDetailsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getServiceRequestDetails(type, callback, failedCallback) {
var serviceTypeClient = new Service({
ros: this,
name: 'rosapi/service_request_details',
serviceType: 'rosapi/ServiceRequestDetails'
});
var request = {
type: type
};
if (typeof failedCallback === 'function') {
serviceTypeClient.callService(
request,
function (result) {
callback(result);
},
function (message) {
failedCallback(message);
}
);
} else {
serviceTypeClient.callService(request, function (result) {
callback(result);
});
}
}
/**
* @callback getServiceResponseDetailsCallback
* @param {{typedefs: string[]}} result - The result object with the following params:
*/
/**
* @callback getServiceResponseDetailsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve the details of a ROS service response.
*
* @param {string} type - The type of the service.
* @param {getServiceResponseDetailsCallback} callback - Function with the following params:
* @param {getServiceResponseDetailsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getServiceResponseDetails(type, callback, failedCallback) {
/** @satisfies {Service<{},{typedefs: string[]}>} */
var serviceTypeClient = new Service({
ros: this,
name: 'rosapi/service_response_details',
serviceType: 'rosapi/ServiceResponseDetails'
});
var request = {
type: type
};
if (typeof failedCallback === 'function') {
serviceTypeClient.callService(
request,
function (result) {
callback(result);
},
function (message) {
failedCallback(message);
}
);
} else {
serviceTypeClient.callService(request, function (result) {
callback(result);
});
}
}
/**
* @callback getNodesCallback
* @param {string[]} nodes - Array of node names.
*/
/**
* @callback getNodesFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve a list of active node names in ROS.
*
* @param {getNodesCallback} callback - Function with the following params:
* @param {getNodesFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getNodes(callback, failedCallback) {
var nodesClient = new Service({
ros: this,
name: 'rosapi/nodes',
serviceType: 'rosapi/Nodes'
});
var request = {};
if (typeof failedCallback === 'function') {
nodesClient.callService(
request,
function (result) {
callback(result.nodes);
},
function (message) {
failedCallback(message);
}
);
} else {
nodesClient.callService(request, function (result) {
callback(result.nodes);
});
}
}
/**
* @callback getNodeDetailsCallback
* @param {string[]} subscriptions - Array of subscribed topic names.
* @param {string[]} publications - Array of published topic names.
* @param {string[]} services - Array of service names hosted.
*/
/**
* @callback getNodeDetailsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* @callback getNodeDetailsLegacyCallback
* @param {Object} result - The result object with the following params:
* @param {string[]} result.subscribing - Array of subscribed topic names.
* @param {string[]} result.publishing - Array of published topic names.
* @param {string[]} result.services - Array of service names hosted.
*/
/**
* Retrieve a list of subscribed topics, publishing topics and services of a specific node.
* <br>
* These are the parameters if failedCallback is <strong>defined</strong>.
*
* @param {string} node - Name of the node.
* @param {getNodeDetailsCallback} callback - Function with the following params:
* @param {getNodeDetailsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*
* @also
*
* Retrieve a list of subscribed topics, publishing topics and services of a specific node.
* <br>
* These are the parameters if failedCallback is <strong>undefined</strong>.
*
* @param {string} node - Name of the node.
* @param {getNodeDetailsLegacyCallback} callback - Function with the following params:
* @param {getNodeDetailsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getNodeDetails(node, callback, failedCallback) {
var nodesClient = new Service({
ros: this,
name: 'rosapi/node_details',
serviceType: 'rosapi/NodeDetails'
});
var request = {
node: node
};
if (typeof failedCallback === 'function') {
nodesClient.callService(
request,
function (result) {
callback(result.subscribing, result.publishing, result.services);
},
function (message) {
failedCallback(message);
}
);
} else {
nodesClient.callService(request, function (result) {
// @ts-expect-error -- callback parameter polymorphism, see JSDoc comment above
callback(result);
});
}
}
/**
* @callback getParamsCallback
* @param {string[]} params - Array of param names.
*/
/**
* @callback getParamsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve a list of parameter names from the ROS Parameter Server.
*
* @param {getParamsCallback} callback - Function with the following params:
* @param {getParamsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getParams(callback, failedCallback) {
var paramsClient = new Service({
ros: this,
name: 'rosapi/get_param_names',
serviceType: 'rosapi/GetParamNames'
});
var request = {};
if (typeof failedCallback === 'function') {
paramsClient.callService(
request,
function (result) {
callback(result.names);
},
function (message) {
failedCallback(message);
}
);
} else {
paramsClient.callService(request, function (result) {
callback(result.names);
});
}
}
/**
* @callback getTopicTypeCallback
* @param {string} type - The type of the topic.
*/
/**
* @callback getTopicTypeFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve the type of a ROS topic.
*
* @param {string} topic - Name of the topic.
* @param {getTopicTypeCallback} callback - Function with the following params:
* @param {getTopicTypeFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getTopicType(topic, callback, failedCallback) {
var topicTypeClient = new Service({
ros: this,
name: 'rosapi/topic_type',
serviceType: 'rosapi/TopicType'
});
var request = {
topic: topic
};
if (typeof failedCallback === 'function') {
topicTypeClient.callService(
request,
function (result) {
callback(result.type);
},
function (message) {
failedCallback(message);
}
);
} else {
topicTypeClient.callService(request, function (result) {
callback(result.type);
});
}
}
/**
* @callback getServiceTypeCallback
* @param {string} type - The type of the service.
*/
/**
* @callback getServiceTypeFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve the type of a ROS service.
*
* @param {string} service - Name of the service.
* @param {getServiceTypeCallback} callback - Function with the following params:
* @param {getServiceTypeFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getServiceType(service, callback, failedCallback) {
var serviceTypeClient = new Service({
ros: this,
name: 'rosapi/service_type',
serviceType: 'rosapi/ServiceType'
});
var request = {
service: service
};
if (typeof failedCallback === 'function') {
serviceTypeClient.callService(
request,
function (result) {
callback(result.type);
},
function (message) {
failedCallback(message);
}
);
} else {
serviceTypeClient.callService(request, function (result) {
callback(result.type);
});
}
}
/**
* @callback getMessageDetailsCallback
* @param {string} details - An array of the message details.
*/
/**
* @callback getMessageDetailsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve the details of a ROS message.
*
* @param {string} message - The name of the message type.
* @param {getMessageDetailsCallback} callback - Function with the following params:
* @param {getMessageDetailsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getMessageDetails(message, callback, failedCallback) {
var messageDetailClient = new Service({
ros: this,
name: 'rosapi/message_details',
serviceType: 'rosapi/MessageDetails'
});
var request = {
type: message
};
if (typeof failedCallback === 'function') {
messageDetailClient.callService(
request,
function (result) {
callback(result.typedefs);
},
function (message) {
failedCallback(message);
}
);
} else {
messageDetailClient.callService(request, function (result) {
callback(result.typedefs);
});
}
}
/**
* @callback getActionGoalDetailsCallback
* @param {{typedefs: string[]}} result - The result object with the following params:
*/
/**
* @callback getActionGoalDetailsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve the details of a ROS action goal.
*
* @param {string} type - The type of the action.
* @param {getActionGoalDetailsCallback} callback - Function with the following params:
* @param {getActionGoalDetailsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getActionGoalDetails(type, callback, failedCallback) {
this._getActionInterfaceDetails(
'rosapi/action_goal_details',
'rosapi/ActionGoalDetails',
type,
callback,
failedCallback
);
}
/**
* @callback getActionResultDetailsCallback
* @param {{typedefs: string[]}} result - The result object with the following params:
*/
/**
* @callback getActionResultDetailsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve the details of a ROS action result.
*
* @param {string} type - The type of the action.
* @param {getActionResultDetailsCallback} callback - Function with the following params:
* @param {getActionResultDetailsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getActionResultDetails(type, callback, failedCallback) {
this._getActionInterfaceDetails(
'rosapi/action_result_details',
'rosapi/ActionResultDetails',
type,
callback,
failedCallback
);
}
/**
* @callback getActionFeedbackDetailsCallback
* @param {{typedefs: string[]}} result - The result object with the following params:
*/
/**
* @callback getActionFeedbackDetailsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve the details of a ROS Action feedback.
*
* @param {string} type - The type of the action.
* @param {getActionFeedbackDetailsCallback} callback - Function with the following params:
* @param {getActionFeedbackDetailsFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getActionFeedbackDetails(type, callback, failedCallback) {
this._getActionInterfaceDetails(
'rosapi/action_feedback_details',
'rosapi/ActionFeedbackDetails',
type,
callback,
failedCallback
);
}
/**
* @callback _getActionDetailsCallback
* @param {{typedefs: string[]}} result - The result object with the following params:
*/
/**
* @callback _getActionDetailsFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* @param {string} srv
* @param {string} srvType
* @param {string} type
* @param {_getActionDetailsCallback} callback
* @param {_getActionDetailsFailedCallback} [failedCallback]
*/
_getActionInterfaceDetails(srv, srvType, type, callback, failedCallback) {
/** @satisfies {Service<{},{typedefs: string[]}>} */
const client = new Service({
ros: this,
name: srv,
serviceType: srvType
});
const request = {
type,
};
if (typeof failedCallback === 'function') {
client.callService(
request,
function (result) {
callback(result);
},
function (message) {
failedCallback(message);
}
);
} else {
client.callService(request, function (result) {
callback(result);
});
}
}
/**
* Decode a typedef array into a dictionary like `rosmsg show foo/bar`.
*
* @param {Object[]} defs - Array of type_def dictionary.
*/
decodeTypeDefs(defs) {
var decodeTypeDefsRec = (theType, hints) => {
// calls itself recursively to resolve type definition using hints.
var typeDefDict = {};
for (var i = 0; i < theType.fieldnames.length; i++) {
var arrayLen = theType.fieldarraylen[i];
var fieldName = theType.fieldnames[i];
var fieldType = theType.fieldtypes[i];
if (fieldType.indexOf('/') === -1) {
// check the fieldType includes '/' or not
if (arrayLen === -1) {
typeDefDict[fieldName] = fieldType;
} else {
typeDefDict[fieldName] = [fieldType];
}
} else {
// lookup the name
var sub = false;
for (var j = 0; j < hints.length; j++) {
if (hints[j].type.toString() === fieldType.toString()) {
sub = hints[j];
break;
}
}
if (sub) {
var subResult = decodeTypeDefsRec(sub, hints);
if (arrayLen === -1) {
typeDefDict[fieldName] = subResult; // add this decoding result to dictionary
} else {
typeDefDict[fieldName] = [subResult];
}
} else {
this.emit(
'error',
'Cannot find ' + fieldType + ' in decodeTypeDefs'
);
}
}
}
return typeDefDict;
};
return decodeTypeDefsRec(defs[0], defs);
}
/**
* @callback getTopicsAndRawTypesCallback
* @param {Object} result - The result object with the following params:
* @param {string[]} result.topics - Array of topic names.
* @param {string[]} result.types - Array of message type names.
* @param {string[]} result.typedefs_full_text - Array of full definitions of message types, similar to `gendeps --cat`.
*/
/**
* @callback getTopicsAndRawTypesFailedCallback
* @param {string} error - The error message reported by ROS.
*/
/**
* Retrieve a list of topics and their associated type definitions.
*
* @param {getTopicsAndRawTypesCallback} callback - Function with the following params:
* @param {getTopicsAndRawTypesFailedCallback} [failedCallback] - The callback function when the service call failed with params:
*/
getTopicsAndRawTypes(callback, failedCallback) {
var topicsAndRawTypesClient = new Service({
ros: this,
name: 'rosapi/topics_and_raw_types',
serviceType: 'rosapi/TopicsAndRawTypes'
});
var request = {};
if (typeof failedCallback === 'function') {
topicsAndRawTypesClient.callService(
request,
function (result) {
callback(result);
},
function (message) {
failedCallback(message);
}
);
} else {
topicsAndRawTypesClient.callService(request, function (result) {
callback(result);
});
}
}
Topic(options) {
return new Topic({ ros: this, ...options });
}
Param(options) {
return new Param({ ros: this, ...options });
}
Service(options) {
return new Service({ ros: this, ...options });
}
TFClient(options) {
return new TFClient({ ros: this, ...options });
}
ActionClient(options) {
return new ActionClient({ ros: this, ...options });
}
SimpleActionServer(options) {
return new SimpleActionServer({ ros: this, ...options });
}
}