Skip to content

Commit

Permalink
add lifetime in MarkerClient class (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
KazutoMurase authored and jihoonl committed Feb 2, 2019
1 parent 7b02e44 commit d7f88d5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/markers.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
ros : ros,
tfClient : tfClient,
topic : '/visualization_marker',
lifetime : 0,
rootObject : viewer.scene
});
}
Expand Down
20 changes: 20 additions & 0 deletions src/markers/MarkerClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* * tfClient - the TF client handle to use
* * rootObject (optional) - the root object to add this marker to
* * path (optional) - the base path to any meshes that will be loaded
* * lifetime - the lifetime of marker
*/
ROS3D.MarkerClient = function(options) {
options = options || {};
Expand All @@ -25,10 +26,12 @@ ROS3D.MarkerClient = function(options) {
this.tfClient = options.tfClient;
this.rootObject = options.rootObject || new THREE.Object3D();
this.path = options.path || '/';
this.lifetime = options.lifetime || 0;

// Markers that are displayed (Map ns+id--Marker)
this.markers = {};
this.rosTopic = undefined;
this.updatedTime = {};

this.subscribe();
};
Expand All @@ -40,6 +43,20 @@ ROS3D.MarkerClient.prototype.unsubscribe = function(){
}
};

ROS3D.MarkerClient.prototype.checkTime = function(name){
var curTime = new Date().getTime();
if (curTime - this.updatedTime[name] > this.lifetime) {
var oldNode = this.markers[name];
oldNode.unsubscribeTf();
this.rootObject.remove(oldNode);
this.emit('change');
} else {
var that = this;
setTimeout(function() {that.checkTime(name)},
100);
}
};

ROS3D.MarkerClient.prototype.subscribe = function(){
this.unsubscribe();

Expand All @@ -61,9 +78,12 @@ ROS3D.MarkerClient.prototype.processMessage = function(message){

// remove old marker from Three.Object3D children buffer
var oldNode = this.markers[message.ns + message.id];
this.updatedTime[message.ns + message.id] = new Date().getTime();
if (oldNode) {
oldNode.unsubscribeTf();
this.rootObject.remove(oldNode);
} else if (this.lifetime) {
this.checkTime(message.ns + message.id);
}

this.markers[message.ns + message.id] = new ROS3D.SceneNode({
Expand Down

0 comments on commit d7f88d5

Please sign in to comment.