UNPKG

@robot-web-tools/ros3djs

Version:

The standard ROS Javascript Visualization Library

80 lines (67 loc) 2.46 kB
/** * @author David V. Lu!! - davidvlu@gmail.com */ /** * A PoseStamped client * * @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 * * rootObject (optional) - the root object to add this marker to * * color (optional) - color for line (default: 0xcc00ff) * * length (optional) - the length of the arrow (default: 1.0) * * headLength (optional) - the head length of the arrow (default: 0.2) * * shaftDiameter (optional) - the shaft diameter of the arrow (default: 0.05) * * headDiameter (optional) - the head diameter of the arrow (default: 0.1) */ ROS3D.Pose = function(options) { this.options = options || {}; this.ros = options.ros; this.topicName = options.topic || '/pose'; this.tfClient = options.tfClient; this.color = options.color || 0xcc00ff; this.rootObject = options.rootObject || new THREE.Object3D(); THREE.Object3D.call(this); this.sn = null; this.rosTopic = undefined; this.subscribe(); }; ROS3D.Pose.prototype.__proto__ = THREE.Object3D.prototype; ROS3D.Pose.prototype.unsubscribe = function(){ if(this.rosTopic){ this.rosTopic.unsubscribe(); } }; ROS3D.Pose.prototype.subscribe = function(){ this.unsubscribe(); // subscribe to the topic this.rosTopic = new ROSLIB.Topic({ ros : this.ros, name : this.topicName, messageType : 'geometry_msgs/PoseStamped' }); this.rosTopic.subscribe(this.processMessage.bind(this)); }; ROS3D.Pose.prototype.processMessage = function(message){ if(this.sn!==null){ this.sn.unsubscribeTf(); this.rootObject.remove(this.sn); } this.options.origin = new THREE.Vector3( message.pose.position.x, message.pose.position.y, message.pose.position.z); var rot = new THREE.Quaternion(message.pose.orientation.x, message.pose.orientation.y, message.pose.orientation.z, message.pose.orientation.w); this.options.direction = new THREE.Vector3(1,0,0); this.options.direction.applyQuaternion(rot); this.options.material = new THREE.MeshBasicMaterial({color: this.color}); var arrow = new ROS3D.Arrow(this.options); this.sn = new ROS3D.SceneNode({ frameID : message.header.frame_id, tfClient : this.tfClient, object : arrow }); this.rootObject.add(this.sn); };