-
Notifications
You must be signed in to change notification settings - Fork 0
/
dragToMove.js
121 lines (101 loc) · 3.39 KB
/
dragToMove.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
/**
* Created by timadamson on 8/23/17.
*/
// Adds jQuery to the html
var script = document.createElement('script');
script.src = 'http://code.jquery.com/jquery-1.11.0.min.js';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
/*
// Adds a colored div when body is clicked
$(document).ready(function () {
$('body').click(function (ev) {
mouseX = ev.pageX;
mouseY = ev.pageY
var color = '#1daeae';
var size = '2px';
$("body").append($('<div></div>')
.css('position', 'absolute')
.css('top', mouseY + 'px')
.css('left', mouseX + 'px')
.css('width', size)
.css('height', size)
.css('background-color', color));
});
});
*/
// Public variables
var downX;
var downY;
var cmdVelTopic;
var twist;
function init() {
var arm_div = document.querySelectorAll('.js_arm_div');
this.app = new App();
var self = this;
app.ros.on('connection', function () {
console.log("We are connected!");
arm_div.forEach(function(element)
{
element.onmousedown = function (e) {
e = e || window.event;
var elementId = (e.target || e.srcElement).parentElement.id;
console.log(elementId);
downX = e.offsetX;
downY = e.offsetY;
};
element.onmouseup = function (e) {
e = e || window.event;
var elementId = (e.target || e.srcElement).parentElement.id;
console.log(elementId);
console.log("offsetX :" + e.offsetX + " offsetY : " + e.offsetY);
self.app.arm.moveArmByDelta(e.offsetX - downX, e.offsetY - downY, elementId);
};
});
});
// Publishing a Topic
// ------------------
cmdVelTopic = new ROSLIB.Topic({
ros : app.ros,
name : '/cmd_vel',
messageType : 'geometry_msgs/Twist'
});
// These lines create a message that conforms to the structure of the Twist defined in our ROS installation
// It initalizes all properties to zero. They will be set to appropriate values before we publish this message.
twist = new ROSLIB.Message({
linear : {
x : 0.0,
y : 0.0,
z : 0.0
},
angular : {
x : 0.0,
y : 0.0,
z : 0.0
}
});
}
/* This function:
- retrieves numeric values from the text boxes
- assigns these values to the appropriate values in the twist message
- publishes the message to the cmd_vel topic.
*/
function pubMessage() {
/**
Set the appropriate values on the twist message object according to values in text boxes
It seems that turtlesim only uses the x property of the linear object
and the z property of the angular object
**/
var linearX = 0.0;
var angularZ = 0.0;
// get values from text input fields. Note for simplicity we are not validating.
linearX = Number(document.getElementById('linearXText').value);
angularZ = Number(document.getElementById('angularZText').value);
// Set the appropriate values on the message object
twist.linear.x = linearX;
twist.angular.z = angularZ;
// Publish the message
cmdVelTopic.publish(twist);
// that.emit('change', twist);
document.getElementById("demo").innerHTML = "Updated Position";
}