Skip to content

HostSim FOC support: run loaded graph in simulator and add FOC runtime layout - #29

Open
datacrystals wants to merge 1 commit into
hostsim-live-telemetry-cleanfrom
hostsim-foc-platform-api-v2
Open

HostSim FOC support: run loaded graph in simulator and add FOC runtime layout#29
datacrystals wants to merge 1 commit into
hostsim-live-telemetry-cleanfrom
hostsim-foc-platform-api-v2

Conversation

@datacrystals

Copy link
Copy Markdown
Contributor

Summary

This PR fixes two blockers that prevented the FOC demo graph from actually running in HostSim:

  1. Build Simulation always built the SPWM demo graph, regardless of which graph was loaded in NodeGUI.
  2. The runtime plot layout was hardcoded to the SPWM demo (gate signals / duty / phase currents), so even if FOC telemetry arrived it was plotted in the wrong context.

Changes

  • HostSim platform API stubs (Images/HostSim/include/platform_api.h, Images/HostSim/src/platform_api.cpp):

    • Added the missing functions the FOC demo graph calls:
      • platform_get_motor_rpm
      • platform_phase_voltage_u/v/w
      • platform_get_throttle_valid
      • platform_digital_read/write
      • platform_can_send/rx
  • HostSim launcher scripts (Images/HostSim/scripts/run_spwm_live.sh, .ps1):

    • Added --graph / -Graph and --scenario / -Scenario arguments.
    • Scenario defaults to a file matching the graph name (with _graph suffix stripped), falling back to default_motor.json.
    • Backward compatible: calling without --graph still runs the SPWM demo.
  • NodeGUI Build Simulation (Source/NodeGUI/src/MainWindow.cpp):

    • Passes the currently loaded graph to the launcher instead of hardcoding SPWM.
  • Runtime plot layouts (Source/NodeGUI/src/runtime/RuntimeTab.cpp/.h):

    • Added a FOC preset: cg_id_a/cg_iq_a, cg_vd_v/cg_vq_v, cg_iu_a/cg_iv_a/cg_iw_a.
    • Added a FOC button next to the existing SPWM preset button.
    • Auto-detects FOC graphs when cg_id_a telemetry is present and applies the FOC layout; falls back to SPWM layout on duty_u.

Verification

  • cmake --build build --target NodeGUI succeeds.
  • Linux launcher tested with both SPWM and FOC graphs:
    • bash Images/HostSim/scripts/run_spwm_live.sh --no-gui --force-emit
    • bash ... --graph Assets/Examples/foc_demo.json --no-gui --force-emit

Known limitations / follow-up work

HostSim is still a simplified SIL backend. The items below need to be addressed before this can be considered a true HAL-level SIL simulator:

  • Ngspice plant backend: Images/HostSim/src/plant/ngspice_plant.cpp exists but is not wired as the default. FOC graphs should run against an ngspice inverter/motor netlist, not the built-in ODE motor model.
  • Non-realtime ngspice execution: ngspice will not run at realtime 10 kHz ISR rates. Need a time-scaling / co-simulation scheduler that decouples simulation wall-clock from firmware ISR time.
  • Command injection / CAN simulation: platform_can_rx is currently a no-op. The simulator needs a way to inject control-start commands, throttle commands, and config writes so the firmware does not sit waiting for a control-start frame.
  • Preset firmware state: Provide scenario-level hooks to pre-arm the control state, skip boot waits, and pre-load config values (e.g. Ctrl.ThrottleEnable, current limits) so valuable simulation time is not spent simulating idle startup.
  • Sensor noise and non-ideality: Add configurable noise, offset, gain error, and quantization to phase currents, DC-link voltage, encoder angle, and throttle inputs.
  • HAL-level platform API: Several stubs are currently fake (digital IO no-ops, phase voltages computed as duty * vdc / 100, CAN no-ops). These need to be backed by the simulated plant/netlist.
  • Graph-specific scenario selection: The scenario heuristic is crude; NodeGUI should allow picking a scenario per graph or embed scenario metadata in the graph JSON.

/cc @hibyemy

…me layout

- Add missing platform_api stubs so the FOC demo graph builds in HostSim:
  platform_get_motor_rpm, platform_phase_voltage_u/v/w,
  platform_get_throttle_valid, platform_digital_read/write,
  platform_can_send/rx.
- Make run_spwm_live.sh/.ps1 accept --graph/--scenario so any graph can be
  emitted/built/run, defaulting to a matching scenario or default_motor.json.
- Wire NodeGUI Build Simulation to pass the currently loaded graph to the
  launcher instead of hardcoding the SPWM demo graph.
- Add a FOC runtime plot preset (cg_id_a/iq_a, cg_vd_v/vq_v, cg_iu_a/iv_a/iw_a)
  and auto-select it when cg_id_a telemetry is present.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant