-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
358 lines (288 loc) · 9.68 KB
/
index.html
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>skillsPlanner</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.13.1/jquery-ui.min.js"></script>
</head>
<body style="overflow: hidden; margin: 0; padding: 0;">
<canvas id="canvas" width="500" height="500" style="
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
"></canvas>
<h1 id="robotXY">
Robot X = 0;
Robot Y = 0;
</h1>
<button onclick="newWaypoint()">Create waypoint</button>
<button onclick="output()">Output Data</button>
<br> <br>
Input Waypoints<br>
<textarea name="Input" cols="40" rows="5"></textarea>
<!--
<p>List o' Instructioins (Right Click To Add Point, Ctrl+Z To Undo, Ctrl+Shift+Z To Redo Once):</p>
<div id="listOInstructions">
-->
</div>
<script type="text/javascript">
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext('2d');
//dimensions of square robot
var robotDim = 10;
const listOInstructions = document.getElementById("listOInstructions");
var listOfPoints = [];
//get field img
const fieldImg = new Image();
fieldImg.src = 'field2D.jpg';
fieldImg.onload = function(){
redraw();
}
const robotXYHTML = document.getElementById("robotXY");
const moveDis = document.getElementById("moveDis");
var testingVal = 70.86614;
//*******************
//*******************
//*******************
//*******************
//*******************
//*******************
//*******************
//*******************
//Pure pursuit stuffs
var spacing = 6;
var weight_smooth = 0.865;
var weight_data = 1 - weight_smooth;
var tolerance = 0.001;
class vector2 {
x;
y;
};
var robotPos = new vector2();
robotPos.x = 0;
robotPos.y = 0;
var waypoints = [robotPos];
var path = [];
function newWaypoint() {
ctx.beginPath();
ctx.arc(canvas.width/2, canvas.height/2, robotDim, 0, 2 * Math.PI, false);
ctx.fill();
var tmp = new vector2();
tmp.x = 0;
tmp.y = 0;
waypoints.push(tmp);
redraw();
}
function round(num) {
return Math.round((num + Number.EPSILON) * 100) / 100;
}
function output() {
var out = "{" + round(fieldW(waypoints[0].x))+ ", " + -1 * round(fieldH(waypoints[0].y)) + "}"
if(waypoints.length > 1)
out += ",<br>";
else
out += "<br>";
for(let i = 1; i < waypoints.length - 1; i++) {
out += " "
out += "{" + round(fieldW(waypoints[i].x))+ ", " + -1 * round(fieldH(waypoints[i].y)) + "},<br>";
}
if(waypoints.length > 1) {
out += " "
out += "{" + round(fieldW(waypoints[waypoints.length - 1].x))+ ", " + -1 * round(fieldH(waypoints[waypoints.length - 1].y)) + "}<br>";
}
var w = window.open("about:blank");
w.document.write(out);
}
function canvasW(w) {
return w / (testingVal/(canvas.width/2));
}
function canvasH(h) {
return h / (testingVal/(canvas.width/2));
}
function fieldW(w) {
return w * (testingVal/(canvas.width/2));
}
function fieldH(h) {
return h * (testingVal/(canvas.width/2));
}
function redraw(x, y){
//draw starting picture
ctx.drawImage(fieldImg, 0, 0, fieldImg.width, fieldImg.height, 0, 0, canvas.width, canvas.height);
//draw axis
ctx.fillStyle = "white";
ctx.fillRect(canvas.width/2 - 1, 0, 3, canvas.height);
ctx.fillRect(0, canvas.height/2 - 1, canvas.width, 3);
//Path computing
for(let i = 0; i < waypoints.length - 1; i++) {
var vector = new vector2();
vector.x = waypoints[i+1].x - waypoints[i].x;
vector.y = waypoints[i+1].y - waypoints[i].y;
vector.x = fieldW(vector.x);
vector.y = fieldH(vector.y);
var magnitude = Math.sqrt(Math.pow(vector.x, 2) + Math.pow(vector.y, 2));
var num_points_that_fit = Math.ceil(magnitude / spacing);
vector.x = (vector.x / magnitude) * spacing;
vector.y = (vector.y / magnitude) * spacing;
for(let j = 0; j < num_points_that_fit; j++) {
var tmp_vector = new vector2();
tmp_vector.x = fieldW(waypoints[i].x) + (vector.x * j);
tmp_vector.y = fieldH(waypoints[i].y) + (vector.y * j);
path.push(tmp_vector);
}
}
var curvedPath = [];
//Copy array
for(let i = 0; i < path.length; i++) {
var tmp = new vector2();
tmp.x = path[i].x;
tmp.y = path[i].y;
curvedPath.push(tmp);
}
//Smoother
var change = tolerance;
while (change >= tolerance) {
change = 0.0;
for(let i = 1; i < path.length - 1; i++) {
var auxX = curvedPath[i].x;
curvedPath[i].x += weight_data * (path[i].x - curvedPath[i].x) + weight_smooth * (curvedPath[i-1].x + curvedPath[i+1].x - (2.0 * curvedPath[i].x));
change += Math.abs(auxX - curvedPath[i].x);
var auxY = curvedPath[i].y;
curvedPath[i].y += weight_data * (path[i].y - curvedPath[i].y) + weight_smooth * (curvedPath[i-1].y + curvedPath[i+1].y - (2.0 * curvedPath[i].y));
change += Math.abs(auxY - curvedPath[i].y);
}
}
//Path drawing
ctx.fillStyle = "blue";
for(let i = 0; i < path.length; i++) {
ctx.beginPath();
ctx.arc(canvasW(curvedPath[i].x) + canvas.width/2, canvasH(curvedPath[i].y) + canvas.height/2, robotDim - 4, 0, 2 * Math.PI, false);
ctx.fill();
}
path.length = 0;
//curvedPath.length = 0;
//Waypoints
ctx.fillStyle = "red";
//ctx.fillRect( (robotPos.x - robotDim/2) + canvas.width/2 - 5, (robotPos.y - robotDim/2) + canvas.height/2 - 5, robotDim, robotDim);
for(let i = 0; i < waypoints.length; i++) {
ctx.beginPath();
ctx.arc(waypoints[i].x + canvas.width/2, waypoints[i].y + canvas.height/2, robotDim, 0, 2 * Math.PI, false);
ctx.fill();
}
}
var movingRobot = false;
var moveIndex = 0;
var distanceFromRobotX = 0;
var distanceFromRobotY = 0;
canvas.addEventListener('mousedown', function(e){
var lastClosestDistance = 1000;
var lastClosestIndex = 0;
var smallestX = 0;
var smallestY = 0;
for(let i = 0; i < waypoints.length; i++) {
var rect = canvas.getBoundingClientRect();
//gets points based on middle of canvas being (0,0)
var mX = e.x - rect.left - canvas.width/2;
var mY = e.y - rect.top - canvas.height/2;
//console.log(mX + ", " + mY);
var scaledX = mX * (testingVal/(canvas.width/2));
var scaledY = -1 * mY * (testingVal/( canvas.height/2));
distanceFromRobotX = (e.x - rect.left - canvas.width/2) - (waypoints[i].x - robotDim/2);
distanceFromRobotY = (e.y - rect.top - canvas.height/2) - (waypoints[i].y + robotDim/2);
var distance = Math.sqrt(Math.pow(distanceFromRobotX, 2) + Math.pow(distanceFromRobotY, 2));
//console.log(lastClosestIndex);
if(distance < lastClosestDistance) {
lastClosestDistance = distance;
lastClosestIndex = i;
smallestX = distanceFromRobotX;
smallestY = distanceFromRobotY;
}
}
distanceFromRobotX = smallestX;
distanceFromRoboty = smallestY;
if(Math.abs( smallestX ) < robotDim * 9/10 && Math.abs( smallestY ) < robotDim * 9/10 && !movingRobot){
movingRobot = true;
moveIndex = lastClosestIndex;
robotXYHTML.innerText = "Robot X = " + round(fieldW(waypoints[moveIndex].x)) + " Robot Y = " + -1 * round(fieldH(waypoints[moveIndex].y));
redraw();
}
});
canvas.addEventListener('mousemove', function(e){
if(movingRobot) {
var rect = canvas.getBoundingClientRect();
waypoints[moveIndex].x = e.x - rect.left - canvas.width/2; //- distanceFromRobotX/2;
waypoints[moveIndex].y = e.y - rect.top - canvas.height/2; //- distanceFromRobotY/2;
//vex field 140.5 x 140.5 in
//140.5 / 2 = 70.25
robotXYHTML.innerText = "Robot X = " + round(fieldW(waypoints[moveIndex].x)) + " Robot Y = " + -1 * round(fieldH(waypoints[moveIndex].y));
redraw();
}
});
canvas.addEventListener('mouseup', function(e){
movingRobot = false;
});
canvas.addEventListener('contextmenu', function(e) {
event.preventDefault(); //stop right click menu showin up
//var mX = e.x - rect.left - canvas.width/2;
//var mY = e.y - rect.top - canvas.height/2;
});
var area = document.querySelector('textarea[name="Input"]');
area.addEventListener("input", function() {
if(area != null) {
let inputData = area.value;
console.log(inputData);
var tmpWaypoints = [];
var nextSearch = 0;
for(let i = 0; i < (inputData.match(/\n/g) || []).length + 1; i++) {
var tmp = new vector2();
var tmpx = inputData.substring(
inputData.indexOf("{", nextSearch) + 1,
inputData.indexOf(",", nextSearch)
)
console.log(tmpx);
tmpx = canvasW(tmpx * 1);
var tmpy = inputData.substring(
inputData.indexOf(",", nextSearch) + 2,
inputData.indexOf("}", nextSearch)
)
console.log(tmpy);
tmpy = canvasH(tmpy * -1);
tmp.x = tmpx;
tmp.y = tmpy;
tmpWaypoints.push(tmp);
nextSearch = inputData.indexOf("\n", nextSearch + 1);
}
waypoints = tmpWaypoints;
for(let i = 0; i < tmpWaypoints.length; i++)
console.log(tmpWaypoints[i]);
for(let i = 0; i < waypoints.length; i++)
console.log(tmpWaypoints[i]);
redraw();
}
}, false);
/*
var toggleKeyRedo = false;
var toggleKey = false;
var removedElement;
canvas.addEventListener("keydown", function(e){
if (e.ctrlKey && e.which = "z") {
if (e.shiftKey && removedElement ) {
listOPoints.push(removedElement);
removedElement = undefined;
} else {
if (e.which = "z" && !toggleKey) {
removedElement = listOPoints.pop(); //remove last element
toggleKey = true;
}
}
}
});
canvas.addEventListener("keyup", function(e){
toggleKey = false;
});
*/
</script>
</body>
</html>