sprint-1: firmware ESP32 base -- STANDBY + Modbus + NMEA 2000 + watchdog
End-to-end implementation of Sprint 1 per docs/sprint-1-plan.md.
Builds: pio run -e esp32-dev SUCCESS, RAM 6.7%, Flash 26.5% (347 KB).
Tests: pytest 110/110 green; pio test -e native deferred (needs host
C++ compiler -- none on this Windows machine).
Firmware (firmware/ar_autopilot_v1/):
- platformio.ini: 4 envs (esp32-dev release, esp32-debug, native unity
tests, check static analysis). NMEA2000-library@4.22, NMEA2000_esp32@
1.0, eModbus@1.7.4 pinned.
- main.cpp: boot in STANDBY, FreeRTOS task spawn, returns to scheduler.
- system/: ar_log.h facade, task_config.h (priorities/stacks/cores
central table), heartbeat (1 Hz LED + uptime).
- modes/: STANDBY-only state machine; non-STANDBY rejected.
- hal/: di_do.cpp (5 DI + 10 DO with debounce + last-state cache),
rudder_sensor.cpp (100 Hz ADC + 5-sample median filter, Core 1),
rudder_actuator.cpp (DO1/DO2/DO3 with three safety interlocks:
power-off, STANDBY mode, limit switch).
- safety/: TWDT @ 2 s panic-on-expire; 50 Hz safety task on Core 1
enforcing DI1 physical disengage button, DI4 external alarm,
both-limit-switch interlock.
- protocols/modbus_slave.cpp: eModbus RTU server on UART2 @ 38400 8N1,
slave ID 1. 17 inputs + 19 discretes + 5 holdings + 4 coils. Reads
pull live telemetry; writes validate range and route to handlers.
- protocols/nmea2000_consumer.cpp: stack open with CAN TX=GPIO3
RX=GPIO1, subscribed to PGN 127250 (Heading) + PGN 127251 (Rate of
Turn). 5 s staleness flag built in for Sprint 6 alarm wiring.
- filters/median.h: templated MedianFilter<T,N> (host testable).
Cross-cutting:
- modbus_registers.yaml: single source of truth for the Modbus register
map. 45 entries.
- tools/gen_modbus_registers.py: YAML -> C++ header + Python module
generator with --check for drift detection.
- arautopilot/shared/modbus_register_map.py: generated Python mirror,
imported by Studio + tools.
- arautopilot/tests/test_modbus_register_map.py: 30 tests covering
schema, address uniqueness, range, spot-checks, and drift detection
(fails if YAML edited without regenerating).
- firmware/ar_autopilot_v1/tools/modbus_client_test.py: manual Modbus
client for poking the slave from a PC with USB-RS485 dongle.
- firmware/ar_autopilot_v1/test/test_median_filter/test_median.cpp:
8 Unity tests of the median filter (host-side, no Arduino dependency).
- docs/firmware.md: full operator + integrator guide (toolchain, build,
flash, expected boot log, troubleshooting, Sprint 1 capability matrix).
Architecture note: opted for Arduino-on-ESP32 only instead of the
proposed dual Arduino-as-ESP-IDF-component setup. Rationale documented
in CHANGELOG and docs/firmware.md -- Arduino-on-ESP32 already provides
the FreeRTOS primitives we need; dual framework adds fragility without
benefit at Sprint 1 scope. Reconsider in Sprint 8 (OTA + secure boot).
NOT in Sprint 1 (intentional per brief sec. 12):
- PID loops (inner/outer)
- True Course / Track Keeping
- Full alarm catalogue beyond DI1/DI4
- Knob driver
- Studio GUI / dedicated display
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
// =============================================================================
|
||||
// test_median.cpp -- Unity host-side tests for MedianFilter<T, N>
|
||||
// =============================================================================
|
||||
//
|
||||
// Runs on the developer machine via:
|
||||
// .venv/Scripts/pio.exe test -e native
|
||||
//
|
||||
// No ESP32 hardware required. Tests the pure-C++ template in
|
||||
// src/filters/median.h.
|
||||
// =============================================================================
|
||||
|
||||
#include <unity.h>
|
||||
|
||||
// Compile-time switch: when AR_HOST_TEST is defined we drop the Arduino.h
|
||||
// dependency. The median filter only needs <algorithm>, <array>, <cstddef>.
|
||||
#include "../../src/filters/median.h"
|
||||
|
||||
using arautopilot::filters::MedianFilter;
|
||||
|
||||
void setUp() {}
|
||||
void tearDown() {}
|
||||
|
||||
void test_empty_filter_returns_default_constructed_value() {
|
||||
MedianFilter<int, 5> f;
|
||||
TEST_ASSERT_EQUAL_INT(0, f.median());
|
||||
TEST_ASSERT_EQUAL_size_t(0, f.size());
|
||||
TEST_ASSERT_FALSE(f.is_full());
|
||||
}
|
||||
|
||||
void test_single_sample_returns_itself() {
|
||||
MedianFilter<int, 5> f;
|
||||
TEST_ASSERT_EQUAL_INT(42, f.push(42));
|
||||
TEST_ASSERT_EQUAL_size_t(1, f.size());
|
||||
TEST_ASSERT_FALSE(f.is_full());
|
||||
}
|
||||
|
||||
void test_two_samples_returns_upper_of_pair() {
|
||||
MedianFilter<int, 5> f;
|
||||
f.push(10);
|
||||
// size = 2, median index = 2/2 = 1, so the second element after sort.
|
||||
TEST_ASSERT_EQUAL_INT(20, f.push(20));
|
||||
TEST_ASSERT_EQUAL_INT(20, f.push(15)); // sorted: 10,15,20 -> 15
|
||||
TEST_ASSERT_EQUAL_INT(15, f.median());
|
||||
}
|
||||
|
||||
void test_filter_suppresses_single_spike() {
|
||||
// A common scenario: 100 100 99 9999 101 -- the median ignores 9999.
|
||||
MedianFilter<int, 5> f;
|
||||
f.push(100);
|
||||
f.push(100);
|
||||
f.push(99);
|
||||
f.push(9999);
|
||||
f.push(101);
|
||||
TEST_ASSERT_TRUE(f.is_full());
|
||||
TEST_ASSERT_EQUAL_INT(100, f.median());
|
||||
}
|
||||
|
||||
void test_filter_window_is_circular() {
|
||||
MedianFilter<int, 3> f;
|
||||
f.push(1);
|
||||
f.push(2);
|
||||
f.push(3); // window = [1,2,3] -> median 2
|
||||
TEST_ASSERT_EQUAL_INT(2, f.median());
|
||||
f.push(100); // window = [100,2,3] -> median 3
|
||||
TEST_ASSERT_EQUAL_INT(3, f.median());
|
||||
f.push(100); // window = [100,100,3] -> median 100
|
||||
TEST_ASSERT_EQUAL_INT(100, f.median());
|
||||
f.push(100); // window = [100,100,100] -> median 100
|
||||
TEST_ASSERT_EQUAL_INT(100, f.median());
|
||||
}
|
||||
|
||||
void test_reset_clears_state() {
|
||||
MedianFilter<int, 5> f;
|
||||
f.push(1);
|
||||
f.push(2);
|
||||
f.push(3);
|
||||
f.reset();
|
||||
TEST_ASSERT_EQUAL_size_t(0, f.size());
|
||||
TEST_ASSERT_FALSE(f.is_full());
|
||||
TEST_ASSERT_EQUAL_INT(0, f.median());
|
||||
TEST_ASSERT_EQUAL_INT(42, f.push(42));
|
||||
}
|
||||
|
||||
void test_negative_values() {
|
||||
MedianFilter<int16_t, 5> f;
|
||||
f.push(-100);
|
||||
f.push(-50);
|
||||
f.push(0);
|
||||
f.push(50);
|
||||
f.push(100);
|
||||
TEST_ASSERT_EQUAL_INT16(0, f.median());
|
||||
}
|
||||
|
||||
void test_window_of_one_is_identity() {
|
||||
MedianFilter<int, 1> f;
|
||||
TEST_ASSERT_EQUAL_INT(7, f.push(7));
|
||||
TEST_ASSERT_EQUAL_INT(9, f.push(9));
|
||||
TEST_ASSERT_TRUE(f.is_full());
|
||||
}
|
||||
|
||||
int main(int /*argc*/, char** /*argv*/) {
|
||||
UNITY_BEGIN();
|
||||
RUN_TEST(test_empty_filter_returns_default_constructed_value);
|
||||
RUN_TEST(test_single_sample_returns_itself);
|
||||
RUN_TEST(test_two_samples_returns_upper_of_pair);
|
||||
RUN_TEST(test_filter_suppresses_single_spike);
|
||||
RUN_TEST(test_filter_window_is_circular);
|
||||
RUN_TEST(test_reset_clears_state);
|
||||
RUN_TEST(test_negative_values);
|
||||
RUN_TEST(test_window_of_one_is_identity);
|
||||
return UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user