Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
e85ba46
feat: Integrate plugin support into RPC context and update route hand…
LeoLTM Sep 5, 2026
89cfd24
feat: Implement Lap Timer plugin with database integration and RPC me…
LeoLTM Sep 5, 2026
b769a6c
feat: Move lap timer data models to plugin module and re-export for use
LeoLTM Sep 5, 2026
26b8a67
refactor: Remove unused lap-related database tables and clean up comm…
LeoLTM Sep 5, 2026
4135f9d
feat(laptimer): implement lap timer plugin with session management an…
LeoLTM Sep 5, 2026
e7ae65b
docs: add backend plugin system guide for structuring and implementin…
LeoLTM Sep 5, 2026
0385b51
feat(laptimer): enhance lap timer functionality with session manageme…
LeoLTM Sep 5, 2026
80aa90b
feat(laptimer): implement lap session management with RPC methods and…
LeoLTM Sep 5, 2026
753def1
chore: refactor lap timer functionality into module;
LeoLTM Sep 6, 2026
4017361
docs: add module architecture guide for ESP firmware
LeoLTM Sep 6, 2026
973866e
feat(laptimer): move entire lap timer logic to ESP to prevent communi…
LeoLTM Sep 6, 2026
3805528
feat(alignment): integrate alignment module into JSON output and seri…
LeoLTM Sep 6, 2026
4002c81
feat(alignment): add alignment module with sensor status handling and…
LeoLTM Sep 6, 2026
3158c16
feat(plugins): register alignment plugin and update serial status pay…
LeoLTM Sep 6, 2026
620ee09
feat(mock): implement alignment command handling and status reporting
LeoLTM Sep 6, 2026
ab7752d
feat(alignment): implement sensor alignment plugin with UI components…
LeoLTM Sep 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions arduino/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This submodule contains the firmware for an ESP8266 that reads two light barriers and measures the speed of passing vehicles. It sends the measurement data to the companion app using serial communication. It also controls a relay to trigger the flash when the companion app requests it when it takes a picture.

## Module Architecture

The firmware follows a monolithic companion architecture with dormant feature modules (e.g. `LapTimer`). All modules compile into a single firmware binary and are activated on-demand via serial commands from the companion daemon (`headless-rust`).

For implementation details and instructions on adding new modules, see the [Module Architecture Guide](src/modules/README.md).

## Setup

1. Install the Platformio IDE extension in Visual Studio Code.
Expand Down
32 changes: 11 additions & 21 deletions arduino/src/json_output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#include "config.h"
#include "state.h"

#include "modules/laptimer.h"
#include "modules/alignment.h"

// ─── Serial Output Helpers ────────────────────────────────────────────────────
// Serial.flush() is intentionally omitted for most messages: the TX FIFO
// buffers them at 115200 baud while the loop continues polling sensors.
Expand Down Expand Up @@ -58,35 +61,22 @@ void sendJsonPong() {
cfg["maxSpeed"] = maxSpeedKmH;
cfg["sensorDistance"] = sensorDistance;
cfg["debugEnabled"] = debugEnabled;
cfg["lapMode"] = (lapMode == LapMode::MULTI) ? "multi" : "single";
cfg["lapActive"] = (lapSessionState != LapSessionState::IDLE);
cfg["lapDirFilter"] = (lapDirectionFilter == LapDirectionFilter::FORWARD_ONLY) ? "forward"
: (lapDirectionFilter == LapDirectionFilter::REVERSE_ONLY) ? "reverse" : "both";
LapTimer::populatePongConfig(cfg);
Alignment::populatePongConfig(cfg);
String out;
serializeJson(doc, out);
out += '\n';
Serial.write(out.c_str(), out.length());
Serial.flush(); // Ensure full pong reply is sent before next command is processed
}

void sendLapStart(int lapNum, float speedAtStart) {
JsonDocument doc;
doc["status"] = "lapStart";
doc["lapNumber"] = lapNum;
doc["speedAtStart"] = round(speedAtStart * 10.0f) / 10.0f;
String out;
serializeJson(doc, out);
out += '\n';
Serial.write(out.c_str(), out.length());
}

