-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath_finder.html
More file actions
449 lines (391 loc) · 13.6 KB
/
Copy pathpath_finder.html
File metadata and controls
449 lines (391 loc) · 13.6 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Robot Simulator - Light Mode</title>
<style>
* { box-sizing: border-box; outline: none; }
body {
/* Fond principal très clair */
background-color: #f3f4f6;
color: #1f2937;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
user-select: none;
}
#simContainer {
/* Conteneur de la map blanc avec une ombre douce */
background-color: #ffffff;
border: 1px solid #e5e7eb;
border-radius: 12px;
padding: 10px;
margin-bottom: 25px;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
}
canvas {
background-color: transparent;
cursor: crosshair;
border-radius: 8px;
display: block;
}
.footer {
display: flex;
gap: 20px;
flex-wrap: wrap;
justify-content: center;
}
/* Style des boutons en Light Mode */
button {
background: #ffffff;
border: 2px solid #3b82f6;
border-radius: 8px;
color: #3b82f6;
padding: 12px 28px;
font-size: 14px;
font-weight: 700;
letter-spacing: 1px;
cursor: pointer;
transition: all 0.2s ease;
box-shadow: 0 4px 6px rgba(59, 130, 246, 0.1);
text-transform: uppercase;
}
button:hover {
background: #eff6ff; /* Bleu très clair */
border-color: #2563eb;
color: #2563eb;
box-shadow: 0 6px 12px rgba(59, 130, 246, 0.2);
transform: translateY(-2px);
}
button:active {
transform: translateY(1px);
box-shadow: 0 2px 4px rgba(59, 130, 246, 0.1);
}
/* Bouton Goto spécifique (Violet) */
#btnGoto {
border-color: #8b5cf6;
color: #8b5cf6;
box-shadow: 0 4px 6px rgba(139, 92, 246, 0.1);
}
#btnGoto:hover {
background: #f5f3ff;
border-color: #7c3aed;
color: #7c3aed;
box-shadow: 0 6px 12px rgba(139, 92, 246, 0.2);
}
</style>
</head>
<body>
<div id="simContainer">
<canvas id="simCanvas"></canvas>
</div>
<div class="footer">
<button id="btnMargins">Add Margins</button>
<button id="btnExportPng">Export PNG</button>
<button id="btnExportC">Export C Map</button>
<button id="btnGoto">Goto Target</button>
</div>
<script>
const WHEEL_RADIUS_MM = 20.0
const WHEEL_BASE_MM = 55.2
const GRID_W = 87
const GRID_H = 41
const PX_PER_CM = 15
const MAP_W = GRID_W * PX_PER_CM
const MAP_H = GRID_H * PX_PER_CM
const ROBOT_SIZE_CM = 7
const LOOKAHEAD_DISTANCE = 4
// Couleurs adaptées au thème clair
const C_FREE_FILL = "rgba(255, 255, 255, 0)"
const C_FREE_BORDER = "rgba(0, 0, 0, 0.08)" // Grille plus foncée pour être vue sur blanc
const C_OBS_FILL = "rgba(239, 68, 68, 0.6)" // Rouge clair
const C_OBS_BORDER = "rgba(220, 38, 38, 0.9)"
const C_MARGIN_BORDER = "rgba(245, 158, 11, 0.9)" // Orange bien visible
const C_PATH = "rgba(37, 99, 235, 0.7)" // Bleu soutenu pour le chemin
const C_TARGET = "rgba(139, 92, 246, 0.9)"
const C_LHD = "rgba(16, 185, 129, 0.9)" // Vert émeraude
const canvas = document.getElementById("simCanvas")
const ctx = canvas.getContext("2d")
canvas.width = MAP_W
canvas.height = MAP_H
// Chargement de l'image de fond
const bgImage = new Image()
bgImage.src = "images/map.png"
let grid = Array.from({length: GRID_H}, () => Array(GRID_W).fill(0))
let rx = 5.0, ry = 5.0, rTheta = 0.0
let targetCell = null
let path = []
let autoMode = false
let keys = new Set()
let drawMode = null
canvas.addEventListener("contextmenu", e => e.preventDefault())
function getGridCoords(e) {
const rect = canvas.getBoundingClientRect()
const x = e.clientX - rect.left
const y = e.clientY - rect.top
return {
cx: Math.max(0, Math.min(GRID_W - 1, Math.floor(x / PX_PER_CM))),
cy: Math.max(0, Math.min(GRID_H - 1, Math.floor(y / PX_PER_CM)))
}
}
canvas.addEventListener("mousedown", e => {
const {cx, cy} = getGridCoords(e)
if (e.button === 0) {
drawMode = grid[cy][cx] === 1 ? 0 : 1
grid[cy][cx] = drawMode
} else if (e.button === 2) {
targetCell = {x: cx, y: cy}
autoMode = false
}
})
canvas.addEventListener("mousemove", e => {
const {cx, cy} = getGridCoords(e)
if (e.buttons & 1 && drawMode !== null) {
grid[cy][cx] = drawMode
} else if (e.buttons & 2) {
targetCell = {x: cx, y: cy}
autoMode = false
}
})
window.addEventListener("mouseup", () => drawMode = null)
window.addEventListener("keydown", e => {
if (!e.repeat) keys.add(e.key.toLowerCase())
})
window.addEventListener("keyup", e => keys.delete(e.key.toLowerCase()))
document.getElementById("btnMargins").addEventListener("click", () => {
const marginRadius = ROBOT_SIZE_CM / 2.0
let newGrid = grid.map(row => [...row])
for (let y = 0; y < GRID_H; y++) {
for (let x = 0; x < GRID_W; x++) {
if (grid[y][x] === 1) continue
let isMargin = false
const minDy = Math.max(0, y - Math.ceil(marginRadius))
const maxDy = Math.min(GRID_H - 1, y + Math.ceil(marginRadius))
const minDx = Math.max(0, x - Math.ceil(marginRadius))
const maxDx = Math.min(GRID_W - 1, x + Math.ceil(marginRadius))
for (let oy = minDy; oy <= maxDy; oy++) {
for (let ox = minDx; ox <= maxDx; ox++) {
if (grid[oy][ox] === 1 && Math.hypot(x - ox, y - oy) <= marginRadius) {
isMargin = true
break
}
}
if (isMargin) break
}
if (isMargin) newGrid[y][x] = 2
}
}
grid = newGrid
console.log("Margins generated.")
})
document.getElementById("btnExportPng").addEventListener("click", () => {
ctx.clearRect(0, 0, MAP_W, MAP_H)
if (bgImage.complete && bgImage.naturalHeight !== 0) {
ctx.drawImage(bgImage, 0, 0, MAP_W, MAP_H)
}
drawGridMap()
const link = document.createElement("a")
link.download = "map_with_obstacle.png"
link.href = canvas.toDataURL("image/png")
link.click()
console.log("Map successfully exported to map_with_obstacle.png")
})
document.getElementById("btnExportC").addEventListener("click", () => {
let output = `const uint8_t map[${GRID_H}][${GRID_W}] = {\n`
for (let y = 0; y < GRID_H; y++) {
const row = grid[y].map(val => val > 0 ? "1" : "0").join(", ")
output += ` {${row}},\n`
}
output += "};\n"
console.log(output)
})
document.getElementById("btnGoto").addEventListener("click", () => {
if (targetCell) {
path = aStar({x: Math.floor(rx), y: Math.floor(ry)}, targetCell)
autoMode = true
}
})
function aStar(start, goal) {
let openSet = [{pos: start, g: 0, f: Math.hypot(start.x - goal.x, start.y - goal.y)}]
let cameFrom = new Map()
let gScore = new Map()
gScore.set(`${start.x},${start.y}`, 0)
while (openSet.length > 0) {
openSet.sort((a, b) => a.f - b.f)
let current = openSet.shift().pos
if (current.x === goal.x && current.y === goal.y) {
let p = []
let currKey = `${current.x},${current.y}`
while (cameFrom.has(currKey)) {
p.push({x: parseInt(currKey.split(',')[0]), y: parseInt(currKey.split(',')[1])})
currKey = cameFrom.get(currKey)
}
p.push(start)
return p.reverse()
}
const dirs = [[-1,0],[1,0],[0,-1],[0,1],[-1,-1],[-1,1],[1,-1],[1,1]]
for (let [dx, dy] of dirs) {
let nx = current.x + dx, ny = current.y + dy
if (nx >= 0 && nx < GRID_W && ny >= 0 && ny < GRID_H && grid[ny][nx] === 0) {
let cost = (dx !== 0 && dy !== 0) ? 1.414 : 1.0
let tg = gScore.get(`${current.x},${current.y}`) + cost
let nKey = `${nx},${ny}`
if (!gScore.has(nKey) || tg < gScore.get(nKey)) {
cameFrom.set(nKey, `${current.x},${current.y}`)
gScore.set(nKey, tg)
openSet.push({
pos: {x: nx, y: ny},
g: tg,
f: tg + Math.hypot(nx - goal.x, ny - goal.y)
})
}
}
}
}
return []
}
function getLookahead() {
if (path.length === 0) return null
let minD = Infinity
let closestIdx = 0
for (let i = 0; i < path.length; i++) {
let cx = path[i].x + 0.5, cy = path[i].y + 0.5
let d = Math.hypot(cx - rx, cy - ry)
if (d < minD) {
minD = d
closestIdx = i
}
}
for (let i = closestIdx; i < path.length; i++) {
let px = path[i].x + 0.5, py = path[i].y + 0.5
if (Math.hypot(px - rx, py - ry) >= LOOKAHEAD_DISTANCE) {
return {x: px, y: py, idx: i}
}
}
let last = path[path.length - 1]
return {x: last.x + 0.5, y: last.y + 0.5, idx: path.length - 1}
}
function applyKinematics(vl, vr, dt) {
let r = WHEEL_RADIUS_MM / 10.0
let L = WHEEL_BASE_MM / 10.0
let v = (r / 2.0) * (vr + vl)
let w = (r / L) * (vr - vl)
rx += v * Math.cos(rTheta) * dt
ry += v * Math.sin(rTheta) * dt
rTheta += w * dt
rTheta = (rTheta + Math.PI) % (2 * Math.PI) - Math.PI
rx = Math.max(0, Math.min(GRID_W, rx))
ry = Math.max(0, Math.min(GRID_H, ry))
}
function drawGridMap() {
for (let y = 0; y < GRID_H; y++) {
for (let x = 0; x < GRID_W; x++) {
let val = grid[y][x]
let px = x * PX_PER_CM
let py = y * PX_PER_CM
if (val === 1) {
ctx.fillStyle = C_OBS_FILL
ctx.strokeStyle = C_OBS_BORDER
} else if (val === 2) {
ctx.fillStyle = C_FREE_FILL
ctx.strokeStyle = C_MARGIN_BORDER
} else {
ctx.fillStyle = C_FREE_FILL
ctx.strokeStyle = C_FREE_BORDER
}
ctx.fillRect(px, py, PX_PER_CM, PX_PER_CM)
ctx.lineWidth = 1
ctx.strokeRect(px, py, PX_PER_CM, PX_PER_CM)
}
}
}
function render() {
ctx.clearRect(0, 0, MAP_W, MAP_H)
// Dessine l'image de fond si elle est chargée
if (bgImage.complete && bgImage.naturalHeight !== 0) {
ctx.drawImage(bgImage, 0, 0, MAP_W, MAP_H)
}
drawGridMap()
if (autoMode) {
ctx.fillStyle = C_PATH
ctx.font = "11px Segoe UI, sans-serif"
ctx.fontWeight = "bold"
for (let i = 0; i < path.length; i++) {
let px = path[i].x * PX_PER_CM
let py = path[i].y * PX_PER_CM
ctx.fillStyle = C_PATH
ctx.fillRect(px, py, PX_PER_CM, PX_PER_CM)
// Le texte de l'ordre du chemin est blanc pour contraster avec le bleu
ctx.fillStyle = "#ffffff"
ctx.fillText((i + 1).toString(), px + 2, py + 12)
}
}
if (targetCell) {
ctx.strokeStyle = C_TARGET
ctx.lineWidth = 3
ctx.strokeRect(targetCell.x * PX_PER_CM, targetCell.y * PX_PER_CM, PX_PER_CM, PX_PER_CM)
}
let vl = 0.0, vr = 0.0
if (!autoMode) {
let speed = 20.0
if (keys.has('w')) { vl = speed; vr = speed }
if (keys.has('s')) { vl = -speed; vr = -speed }
if (keys.has('a')) { vl = -speed/2; vr = speed/2 }
if (keys.has('d')) { vl = speed/2; vr = -speed/2 }
} else {
let lhd = getLookahead()
if (lhd) {
ctx.fillStyle = C_LHD
ctx.beginPath()
ctx.arc(lhd.x * PX_PER_CM, lhd.y * PX_PER_CM, 6, 0, 2 * Math.PI)
ctx.fill()
let d = Math.hypot(lhd.x - rx, lhd.y - ry)
if (lhd.idx === path.length - 1 && d <= 1.0) {
autoMode = false
vl = 0.0; vr = 0.0
} else {
let aReq = Math.atan2(lhd.y - ry, lhd.x - rx)
let eTh = (aReq - rTheta + Math.PI) % (2 * Math.PI) - Math.PI
if (eTh < -Math.PI) eTh += 2 * Math.PI
let vReq = Math.min(15.0, d * 2.0)
if (Math.abs(eTh) > 0.5) vReq *= 0.2
let wReq = eTh * 4.0
let r = WHEEL_RADIUS_MM / 10.0
let L = WHEEL_BASE_MM / 10.0
vr = (2 * vReq + wReq * L) / (2 * r)
vl = (2 * vReq - wReq * L) / (2 * r)
let scale = Math.max(Math.abs(vr), Math.abs(vl)) / 30.0
if (scale > 1.0) {
vr /= scale
vl /= scale
}
}
}
}
applyKinematics(vl, vr, 0.02)
ctx.save()
ctx.translate(rx * PX_PER_CM, ry * PX_PER_CM)
ctx.rotate(rTheta)
let robPx = ROBOT_SIZE_CM * PX_PER_CM
// Couleur du robot bien contrastée sur fond clair
ctx.fillStyle = "#334155"
ctx.beginPath()
ctx.arc(0, 0, robPx / 2, 0, 2 * Math.PI)
ctx.fill()
ctx.strokeStyle = "#ef4444" // Capteur frontal rouge
ctx.lineWidth = 3
ctx.beginPath()
ctx.moveTo(0, 0)
ctx.lineTo(robPx / 2, 0)
ctx.stroke()
ctx.restore()
}
setInterval(render, 20)
</script>
</body>
</html>