-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMarkerArrayClient.js
executable file
·137 lines (125 loc) · 3.97 KB
/
MarkerArrayClient.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
/**
* @author Russell Toris - [email protected]
* @author Nils Berg - [email protected]
*/
var ROSLIB = require('roslib');
var ROS3D = require('ros3d');
var EventEmitter2 = require('eventemitter2');
/**
* A MarkerArray client that listens to a given topic.
*
* Emits the following events:
*
* * 'change' - there was an update or change in the MarkerArray
*
* @constructor
* @param options - object with following keys:
*
* * ros - the ROSLIB.Ros connection handle
* * topic - the marker topic to listen to
* * tfClient - the TF client handle to use
* * canvas - ...
* * path (optional) - the base path to any meshes that will be loaded
*/
var MarkerArrayClient = function(options) {
options = options || {};
this.ros = options.ros;
this.topicName = options.topic;
this.tfClient = options.tfClient;
this.canvas = options.canvas;
this.path = options.path || '/';
// Markers that are displayed (Map ns+id--Marker)
this.markers = {};
this.rosTopic = undefined;
this.subscribe();
};
MarkerArrayClient.prototype.__proto__ = EventEmitter2.prototype;
MarkerArrayClient.prototype.subscribe = function(){
this.unsubscribe();
// subscribe to MarkerArray topic
this.rosTopic = new ROSLIB.Topic({
ros : this.ros,
name : this.topicName,
messageType : 'visualization_msgs/MarkerArray',
compression : 'png'
});
this.rosTopic.subscribe(this.processMessage.bind(this));
};
MarkerArrayClient.prototype.processMessage = function(arrayMessage){
var that = this;
arrayMessage.markers.forEach(function(message) {
var markerName = message.ns + message.id;
if(message.action === 0) {
var updated = false;
if(markerName in this.markers) { // "MODIFY"
var markerItem = that.markers[markerName];
var marker = markerItem[0];
var node = markerItem[1];
updated = marker.update(message);
if(!updated) { // "REMOVE"
node.unsubscribeTf();
that.canvas.rosViewer.removeMarker(marker,node);
delete that.markers[markerName];
}
}
if(!updated) { // "ADD"
var newMarker = new ROS3D.Marker({
message : message,
path : this.path,
});
newMarker.isSelectable = true; // XXX
newMarker.isSceneOrtho = false; // XXX
newMarker.id = message.id;
newMarker.ns = message.ns;
newMarker.frame_id = message.header.frame_id; // XXX
newMarker.marker_type = message.type; // XXX
var newNode = new ROS3D.SceneNode({
frameID : message.header.frame_id,
tfClient : this.tfClient,
object : newMarker
});
this.markers[markerName] = [newMarker,newNode];
that.canvas.rosViewer.addMarker(newMarker,newNode);
}
}
else if(message.action === 1) { // "DEPRECATED"
console.warn('Received marker message with deprecated action identifier "1"');
}
else if(message.action === 2 && markerName in this.markers) { // "DELETE"
var markerItem = that.markers[markerName];
var marker = markerItem[0];
var node = markerItem[1];
node.unsubscribeTf();
canvas.rosViewer.removeMarker(marker,node);
delete that.markers[markerName];
}
else if(message.action === 3) { // "DELETE ALL"
for (var markerItem in that.markers){
var marker = markerItem[0];
var node = markerItem[1];
node.unsubscribeTf();
canvas.rosViewer.removeMarker(marker,node);
}
that.markers = {};
}
else {
console.warn('Received marker message with unknown action identifier "'+message.action+'"');
}
}.bind(this));
this.emit('change');
};
MarkerArrayClient.prototype.getObjectMarker = function(objectName){
x = this.markers[objectName]
if(x) {
return x[0];
}
else {
return undefined;
}
}
MarkerArrayClient.prototype.unsubscribe = function(){
if(this.rosTopic){
this.rosTopic.unsubscribe();
}
};
module.exports = MarkerArrayClient;