void sendLapEnd(int lapNum, float durationMs, float speedAtStart, float speedAtEnd) {
void sendJsonCapabilities() {
JsonDocument doc;
doc["status"] = "lapEnd";
doc["lapNumber"] = lapNum;
doc["durationMs"] = durationMs; // full float precision (~0.001 ms)
doc["speedAtStart"] = round(speedAtStart * 10.0f) / 10.0f;
doc["speedAtEnd"] = round(speedAtEnd * 10.0f) / 10.0f;
doc["status"] = "capabilities";
JsonArray features = doc["features"].to<JsonArray>();
features.add("speed");
features.add("laptimer");
features.add("alignment");
String out;
serializeJson(doc, out);
out += '\n';
Expand Down
7 changes: 2 additions & 5 deletions arduino/src/json_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@ void sendJsonStatus(const char *status, float value, float tolerance, const char
void sendJsonConfig(const char *key, float value);
void sendJsonConfigError(const char *message);

// ─── Ping / Pong ─────────────────────────────────────────────────────────────
// ─── Ping / Capabilities ─────────────────────────────────────────────────────
void sendJsonPong();

// ─── Lap Timer ───────────────────────────────────────────────────────────────
void sendLapStart(int lapNum, float speedAtStart);
void sendLapEnd(int lapNum, float durationMs, float speedAtStart, float speedAtEnd);
void sendJsonCapabilities();

// ─── Debug ───────────────────────────────────────────────────────────────────
void sendDebug(const String &message);
82 changes: 21 additions & 61 deletions arduino/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "state.h"
#include "json_output.h"
#include "serial_io.h"
#include "modules/laptimer.h"
#include "modules/alignment.h"

// ─── State Reset ─────────────────────────────────────────────────────────────
void resetMeasurement() {
Expand All @@ -17,11 +19,16 @@ void setup() {
Serial.begin(115200);
pinMode(sensor1, INPUT_PULLUP);
pinMode(sensor2, INPUT_PULLUP);
LapTimer::init();
Alignment::init();
sendJsonStatus("ready");
}

// ─── Main Loop ───────────────────────────────────────────────────────────────
void loop() {
LapTimer::update();
Alignment::update();

// Process incoming serial commands every iteration
handleSerial();
if (stringComplete) {
Expand Down Expand Up @@ -102,68 +109,18 @@ void loop() {
resetMeasurement();

if (speedInKmH > 0.0f && speedInKmH < (float)maxSpeedHardCap) {
MeasurementEvent event = { speedInKmH, speedTolerance, direction, boundaryUs };

// ================================================================
// SPEED CAMERA MODE (no active lap session)
// Behavior unchanged: host-controlled flash, violation tracking.
// ================================================================
if (lapSessionState == LapSessionState::IDLE) {
if (speedInKmH > maxSpeedKmH) {
sendJsonStatus("speeding", speedInKmH, speedTolerance, direction);
} else {
sendJsonStatus("legal", speedInKmH, speedTolerance, direction);
}

// ================================================================
// LAP TIMER MODE (active lap session)
// Lap boundaries are firstTriggerUs (µs-precise first beam break),
// captured before any serial output or state change.
// ================================================================
// Base speed status (speeding or legal)
if (speedInKmH > maxSpeedKmH) {
sendJsonStatus("speeding", speedInKmH, speedTolerance, direction);
} else {
// Emit speed event for UI display only — no blocking call follows
if (speedInKmH > maxSpeedKmH) {
sendJsonStatus("speeding", speedInKmH, speedTolerance, direction);
} else {
sendJsonStatus("legal", speedInKmH, speedTolerance, direction);
}

// Direction filter — configured per session
bool passValid = (lapDirectionFilter == LapDirectionFilter::BOTH)
|| (lapDirectionFilter == LapDirectionFilter::FORWARD_ONLY && strcmp(direction, "forward") == 0)
|| (lapDirectionFilter == LapDirectionFilter::REVERSE_ONLY && strcmp(direction, "reverse") == 0);

if (passValid) {
if (lapSessionState == LapSessionState::WAITING) {
// ── WAITING → TIMING: open lap N ────────────────────────────────
lapStartUs = boundaryUs; // µs-precise first beam break
lapStartSpeedKmH = speedInKmH;
lapSessionState = LapSessionState::TIMING;
sendLapStart(lapNumber, speedInKmH);

} else if (lapSessionState == LapSessionState::TIMING) {
// ── TIMING: close lap N ──────────────────────────────────────────
float lapDurationMs = (float)(boundaryUs - lapStartUs) * 0.001f;
sendLapEnd(lapNumber, lapDurationMs, lapStartSpeedKmH, speedInKmH);

if (lapMode == LapMode::SINGLE) {
lapSessionState = LapSessionState::WAITING;
lapNumber = 1;
lapStartUs = 0;
lapStartSpeedKmH = 0.0f;

} else {
// MULTI: this same boundary crossing closes lap N and opens lap N+1.
// Reuse boundaryUs so there is zero gap between consecutive laps.
lapStartUs = boundaryUs;
lapStartSpeedKmH = speedInKmH;
lapNumber++;
// lapSessionState stays TIMING
sendLapStart(lapNumber, speedInKmH); // resets host live timer immediately
}
}
}
sendJsonStatus("legal", speedInKmH, speedTolerance, direction);
}

// ponytail: notify active modules with zero heap allocation or dynamic dispatch
LapTimer::onMeasurement(event);

} else {
sendJsonStatus("timeout"); // Speed out of valid range — discard
}
Expand All @@ -174,21 +131,24 @@ void loop() {

// ─── Protocol Reference ───────────────────────────────────────────────────────
//
// SPEED CAMERA MODE (lapSessionState == IDLE)
// SYSTEM / CORE
// Out: speeding {value, tolerance, direction} | legal {value, tolerance, direction}
// timeout | measuring | ready | pong | config | configError | jsonError
// capabilities {features: [...]}
// In: setMaxSpeed {value:1–250} — set speed limit
// setDebug {value:0|1}
// ping
// getCapabilities
//
// LAP TIMER MODE (lapSessionState != IDLE)
// MODULE: LAP TIMER (dormant until startLapSession received)
// Out: lapWaiting — session opened, awaiting first pass
// lapStopped — session closed by host
// lapStart {lapNumber, speedAtStart} — first beam break
// lapEnd {lapNumber, durationMs, speedAtStart, speedAtEnd} — lap closed
// speeding / legal still emitted (UI speed display)
// In: startLapSession {mode, dirFilter?} — open session
// stopLapSession — close session
// stopLapSession — close session
//
// startLapSession optional fields:
// dirFilter: "both" | "forward" | "reverse" (default "both")

Loading