Skip to content

Add closed-loop simulator sketch + physics library (Router)#16

Open
toprakcelikel wants to merge 3 commits into
elcano:mainfrom
toprakcelikel:closed-loop-sim-pr
Open

Add closed-loop simulator sketch + physics library (Router)#16
toprakcelikel wants to merge 3 commits into
elcano:mainfrom
toprakcelikel:closed-loop-sim-pr

Conversation

@toprakcelikel

Copy link
Copy Markdown
Contributor

Add closed-loop simulator sketch + shared physics library (Router)

Summary

Router-side closed-loop simulator and the shared integer physics engine, split
out from #15 so the closed-loop sketch can be reviewed and merged on its own.

Restores the CAN backbone that PR #12 (full-sim-loop) introduced and PR #14
(modify stage 1 and 3) inadvertently dropped.

What's included

  • Non_grapic_simulator/simulator_closed_loop/simulator_closed_loop.ino
    Router-side closed-loop variant that reads real DBW inputs (throttle, brake,
    steering) and publishes pose / heading / speed / actual wheel angle as CAN
    frames (0x4C0, 0x4E0, 0x4F0, 0x430) for Sensor Hub Nav and DBW's
    steering PID to consume. Closes the Nav → DBW → Router → Nav loop.
  • Non_grapic_simulator/libraries/simulator_physics/ — shared integer-only
    physics engine extracted into a small in-repo Arduino library
    (computeSpeed, updateAngle, updatePosition, sin1000/cos1000,
    wheel-pulse + angle-sensor output). Both sketches #include <simulator_physics.h>
    so future physics changes happen in one place.
  • Non_grapic_simulator/README.md — describes the two simulator sketches and
    the shared physics engine, plus IDE setup/troubleshooting.
  • .gitignore — ignore build/IDE artifacts.
  • simulator_stage1.ino — only changes are the #include line + a SETUP
    banner; runtime behavior is identical to PR Fix #define comments and update brake model #14.

Notable changes

  • Removed the Stage 2 ASCII command pipe and Stage 3 binary telemetry from
    the closed-loop sketch. Those were an in-sketch PC→Router command-injection
    path; the closed loop is now driven purely by DBW's pins and the CAN bus. All
    command-injection/test plumbing is consolidated into the upcoming
    test-automation PR (avoids two competing command paths).

  • Fixed brake detection. Previously the Router inferred the brake from
    throttle level:

    bool brakeOn = (throttle < MIN_EFFECTIVE_THROTTLE);

    At low cruise speeds (e.g. Nav commanding ~100 cm/s) DBW's throttle PID can sit
    below MIN_EFFECTIVE_THROTTLE during ramp-up, which falsely engaged the brake
    and stalled the vehicle (~30 s stuck at waypoint 1). The Router now reads DBW's
    actual brake wire (DBW D44 → Router D48):

    int brakePinRaw = digitalRead(BRAKE_ON_PIN);
    bool brakeOn = BRAKE_ON_ACTIVE_LOW ? (brakePinRaw == LOW)
                                       : (brakePinRaw == HIGH);

    Adds a BRAKE_ON_ACTIVE_LOW config (default true for the Bridge's active-low
    relay wiring; flip to false for active-high wiring) and a B= field in the
    debug line for live visibility.

One-time setup (per developer)

Arduino IDE → File → Preferences → Sketchbook Location → set to
<repo>/Non_grapic_simulator. This lets the IDE find the shared
simulator_physics library. Full setup + troubleshooting in
Non_grapic_simulator/README.md.

Verification

  • Flashed to the Router; closed loop runs and the vehicle moves correctly at low
    cruise speed (previously stalled ~30 s at waypoint 1).
  • DBW continues to actuate — Router motion is derived from DBW's throttle/brake/
    steer output pins, so motion confirms DBW participation.

Out of scope (follow-up PR)

  • NavigateTestRunner gateway, tools/test_runner.py, test CSVs, and assertion
    design notes.
  • Reconciling the test-command interface with Folsom's merged Simulator.ino
    design.

Notes for reviewers

  • Integer-only arithmetic throughout (no floating point), per project constraint.
  • If the brake ever reads inverted on different wiring, flip BRAKE_ON_ACTIVE_LOW.
  • Commits: closed-loop sketch + physics lib (30bcffd) → Stage 2/3 removal
    (8da87c0, cherry-picked) → Router brake fix.

Restores the CAN backbone for end-to-end closed-loop testing that PR elcano#14
inadvertently dropped, as a sister sketch alongside simulator_stage1
(refactored only to share the physics engine; runtime behavior unchanged).

- New simulator_closed_loop sketch: reads real DBW inputs (throttle,
  L_TURN, R_TURN), publishes 0x4C0 position, 0x4E0 heading, 0x4F0 speed,
  and 0x430 SimSteerActual via CAN for Sensor Hub Nav and DBW to close
  their respective loops.
- Shared simulator_physics Arduino library used by both sketches.
- LOG_PORT macro with USE_NATIVE_USB switch (default on for Bridge board)
  so the Native USB port is used and D0/D1 stay quiet for the
  inter-board UART.
- analogWriteResolution(12), PIO_PUDR pull-up disable on L_TURN/R_TURN,
  MAX_SPEED reduced to 2000 mm/s for waypoint capture turn radius.
- README documents the one-time Arduino IDE Sketchbook Location change
  and the library install copy from the default sketchbook.
- .gitignore keeps third-party libraries (due_can, Time, RTClib, etc.)
  out of the repo while still tracking our own simulator_physics.

End-to-end closed loop verified on bench against DBW main (PR elcano#7).
Inferring brake from throttle < MIN_EFFECTIVE_THROTTLE falsely engaged the brake during low-speed PID ramp-up (e.g. Nav speed=100 cm/s), stalling the sim. Read BRAKE_ON_PIN (D48) directly; add BRAKE_ON_ACTIVE_LOW config (default true for Bridge active-low relay wiring) and brake state to the debug line.
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