velxio/test/test_circuit/plan/phase_4_actives.md

48 lines
1.2 KiB
Markdown
Raw Normal View History

feat: electrical simulation via ngspice-WASM (eecircuit-engine) Adds full SPICE-accurate electrical simulation to Velxio, behind a lazy- loaded ⚡ toolbar toggle. Arduino / ESP32 / RP2040 sketches now co-simulate with real analog behaviour: correct voltages on wires, real I–V curves on LEDs, working potentiometers, NTC thermistors read by analogRead(), PWM driving RC filters, transistors, op-amps, diodes, MOSFETs, etc. Engine: eecircuit-engine (ngspice compiled to WebAssembly). Main bundle stays at 2.4 MB; the 20 MB SPICE chunk only loads when the user activates electrical mode. Disabled at build time via VITE_ELECTRICAL_SIM=false. Frontend additions: - simulation/spice/: SpiceEngine wrapper + lazy entry, NetlistBuilder with UnionFind over wires, componentToSpice mapping (24 metadataIds incl. real part numbers: 2N2222, 2N3055, BC547, IRF540, 2N7000, 1N4148, 1N4007, 1N4733, LEDs, NTC, op-amp ideal), CircuitScheduler with debounced coalescing, AVRSpiceBridge for quasi-static co-simulation. - store/useElectricalStore: Zustand slice, feature-flag aware. - components/analog-ui/: ⚡ toolbar toggle + SVG voltage overlay. - components/components-instruments/: Voltmeter, Ammeter probes. - 62 tests (spice-*, netlist-builder, component-to-spice, instruments). Sandbox (test/test_circuit/): 47-test validation sandbox that proved the approach (hand-rolled MNA baseline + ngspice pipeline) before porting to the app. Kept as reference. Docs: docs/wiki/circuit-emulation-*.md (13 engineering pages covering architecture, solvers, components, AVR bridge, gotchas, performance, integration plan, API reference, appendix) + electrical-simulation- user-guide.md (end-user facing). Reference plan: test/test_circuit/plan/phase_8_velxio_implementation.md Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 19:11:54 +07:00
# Fase 4 — Componentes activos
## Modelo de diodo / LED (Shockley)
```
I_d = Is · (exp(V_d / (n · Vt)) 1)
Vt = 25.85 mV @ 300 K
```
Linealización en cada iteración de Newton:
```
gd = (Is / (n·Vt)) · exp(V_d_prev / (n·Vt))
Ieq = I_d(V_d_prev) gd · V_d_prev
```
Stamp:
- G[a,a] += gd, G[b,b] += gd, G[a,b] = gd, G[b,a] = gd
- b[a] = Ieq, b[b] += Ieq
## Parámetros típicos
| LED color | Is (A) | n | V_forward @ 10 mA |
|---|---|---|---|
| rojo | 1e-20 | 1.7 | ~2.0 V |
| verde | 1e-22 | 1.9 | ~2.2 V |
| azul / blanco | 1e-24 | 2.0 | ~3.2 V |
## BJT (Ebers-Moll, simplificado)
```
I_c = Is · (exp(V_be / Vt) exp(V_bc / Vt)) (1/β_r) · Is · (exp(V_bc/Vt) 1)
I_b = Is/β_f · (exp(V_be / Vt) 1) + Is/β_r · (exp(V_bc / Vt) 1)
```
## Tests
1. **LED rojo con R=220Ω, V=5V** → esperamos `V_LED ≈ 2.0 V`, `I ≈ 13.6 mA`
2. **LED azul con R=220Ω, V=5V**`V_LED ≈ 3.2 V`, `I ≈ 8.2 mA`
3. **Diodo rectificador en serie con R** → caída ≈ 0.6 V
4. **Transistor NPN como switch** (base vía R=10k) → `V_CE < 0.2 V` cuando ON
5. **Amplificador emisor común** (opcional) — verificar ganancia en pequeña señal
## Archivos
- `test/diodes.test.js`
- `test/bjt.test.js` (opcional / si alcanza tiempo)