Files
RoboMovel/slides/progresso-2026-11-05/circuito/circuito.js
T
mguinhosandClaude Opus 5.5 68c7dab989 Projeto: slides de progresso de 05/11 e animacao do circuito
Deck curto com o teste dos motores na bateria, a troca pelo UNO com Wi-Fi,
a animacao SVG do circuito, a base 35x35 e o que falta.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 09:07:20 -03:00

334 lines
22 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* Animação do circuito em SVG. initCircuito(svg, raiz) desenha no <svg> dado e devolve render(t).
raiz é o caminho até a pasta do deck. Usado pelo slide 4 e por render_video.py. */
function initCircuito(c, raiz = "") {
const NS = "http://www.w3.org/2000/svg";
function el(n, a = {}, p = c, txt) { const e = document.createElementNS(NS, n); for (const k in a) e.setAttribute(k, a[k]); if (txt !== undefined) e.textContent = txt; p.appendChild(e); return e; }
const C = { cream:"#EFE9D9", cream2:"#E4DCC4", ink:"#0F0F0F", green:"#1F8A4C", pink:"#F06CA8", orange:"#E85A1F", yellow:"#F5C518", off:"#BDB5A0" };
const texto = (x, y, s, cls, size, a = {}, p = c) => el("text", { x, y, class: "cir-" + cls, "font-size": size, ...a }, p, s);
const img = (nome, x, y, w, h, p = c) => el("image", { href: `${raiz}circuito/svg/${nome}.svg`, x, y, width: w, height: h }, p);
const pts = l => l.map(q => q.join(",")).join(" ");
function rota(lista, raio = 6) {
const l = lista.filter((q, i) => i === 0 || q[0] !== lista[i-1][0] || q[1] !== lista[i-1][1]);
let d = `M${l[0][0]},${l[0][1]}`;
for (let i = 1; i < l.length - 1; i++) {
const [a, b, e] = [l[i-1], l[i], l[i+1]];
const da = Math.hypot(a[0]-b[0], a[1]-b[1]), de = Math.hypot(e[0]-b[0], e[1]-b[1]);
const r = Math.min(raio, da / 2, de / 2);
const p = [b[0] + (a[0]-b[0]) * r / da, b[1] + (a[1]-b[1]) * r / da];
const q = [b[0] + (e[0]-b[0]) * r / de, b[1] + (e[1]-b[1]) * r / de];
d += ` L${p[0]},${p[1]} Q${b[0]},${b[1]} ${q[0]},${q[1]}`;
}
const f = l[l.length - 1];
return d + ` L${f[0]},${f[1]}`;
}
/* ---------- física: mesma lógica de updateTargets() e ramp() do base_movel.ino ---------- */
const STEPS_MM = 3200 / (2 * Math.PI * 30), ACCEL = 6000, DT = 0.01;
const K = 110 + 160; // 35×35: meio entre-eixos 110 mm + meia bitola ~160 mm (montagem_base_35x35.py)
const INVERT = { FL:false, FR:true, RL:false, RR:true };
const RODAS = ["FL","FR","RL","RR"];
const CMD = [ { t:12.5, v:[300,0,0], txt:"V 300 0 0" }, { t:18.5, v:[0,300,0], txt:"V 0 300 0" }, { t:24.5, v:[0,0,30], txt:"V 0 0 30" }, { t:30.5, v:[0,0,0], txt:"S" } ];
const FIM = 36, N = Math.ceil(FIM / DT) + 2;
function alvo(t) { let v = [0,0,0]; for (const m of CMD) if (t >= m.t) v = m.v; return v; }
const hist = (() => {
const h = [], cur = [0,0,0,0], fase = [0,0,0,0]; let x = 0, y = 0, a = 0;
for (let i = 0; i < N; i++) {
const t = i * DT, [vx, vy, wd] = alvo(t), wz = wd * Math.PI / 180;
const alv = [vx - vy - K*wz, vx + vy + K*wz, vx + vy - K*wz, vx - vy + K*wz].map(m => m * STEPS_MM);
for (let j = 0; j < 4; j++) {
let d = alv[j] - cur[j], mx = ACCEL * DT;
d = Math.max(-mx, Math.min(mx, d));
if (cur[j] !== 0 && (cur[j] + d) * cur[j] < 0) cur[j] = 0; else cur[j] += d;
fase[j] += cur[j] * DT;
}
const m = cur.map(s => s / STEPS_MM);
const bx = (m[0]+m[1]+m[2]+m[3]) / 4, by = (-m[0]+m[1]+m[2]-m[3]) / 4, bw = (-m[0]+m[1]-m[2]+m[3]) / (4*K);
x += (bx*Math.cos(a) - by*Math.sin(a)) * DT; y += (bx*Math.sin(a) + by*Math.cos(a)) * DT; a += bw * DT;
h.push({ s:[...cur], f:[...fase], x, y, a });
}
return h;
})();
const H = t => hist[Math.max(0, Math.min(N - 1, Math.round(t / DT)))];
/* ---------- geometria ---------- */
const SH = { x:770, y:475, w:482, h:375 }; // CNC Shield
const sh = (u, v) => [SH.x + u*SH.w, SH.y + v*SH.h];
const MOT = [
{ eixo:"X", roda:"RR", i:3, nome:"Traseiro dir.", cx:940, trilha:402, col:1014, direita:true },
{ eixo:"Z", roda:"RL", i:2, nome:"Traseiro esq.", cx:1190, trilha:402, col:1031, desvio:1014 },
{ eixo:"Y", roda:"FR", i:1, nome:"Dianteiro dir.", cx:1440, trilha:418, col:1162 },
{ eixo:"A", roda:"FL", i:0, nome:"Dianteiro esq.", cx:1690, trilha:436, col:1180, desvio:1163 },
];
const FIOS = ["#111111","#1F8A4C","#D7301F","#2F6FD0"]; // cores do cabo do motor, como no Fritzing
/* ---------- roda mecanum Ø60, mesma perspectiva dos motores do Fritzing (eixo vertical, achatamento 0,5) ---------- */
function rodaMecanum(p, cx, yt, phi, mao) {
const KY = 0.5, H = 52, LOBOS = 10, ROLOS = 10;
const contorno = (y, r0, a) => {
const l = [];
for (let i = 0; i < 160; i++) { const t = i / 160 * 2 * Math.PI, r = r0 + a * Math.cos(LOBOS * t); l.push([(cx + r * Math.cos(t + phi)).toFixed(1), (y + KY * r * Math.sin(t + phi)).toFixed(1)]); }
return pts(l);
};
const flange = (y, cor) => el("polygon", { points: contorno(y, 76, 8), fill: cor, stroke: "#0F0F0F", "stroke-width": 2, "stroke-linejoin": "round" }, p);
flange(yt + H, "#C99B00");
const rolos = [];
for (let i = 0; i < ROLOS; i++) rolos.push(phi + (i + .5) * 2 * Math.PI / ROLOS);
rolos.sort((a, b) => Math.sin(a) - Math.sin(b));
for (const t of rolos) {
const d = 0.3 * mao, r = 76;
const a = [cx + r * Math.cos(t - d), yt + 8 + KY * r * Math.sin(t - d)], b = [cx + r * Math.cos(t + d), yt + H - 14 + KY * r * Math.sin(t + d)];
el("line", { x1: a[0], y1: a[1], x2: b[0], y2: b[1], stroke: "#0F0F0F", "stroke-width": 25, "stroke-linecap": "round" }, p);
el("line", { x1: a[0], y1: a[1], x2: b[0], y2: b[1], stroke: "#242424", "stroke-width": 20, "stroke-linecap": "round" }, p);
el("line", { x1: a[0] - 4, y1: a[1] - 2, x2: b[0] - 4, y2: b[1] - 2, stroke: "#565656", "stroke-width": 4, "stroke-linecap": "round" }, p);
}
for (let e = 7; e >= 1; e--) el("polygon", { points: contorno(yt + e, 76, 8), fill: "#C99B00" }, p);
flange(yt, "#F5C518");
el("ellipse", { cx, cy: yt, rx: 54, ry: 27, fill: "none", stroke: "#D9A900", "stroke-width": 2 }, p);
el("ellipse", { cx, cy: yt + 2, rx: 33, ry: 16.5, fill: "#0F0F0F" }, p);
el("ellipse", { cx, cy: yt, rx: 33, ry: 16.5, fill: "#242424", stroke: "#0F0F0F", "stroke-width": 2 }, p);
el("ellipse", { cx, cy: yt, rx: 11, ry: 5.5, fill: "#0F0F0F" }, p);
for (let i = 0; i < 6; i++) { const t = phi + i * Math.PI / 3; el("ellipse", { cx: cx + 22 * Math.cos(t), cy: yt + 11 * Math.sin(t), rx: 3.4, ry: 1.9, fill: i ? "#B8B8B8" : "#F5C518" }, p); }
}
/* ---------- estático ---------- */
c.innerHTML = "";
el("rect", { width:1920, height:1080, fill:C.cream });
const camFios = el("g"), camComp = el("g"), camInfo = el("g");
// fios: base apagada + camada viva com pontos correndo
const redes = {};
function fio(nome, lista, cor, larg = 6) {
redes[nome] = redes[nome] || [];
const b = el("path", { d: rota(lista), fill:"none", stroke:C.off, "stroke-width":larg, "stroke-linejoin":"round" }, camFios);
const f = el("path", { d: rota(lista), fill:"none", stroke:C.cream, "stroke-width":larg-2, "stroke-dasharray":"7 21", "stroke-linecap":"round", opacity:0 }, camFios);
redes[nome].push({ b, f, cor });
}
const BAT = { x:70, y:560, w:200, h:218 };
const PINO = {
batP:[262.6,584.6], batN:[262.6,593], fus1:[302.1,621.5], fus2:[417.9,621.5], chCom:[484.7,649.4], chL2:[503.6,649.4],
lmInP:[320.3,410.4], lmInN:[320.4,517.1], lmOutP:[561.8,410.5], lmOutN:[561.8,517.5],
shP:[825.4,748], shN:[790.2,748], sh5V:[847,635.2], shGND:[865.9,634.5], shA0:[1218.3,752.4], shA1:[1218.3,770.3],
esp5V:[1601.3,495.9], espGND:[1601.3,510.2], espU0R:[1728.7,567.2], espU0T:[1728.7,581.4], espGND2:[1728.7,538.7],
};
const R148 = 600, CINCO_X = 700, TERRA_X = 690;
fio("p148", [PINO.batP,[300,PINO.batP[1]],[300,PINO.fus1[1]],PINO.fus1], C.orange);
fio("p148", [PINO.fus2,[440,PINO.fus2[1]],[440,664],[PINO.chCom[0],664],PINO.chCom], C.orange);
fio("p148", [PINO.chL2,[PINO.chL2[0],664],[540,664],[540,622],[R148,622]], C.orange);
fio("p148", [[R148,622],[R148,722],[PINO.shP[0],722],PINO.shP], C.orange);
fio("p148", [[R148,622],[R148,548],[286,548],[286,PINO.lmInP[1]],PINO.lmInP], C.orange);
fio("p5", [PINO.lmOutP,[CINCO_X,PINO.lmOutP[1]],[CINCO_X,PINO.sh5V[1]],PINO.sh5V], C.pink);
fio("p5", [[CINCO_X,464],[1566,464],[1566,PINO.esp5V[1]],PINO.esp5V], C.pink);
fio("tx", [PINO.shA0,[1760,PINO.shA0[1]],[1760,PINO.espU0R[1]],PINO.espU0R], C.green, 5);
fio("tx", [[1760,650],[1840,650]], C.green, 5);
fio("rx", [PINO.shA1,[1745,PINO.shA1[1]],[1745,PINO.espU0T[1]],PINO.espU0T], C.green, 5);
fio("wifi", [[1590,640],[1566,640],[1566,682],[1540,682]], C.ink, 5);
function terra(lista) { el("path", { d: rota(lista), fill:"none", stroke:C.ink, "stroke-width":4, "stroke-linejoin":"round" }, camFios); }
function no(x, y, cor) { el("circle", { cx:x, cy:y, r:6, fill:cor, stroke:C.ink, "stroke-width":1.5 }, camFios); }
terra([PINO.batN,[282,PINO.batN[1]],[282,700],[586,700],[586,790],[PINO.shN[0],790],PINO.shN]);
terra([[586,700],[586,535],[300,535],[300,PINO.lmInN[1]],PINO.lmInN]);
terra([PINO.lmOutN,[TERRA_X,PINO.lmOutN[1]],[TERRA_X,650],[PINO.shGND[0],650],PINO.shGND]);
terra([[TERRA_X,PINO.lmOutN[1]],[TERRA_X,472],[1558,472],[1558,PINO.espGND[1]],PINO.espGND]);
terra([[1840,650],[1840,PINO.espGND2[1]],PINO.espGND2]);
no(586,700,C.ink); no(TERRA_X,PINO.lmOutN[1],C.ink); no(R148,622,C.orange); no(CINCO_X,464,C.pink); no(1760,650,C.green);
const PINOS_MOTOR = { X:[[1003.2,551.4],[1003.2,569.3],[1003.2,587.1],[1003.2,605]], Z:[[1003.9,722.9],[1003.9,740.8],[1003.9,758.6],[1003.9,776.5]],
Y:[[1151.4,552.1],[1151.4,570],[1151.4,587.9],[1151.4,605.7]], A:[[1152.9,722.2],[1152.9,740.1],[1152.9,757.9],[1152.9,775.8]] };
for (const m of MOT) {
m.fios = [];
FIOS.forEach((cor, k) => {
const d = (k - 1.5) * 1.6 * 4.5, pino = PINOS_MOTOR[m.eixo][k], col = m.col + k * 4.5;
const y = m.direita ? m.trilha + (3 - k) * 4.5 : m.trilha + k * 4.5;
const l = [[m.cx + d, 396],[m.cx + d, y]];
if (m.desvio) { const yd = 655 + k * 4.5, c2 = m.desvio + k * 4.5; l.push([col, y],[col, yd],[c2, yd],[c2, pino[1]],pino); }
else l.push([col, y],[col, pino[1]],pino);
el("path", { d:rota(l, 4), fill:"none", stroke:cor, "stroke-width":3.5, "stroke-linejoin":"round" }, camFios);
m.fios.push(el("path", { d:rota(l, 4), fill:"none", stroke:C.cream, "stroke-width":2.4, "stroke-dasharray":"3 27", "stroke-linecap":"round", opacity:0 }, camFios));
});
}
c.insertBefore(camComp, camFios);
// componentes
img("bateria", BAT.x, BAT.y, BAT.w, BAT.h, camComp);
el("rect", { x:112, y:622, width:122, height:128, fill:C.cream2, stroke:C.ink, "stroke-width":3 }, camComp);
texto(173, 664, "4S", "disp", 36, { "text-anchor":"middle" }, camComp);
texto(173, 696, "14,8 V", "mono", 19, { "text-anchor":"middle" }, camComp);
texto(173, 724, "2600 mAh", "mono", 15, { "text-anchor":"middle" }, camComp);
img("fusivel", 300, 598, 120, 47, camComp);
texto(360, 672, "Fusível 5 A", "mono", 17, { "text-anchor":"middle" }, camComp);
const chave = el("g", {}, camComp); img("chave", 452, 587, 70, 71, chave);
texto(487, 682, "Chave", "mono", 17, { "text-anchor":"middle" }, camComp);
img("lm2596", 312, 402, 258, 124, camComp);
texto(441, 392, "LM2596 · 14,8 V → 5,0 V", "mono", 17, { "text-anchor":"middle" }, camComp);
img("cnc", SH.x, SH.y, SH.w, SH.h, camComp);
img("a4988", ...sh(.532,.545), SH.w*.218, SH.h*.372, camComp); // driver do eixo A no soquete vazio
const glow = {};
for (const [e, u, v] of [["X",.215,.09],["Y",.53,.075],["Z",.215,.545],["A",.532,.545]])
glow[e] = el("rect", { x:sh(u,v)[0]-3, y:sh(u,v)[1]-3, width:SH.w*.218+6, height:SH.h*.372+6, fill:"none", stroke:C.yellow, "stroke-width":5, opacity:0 }, camComp);
const fotoU = el("foreignObject", { x:1290, y:475, width:250, height:150 }, camComp);
fotoU.innerHTML = `<img xmlns="http://www.w3.org/1999/xhtml" src="${raiz}img/placa_uno_wifi.jpg" style="width:250px;height:150px;object-fit:cover;object-position:50% 45%;display:block">`;
el("rect", { x:1290, y:475, width:250, height:150, fill:"none", stroke:C.ink, "stroke-width":4 }, camComp);
el("rect", { x:1290, y:591, width:250, height:34, fill:C.ink }, camComp);
el("text", { x:1415, y:614, class:"cir-mono", "font-size":15, "text-anchor":"middle", style:"fill:#EFE9D9" }, camComp, "UNO Wi-Fi sob a shield");
el("line", { x1:SH.x+SH.w, y1:550, x2:1290, y2:550, stroke:C.ink, "stroke-width":10 }, camComp);
const esp = img("esp32cam", 1590, 468, 150, 226, camComp);
const flash = el("circle", { cx:1665, cy:523, r:22, fill:C.yellow, opacity:0 }, camComp);
const srvBox = el("rect", { x:1290, y:640, width:250, height:92, fill:C.cream2, stroke:C.ink, "stroke-width":4 }, camComp);
const srvT = texto(1415, 672, "Servidor (PC)", "disp", 21, { "text-anchor":"middle" }, camComp);
const srvS = texto(1415, 698, "Depth Anything · V-SLAM", "mono", 14, { "text-anchor":"middle" }, camComp);
texto(1415, 718, "S2E · ViNT", "mono", 14, { "text-anchor":"middle" }, camComp);
texto(1270, sh(.97,.79)[1] + 18, "Serial 19200 · divisor 1k/2k (5 V → 3,3 V)", "mono", 14, {}, camComp);
img("resistor", 1490, PINO.shA0[1] - 8, 72, 16, camInfo);
img("resistor", 1766, 642, 68, 16, camInfo);
texto(1570, PINO.shA0[1] - 6, "1 kΩ", "mono", 14, {}, camInfo);
texto(1800, 636, "2 kΩ", "mono", 14, { "text-anchor":"middle" }, camInfo);
texto(1544, 632, "Wi-Fi", "mono", 13, { }, camInfo);
texto(614, 590, "14,8 V", "mono", 17, {}, camInfo);
texto(714, 500, "5 V", "mono", 17, {}, camInfo);
// motores e rodas
const clipId = i => `clipRoda${i}`;
for (const m of MOT) {
img("motor", m.cx - 61, 200, 122, 200, camComp);
// roletes "D" em FL e RR, "E" em FR e RL (WHEEL_HAND da montagem)
m.g = el("g", {}, camComp);
rodaMecanum(m.g, m.cx, 163, 0, (m.roda === "FL" || m.roda === "RR") ? 1 : -1);
texto(m.cx + 72, 262, m.eixo, "disp", 38, {}, camInfo);
texto(m.cx + 72, 282, m.nome.split(" ")[0], "mono", 13, {}, camInfo);
texto(m.cx + 72, 298, m.nome.split(" ")[1], "mono", 13, {}, camInfo);
m.tDir = texto(m.cx + 72, 326, "DIR —", "mono", 15, {}, camInfo);
m.tRate = texto(m.cx + 72, 348, "0 p/s", "mono", 15, {}, camInfo);
m.seta = texto(m.cx + 72, 382, "", "disp", 30, {}, camInfo);
}
// moldura (tema creative-mode)
texto(64, 72, "Robô Móvel de Carga · Circuito completo", "mono", 24);
const pilula = el("rect", { y:42, height:40, rx:20, fill:"none", stroke:C.ink, "stroke-width":2 });
const tPil = texto(0, 71, "", "mono", 22);
const kick = el("rect", { x:64, y:104, height:36, fill:C.ink });
const tKick = el("text", { x:78, y:130, class:"cir-mono", "font-size":21, style:"fill:#EFE9D9" }, c, "");
const tTit1 = texto(64, 202, "", "disp", 58), tTit2 = texto(64, 262, "", "disp", 58);
const tDesc = texto(64, 318, "", "corpo", 26);
// rodapé
const LEG = [["14,8 V",C.orange],["5 V",C.pink],["GND",C.ink],["Serial",C.green]];
let lx = 64;
for (const [n, cor] of LEG) { el("rect", { x:lx, y:1036, width:34, height:10, fill:cor }); texto(lx+42, 1047, n, "mono", 17); lx += 42 + n.length*12 + 36; }
texto(1856, 1047, "Animação do circuito projetado · não é medição", "mono", 17, { "text-anchor":"end" });
/* osciloscópio STEP */
const OS = { x:64, y:860, w:1206, h:150 };
el("rect", { x:OS.x, y:OS.y, width:OS.w, height:OS.h, fill:C.cream2, stroke:C.ink, "stroke-width":4 });
texto(OS.x+14, OS.y+22, "Pulsos stepping dos motores", "mono", 15);
for (const [k, m] of MOT.entries()) {
texto(OS.x+14, OS.y + 48 + k*28, m.eixo, "disp", 18);
m.tr = el("polyline", { fill:"none", stroke:C.ink, "stroke-width":2.5, "stroke-linejoin":"round" });
}
/* mapa visto de cima */
const MP = { x:1290, y:800, w:566, h:210 };
el("rect", { x:MP.x, y:MP.y, width:MP.w, height:MP.h, fill:"#fff" === 0 ? "" : C.cream2, stroke:C.ink, "stroke-width":4 });
for (let i = 1; i < 11; i++) el("line", { x1:MP.x + i*51.4, y1:MP.y+4, x2:MP.x + i*51.4, y2:MP.y+MP.h-4, stroke:C.off, "stroke-width":1 });
for (let i = 1; i < 4; i++) el("line", { x1:MP.x+4, y1:MP.y + i*52, x2:MP.x+MP.w-4, y2:MP.y + i*52, stroke:C.off, "stroke-width":1 });
texto(MP.x+14, MP.y+24, "Vista de cima", "mono", 15);
const defsMapa = el("defs"); el("clipPath", { id:"clipMapa" }, defsMapa).appendChild(Object.assign(document.createElementNS(NS,"rect"), {}));
defsMapa.querySelector("rect").setAttribute("x", MP.x+2); defsMapa.querySelector("rect").setAttribute("y", MP.y+2);
defsMapa.querySelector("rect").setAttribute("width", MP.w-4); defsMapa.querySelector("rect").setAttribute("height", MP.h-4);
const gm = el("g", { "clip-path":"url(#clipMapa)" });
const trilha = el("polyline", { fill:"none", stroke:C.orange, "stroke-width":4, "stroke-dasharray":"8 6" }, gm);
const robo = el("g", {}, gm);
// frente do robô em +x do mapa; A = dianteiro-esq., Y = dianteiro-dir., Z = traseiro-esq., X = traseiro-dir.
// frente em +x; A = dianteiro-esq., Y = dianteiro-dir., Z = traseiro-esq., X = traseiro-dir.
const POS_RODA = { A:[15,-29], Y:[15,29], Z:[-15,-29], X:[-15,29] };
for (const m of MOT) {
const [wx, wy] = POS_RODA[m.eixo], g = el("g", { transform:`translate(${wx},${wy})` }, robo);
const cl = el("clipPath", { id:`clipMini${m.i}` }, defsMapa);
el("rect", { x:-10, y:-4.5, width:20, height:9, rx:2 }, cl);
el("rect", { x:-10, y:-4.5, width:20, height:9, rx:2, fill:C.yellow }, g);
// roletes de cima: A e X em "\\", Y e Z em "/" (mesma lateralidade das rodas grandes)
const inc = (m.roda === "FL" || m.roda === "RR") ? 1 : -1, clip = el("g", { "clip-path":`url(#clipMini${m.i})` }, g);
m.miniRolos = el("g", {}, clip);
for (let k = -5; k <= 5; k++) el("line", { x1:k*6 - inc*3.5, y1:-5.5, x2:k*6 + inc*3.5, y2:5.5, stroke:"#242424", "stroke-width":2.5 }, m.miniRolos);
m.miniBorda = el("rect", { x:-10, y:-4.5, width:20, height:9, rx:2, fill:"none", stroke:C.ink, "stroke-width":2 }, g);
}
el("rect", { x:-26, y:-25, width:52, height:50, rx:5, fill:"#D9B27C", stroke:C.ink, "stroke-width":3 }, robo);
el("rect", { x:-19, y:-18, width:38, height:36, rx:2, fill:"#C99C63" }, robo);
el("path", { d:"M9,0 L-2,-6 Q-4,-7 -4,-5 L-4,5 Q-4,7 -2,6 Z", fill:C.ink, stroke:C.ink, "stroke-width":2, "stroke-linejoin":"round" }, robo);
for (const m of MOT) {
const [wx, wy] = POS_RODA[m.eixo];
m.miniTxt = el("text", { x:wx * .95, y:Math.sign(wy) * 13, class:"cir-disp", "font-size":10, "text-anchor":"middle", "dominant-baseline":"middle" }, robo, m.eixo);
}
el("rect", { x:MP.x+392, y:MP.y+14, width:18, height:12, rx:2, fill:C.yellow, stroke:C.green, "stroke-width":3 });
texto(MP.x+416, MP.y+25, "Frente", "mono", 13);
el("rect", { x:MP.x+484, y:MP.y+14, width:18, height:12, rx:2, fill:C.yellow, stroke:C.orange, "stroke-width":3 });
texto(MP.x+508, MP.y+25, "Trás", "mono", 13);
const ESC = 0.05, MX0 = MP.x + 170, MY0 = MP.y + 150; // 0,072 px/mm
const ptMapa = (h) => [MX0 + h.x*ESC, MY0 - h.y*ESC];
/* ---------- roteiro ---------- */
const ETAPAS = [
[0, "Antes de ligar", "Circuito completo", "desligado", "Bateria, fonte de 5 V, placas, 4 drivers e 4 motores.", "Desligado"],
[2.5, "1 · Potência", "14,8 V saindo", "da bateria", "Passa pelo fusível e pela chave e chega à shield e ao LM2596.", "14,8 V"],
[5, "2 · Lógica", "LM2596 gera 5 V", "para as placas", "Alimenta o Arduino UNO com Wi-Fi e a ESP32-CAM.", "5 V"],
[8, "3 · Visão", "Câmera manda", "para o servidor", "Os quadros vão por Wi-Fi. O servidor decide o movimento.", "Câmera"],
[11, "4 · Comando", "Servidor responde", "pela serial", "A ESP32-CAM repassa o comando de velocidade ao Arduino.", "Serial"],
[12.5,"5 · Frente", "Robô anda", "para a frente", "As quatro rodas giram no mesmo sentido.", "V 300 0 0"],
[18.5,"6 · Lateral", "Anda de lado", "sem virar", "As rodas da diagonal giram juntas: é o efeito mecanum.", "V 0 300 0"],
[24.5,"7 · Giro", "Gira no lugar", "esquerda × direita", "O lado esquerdo vai para trás e o direito para a frente.", "V 0 0 30"],
[30.5,"8 · Parada", "Para com rampa", "até zerar", "Sem comando por 500 ms, o firmware para sozinho.", "S"],
];
function render(t) {
const on148 = t >= 2.5, on5 = t >= 5, cam = t >= 8, cmd = t >= 11;
const e = [...ETAPAS].reverse().find(x => t >= x[0]);
tKick.textContent = e[1]; const kw = tKick.getComputedTextLength() + 28; kick.setAttribute("width", kw);
tTit1.textContent = e[2]; tTit2.textContent = e[3]; tDesc.textContent = e[4];
tPil.textContent = e[5]; const pw = tPil.getComputedTextLength() + 32;
pilula.setAttribute("x", 1856 - pw); pilula.setAttribute("width", pw); tPil.setAttribute("x", 1856 - pw + 16);
chave.setAttribute("transform", on148 ? "" : "translate(974,0) scale(-1,1)");
const estado = { p148:[on148,C.orange,-t*70], p5:[on5,C.pink,-t*70], wifi:[cam,C.ink, (cmd && t < 12.5 ? t : -t)*60],
tx:[cmd,C.green,-t*60], rx:[cmd,C.green,t*60] };
for (const n in redes) for (const r of redes[n]) {
const [liga, cor, of] = estado[n];
r.b.setAttribute("stroke", liga ? cor : C.off); r.f.setAttribute("opacity", liga ? 1 : 0); r.f.setAttribute("stroke-dashoffset", of);
}
const h = H(t);
// flash da câmera a cada captura; servidor destacado enquanto processa
flash.setAttribute("opacity", cam && (t % 1) < .12 ? .75 : 0);
srvBox.setAttribute("fill", cam ? C.yellow : C.cream2);
for (const m of MOT) {
const s = h.s[m.i], gira = Math.abs(s) > 1;
m.fios.forEach(f => { f.setAttribute("opacity", gira ? 1 : 0); f.setAttribute("stroke-dashoffset", -h.f[m.i] / 90); });
const fis = INVERT[m.roda] ? -s : s;
m.tDir.textContent = on5 ? `DIR ${fis >= 0 ? "HIGH" : "LOW"}` : "DIR —";
m.tRate.textContent = `${Math.round(Math.abs(s))} p/s`;
m.seta.textContent = !gira ? "" : (s > 0 ? "▲ frente" : "▼ trás");
m.seta.setAttribute("font-size", 17);
// gira no sentido físico do motor (o DIR já inclui INVERT); meia velocidade para não embaralhar os lobos no vídeo
m.miniRolos.setAttribute("transform", `translate(${(h.f[m.i] / 3200 * 30) % 6},0)`);
m.miniBorda.setAttribute("stroke", !gira ? C.ink : s > 0 ? C.green : C.orange);
m.miniBorda.setAttribute("stroke-width", gira ? 2.5 : 2);
m.miniTxt.setAttribute("transform", `rotate(${h.a * 180 / Math.PI} ${m.miniTxt.getAttribute("x")} ${m.miniTxt.getAttribute("y")})`);
const voltas = (INVERT[m.roda] ? -1 : 1) * h.f[m.i] / 3200 * 0.5;
m.g.replaceChildren();
rodaMecanum(m.g, m.cx, 163, voltas * 2 * Math.PI, (m.roda === "FL" || m.roda === "RR") ? 1 : -1);
glow[m.eixo].setAttribute("opacity", on5 ? (gira ? 1 : .55) : 0);
// STEP: últimos 2 s, frequência visual proporcional à real
const y0 = OS.y + 52 + MOT.indexOf(m)*28, l = [];
for (let j = 0; j <= 220; j++) {
const tt = t - (220 - j) * 2 / 220, hh = H(Math.max(0, tt));
const nivel = (Math.abs(hh.f[m.i]) / 850) % 1 < .5 && Math.abs(hh.s[m.i]) > 1 ? 1 : 0;
l.push([OS.x + 46 + j * (OS.w - 66) / 220, y0 - nivel*16]);
}
m.tr.setAttribute("points", pts(l));
}
// mapa
const [mx, my] = ptMapa(h);
robo.setAttribute("transform", `translate(${mx},${my}) rotate(${-h.a*180/Math.PI}) scale(1.3)`);
const tr = []; for (let k = 0; k <= t; k += .1) tr.push(ptMapa(H(k)));
trilha.setAttribute("points", pts(tr));
}
render(0);
return render;
}