Skip to content

armgit / moss

Public Imported from GitHub
0

MOSS: a small, mostly 3D-printed litter-picking rover. Cleaner streets, one maker at a time.

Python

firmware V0.4: QT Py ESP32-S3 + MDD3A variant (same protocol, configurable ramp, native USB), host test; not yet run on hardware

metrox2f370b90908a
diff --git a/firmware/MOSS_QTPY_MDD3A/Control.h b/firmware/MOSS_QTPY_MDD3A/Control.h
new file mode 100644
index 0000000..0f798ff
--- /dev/null
+++ b/firmware/MOSS_QTPY_MDD3A/Control.h
@@ -0,0 +1,128 @@
+#pragma once
+// MOSS V0.4 control logic - same protocol as the V0.3 bench firmware (arm /
+// drive L R / stop / status / zero / test), plus a configurable ramp:
+// ramp UP DOWN milliseconds per 1 % of PWM, rise and fall separately
+// The V0.3 ramp was fixed at 10 ms per percent (0 -> 25 % in 0.25 s), too
+// abrupt for a rover carrying an arm. Defaults below: 0 -> 70 % in 2.1 s,
+// 70 -> 0 in 1.05 s, to be tuned on the floor with the arm mounted.
+// MAX_PERCENT stays a compile-time commissioning limit: raising it is a
+// decision taken after mechanical tests, not a serial command.
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+namespace moss {
+constexpr int MAX_PERCENT = 25; // Commissioning limit; raise only after mechanical tests.
+constexpr uint32_t COMMAND_TIMEOUT_MS = 300;
+constexpr uint32_t ARM_TIMEOUT_MS = 10000;
+constexpr uint32_t TEST_MS = 1000;
+constexpr uint32_t RAMP_RISE_MS_DEFAULT = 30; // ms per 1 % up (0 -> 70 % in 2.1 s)
+constexpr uint32_t RAMP_FALL_MS_DEFAULT = 15; // ms per 1 % down (70 -> 0 % in 1.05 s)
+constexpr uint32_t RAMP_MS_MIN = 1, RAMP_MS_MAX = 500;
+constexpr uint32_t REVERSE_DWELL_MS = 100; // at zero before changing direction
+
+enum Mode { OFF, READY, DRIVE, TEST };
+enum Reply { OK, STATUS, ZERO, INVALID, NOT_ARMED, BUSY };
+
+struct Control {
+ Mode mode = OFF;
+ int left = 0, right = 0;
+ uint32_t since = 0;
+ const char* reason = "boot";
+ uint32_t rampRiseMs = RAMP_RISE_MS_DEFAULT, rampFallMs = RAMP_FALL_MS_DEFAULT;
+ void stop(const char* why) {
+ mode = OFF; left = right = 0; reason = why;
+ }
+ bool poll(uint32_t now) {
+ uint32_t age = now - since; // Defined across millis() rollover.
+ if (mode == DRIVE && age >= COMMAND_TIMEOUT_MS) { stop("timeout"); return true; }
+ if (mode == READY && age >= ARM_TIMEOUT_MS) { stop("arm_expired"); return true; }
+ if (mode == TEST && age >= TEST_MS) { stop("test_done"); return true; }
+ return false;
+ }
+ static bool number(const char* s, long lo, long hi, long& out) {
+ if (!s || !*s) return false;
+ errno = 0; char* end;
+ long n = strtol(s, &end, 10);
+ if (errno || *end || n < lo || n > hi) return false;
+ out = n; return true;
+ }
+ static bool percent(const char* s, int& out) {
+ long n; if (!number(s, -MAX_PERCENT, MAX_PERCENT, n)) return false;
+ out = static_cast<int>(n); return true;
+ }
+ Reply command(char* line, uint32_t now) {
+ poll(now);
+ char* argv[4]; int argc = 0; char* save = nullptr;
+ for (char* t = strtok_r(line, " \t", &save); t; t = strtok_r(nullptr, " \t", &save)) {
+ if (argc == 4) { stop("invalid"); return INVALID; }
+ argv[argc++] = t;
+ }
+ if (!argc) return OK;
+ if (argc == 1 && !strcmp(argv[0], "stop")) { stop("stop"); return OK; }
+ if (argc == 1 && !strcmp(argv[0], "status")) return STATUS;
+ if (argc == 1 && !strcmp(argv[0], "zero")) {
+ if (mode != OFF) { stop("zero_requires_stop"); return BUSY; }
+ return ZERO;
+ }
+ if (argc == 1 && !strcmp(argv[0], "arm")) {
+ if (mode == DRIVE || mode == TEST) { stop("arm_while_moving"); return BUSY; }
+ mode = READY; left = right = 0; since = now; reason = "armed"; return OK;
+ }
+ int l, r;
+ if (argc == 3 && !strcmp(argv[0], "drive") && percent(argv[1], l) && percent(argv[2], r)) {
+ if (mode != READY && mode != DRIVE) { stop("not_armed"); return NOT_ARMED; }
+ left = l; right = r; mode = DRIVE; since = now; reason = "drive"; return OK;
+ }
+ if (argc == 3 && !strcmp(argv[0], "test") && percent(argv[2], l) &&
+ (!strcmp(argv[1], "left") || !strcmp(argv[1], "right"))) {
+ if (mode != OFF) { stop("test_requires_stop"); return BUSY; }
+ left = !strcmp(argv[1], "left") ? l : 0;
+ right = !strcmp(argv[1], "right") ? l : 0;
+ mode = TEST; since = now; reason = "test"; return OK;
+ }
+ long up, down;
+ if (argc == 3 && !strcmp(argv[0], "ramp") &&
+ number(argv[1], RAMP_MS_MIN, RAMP_MS_MAX, up) && number(argv[2], RAMP_MS_MIN, RAMP_MS_MAX, down)) {
+ // Allowed while moving: the next ticks simply use the new slopes.
+ rampRiseMs = static_cast<uint32_t>(up); rampFallMs = static_cast<uint32_t>(down);
+ return OK;
+ }
+ stop("invalid"); return INVALID;
+ }
+};
+
+// Output ramp: value moves 1 % per rampRiseMs (towards a larger magnitude) or
+// per rampFallMs (towards zero). A sign change first ramps to zero, then waits
+// REVERSE_DWELL_MS before the direction bit flips.
+struct Ramp {
+ int value = 0; // 0..MAX_PERCENT magnitude currently output
+ int sign = 1;
+ uint32_t lastStep = 0;
+ uint32_t zeroSince = 0;
+ void stop(uint32_t now) { value = 0; zeroSince = now; lastStep = now; }
+ int tick(int target, uint32_t now, uint32_t riseMs, uint32_t fallMs) {
+ if (!target) {
+ if (value && uint32_t(now - lastStep) >= fallMs) { --value; lastStep = now; if (!value) zeroSince = now; }
+ return sign * value;
+ }
+ int desiredSign = target < 0 ? -1 : 1;
+ int magnitude = target < 0 ? -target : target;
+ if (desiredSign != sign) {
+ if (value > 0) {
+ if (uint32_t(now - lastStep) >= fallMs) { --value; lastStep = now; if (!value) zeroSince = now; }
+ return sign * value;
+ }
+ if (uint32_t(now - zeroSince) < REVERSE_DWELL_MS) return 0;
+ sign = desiredSign; lastStep = now; // the rise slope starts after the dwell, not before
+ }
+ if (value < magnitude) {
+ if (uint32_t(now - lastStep) >= riseMs) { ++value; lastStep = now; }
+ } else if (value > magnitude) {
+ if (uint32_t(now - lastStep) >= fallMs) { --value; lastStep = now; if (!value) zeroSince = now; }
+ }
+ return sign * value;
+ }
+};
+}
diff --git a/firmware/MOSS_QTPY_MDD3A/MOSS_QTPY_MDD3A.ino b/firmware/MOSS_QTPY_MDD3A/MOSS_QTPY_MDD3A.ino
new file mode 100644
index 0000000..6915a32
--- /dev/null
+++ b/firmware/MOSS_QTPY_MDD3A/MOSS_QTPY_MDD3A.ino
@@ -0,0 +1,187 @@
+// MOSS V0.4 - Adafruit QT Py ESP32-S3 (5426) on a Terminal Block BFF (6495),
+// Cytron MDD3A dual motor driver, INA219 on the STEMMA QT port.
+// Same serial protocol as the V0.3 DevKitC firmware (see ../README.md), over
+// the QT Py's native USB: build with "USB CDC On Boot: Enabled"
+// (arduino-cli: --fqbn esp32:esp32:adafruit_qtpy_esp32s3_nopsram:CDCOnBoot=cdc).
+// Motors never start at boot. NOT YET RUN ON HARDWARE: pin map to confirm on the
+// terminal block, then bench with "test left 10" before any "drive".
+#include <Arduino.h>
+#include <Wire.h>
+#include <driver/gpio.h>
+#include "Control.h"
+
+// ── pin map (QT Py ESP32-S3 GPIO numbers, core 3.3.x variant) ────────────
+// MDD3A: two PWM inputs per channel. A high / B low = forward, A low / B high
+// = backward, both low = brake (datasheet table 3). Logic high 1.7-12 V, so
+// 3.3 V is fine; PWM up to 20 kHz.
+constexpr int M1A = A0; // GPIO 18 left forward PWM
+constexpr int M1B = A1; // GPIO 17 left backward PWM
+constexpr int M2A = A2; // GPIO 9 right forward PWM
+constexpr int M2B = A3; // GPIO 8 right backward PWM
+constexpr int ENC_L_A = SDA, ENC_L_B = SCL; // GPIO 7 / 6 (primary I2C pins reused as inputs)
+constexpr int ENC_R_A = TX, ENC_R_B = RX; // GPIO 5 / 16
+// INA219 on the STEMMA QT connector = Wire1 (SDA1 = GPIO 41, SCL1 = GPIO 40)
+constexpr uint8_t INA_ADDRESS = 0x40;
+constexpr bool INVERT_LEFT = false, INVERT_RIGHT = false;
+constexpr float SHUNT_OHMS = 0.0f; // voltage only (see V0.3 README)
+constexpr uint32_t PWM_HZ = 20000;
+constexpr int PWM_BITS = 10;
+constexpr const char* FW = "qtpy-mdd3a-v1";
+
+moss::Control control;
+moss::Ramp rampL, rampR;
+bool pwmOK = false, inaOK = false, inaConfigured = false;
+uint32_t lastTelemetry = 0, lastInaTry = 0;
+int busMv = 0, shuntUv = 0;
+volatile int64_t ticksL = 0, ticksR = 0;
+volatile uint32_t badL = 0, badR = 0;
+volatile uint8_t prevL = 0, prevR = 0;
+portMUX_TYPE encoderMux = portMUX_INITIALIZER_UNLOCKED;
+DRAM_ATTR const int8_t quadrature[16] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
+
+void ARDUINO_ISR_ATTR encoderLeft() {
+ portENTER_CRITICAL_ISR(&encoderMux);
+ uint8_t next = (gpio_get_level((gpio_num_t)ENC_L_A) << 1) | gpio_get_level((gpio_num_t)ENC_L_B);
+ if ((prevL ^ next) == 3) badL = badL + 1;
+ ticksL += quadrature[(prevL << 2) | next]; prevL = next;
+ portEXIT_CRITICAL_ISR(&encoderMux);
+}
+void ARDUINO_ISR_ATTR encoderRight() {
+ portENTER_CRITICAL_ISR(&encoderMux);
+ uint8_t next = (gpio_get_level((gpio_num_t)ENC_R_A) << 1) | gpio_get_level((gpio_num_t)ENC_R_B);
+ if ((prevR ^ next) == 3) badR = badR + 1;
+ ticksR += quadrature[(prevR << 2) | next]; prevR = next;
+ portEXIT_CRITICAL_ISR(&encoderMux);
+}
+void sendLine(const char* text) {
+ size_t n = strlen(text);
+ // Drop a message rather than block motor/watchdog processing on a stalled host.
+ if (Serial.availableForWrite() >= int(n + 1)) {
+ Serial.write((const uint8_t*)text, n); Serial.write('\n');
+ }
+}
+void outputsZero() {
+ if (pwmOK) { ledcWrite(M1A, 0); ledcWrite(M1B, 0); ledcWrite(M2A, 0); ledcWrite(M2B, 0); }
+ else { digitalWrite(M1A, LOW); digitalWrite(M1B, LOW); digitalWrite(M2A, LOW); digitalWrite(M2B, LOW); }
+ rampL.stop(millis()); rampR.stop(millis());
+}
+// MDD3A sign-magnitude: PWM on one input, the other held low. Zero = both low = brake.
+void motorWrite(int pinA, int pinB, int percent, bool invert) {
+ if (invert) percent = -percent;
+ uint32_t duty = (uint32_t(abs(percent)) * 1023U) / 100U;
+ if (percent > 0) { ledcWrite(pinB, 0); ledcWrite(pinA, duty); }
+ else if (percent < 0) { ledcWrite(pinA, 0); ledcWrite(pinB, duty); }
+ else { ledcWrite(pinA, 0); ledcWrite(pinB, 0); }
+}
+bool readRegister(uint8_t reg, uint16_t& result) {
+ Wire1.beginTransmission(INA_ADDRESS); Wire1.write(reg);
+ if (Wire1.endTransmission(false) != 0) return false;
+ if (Wire1.requestFrom(INA_ADDRESS, (uint8_t)2) != 2) return false;
+ result = (uint16_t(Wire1.read()) << 8); result |= Wire1.read(); return true;
+}
+bool initIna() {
+ Wire1.beginTransmission(INA_ADDRESS);
+ Wire1.write((uint8_t)0x00); Wire1.write((uint8_t)0x39); Wire1.write((uint8_t)0x9F);
+ if (Wire1.endTransmission() != 0) return false;
+ uint16_t config;
+ return readRegister(0x00, config) && config == 0x399F;
+}
+void updateIna(uint32_t now) {
+ if (!inaConfigured) {
+ if (uint32_t(now - lastInaTry) >= 1000) { lastInaTry = now; inaConfigured = initIna(); }
+ return;
+ }
+ uint16_t bus, shunt;
+ if (!readRegister(0x02, bus) || !readRegister(0x01, shunt)) { inaOK = false; inaConfigured = false; return; }
+ inaOK = true;
+ busMv = (bus >> 3) * 4;
+ shuntUv = int16_t(shunt) * 10;
+}
+void telemetry() {
+ int64_t l, r; uint32_t il, ir;
+ portENTER_CRITICAL(&encoderMux);
+ l = ticksL; r = ticksR; il = badL; ir = badR;
+ portEXIT_CRITICAL(&encoderMux);
+ char bus[24] = "null", shunt[24] = "null", current[32] = "null";
+ if (inaOK) {
+ snprintf(bus, sizeof(bus), "%d", busMv);
+ snprintf(shunt, sizeof(shunt), "%d", shuntUv);
+ if (SHUNT_OHMS > 0) snprintf(current, sizeof(current), "%.2f", shuntUv / (1000.0f * SHUNT_OHMS));
+ }
+ char out[560];
+ snprintf(out, sizeof(out),
+ "{\"ms\":%lu,\"fw\":\"%s\",\"mode\":%d,\"reason\":\"%s\",\"target_pct\":[%d,%d],\"output_pct\":[%d,%d],"
+ "\"ramp_ms\":[%lu,%lu],\"ticks\":[%lld,%lld],\"invalid_edges\":[%lu,%lu],\"ina_ok\":%s,\"bus_mV\":%s,\"shunt_uV\":%s,\"current_mA\":%s}",
+ (unsigned long)millis(), FW, int(control.mode), control.reason, control.left, control.right,
+ rampL.sign*rampL.value, rampR.sign*rampR.value,
+ (unsigned long)control.rampRiseMs, (unsigned long)control.rampFallMs,
+ (long long)l, (long long)r, (unsigned long)il, (unsigned long)ir, inaOK ? "true" : "false", bus, shunt, current);
+ sendLine(out);
+}
+void handleCommand(char* line) {
+ if (!pwmOK) { control.stop("pwm_init_failed"); outputsZero(); sendLine("ERR pwm_init_failed"); return; }
+ moss::Reply reply = control.command(line, millis());
+ if (control.mode == moss::OFF || control.mode == moss::READY) outputsZero();
+ if (reply == moss::ZERO) {
+ portENTER_CRITICAL(&encoderMux); ticksL = 0; ticksR = 0; badL = 0; badR = 0; portEXIT_CRITICAL(&encoderMux);
+ }
+ if (reply == moss::STATUS) telemetry();
+ else sendLine(reply == moss::INVALID ? "ERR invalid_command" : reply == moss::NOT_ARMED ? "ERR not_armed" : reply == moss::BUSY ? "ERR stopped_busy" : "OK");
+}
+void serviceSerial() {
+ static char line[96]; static size_t used = 0;
+ static bool discard = false; static uint32_t began = 0;
+ if (used && uint32_t(millis() - began) >= 100) {
+ used = 0; discard = true; control.stop("partial_line_timeout"); outputsZero();
+ }
+ for (int budget = 0; budget < 64 && Serial.available(); ++budget) {
+ char c = char(Serial.read());
+ if (c == '\r') continue;
+ if (c == '\n') {
+ if (!discard && used) { line[used] = 0; handleCommand(line); }
+ used = 0; discard = false; continue;
+ }
+ if (discard) continue;
+ if ((c < 32 && c != '\t') || c > 126 || used == sizeof(line)-1) {
+ control.stop("serial_invalid"); outputsZero(); used = 0; discard = true; continue;
+ }
+ if (!used) began = millis();
+ line[used++] = c;
+ }
+}
+void setup() {
+ for (int pin : {M1A, M1B, M2A, M2B}) { pinMode(pin, OUTPUT); digitalWrite(pin, LOW); }
+ Serial.begin(115200);
+ Serial.setTxTimeoutMs(0); // native USB: never block on a host that stopped reading
+ bool ok = true;
+ for (int pin : {M1A, M1B, M2A, M2B}) ok = ledcAttach(pin, PWM_HZ, PWM_BITS) && ok;
+ pwmOK = ok;
+ if (!pwmOK) {
+ for (int pin : {M1A, M1B, M2A, M2B}) { ledcWrite(pin, 0); ledcDetach(pin); pinMode(pin, OUTPUT); digitalWrite(pin, LOW); }
+ control.stop("pwm_init_failed");
+ }
+ outputsZero();
+ for (int pin : {ENC_L_A, ENC_L_B, ENC_R_A, ENC_R_B}) pinMode(pin, INPUT_PULLUP);
+ prevL = (digitalRead(ENC_L_A) << 1) | digitalRead(ENC_L_B);
+ prevR = (digitalRead(ENC_R_A) << 1) | digitalRead(ENC_R_B);
+ attachInterrupt(ENC_L_A, encoderLeft, CHANGE); attachInterrupt(ENC_L_B, encoderLeft, CHANGE);
+ attachInterrupt(ENC_R_A, encoderRight, CHANGE); attachInterrupt(ENC_R_B, encoderRight, CHANGE);
+ Wire1.begin(SDA1, SCL1, 100000); Wire1.setTimeOut(5);
+ inaConfigured = initIna();
+ sendLine("MOSS V0.4 firmware qtpy-mdd3a-v1; USB CDC 115200; limit 25%; ramp 30/15 ms per %; type status");
+}
+void loop() {
+ uint32_t now = millis();
+ if (control.poll(now)) outputsZero();
+ serviceSerial();
+ now = millis();
+ if (control.poll(now)) outputsZero();
+ if (pwmOK && (control.mode == moss::DRIVE || control.mode == moss::TEST)) {
+ motorWrite(M1A, M1B, rampL.tick(control.left, now, control.rampRiseMs, control.rampFallMs), INVERT_LEFT);
+ motorWrite(M2A, M2B, rampR.tick(control.right, now, control.rampRiseMs, control.rampFallMs), INVERT_RIGHT);
+ }
+ if (uint32_t(now - lastTelemetry) >= 200) {
+ lastTelemetry = now; updateIna(now); telemetry();
+ }
+ delay(1);
+}
diff --git a/firmware/tests/control_v04_test.cpp b/firmware/tests/control_v04_test.cpp
new file mode 100644
index 0000000..a207321
--- /dev/null
+++ b/firmware/tests/control_v04_test.cpp
@@ -0,0 +1,83 @@
+// Host logic checks for firmware/MOSS_QTPY_MDD3A/Control.h (V0.4):
+// the V0.3 protocol cases, plus the configurable ramp.
+// g++ -std=c++17 -o /tmp/control_v04_test tests/control_v04_test.cpp && /tmp/control_v04_test
+#include "../MOSS_QTPY_MDD3A/Control.h"
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+using namespace moss;
+Reply cmd(Control& c, const char* s, uint32_t t) {
+ char line[256]; strcpy(line, s); return c.command(line, t);
+}
+int main() {
+ Control c;
+ assert(c.mode == OFF && c.left == 0 && c.right == 0);
+ assert(c.rampRiseMs == 30 && c.rampFallMs == 15);
+ assert(cmd(c,"drive 10 10",0)==NOT_ARMED);
+ assert(cmd(c,"arm",10)==OK);
+ assert(cmd(c,"drive 15 -20",20)==OK && c.left==15 && c.right==-20);
+ assert(!c.poll(319));
+ assert(cmd(c,"status",319)==STATUS);
+ assert(c.poll(320) && c.mode==OFF && c.left==0 && c.right==0);
+ assert(cmd(c,"drive 1 1",321)==NOT_ARMED);
+ assert(cmd(c,"arm",330)==OK);
+ assert(cmd(c,"drive 1 1",340)==OK);
+ assert(cmd(c,"arm",350)==BUSY && c.mode==OFF);
+ const char* invalid[]={"drive 26 0","drive -26 0","drive 1 2 extra","drive 1",
+ "drive nan 1","drive 1e2 0","drive 99999999999999999999 0","go","stop x","drive 1 1 x y",
+ "ramp 0 10","ramp 501 10","ramp 10","ramp a b"};
+ for(auto s: invalid) {
+ cmd(c,"arm",0); cmd(c,"drive 20 20",1);
+ assert(cmd(c,s,2)==INVALID && c.mode==OFF && !c.left && !c.right);
+ }
+ // ramp: accepted, bounds inclusive, allowed while moving, does not stop
+ cmd(c,"arm",0); cmd(c,"drive 20 20",1);
+ assert(cmd(c,"ramp 30 15",2)==OK && c.mode==DRIVE && c.rampRiseMs==30 && c.rampFallMs==15);
+ assert(cmd(c,"ramp 1 500",3)==OK && c.rampRiseMs==1 && c.rampFallMs==500);
+ assert(cmd(c,"ramp 30 15",4)==OK);
+ cmd(c,"stop",5);
+ cmd(c,"arm",0); assert(c.poll(10000) && c.mode==OFF);
+ cmd(c,"arm",0xfffffff0U); cmd(c,"drive 2 3",0xfffffff5U);
+ assert(!c.poll(uint32_t(0xfffffff5U+299U)));
+ assert(c.poll(uint32_t(0xfffffff5U+300U)));
+ assert(cmd(c,"test left 15",100)==OK && c.left==15 && c.right==0);
+ assert(!c.poll(1099)); assert(c.poll(1100) && c.mode==OFF);
+ assert(cmd(c,"test right -15",2000)==OK && c.left==0 && c.right==-15);
+ assert(cmd(c,"drive 5 5",2001)==NOT_ARMED && c.mode==OFF);
+ cmd(c,"test left 15",3000); assert(cmd(c,"stop",3001)==OK && c.mode==OFF);
+ cmd(c,"arm",4000); cmd(c,"drive 5 5",4001);
+ assert(cmd(c,"zero",4002)==BUSY && c.mode==OFF);
+ assert(cmd(c,"zero",4003)==ZERO);
+
+ // ── the ramp, known value in -> known value out ─────────────────────
+ Ramp r; uint32_t rise = 30, fall = 15;
+ // 0 -> 25 at 30 ms/%: 25 steps, first step at t=30 -> value 25 at t=750
+ int v = 0; for (uint32_t t = 0; t <= 750; ++t) v = r.tick(25, t, rise, fall);
+ assert(v == 25);
+ v = r.tick(25, 749, rise, fall); // no time travel needed: still 25
+ // hold: stays 25
+ for (uint32_t t = 751; t < 1000; ++t) assert(r.tick(25, t, rise, fall) == 25);
+ // 25 -> 0 at 15 ms/%: 25 steps = 375 ms -> zero at t=1375
+ for (uint32_t t = 1000; t < 1375; ++t) v = r.tick(0, t, rise, fall);
+ assert(v == 0 && r.value == 0);
+ // reversal: from +10 to -10 -> falls to 0 (150 ms), dwells 100 ms, then rises
+ Ramp q; for (uint32_t t = 0; t <= 300; ++t) q.tick(10, t, rise, fall);
+ assert(q.value == 10 && q.sign == 1);
+ int last = 10;
+ for (uint32_t t = 301; t <= 450; ++t) last = q.tick(-10, t, rise, fall); // 10 steps of 15 ms from t=300
+ assert(last == 0 && q.value == 0 && q.sign == 1); // reached zero at t=450, direction not flipped yet
+ for (uint32_t t = 451; t < 550; ++t) assert(q.tick(-10, t, rise, fall) == 0); // 100 ms dwell
+ assert(q.tick(-10, 550, rise, fall) == 0 && q.sign == -1); // direction flips at t=550, no step yet
+ assert(q.tick(-10, 579, rise, fall) == 0); // rise slope counts from the flip
+ assert(q.tick(-10, 580, rise, fall) == -1);
+ int after = 0; for (uint32_t t = 581; t <= 900; ++t) after = q.tick(-10, t, rise, fall);
+ assert(after == -10 && q.sign == -1);
+ // slope change mid-ramp: 1 ms/% rise -> 25 % in 25 ms
+ Ramp s; int w = 0; for (uint32_t t = 0; t <= 25; ++t) w = s.tick(25, t, 1, 15);
+ assert(w == 25);
+ // rollover-safe stepping
+ Ramp o; o.lastStep = 0xffffffe0U; int z = o.tick(5, 0xffffffe0U + 30U, 30, 15);
+ assert(z == 1);
+ printf("control_v04_test: OK (protocol + ramp)\n");
+ return 0;
+}