-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulateur_lookahead.html
More file actions
397 lines (338 loc) · 15.8 KB
/
Copy pathsimulateur_lookahead.html
File metadata and controls
397 lines (338 loc) · 15.8 KB
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simulateur Lookahead</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500&display=swap');
body {
font-family: 'Inter', sans-serif;
background-color: #0f172a;
color: #f8fafc;
margin: 0;
overflow: hidden;
}
.canvas-container {
position: relative;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
background: #1e293b;
border-radius: 12px;
box-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.2);
overflow: hidden;
touch-action: none; /* Prevent scrolling while dragging on mobile */
}
canvas {
display: block;
cursor: crosshair;
max-width: 100%;
height: auto;
}
.console-font {
font-family: 'Fira Code', monospace;
}
/* Custom Scrollbar */
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: #1e293b; }
::-webkit-scrollbar-thumb { background: #475569; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #64748b; }
</style>
</head>
<body class="h-screen flex flex-col md:flex-row">
<aside class="w-full md:w-80 bg-slate-900 border-r border-slate-800 flex flex-col h-1/3 md:h-full shrink-0">
<div class="p-5 border-b border-slate-800">
<h1 class="text-xl font-bold bg-clip-text text-transparent bg-gradient-to-r from-blue-400 to-indigo-400">
Simulateur Pure Pursuit
</h1>
<p class="text-sm text-slate-400 mt-1">Test interactif de la fonction Lookahead</p>
</div>
<div class="p-5 border-b border-slate-800">
<label class="block text-sm font-medium text-slate-300 mb-2">
Lookahead Distance: <span id="lookaheadValue" class="text-blue-400 font-bold">50</span> mm
</label>
<input type="range" id="lookaheadSlider" min="10" max="200" value="50" class="w-full accent-blue-500">
</div>
<div class="p-5 flex-1 flex flex-col overflow-hidden">
<h2 class="text-xs font-bold text-slate-500 uppercase tracking-wider mb-3">Sortie Console (chprintf)</h2>
<div id="consoleOutput" class="flex-1 bg-slate-950 rounded-md p-3 overflow-y-auto console-font text-xs text-green-400 border border-slate-800">
<!-- Logs will be appended here -->
</div>
</div>
</aside>
<main class="flex-1 flex flex-col p-4 md:p-6 bg-slate-950 overflow-hidden h-2/3 md:h-full">
<div class="flex-1 flex flex-col bg-slate-800 rounded-xl border border-slate-700 shadow-xl overflow-hidden p-2">
<div class="flex justify-between items-center px-4 py-3 bg-slate-800/50 rounded-t-lg border-b border-slate-700">
<div class="flex space-x-4 text-sm">
<div class="flex items-center"><span class="w-3 h-3 rounded-full bg-green-500 mr-2 shadow-[0_0_8px_rgba(34,197,94,0.6)]"></span> Robot</div>
<div class="flex items-center"><span class="w-3 h-3 rounded-full bg-amber-500 mr-2 shadow-[0_0_8px_rgba(245,158,11,0.6)]"></span> Lookahead Point</div>
<div class="flex items-center"><span class="w-3 h-3 rounded-full border border-green-500 border-dashed mr-2"></span> Cercle de Lookahead</div>
</div>
<div class="text-xs text-slate-400 bg-slate-900 px-3 py-1 rounded-full">
Cliquer et glisser pour déplacer
</div>
</div>
<div class="canvas-container flex-1" id="canvasWrapper">
<canvas id="simCanvas" width="800" height="450"></canvas>
</div>
</div>
</main>
<script>
const canvas = document.getElementById('simCanvas');
const ctx = canvas.getContext('2d');
const wrapper = document.getElementById('canvasWrapper');
const lookaheadSlider = document.getElementById('lookaheadSlider');
const lookaheadValueDisplay = document.getElementById('lookaheadValue');
const consoleOutput = document.getElementById('consoleOutput');
// Waypoints from the prompt
const waypoints = [
{x: 700, y: 40}, {x: 690, y: 50}, {x: 680, y: 60}, {x: 670, y: 70}, {x: 660, y: 80}, {x: 650, y: 90},
{x: 640, y: 100}, {x: 630, y: 110}, {x: 620, y: 120}, {x: 610, y: 130}, {x: 600, y: 140}, {x: 590, y: 150},
{x: 580, y: 160}, {x: 570, y: 170}, {x: 560, y: 180}, {x: 560, y: 190}, {x: 560, y: 200}, {x: 560, y: 210},
{x: 560, y: 220}, {x: 560, y: 230}, {x: 560, y: 240}, {x: 550, y: 250}, {x: 540, y: 260}, {x: 530, y: 270},
{x: 520, y: 270}, {x: 510, y: 270}, {x: 500, y: 280}, {x: 490, y: 290}, {x: 480, y: 300}, {x: 470, y: 310},
{x: 460, y: 310}, {x: 450, y: 310}, {x: 440, y: 310}, {x: 430, y: 310}, {x: 420, y: 310}, {x: 410, y: 310},
{x: 400, y: 310}, {x: 390, y: 310}, {x: 380, y: 310}, {x: 370, y: 310}, {x: 360, y: 320}, {x: 350, y: 330},
{x: 340, y: 330}, {x: 330, y: 330}, {x: 320, y: 330}, {x: 310, y: 330}, {x: 300, y: 330}, {x: 290, y: 330},
{x: 280, y: 330}, {x: 270, y: 330}, {x: 260, y: 330}, {x: 250, y: 330}, {x: 240, y: 330}, {x: 230, y: 330},
{x: 220, y: 320}, {x: 210, y: 310}, {x: 200, y: 300}, {x: 190, y: 290}, {x: 180, y: 280}, {x: 170, y: 270},
{x: 160, y: 270}, {x: 150, y: 270}, {x: 140, y: 270}, {x: 130, y: 280}, {x: 120, y: 290}, {x: 110, y: 300},
{x: 100, y: 310}, {x: 90, y: 320}, {x: 80, y: 330}, {x: 70, y: 340}, {x: 60, y: 350}, {x: 50, y: 360}
];
// State
let robotX = 650;
let robotY = 150;
let lookaheadDistance = 50.0;
let isDragging = false;
// Visual Offset (to center the plot visually in the canvas)
const offsetX = 40;
const offsetY = 40;
// Math coordinates to Canvas coordinates (Flip Y axis)
function worldToCanvas(x, y) {
return {
cx: x + offsetX,
cy: canvas.height - y - offsetY
};
}
// Canvas coordinates to Math coordinates
function canvasToWorld(cx, cy) {
return {
x: cx - offsetX,
y: canvas.height - cy - offsetY
};
}
function logConsole(msg) {
const entry = document.createElement('div');
entry.textContent = `> ${msg}`;
consoleOutput.appendChild(entry);
// Auto-scroll
consoleOutput.scrollTop = consoleOutput.scrollHeight;
// Keep only last 50 logs to prevent memory bloat
while (consoleOutput.children.length > 50) {
consoleOutput.removeChild(consoleOutput.firstChild);
}
}
// This function exactly mirrors the logic of your C code
function getLookaheadPoint(positionX, positionY) {
let out_x = 0;
let out_y = 0;
let total_waypoints = waypoints.length;
// --- Search Closest Point ---
let closest_point_index = 0;
let delta_x = waypoints[0].x - positionX;
let delta_y = waypoints[0].y - positionY;
let min_distance_squared = delta_x*delta_x + delta_y*delta_y;
for (let i = 1; i < total_waypoints; i++) {
let dx = waypoints[i].x - positionX;
let dy = waypoints[i].y - positionY;
let dist_squared = dx*dx + dy*dy;
if (dist_squared <= min_distance_squared) {
min_distance_squared = dist_squared;
closest_point_index = i;
}
}
logConsole(`closest_point_index = ${closest_point_index}`);
logConsole(`position = (${positionX.toFixed(2)}, ${positionY.toFixed(2)})`);
// --- Search Look Ahead Point ---
let lookahead_max_dist_squared = lookaheadDistance * lookaheadDistance;
for (let i = closest_point_index; i < total_waypoints; i++) {
let dx = waypoints[i].x - positionX;
let dy = waypoints[i].y - positionY;
let dist_squared = dx*dx + dy*dy;
if (dist_squared > lookahead_max_dist_squared) {
out_x = waypoints[i].x;
out_y = waypoints[i].y;
logConsole(`found lookahead: (${waypoints[i].x}, ${waypoints[i].y})`);
return { x: out_x, y: out_y, closestIdx: closest_point_index, lookaheadIdx: i };
}
}
// --- No Point Ahead Enough ---
out_x = waypoints[total_waypoints-1].x;
out_y = waypoints[total_waypoints-1].y;
logConsole(`not lookahead: (${waypoints[total_waypoints-1].x}, ${waypoints[total_waypoints-1].y})`);
return { x: out_x, y: out_y, closestIdx: closest_point_index, lookaheadIdx: total_waypoints-1 };
}
function drawGrid() {
ctx.strokeStyle = '#334155';
ctx.lineWidth = 1;
ctx.beginPath();
for (let i = 0; i <= canvas.width; i += 50) {
ctx.moveTo(i, 0); ctx.lineTo(i, canvas.height);
}
for (let i = 0; i <= canvas.height; i += 50) {
ctx.moveTo(0, i); ctx.lineTo(canvas.width, i);
}
ctx.stroke();
}
function drawPath() {
if (waypoints.length === 0) return;
// Draw line
ctx.strokeStyle = '#64748b';
ctx.lineWidth = 2;
ctx.beginPath();
let start = worldToCanvas(waypoints[0].x, waypoints[0].y);
ctx.moveTo(start.cx, start.cy);
for (let i = 1; i < waypoints.length; i++) {
let pt = worldToCanvas(waypoints[i].x, waypoints[i].y);
ctx.lineTo(pt.cx, pt.cy);
}
ctx.stroke();
// Draw points
for (let i = 0; i < waypoints.length; i++) {
let pt = worldToCanvas(waypoints[i].x, waypoints[i].y);
ctx.fillStyle = '#3b82f6';
ctx.beginPath();
ctx.arc(pt.cx, pt.cy, 3, 0, Math.PI * 2);
ctx.fill();
// Draw numbers (small)
if (i % 5 === 0 || i === waypoints.length - 1) { // Optimize clutter
ctx.fillStyle = '#f87171';
ctx.font = '10px Inter';
ctx.fillText(i, pt.cx + 5, pt.cy - 5);
}
}
}
function drawRobotAndLookahead(result) {
let rCanvas = worldToCanvas(robotX, robotY);
let closestCanvas = worldToCanvas(waypoints[result.closestIdx].x, waypoints[result.closestIdx].y);
let lCanvas = worldToCanvas(result.x, result.y);
// 1. Draw segment to closest point (visual aid)
ctx.strokeStyle = '#f43f5e';
ctx.setLineDash([4, 4]);
ctx.lineWidth = 1;
ctx.beginPath();
ctx.moveTo(rCanvas.cx, rCanvas.cy);
ctx.lineTo(closestCanvas.cx, closestCanvas.cy);
ctx.stroke();
ctx.setLineDash([]); // Reset
// 2. Draw Lookahead Circle
ctx.strokeStyle = '#22c55e';
ctx.setLineDash([5, 5]);
ctx.lineWidth = 1;
ctx.beginPath();
ctx.arc(rCanvas.cx, rCanvas.cy, lookaheadDistance, 0, Math.PI * 2);
ctx.stroke();
ctx.setLineDash([]); // Reset
// 3. Highlight closest point
ctx.fillStyle = '#f43f5e';
ctx.beginPath();
ctx.arc(closestCanvas.cx, closestCanvas.cy, 5, 0, Math.PI * 2);
ctx.fill();
// 4. Draw Lookahead Point
ctx.fillStyle = '#f59e0b';
ctx.beginPath();
ctx.arc(lCanvas.cx, lCanvas.cy, 6, 0, Math.PI * 2);
ctx.fill();
// Glow effect
ctx.shadowBlur = 10;
ctx.shadowColor = '#f59e0b';
ctx.fill();
ctx.shadowBlur = 0; // reset
// 5. Draw Robot
ctx.fillStyle = '#22c55e';
ctx.beginPath();
ctx.arc(rCanvas.cx, rCanvas.cy, 8, 0, Math.PI * 2);
ctx.fill();
// Glow effect
ctx.shadowBlur = 15;
ctx.shadowColor = '#22c55e';
ctx.fill();
ctx.shadowBlur = 0; // reset
// Robot Crosshair
ctx.strokeStyle = '#ffffff';
ctx.lineWidth = 2;
ctx.beginPath();
ctx.moveTo(rCanvas.cx - 4, rCanvas.cy); ctx.lineTo(rCanvas.cx + 4, rCanvas.cy);
ctx.moveTo(rCanvas.cx, rCanvas.cy - 4); ctx.lineTo(rCanvas.cx, rCanvas.cy + 4);
ctx.stroke();
}
function render() {
// Clear
ctx.clearRect(0, 0, canvas.width, canvas.height);
drawGrid();
drawPath();
// Compute lookahead based on pure C logic
logConsole("--- Frame Update ---");
const lookaheadResult = getLookaheadPoint(robotX, robotY);
drawRobotAndLookahead(lookaheadResult);
}
function updateRobotPos(clientX, clientY) {
const rect = canvas.getBoundingClientRect();
// Calculate scale in case the canvas is stretched by CSS
const scaleX = canvas.width / rect.width;
const scaleY = canvas.height / rect.height;
const cx = (clientX - rect.left) * scaleX;
const cy = (clientY - rect.top) * scaleY;
const worldPos = canvasToWorld(cx, cy);
robotX = worldPos.x;
robotY = worldPos.y;
// Re-render
requestAnimationFrame(render);
}
// Mouse Events
canvas.addEventListener('mousedown', (e) => {
isDragging = true;
updateRobotPos(e.clientX, e.clientY);
});
window.addEventListener('mousemove', (e) => {
if (isDragging) {
updateRobotPos(e.clientX, e.clientY);
}
});
window.addEventListener('mouseup', () => {
isDragging = false;
});
// Touch Events for mobile
canvas.addEventListener('touchstart', (e) => {
isDragging = true;
e.preventDefault(); // Prevent scrolling
updateRobotPos(e.touches[0].clientX, e.touches[0].clientY);
}, { passive: false });
window.addEventListener('touchmove', (e) => {
if (isDragging) {
e.preventDefault();
updateRobotPos(e.touches[0].clientX, e.touches[0].clientY);
}
}, { passive: false });
window.addEventListener('touchend', () => {
isDragging = false;
});
// Slider Event
lookaheadSlider.addEventListener('input', (e) => {
lookaheadDistance = parseFloat(e.target.value);
lookaheadValueDisplay.textContent = lookaheadDistance.toFixed(0);
requestAnimationFrame(render);
});
// Initial Render
// We do a small timeout to clear the initial console logs spam and just show one frame
setTimeout(() => {
consoleOutput.innerHTML = '';
render();
}, 100);
</script>
</body>
</html>