- Painel com os componentes do Fritzing em nos de interface, que responde ao robo simulado. - Camera em volta do robo com o mouse e zoom pela roda. - Rodas 3D mantem a rotacao de repouso dos ossos e giram tambem no giro em torno do proprio eixo.
223 lines
11 KiB
Python
223 lines
11 KiB
Python
#!/usr/bin/env python3
|
|
"""Gera cenas/painel_circuito.tscn: o circuito do robô feito de nós de interface.
|
|
|
|
python3 tools/gerar_painel_circuito.py
|
|
|
|
Ligações: documentacao/ESPECIFICACOES.md, seção 4. Coordenadas dos pinos: arquivos de peça do Fritzing.
|
|
O comportamento fica em scripts/circuito/; aqui só se monta a árvore de nós, em uma tela de 1920x1080.
|
|
"""
|
|
import math
|
|
import pathlib
|
|
|
|
RAIZ = pathlib.Path(__file__).resolve().parent.parent
|
|
CREME, CREME2, TINTA = "0.937,0.914,0.851,1", "0.894,0.863,0.769,1", "0.059,0.059,0.059,1"
|
|
LARANJA, ROSA, VERDE = (0.91, 0.353, 0.122), (0.941, 0.424, 0.659), (0.122, 0.541, 0.298)
|
|
PRETO = (0.059, 0.059, 0.059)
|
|
|
|
recursos, subs, nos = [], [], []
|
|
|
|
|
|
def ext(tipo, caminho):
|
|
rid = f"r{len(recursos)}"
|
|
recursos.append(f'[ext_resource type="{tipo}" path="{caminho}" id="{rid}"]')
|
|
return f'ExtResource("{rid}")'
|
|
|
|
|
|
FONTES = {k: ext("FontFile", f"res://interface/circuito/fontes/{v}.ttf")
|
|
for k, v in {"disp": "ArchivoBlack", "mono": "JetBrainsMono", "corpo": "SpaceGrotesk"}.items()}
|
|
SHADER = ext("Shader", "res://interface/circuito/fio.gdshader")
|
|
_estilos = {}
|
|
|
|
|
|
def sub(tipo, *linhas):
|
|
sid = f"s{len(subs)}"
|
|
subs.append(f'[sub_resource type="{tipo}" id="{sid}"]\n' + "\n".join(linhas))
|
|
return f'SubResource("{sid}")'
|
|
|
|
|
|
def estilo(fonte, tam, cor=TINTA):
|
|
k = (fonte, tam, cor)
|
|
if k not in _estilos:
|
|
_estilos[k] = sub("LabelSettings", f"font = {FONTES[fonte]}", f"font_size = {tam}", f"font_color = Color({cor})",
|
|
"outline_size = 10", "outline_color = Color(0.937,0.914,0.851,1)")
|
|
return _estilos[k]
|
|
|
|
|
|
def no(nome, tipo, pai=".", **props):
|
|
corpo = "\n".join(f"{k.replace('__', '/')} = {v}" for k, v in props.items())
|
|
nos.append(f'[node name="{nome}" type="{tipo}" parent="{pai}"]\n{corpo}')
|
|
|
|
|
|
def caixa(x, y, w, h):
|
|
return dict(offset_left=x, offset_top=y, offset_right=x + w, offset_bottom=y + h, mouse_filter=2)
|
|
|
|
|
|
def ret(nome, x, y, w, h, cor, pai="."):
|
|
no(nome, "ColorRect", pai, **caixa(x, y, w, h), color=f"Color({cor})")
|
|
|
|
|
|
def img(nome, arq, x, y, w, h, pai="Componentes"):
|
|
no(nome, "TextureRect", pai, **caixa(x, y, w, h), expand_mode=1, stretch_mode=0,
|
|
texture=ext("Texture2D", f"res://interface/circuito/{arq}"))
|
|
|
|
|
|
def texto(nome, s, x, y, fonte, tam, pai="Info", w=500, centro=False, cor=TINTA):
|
|
if fonte != "corpo":
|
|
s = s.upper()
|
|
h = int(tam * 1.4)
|
|
if centro:
|
|
x -= w / 2
|
|
no(nome, "Label", pai, **caixa(x, y - tam, w, h), text=f'"{s}"', label_settings=estilo(fonte, tam, cor),
|
|
horizontal_alignment=1 if centro else 0)
|
|
|
|
|
|
def rota(lista, raio=6.0):
|
|
"""Polilinha com os cantos levemente arredondados."""
|
|
l = [q for i, q in enumerate(lista) if i == 0 or q != lista[i - 1]]
|
|
out = [l[0]]
|
|
for i in range(1, len(l) - 1):
|
|
a, b, e = l[i - 1], l[i], l[i + 1]
|
|
da, de = math.dist(a, b), math.dist(e, b)
|
|
r = min(raio, da / 2, de / 2)
|
|
p = (b[0] + (a[0] - b[0]) * r / da, b[1] + (a[1] - b[1]) * r / da)
|
|
q = (b[0] + (e[0] - b[0]) * r / de, b[1] + (e[1] - b[1]) * r / de)
|
|
for k in range(5):
|
|
t = k / 4
|
|
out.append(((1 - t) ** 2 * p[0] + 2 * t * (1 - t) * b[0] + t * t * q[0],
|
|
(1 - t) ** 2 * p[1] + 2 * t * (1 - t) * b[1] + t * t * q[1]))
|
|
out.append(l[-1])
|
|
return out
|
|
|
|
|
|
_materiais, _contador = {}, {}
|
|
|
|
|
|
def material(rede, cor, periodo):
|
|
if rede not in _materiais:
|
|
c = ", ".join(f"{v:.3f}" for v in cor)
|
|
_materiais[rede] = sub("ShaderMaterial", f"shader = {SHADER}", f"shader_parameter/cor = Vector3({c})",
|
|
f"shader_parameter/periodo = {periodo:.2f}")
|
|
return _materiais[rede]
|
|
|
|
|
|
def fio(rede, lista, cor, larg=6, raio=6.0):
|
|
_contador[rede] = _contador.get(rede, 0) + 1
|
|
pts = ", ".join(f"{x:.1f}, {y:.1f}" for x, y in rota(lista, raio))
|
|
no(f"{rede}_{_contador[rede]}", "Line2D", "Fios", points=f"PackedVector2Array({pts})", width=larg,
|
|
default_color=f"Color({','.join(map(str, cor))},1)", joint_mode=2, begin_cap_mode=2, end_cap_mode=2,
|
|
texture_mode=1, material=material(rede, cor, 28 / larg), metadata__rede=f'"{rede}"')
|
|
|
|
|
|
def terra(lista):
|
|
_contador["gnd"] = _contador.get("gnd", 0) + 1
|
|
pts = ", ".join(f"{x:.1f}, {y:.1f}" for x, y in rota(lista))
|
|
no(f"gnd_{_contador['gnd']}", "Line2D", "Fios", points=f"PackedVector2Array({pts})", width=4,
|
|
default_color=f"Color({TINTA})", joint_mode=2, begin_cap_mode=2, end_cap_mode=2)
|
|
|
|
|
|
def ponto(nome, x, y, cor, pai="Fios"):
|
|
st = sub("StyleBoxFlat", f"bg_color = Color({cor})", "corner_radius_top_left = 7", "corner_radius_top_right = 7",
|
|
"corner_radius_bottom_right = 7", "corner_radius_bottom_left = 7", f"border_color = Color({TINTA})",
|
|
"border_width_left = 2", "border_width_top = 2", "border_width_right = 2", "border_width_bottom = 2")
|
|
no(nome, "Panel", pai, **caixa(x - 6, y - 6, 12, 12), theme_override_styles__panel=st)
|
|
|
|
|
|
# ------------------------------------------------------------------ tela
|
|
no("Componentes", "Control", **caixa(0, 0, 1920, 1080))
|
|
no("Fios", "Node2D")
|
|
no("Info", "Control", **caixa(0, 0, 1920, 1080))
|
|
|
|
# componentes
|
|
img("Bateria", "bateria.png", 70, 560, 200, 218)
|
|
ret("BateriaEtiqueta", 112, 622, 122, 128, CREME2, "Componentes")
|
|
img("Fusivel", "fusivel.png", 300, 598, 120, 47)
|
|
img("Chave", "chave.png", 452, 587, 70, 71)
|
|
img("LM2596", "lm2596.png", 312, 402, 258, 124)
|
|
img("CNCShield", "cnc.png", 770, 475, 482, 375)
|
|
img("DriverA", "a4988.png", 1026.4, 679.4, 105, 140)
|
|
img("PlacaUnoWiFi", "placa_uno_wifi.jpg", 1290, 475, 250, 150)
|
|
ret("PlacaEncaixe", 1252, 545, 38, 10, TINTA, "Componentes")
|
|
img("ESP32CAM", "esp32cam.png", 1590, 468, 150, 226)
|
|
ret("ServidorBorda", 1290, 640, 250, 92, TINTA, "Componentes")
|
|
ret("Servidor", 1296, 646, 238, 80, CREME2, "Componentes")
|
|
for eixo, cx in [("X", 940), ("Z", 1190), ("Y", 1440), ("A", 1690)]:
|
|
img(f"Motor{eixo}", "motor.png", cx - 61, 200, 122, 200)
|
|
|
|
# fios: cada ponta nasce no pino da peça
|
|
P = dict(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))
|
|
R148, CINCO, GX = 600, 700, 690
|
|
fio("p148", [P["batP"], (300, P["batP"][1]), (300, P["fus1"][1]), P["fus1"]], LARANJA)
|
|
fio("p148", [P["fus2"], (440, P["fus2"][1]), (440, 664), (P["chCom"][0], 664), P["chCom"]], LARANJA)
|
|
fio("p148", [P["chL2"], (P["chL2"][0], 664), (540, 664), (540, 622), (R148, 622)], LARANJA)
|
|
fio("p148", [(R148, 622), (R148, 722), (P["shP"][0], 722), P["shP"]], LARANJA)
|
|
fio("p148", [(R148, 622), (R148, 548), (286, 548), (286, P["lmInP"][1]), P["lmInP"]], LARANJA)
|
|
fio("p5", [P["lmOutP"], (CINCO, P["lmOutP"][1]), (CINCO, P["sh5V"][1]), P["sh5V"]], ROSA)
|
|
fio("p5", [(CINCO, 464), (1566, 464), (1566, P["esp5V"][1]), P["esp5V"]], ROSA)
|
|
fio("tx", [P["shA0"], (1760, P["shA0"][1]), (1760, P["espU0R"][1]), P["espU0R"]], VERDE, 5)
|
|
fio("tx", [(1760, 650), (1840, 650)], VERDE, 5)
|
|
fio("rx", [P["shA1"], (1745, P["shA1"][1]), (1745, P["espU0T"][1]), P["espU0T"]], VERDE, 5)
|
|
fio("wifi", [(1590, 640), (1566, 640), (1566, 682), (1540, 682)], PRETO, 5)
|
|
terra([P["batN"], (282, P["batN"][1]), (282, 700), (586, 700), (586, 790), (P["shN"][0], 790), P["shN"]])
|
|
terra([(586, 700), (586, 535), (300, 535), (300, P["lmInN"][1]), P["lmInN"]])
|
|
terra([P["lmOutN"], (GX, P["lmOutN"][1]), (GX, 650), (P["shGND"][0], 650), P["shGND"]])
|
|
terra([(GX, P["lmOutN"][1]), (GX, 472), (1558, 472), (1558, P["espGND"][1]), P["espGND"]])
|
|
terra([(1840, 650), (1840, P["espGND2"][1]), P["espGND2"]])
|
|
ponto("NoGND1", 586, 700, TINTA)
|
|
ponto("NoGND2", GX, P["lmOutN"][1], TINTA)
|
|
ponto("No148", R148, 622, "0.91,0.353,0.122,1")
|
|
ponto("No5V", CINCO, 464, "0.941,0.424,0.659,1")
|
|
ponto("NoSerial", 1760, 650, "0.122,0.541,0.298,1")
|
|
|
|
# cabos dos motores até os 4 pinos de cada conector
|
|
PINOS = {"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)]}
|
|
MOT = [("X", 940, 402, 1014, True, None), ("Z", 1190, 402, 1031, False, 1014),
|
|
("Y", 1440, 418, 1162, False, None), ("A", 1690, 436, 1180, False, 1163)]
|
|
CORES = [PRETO, VERDE, (0.843, 0.188, 0.122), (0.184, 0.435, 0.816)]
|
|
for eixo, cx, trilha, col, direita, desvio in MOT:
|
|
for k, cor in enumerate(CORES):
|
|
d, pino, cc = (k - 1.5) * 7.2, PINOS[eixo][k], col + k * 4.5
|
|
y = trilha + (3 - k if direita else k) * 4.5
|
|
l = [(cx + d, 396), (cx + d, y), (cc, y)]
|
|
if desvio:
|
|
yd, c2 = 655 + k * 4.5, desvio + k * 4.5
|
|
l += [(cc, yd), (c2, yd), (c2, pino[1]), pino]
|
|
else:
|
|
l += [(cc, pino[1]), pino]
|
|
pts = ", ".join(f"{x:.1f}, {yy:.1f}" for x, yy in rota(l, 4))
|
|
no(f"Cabo{eixo}{k + 1}", "Line2D", "Fios", points=f"PackedVector2Array({pts})", width=3.5,
|
|
default_color=f"Color({','.join(map(str, cor))},1)", joint_mode=2, texture_mode=1,
|
|
material=sub("ShaderMaterial", f"shader = {SHADER}",
|
|
f"shader_parameter/cor = Vector3({cor[0]},{cor[1]},{cor[2]})",
|
|
"shader_parameter/periodo = 8.6"),
|
|
metadata__motor=f'"{eixo}"')
|
|
|
|
# rótulos: só o que se lê no tamanho pequeno
|
|
texto("RotuloBateria", "4S", 173, 700, "disp", 44, w=120, centro=True)
|
|
texto("RotuloServidor", "Servidor", 1415, 702, "disp", 34, w=230, centro=True)
|
|
for eixo, cx, *_ in MOT:
|
|
texto(f"Eixo{eixo}", eixo, cx + 72, 270, "disp", 56, w=70)
|
|
texto(f"Sentido{eixo}", "", cx + 72, 370, "disp", 60, w=70, cor="1,1,1,1")
|
|
img("Resistor1k", "resistor.png", 1490, 744.4, 72, 16, "Info")
|
|
img("Resistor2k", "resistor.png", 1766, 642, 68, 16, "Info")
|
|
|
|
# rodas: desenho próprio
|
|
ROD = ext("Script", "res://scripts/circuito/roda_mecanum.gd")
|
|
for eixo, cx, *_ in MOT:
|
|
roda = {"X": "RR", "Z": "RL", "Y": "FR", "A": "FL"}[eixo]
|
|
no(f"Roda{eixo}", "Control", "Info", **caixa(cx, 163, 1, 1), script=ROD, mao=1 if roda in ("FL", "RR") else -1)
|
|
|
|
SCRIPT = ext("Script", "res://scripts/circuito/painel_circuito.gd")
|
|
saida = [f"[gd_scene format=3]\n"] + recursos + [""] + [s + "\n" for s in subs]
|
|
saida.append(f'[node name="PainelCircuito" type="Control"]\noffset_right = 1920.0\noffset_bottom = 1080.0\n'
|
|
f'mouse_filter = 2\nscript = {SCRIPT}\n')
|
|
saida += [n + "\n" for n in nos]
|
|
(RAIZ / "cenas" / "painel_circuito.tscn").write_text("\n".join(saida))
|
|
print("nós:", len(nos) + 1)
|