+
+ function setSpeed(trans, rot) {
+ var msg = new ROSLIB.Message({
+ linear: {
+ x : trans,
+ y : 0,
+ z : 0
+ },
+ angular: {
+ x : 0,
+ y : 0,
+ z : rot
+ },
+ });
+ cmdVelTopic.publish(msg);
+ }
+
+ $('.cmd_vel_circle')
+ .bind('mousedown touchstart', function(e) {
+ isDragging = true;
+ })
+ .bind('mouseup touchend mouseleave', function(e) {
+ isDragging = false;
+ setSpeed(0, 0);
+ })
+ .bind('mousemove touchmove', function(e) {
+ if (isDragging) {
+ // absolute click position
+ var X,Y;
+ if (e.originalEvent.touches) {
+ X = e.originalEvent.touches[0].pageX;
+ Y = e.originalEvent.touches[0].pageY;
+ } else {
+ X = e.pageX;
+ Y = e.pageY;
+ }
+ // relative click position
+ var Xrel = X - this.offsetLeft - $(this).width()/2;
+ var Yrel = Y - this.offsetTop - $(this).height()/2;
+ // scale to -1..+1
+ var trans = -Yrel / ($(this).height()/2);
+ var rot = -Xrel / ($(this).width()/2);
+ setSpeed(trans, rot*3);
+ }
+ });