forked from RobotWebTools/ros3djs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroslib.js
1776 lines (1589 loc) · 43.9 KB
/
roslib.js
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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @author Russell Toris - rctoris@wpi.edu
*/
var ROSLIB = ROSLIB || {
REVISION : '0.8.0'
};
//URDF types
ROSLIB.URDF_SPHERE = 0;
ROSLIB.URDF_BOX = 1;
ROSLIB.URDF_CYLINDER = 2;
ROSLIB.URDF_MESH = 3;
/**
* @author Russell Toris - rctoris@wpi.edu
*/
/**
* An actionlib action client.
*
* Emits the following events:
* * 'timeout' - if a timeout occurred while sending a goal
* * 'status' - the status messages received from the action server
* * 'feedback' - the feedback messages received from the action server
* * 'result' - the result returned from the action server
*
* @constructor
* @param options - object with following keys:
* * ros - the ROSLIB.Ros connection handle
* * serverName - the action server name, like /fibonacci
* * actionName - the action message name, like 'actionlib_tutorials/FibonacciAction'
* * timeout - the timeout length when connecting to the action server
*/
ROSLIB.ActionClient = function(options) {
var that = this;
options = options || {};
this.ros = options.ros;
this.serverName = options.serverName;
this.actionName = options.actionName;
this.timeout = options.timeout;
this.goals = {};
// flag to check if a status has been received
var receivedStatus = false;
// create the topics associated with actionlib
var feedbackListener = new ROSLIB.Topic({
ros : this.ros,
name : this.serverName + '/feedback',
messageType : this.actionName + 'Feedback'
});
var statusListener = new ROSLIB.Topic({
ros : this.ros,
name : this.serverName + '/status',
messageType : 'actionlib_msgs/GoalStatusArray'
});
var resultListener = new ROSLIB.Topic({
ros : this.ros,
name : this.serverName + '/result',
messageType : this.actionName + 'Result'
});
this.goalTopic = new ROSLIB.Topic({
ros : this.ros,
name : this.serverName + '/goal',
messageType : this.actionName + 'Goal'
});
this.cancelTopic = new ROSLIB.Topic({
ros : this.ros,
name : this.serverName + '/cancel',
messageType : 'actionlib_msgs/GoalID'
});
// advertise the goal and cancel topics
this.goalTopic.advertise();
this.cancelTopic.advertise();
// subscribe to the status topic
statusListener.subscribe(function(statusMessage) {
receivedStatus = true;
statusMessage.status_list.forEach(function(status) {
var goal = that.goals[status.goal_id.id];
if (goal) {
goal.emit('status', status);
}
});
});
// subscribe the the feedback topic
feedbackListener.subscribe(function(feedbackMessage) {
var goal = that.goals[feedbackMessage.status.goal_id.id];
if (goal) {
goal.emit('status', feedbackMessage.status);
goal.emit('feedback', feedbackMessage.feedback);
}
});
// subscribe to the result topic
resultListener.subscribe(function(resultMessage) {
var goal = that.goals[resultMessage.status.goal_id.id];
if (goal) {
goal.emit('status', resultMessage.status);
goal.emit('result', resultMessage.result);
}
});
// If timeout specified, emit a 'timeout' event if the action server does not respond
if (this.timeout) {
setTimeout(function() {
if (!receivedStatus) {
that.emit('timeout');
}
}, this.timeout);
}
};
ROSLIB.ActionClient.prototype.__proto__ = EventEmitter2.prototype;
/**
* Cancel all goals associated with this ActionClient.
*/
ROSLIB.ActionClient.prototype.cancel = function() {
var cancelMessage = new ROSLIB.Message();
this.cancelTopic.publish(cancelMessage);
};
/**
* @author Russell Toris - rctoris@wpi.edu
*/
/**
* An actionlib goal goal is associated with an action server.
*
* Emits the following events:
* * 'timeout' - if a timeout occurred while sending a goal
*
* @constructor
* @param object with following keys:
* * actionClient - the ROSLIB.ActionClient to use with this goal
* * goalMessage - The JSON object containing the goal for the action server
*/
ROSLIB.Goal = function(options) {
var that = this;
this.actionClient = options.actionClient;
this.goalMessage = options.goalMessage;
this.isFinished = false;
// Used to create random IDs
var date = new Date();
// Create a random ID
this.goalID = 'goal_' + Math.random() + '_' + date.getTime();
// Fill in the goal message
this.goalMessage = new ROSLIB.Message({
goal_id : {
stamp : {
secs : 0,
nsecs : 0
},
id : this.goalID
},
goal : this.goalMessage
});
this.on('status', function(status) {
that.status = status;
});
this.on('result', function(result) {
that.isFinished = true;
that.result = result;
});
this.on('feedback', function(feedback) {
that.feedback = feedback;
});
// Add the goal
this.actionClient.goals[this.goalID] = this;
};
ROSLIB.Goal.prototype.__proto__ = EventEmitter2.prototype;
/**
* Send the goal to the action server.
*
* @param timeout (optional) - a timeout length for the goal's result
*/
ROSLIB.Goal.prototype.send = function(timeout) {
var that = this;
that.actionClient.goalTopic.publish(that.goalMessage);
if (timeout) {
setTimeout(function() {
if (!that.isFinished) {
that.emit('timeout');
}
}, timeout);
}
};
/**
* Cancel the current goal.
*/
ROSLIB.Goal.prototype.cancel = function() {
var cancelMessage = new ROSLIB.Message({
id : this.goalID
});
this.actionClient.cancelTopic.publish(cancelMessage);
};
/**
* @author Brandon Alexander - baalexander@gmail.com
*/
/**
* Message objects are used for publishing and subscribing to and from topics.
*
* @constructor
* @param values - object matching the fields defined in the .msg definition file
*/
ROSLIB.Message = function(values) {
var that = this;
values = values || {};
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
};
/**
* @author Brandon Alexander - baalexander@gmail.com
*/
/**
* A ROS parameter.
*
* @constructor
* @param options - possible keys include:
* * ros - the ROSLIB.Ros connection handle
* * name - the param name, like max_vel_x
*/
ROSLIB.Param = function(options) {
options = options || {};
this.ros = options.ros;
this.name = options.name;
};
/**
* Fetches the value of the param.
*
* @param callback - function with the following params:
* * value - the value of the param from ROS.
*/
ROSLIB.Param.prototype.get = function(callback) {
var paramClient = new ROSLIB.Service({
ros : this.ros,
name : '/rosapi/get_param',
serviceType : 'rosapi/GetParam'
});
var request = new ROSLIB.ServiceRequest({
name : this.name
});
paramClient.callService(request, function(result) {
var value = JSON.parse(result.value);
callback(value);
});
};
/**
* Sets the value of the param in ROS.
*
* @param value - value to set param to.
*/
ROSLIB.Param.prototype.set = function(value) {
var paramClient = new ROSLIB.Service({
ros : this.ros,
name : '/rosapi/set_param',
serviceType : 'rosapi/SetParam'
});
var request = new ROSLIB.ServiceRequest({
name : this.name,
value : JSON.stringify(value)
});
paramClient.callService(request, function() {
});
};
/**
* Delete this parameter on the ROS server.
*/
ROSLIB.Param.prototype.delete = function() {
var paramClient = new ROSLIB.Service({
ros : this.ros,
name : '/rosapi/delete_param',
serviceType : 'rosapi/DeleteParam'
});
var request = new ROSLIB.ServiceRequest({
name : this.name
});
paramClient.callService(request, function() {
});
};
/**
* @author Brandon Alexander - baalexander@gmail.com
*/
/**
* 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
*
* @constructor
* @param options - possible keys include:
* * url (optional) - the WebSocket URL for rosbridge (can be specified later with `connect`)
*/
ROSLIB.Ros = function(options) {
options = options || {};
var url = options.url;
this.socket = null;
this.idCounter = 0;
// Sets unlimited event listeners.
this.setMaxListeners(0);
// begin by checking if a URL was given
if (url) {
this.connect(url);
}
};
ROSLIB.Ros.prototype.__proto__ = EventEmitter2.prototype;
/**
* Connect to the specified WebSocket.
*
* @param url - WebSocket URL for Rosbridge
*/
ROSLIB.Ros.prototype.connect = function(url) {
var that = this;
/**
* Emits a 'connection' event on WebSocket connection.
*
* @param event - the argument to emit with the event.
*/
function onOpen(event) {
that.emit('connection', event);
}
/**
* Emits a 'close' event on WebSocket disconnection.
*
* @param event - the argument to emit with the event.
*/
function onClose(event) {
that.emit('close', event);
}
/**
* Emits an 'error' event whenever there was an error.
*
* @param event - the argument to emit with the event.
*/
function onError(event) {
that.emit('error', event);
}
/**
* If a message was compressed as a PNG image (a compression hack since
* gzipping over WebSockets * is not supported yet), this function places the
* "image" in a canvas element then decodes the * "image" as a Base64 string.
*
* @param data - object containing the PNG data.
* @param callback - function with params:
* * data - the uncompressed data
*/
function decompressPng(data, callback) {
// Uncompresses the data before sending it through (use image/canvas to do so).
var image = new Image();
// When the image loads, extracts the raw data (JSON message).
image.onload = function() {
// Creates a local canvas to draw on.
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
// Sets width and height.
canvas.width = image.width;
canvas.height = image.height;
// Puts the data into the image.
context.drawImage(image, 0, 0);
// Grabs the raw, uncompressed data.
var imageData = context.getImageData(0, 0, image.width, image.height).data;
// Constructs the JSON.
var jsonData = '';
for ( var i = 0; i < imageData.length; i += 4) {
// RGB
jsonData += String.fromCharCode(imageData[i], imageData[i + 1], imageData[i + 2]);
}
var decompressedData = JSON.parse(jsonData);
callback(decompressedData);
};
// Sends the image data to load.
image.src = 'data:image/png;base64,' + data.data;
}
/**
* Parses message responses from rosbridge and sends to the appropriate
* topic, service, or param.
*
* @param message - the raw JSON message from rosbridge.
*/
function onMessage(message) {
function handleMessage(message) {
if (message.op === 'publish') {
that.emit(message.topic, message.msg);
} else if (message.op === 'service_response') {
that.emit(message.id, message);
}
}
var data = JSON.parse(message.data);
if (data.op === 'png') {
decompressPng(data, function(decompressedData) {
handleMessage(decompressedData);
});
} else {
handleMessage(data);
}
}
this.socket = new WebSocket(url);
this.socket.onopen = onOpen;
this.socket.onclose = onClose;
this.socket.onerror = onError;
this.socket.onmessage = onMessage;
};
/**
* Disconnect from the WebSocket server.
*/
ROSLIB.Ros.prototype.close = function() {
if (this.socket) {
this.socket.close();
}
};
/**
* Sends an authorization request to the server.
*
* @param mac - MAC (hash) string given by the trusted source.
* @param client - IP of the client.
* @param dest - IP of the destination.
* @param rand - Random string given by the trusted source.
* @param t - Time of the authorization request.
* @param level - User level as a string given by the client.
* @param end - End time of the client's session.
*/
ROSLIB.Ros.prototype.authenticate = function(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);
};
/**
* Sends the message over the WebSocket, but queues the message up if not yet
* connected.
*/
ROSLIB.Ros.prototype.callOnConnection = function(message) {
var that = this;
var messageJson = JSON.stringify(message);
if (!this.socket || this.socket.readyState !== WebSocket.OPEN) {
that.once('connection', function() {
that.socket.send(messageJson);
});
} else {
that.socket.send(messageJson);
}
};
/**
* Retrieves list of topics in ROS as an array.
*
* @param callback function with params:
* * topics - Array of topic names
*/
ROSLIB.Ros.prototype.getTopics = function(callback) {
var topicsClient = new ROSLIB.Service({
ros : this,
name : '/rosapi/topics',
serviceType : 'rosapi/Topics'
});
var request = new ROSLIB.ServiceRequest();
topicsClient.callService(request, function(result) {
callback(result.topics);
});
};
/**
* Retrieves list of active service names in ROS.
*
* @param callback - function with the following params:
* * services - array of service names
*/
ROSLIB.Ros.prototype.getServices = function(callback) {
var servicesClient = new ROSLIB.Service({
ros : this,
name : '/rosapi/services',
serviceType : 'rosapi/Services'
});
var request = new ROSLIB.ServiceRequest();
servicesClient.callService(request, function(result) {
callback(result.services);
});
};
/**
* Retrieves list of active node names in ROS.
*
* @param callback - function with the following params:
* * nodes - array of node names
*/
ROSLIB.Ros.prototype.getNodes = function(callback) {
var nodesClient = new ROSLIB.Service({
ros : this,
name : '/rosapi/nodes',
serviceType : 'rosapi/Nodes'
});
var request = new ROSLIB.ServiceRequest();
nodesClient.callService(request, function(result) {
callback(result.nodes);
});
};
/**
* Retrieves list of param names from the ROS Parameter Server.
*
* @param callback function with params:
* * params - array of param names.
*/
ROSLIB.Ros.prototype.getParams = function(callback) {
var paramsClient = new ROSLIB.Service({
ros : this,
name : '/rosapi/get_param_names',
serviceType : 'rosapi/GetParamNames'
});
var request = new ROSLIB.ServiceRequest();
paramsClient.callService(request, function(result) {
callback(result.names);
});
};
/**
* Retrieves a type of ROS topic.
*
* @param callback - function with params:
* * type - String of the topic type
*/
ROSLIB.Ros.prototype.getTopicType = function(topic, callback) {
var topicTypeClient = new ROSLIB.Service({
ros : this,
name : '/rosapi/topic_type',
serviceType : 'rosapi/TopicType'
});
var request = new ROSLIB.ServiceRequest({
topic: topic
});
topicTypeClient.callService(request, function(result) {
callback(result.type);
});
};
/**
* Retrieves a detail of ROS message.
*
* @param callback - function with params:
* * details - Array of the message detail
* @param message - String of a topic type
*/
ROSLIB.Ros.prototype.getMessageDetails = function(message, callback) {
var messageDetailClient = new ROSLIB.Service({
ros : this,
name : '/rosapi/message_details',
serviceType : 'rosapi/MessageDetails'
});
var request = new ROSLIB.ServiceRequest({
type: message
});
messageDetailClient.callService(request, function(result) {
callback(result.typedefs);
});
};
/**
* Decode a typedefs into a dictionary like `rosmsg show foo/bar`
*
* @param defs - array of type_def dictionary
*/
ROSLIB.Ros.prototype.decodeTypeDefs = function(defs) {
var that = this;
// calls itself recursively to resolve type definition using hints.
var decodeTypeDefsRec = function(theType, 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;
}
else {
typeDefDict[fieldName] = [subResult];
}
}
else {
that.emit('error', 'Cannot find ' + fieldType + ' in decodeTypeDefs');
}
}
}
return typeDefDict;
};
return decodeTypeDefsRec(defs[0], defs);
};
/**
* @author Brandon Alexander - baalexander@gmail.com
*/
/**
* A ROS service client.
*
* @constructor
* @params options - possible keys include:
* * ros - the ROSLIB.Ros connection handle
* * name - the service name, like /add_two_ints
* * serviceType - the service type, like 'rospy_tutorials/AddTwoInts'
*/
ROSLIB.Service = function(options) {
options = options || {};
this.ros = options.ros;
this.name = options.name;
this.serviceType = options.serviceType;
};
/**
* Calls the service. Returns the service response in the callback.
*
* @param request - the ROSLIB.ServiceRequest to send
* @param callback - function with params:
* * response - the response from the service request
* @param failedCallback - the callback function when the service call failed (optional). Params:
* * error - the error message reported by ROS
*/
ROSLIB.Service.prototype.callService = function(request, callback, failedCallback) {
this.ros.idCounter++;
var serviceCallId = 'call_service:' + this.name + ':' + this.ros.idCounter;
this.ros.once(serviceCallId, function(message) {
if (message.result !== undefined && message.result === false) {
if (typeof failedCallback === 'function') {
failedCallback(message.values);
}
} else {
var response = new ROSLIB.ServiceResponse(message.values);
callback(response);
}
});
var call = {
op : 'call_service',
id : serviceCallId,
service : this.name,
args : request
};
this.ros.callOnConnection(call);
};
/**
* @author Brandon Alexander - balexander@willowgarage.com
*/
/**
* A ServiceRequest is passed into the service call.
*
* @constructor
* @param values - object matching the fields defined in the .srv definition file
*/
ROSLIB.ServiceRequest = function(values) {
var that = this;
values = values || {};
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
};
/**
* @author Brandon Alexander - balexander@willowgarage.com
*/
/**
* A ServiceResponse is returned from the service call.
*
* @constructor
* @param values - object matching the fields defined in the .srv definition file
*/
ROSLIB.ServiceResponse = function(values) {
var that = this;
values = values || {};
Object.keys(values).forEach(function(name) {
that[name] = values[name];
});
};
/**
* @author Brandon Alexander - baalexander@gmail.com
*/
/**
* Publish and/or subscribe to a topic in ROS.
*
* Emits the following events:
* * 'warning' - if there are any warning during the Topic creation
* * 'message' - the message data from rosbridge
*
* @constructor
* @param options - object with following keys:
* * ros - the ROSLIB.Ros connection handle
* * name - the topic name, like /cmd_vel
* * messageType - the message type, like 'std_msgs/String'
* * compression - the type of compression to use, like 'png'
* * throttle_rate - the rate at which to throttle the topics
*/
ROSLIB.Topic = function(options) {
options = options || {};
this.ros = options.ros;
this.name = options.name;
this.messageType = options.messageType;
this.isAdvertised = false;
this.compression = options.compression || 'none';
this.throttle_rate = options.throttle_rate || 0;
this.latch = options.latch || false;
// Check for valid compression types
if (this.compression && this.compression !== 'png' && this.compression !== 'none') {
this.emit('warning', this.compression +
' compression is not supported. No compression will be used.');
}
// Check if throttle rate is negative
if (this.throttle_rate < 0) {
this.emit('warning', this.throttle_rate + ' is not allowed. Set to 0');
this.throttle_rate = 0;
}
};
ROSLIB.Topic.prototype.__proto__ = EventEmitter2.prototype;
/**
* Every time a message is published for the given topic, the callback
* will be called with the message object.
*
* @param callback - function with the following params:
* * message - the published message
*/
ROSLIB.Topic.prototype.subscribe = function(callback) {
var that = this;
this.on('message', function(message) {
callback(message);
});
this.ros.on(this.name, function(data) {
var message = new ROSLIB.Message(data);
that.emit('message', message);
});
this.ros.idCounter++;
var subscribeId = 'subscribe:' + this.name + ':' + this.ros.idCounter;
var call = {
op : 'subscribe',
id : subscribeId,
type : this.messageType,
topic : this.name,
compression : this.compression,
throttle_rate : this.throttle_rate
};
this.ros.callOnConnection(call);
};
/**
* Unregisters as a subscriber for the topic. Unsubscribing will remove
* all subscribe callbacks.
*/
ROSLIB.Topic.prototype.unsubscribe = function() {
this.ros.removeAllListeners([ this.name ]);
this.ros.idCounter++;
var unsubscribeId = 'unsubscribe:' + this.name + ':' + this.ros.idCounter;
var call = {
op : 'unsubscribe',
id : unsubscribeId,
topic : this.name
};
this.ros.callOnConnection(call);
};
/**
* Registers as a publisher for the topic.
*/
ROSLIB.Topic.prototype.advertise = function() {
if (this.isAdvertised) {
return;
}
this.ros.idCounter++;
this.advertiseId = 'advertise:' + this.name + ':' + this.ros.idCounter;
var call = {
op : 'advertise',
id : this.advertiseId,
type : this.messageType,
topic : this.name,
latch : this.latch
};
this.ros.callOnConnection(call);
this.isAdvertised = true;
};
/**
* Unregisters as a publisher for the topic.
*/
ROSLIB.Topic.prototype.unadvertise = function() {
if (!this.isAdvertised) {
return;
}
var unadvertiseId = this.advertiseId;
var call = {
op : 'unadvertise',
id : unadvertiseId,
topic : this.name
};
this.ros.callOnConnection(call);
this.isAdvertised = false;
};
/**
* Publish the message.
*
* @param message - A ROSLIB.Message object.
*/
ROSLIB.Topic.prototype.publish = function(message) {
if (!this.isAdvertised) {
this.advertise();
}
this.ros.idCounter++;
var publishId = 'publish:' + this.name + ':' + this.ros.idCounter;
var call = {
op : 'publish',
id : publishId,
topic : this.name,
msg : message,
latch : this.latch
};
this.ros.callOnConnection(call);
};
/**
* @author David Gossow - dgossow@willowgarage.com
*/
/**
* A Pose in 3D space. Values are copied into this object.
*
* @constructor
* @param options - object with following keys:
* * position - the Vector3 describing the position
* * orientation - the ROSLIB.Quaternion describing the orientation
*/
ROSLIB.Pose = function(options) {
options = options || {};
// copy the values into this object if they exist
this.position = new ROSLIB.Vector3(options.position);
this.orientation = new ROSLIB.Quaternion(options.orientation);
};
/**
* Apply a transform against this pose.
*
* @param tf the transform
*/
ROSLIB.Pose.prototype.applyTransform = function(tf) {
this.position.multiplyQuaternion(tf.rotation);
this.position.add(tf.translation);
var tmp = tf.rotation.clone();
tmp.multiply(this.orientation);
this.orientation = tmp;
};
/**
* Clone a copy of this pose.
*
* @returns the cloned pose
*/
ROSLIB.Pose.prototype.clone = function() {
return new ROSLIB.Pose(this);
};
/**
* @author David Gossow - dgossow@willowgarage.com
*/
/**
* A Quaternion.
*
* @constructor
* @param options - object with following keys:
* * x - the x value
* * y - the y value
* * z - the z value
* * w - the w value
*/
ROSLIB.Quaternion = function(options) {
options = options || {};
this.x = options.x || 0;
this.y = options.y || 0;
this.z = options.z || 0;
this.w = options.w || 1;
};
/**
* Perform a conjugation on this quaternion.
*/
ROSLIB.Quaternion.prototype.conjugate = function() {
this.x *= -1;
this.y *= -1;
this.z *= -1;
};
/**
* Perform a normalization on this quaternion.
*/
ROSLIB.Quaternion.prototype.normalize = function() {
var l = Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
if (l === 0) {
this.x = 0;