|
| 1 | +/** |
| 2 | + * @author Russell Toris - [email protected] |
| 3 | + * @author Nils Berg - [email protected] |
| 4 | + */ |
| 5 | + |
| 6 | +var ROSLIB = require('roslib'); |
| 7 | +var ROS3D = require('ros3d'); |
| 8 | +var EventEmitter2 = require('eventemitter2'); |
| 9 | + |
| 10 | +/** |
| 11 | + * A MarkerArray client that listens to a given topic. |
| 12 | + * |
| 13 | + * Emits the following events: |
| 14 | + * |
| 15 | + * * 'change' - there was an update or change in the MarkerArray |
| 16 | + * |
| 17 | + * @constructor |
| 18 | + * @param options - object with following keys: |
| 19 | + * |
| 20 | + * * ros - the ROSLIB.Ros connection handle |
| 21 | + * * topic - the marker topic to listen to |
| 22 | + * * tfClient - the TF client handle to use |
| 23 | + * * canvas - ... |
| 24 | + * * path (optional) - the base path to any meshes that will be loaded |
| 25 | + */ |
| 26 | +var MarkerArrayClient = function(options) { |
| 27 | + options = options || {}; |
| 28 | + this.ros = options.ros; |
| 29 | + this.topicName = options.topic; |
| 30 | + this.tfClient = options.tfClient; |
| 31 | + this.canvas = options.canvas; |
| 32 | + this.path = options.path || '/'; |
| 33 | + |
| 34 | + // Markers that are displayed (Map ns+id--Marker) |
| 35 | + this.markers = {}; |
| 36 | + this.rosTopic = undefined; |
| 37 | + |
| 38 | + this.subscribe(); |
| 39 | +}; |
| 40 | +MarkerArrayClient.prototype.__proto__ = EventEmitter2.prototype; |
| 41 | + |
| 42 | +MarkerArrayClient.prototype.subscribe = function(){ |
| 43 | + this.unsubscribe(); |
| 44 | + |
| 45 | + // subscribe to MarkerArray topic |
| 46 | + this.rosTopic = new ROSLIB.Topic({ |
| 47 | + ros : this.ros, |
| 48 | + name : this.topicName, |
| 49 | + messageType : 'visualization_msgs/MarkerArray', |
| 50 | + compression : 'png' |
| 51 | + }); |
| 52 | + this.rosTopic.subscribe(this.processMessage.bind(this)); |
| 53 | +}; |
| 54 | + |
| 55 | +MarkerArrayClient.prototype.processMessage = function(arrayMessage){ |
| 56 | + var that = this; |
| 57 | + |
| 58 | + arrayMessage.markers.forEach(function(message) { |
| 59 | + var markerName = message.ns + message.id; |
| 60 | + if(message.action === 0) { |
| 61 | + var updated = false; |
| 62 | + if(markerName in this.markers) { // "MODIFY" |
| 63 | + var markerItem = that.markers[markerName]; |
| 64 | + var marker = markerItem[0]; |
| 65 | + var node = markerItem[1]; |
| 66 | + updated = marker.update(message); |
| 67 | + if(!updated) { // "REMOVE" |
| 68 | + node.unsubscribeTf(); |
| 69 | + that.canvas.removeMarker(marker,node); |
| 70 | + delete that.markers[markerName]; |
| 71 | + } |
| 72 | + } |
| 73 | + if(!updated) { // "ADD" |
| 74 | + var newMarker = new ROS3D.Marker({ |
| 75 | + message : message, |
| 76 | + path : this.path, |
| 77 | + }); |
| 78 | + newMarker.isSelectable = true; // XXX |
| 79 | + newMarker.isSceneOrtho = false; // XXX |
| 80 | + newMarker.id = message.id; |
| 81 | + newMarker.ns = message.ns; |
| 82 | + newMarker.frame_id = message.header.frame_id; // XXX |
| 83 | + newMarker.marker_type = message.type; // XXX |
| 84 | + var newNode = new ROS3D.SceneNode({ |
| 85 | + frameID : message.header.frame_id, |
| 86 | + tfClient : this.tfClient, |
| 87 | + object : newMarker |
| 88 | + }); |
| 89 | + this.markers[markerName] = [newMarker,newNode]; |
| 90 | + that.canvas.addMarker(newMarker,newNode); |
| 91 | + } |
| 92 | + } |
| 93 | + else if(message.action === 1) { // "DEPRECATED" |
| 94 | + console.warn('Received marker message with deprecated action identifier "1"'); |
| 95 | + } |
| 96 | + else if(message.action === 2 && markerName in this.markers) { // "DELETE" |
| 97 | + var markerItem = that.markers[markerName]; |
| 98 | + var marker = markerItem[0]; |
| 99 | + var node = markerItem[1]; |
| 100 | + node.unsubscribeTf(); |
| 101 | + canvas.removeMarker(marker,node); |
| 102 | + delete that.markers[markerName]; |
| 103 | + } |
| 104 | + else if(message.action === 3) { // "DELETE ALL" |
| 105 | + for (var markerItem in that.markers){ |
| 106 | + var marker = markerItem[0]; |
| 107 | + var node = markerItem[1]; |
| 108 | + node.unsubscribeTf(); |
| 109 | + canvas.removeMarker(marker,node); |
| 110 | + } |
| 111 | + that.markers = {}; |
| 112 | + } |
| 113 | + else { |
| 114 | + console.warn('Received marker message with unknown action identifier "'+message.action+'"'); |
| 115 | + } |
| 116 | + }.bind(this)); |
| 117 | + |
| 118 | + this.emit('change'); |
| 119 | +}; |
| 120 | + |
| 121 | +MarkerArrayClient.prototype.getObjectMarker = function(objectName){ |
| 122 | + x = this.markers[objectName] |
| 123 | + if(x) { |
| 124 | + return x[0]; |
| 125 | + } |
| 126 | + else { |
| 127 | + return undefined; |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +MarkerArrayClient.prototype.unsubscribe = function(){ |
| 132 | + if(this.rosTopic){ |
| 133 | + this.rosTopic.unsubscribe(); |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +module.exports = MarkerArrayClient; |
0 commit comments