MOSS: a small, mostly 3D-printed litter-picking rover. Cleaner streets, one maker at a time.
Python
base: the geometry measured with the 90:1 motors (17 190 ticks per metre, 0.32 m/s at 100 %, both tracks turn from 20 %); tools/moss_speed_profile.py reads the encoders along one run and prints the rate per half second, the top rate and the time to reach it
metroxa94877744297
diff --git a/benches/bench_moss_adapter_cold.py b/benches/bench_moss_adapter_cold.pyindex 69e9599..07cb012 100644--- a/benches/bench_moss_adapter_cold.py+++ b/benches/bench_moss_adapter_cold.py@@ -156,10 +156,10 @@ check(twist_to_tracks(0.15, 0.0, GF) == (25, 25), "floor 12: the limit is untouccheck(twist_to_tracks(0.09, 0.0, GF) == (15, 15), "floor 12: 15 % above the floor passes as is")check(twist_to_tracks(0.06, 0.0, G) == (10, 10), "floor 0 (explicit): L=10 R=10 unchanged")GD = MossGeometry() # the MOSS default: the tracks only turn at full duty-check(GD.min_pct == 50, f"the default floor is 50: {GD.min_pct}")-check(twist_to_tracks(0.02, 0.0, GD) == (50, 50), f"default: a slow forward twist -> 50/50: {twist_to_tracks(0.02, 0.0, GD)}")-check(twist_to_tracks(0.0, 0.2, GD) == (-50, 50), f"default: the planner's slowest pivot (0.2 rad/s) -> -50/+50 before pivot_tracks: {twist_to_tracks(0.0, 0.2, GD)}")-check(twist_to_tracks(-0.02, 0.0, GD) == (-50, -50), "default: slow reverse -> -50/-50 (sign kept)")+check(GD.min_pct == 20, f"the default floor is 20 (measured with the 90:1 motors): {GD.min_pct}")+check(twist_to_tracks(0.02, 0.0, GD) == (20, 20), f"default: a slow forward twist -> 20/20: {twist_to_tracks(0.02, 0.0, GD)}")+check(twist_to_tracks(0.0, 0.2, GD) == (-20, 20), f"default: the planner's slowest pivot (0.2 rad/s) -> -20/+20 before pivot_tracks: {twist_to_tracks(0.0, 0.2, GD)}")+check(twist_to_tracks(-0.02, 0.0, GD) == (-20, -20), "default: slow reverse -> -20/-20 (sign kept)")check(twist_to_tracks(0.0001, 0.0, GD) == (0, 0), "default: a twist that rounds to 0 is still a stop")check(twist_to_tracks(0.06, 0.0, MossGeometry(track_m=0.30, mps_at_limit=0.15, max_pct=25, min_pct=99)) == (25, 25),"a floor above the limit is clamped to the limit")@@ -277,7 +277,7 @@ check(MossGeometry.from_env().min_pct == MossGeometry.from_env().max_pct, "MOSS_os.environ["MOSS_MIN_PCT"] = "-5"check(MossGeometry.from_env().min_pct == 0, "MOSS_MIN_PCT negative -> 0")os.environ.pop("MOSS_MIN_PCT", None)-check(MossGeometry.from_env().min_pct == 50, "MOSS_MIN_PCT unset -> 50 (measured floor 40 on a full battery, 10 of margin)")+check(MossGeometry.from_env().min_pct == 20, "MOSS_MIN_PCT unset -> 20 (both tracks turn from 20 % with the 90:1 motors)")# ── 8. the fake is the firmware: its own refusals ──────────────────────print("8. fake firmware fidelity (Control.h)")diff --git a/moss_dimos/moss_adapter.py b/moss_dimos/moss_adapter.pyindex e83a3a7..76b71d3 100644--- a/moss_dimos/moss_adapter.py+++ b/moss_dimos/moss_adapter.py@@ -286,14 +286,15 @@ def _env_float(name: str, default):@dataclassclass MossGeometry:- """Everything the twist <-> tracks mapping needs. PLACEHOLDERS until- measured on the rover (ticks per revolution included). Each field has an- env override so a flight can be re-scaled without a code change."""+ """Everything the twist <-> tracks mapping needs, measured on the floor+ with the 90:1 motors (tools/moss_floor_probe.py, tools/moss_speed_profile.py;+ the track centre distance is still the CAD's). Each field has an env+ override so a flight can be re-scaled without a code change."""track_m: float = robot_profile.MOSS.track_m # PLACEHOLDER 0.266 (CAD track centre distance), to measure- mps_at_limit: float = 0.45 # PLACEHOLDER: floor speed at 100 % (JGB37-520 178 rpm on a ~60 mm sprocket ~ 0.5 m/s), to measure+ mps_at_limit: float = 0.32 # measured: the plateau at 100 % is 5 420 ticks/s = 0.32 m/s (it takes 3.5 s to get there on the 3 A driver)max_pct: int = FIRMWARE_MAX_PCT- ticks_per_m: float | None = None # None = no encoder calibration yet- min_pct: int = 50 # static-friction floor: a non-zero track command below this percent is raised to it (sign kept). 0 = off. MOSS_MIN_PCT. 50 on MOSS: both tracks turn from 40 % on a full battery, and a half-full one sags under load, so 10 % of margin+ ticks_per_m: float | None = 17190.0 # measured: 1.00 m in 5 s at 100 % = 17 234 / 17 144 ticks (left / right)+ min_pct: int = 20 # static-friction floor: a non-zero track command below this percent is raised to it (sign kept). 0 = off. MOSS_MIN_PCT. 20 with the 90:1 motors: both tracks turn from 20 % (the earlier motors needed 40, plus margin)@classmethoddef from_env(cls) -> "MossGeometry":diff --git a/tools/moss_speed_profile.py b/tools/moss_speed_profile.pynew file mode 100755index 0000000..c263a35--- /dev/null+++ b/tools/moss_speed_profile.py@@ -0,0 +1,108 @@+#!/usr/bin/env python3+"""Speed profile of one straight run (or pivot): drive at a percent for N+seconds, read the encoders all along, print the ticks per half second on+each side, the top rate, the time to reach 90 % of it, and the totals.+Stack down: this opens the motor board itself. Nothing moves without --go.++ tools/moss_speed_profile.py --pct 100 --seconds 5 --go+ tools/moss_speed_profile.py --pct 100 --seconds 3 --pivot --go+"""+from __future__ import annotations++import argparse+import os+import sys+import time+from pathlib import Path++ROOT = Path(__file__).resolve().parent.parent+sys.path.insert(0, str(ROOT))++from moss_dimos.moss_adapter import KEEPALIVE_S, MossBaseAdapter, MossGeometry # noqa: E402++BIN_S = 0.5+++def main() -> int:+ ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)+ ap.add_argument("--pct", type=int, default=100)+ ap.add_argument("--seconds", type=float, default=5.0)+ ap.add_argument("--pivot", action="store_true")+ ap.add_argument("--port", default=None)+ ap.add_argument("--go", action="store_true")+ a = ap.parse_args()+ if not a.go:+ print(f"plan: {'pivot' if a.pivot else 'straight'} at {a.pct} % for {a.seconds:.1f} s; nothing moves without --go")+ return 0+ os.environ["MOSS_PIVOT_PCT"] = "0"+ os.environ["MOSS_BUS_REF_MV"] = "0"+ os.environ["MOSS_ONE_TRACK_PIVOT"] = "0"+ kw = {"geometry": MossGeometry(mps_at_limit=1.0, max_pct=100, min_pct=0)}+ if a.port:+ kw["address"] = a.port+ adapter = MossBaseAdapter(**kw)+ if not adapter.connect():+ print("cannot connect to the firmware")+ return 2+ samples: list[tuple[float, int, int, int | None]] = []+ try:+ t_wait = time.monotonic()+ while (adapter.telemetry is None or "ticks" not in adapter.telemetry) and time.monotonic() - t_wait < 3.0:+ time.sleep(0.05)+ if not adapter.read_enabled() and not adapter.write_enable(True):+ print("cannot arm")+ return 2+ twist = [0.0, 0.0, a.pct / 100.0 / (adapter._geometry.track_m / 2.0)] if a.pivot else [a.pct / 100.0, 0.0, 0.0]+ t0 = time.monotonic()+ last = None+ while time.monotonic() - t0 < a.seconds:+ adapter.write_velocities(twist)+ time.sleep(KEEPALIVE_S)+ tel = adapter.telemetry+ if tel and "ticks" in tel:+ row = (time.monotonic() - t0, int(tel["ticks"][0]), int(tel["ticks"][1]), tel.get("bus"))+ if last is None or row[1:3] != last[1:3]:+ samples.append(row)+ last = row+ adapter.write_stop()+ t_stop = time.monotonic()+ while time.monotonic() - t_stop < 1.5:+ time.sleep(0.1)+ tel = adapter.telemetry+ if tel and "ticks" in tel:+ row = (time.monotonic() - t0, int(tel["ticks"][0]), int(tel["ticks"][1]), tel.get("bus"))+ if row[1:3] != samples[-1][1:3] if samples else True:+ samples.append(row)+ finally:+ try:+ adapter.write_stop()+ adapter.write_enable(False)+ finally:+ adapter.disconnect()+ if len(samples) < 2:+ print("no encoder telemetry during the run")+ return 1+ l0, r0 = samples[0][1], samples[0][2]+ print(f"{'t (s)':>6s} {'left':>7s} {'right':>7s} {'L t/s':>7s} {'R t/s':>7s} {'bus mV':>7s} (ticks since start; rate over the bin)")+ rates = []+ edge = BIN_S+ prev = samples[0]+ for s in samples[1:]:+ if s[0] >= edge or s is samples[-1]:+ dt = s[0] - prev[0]+ lr = (s[1] - prev[1]) / dt if dt > 0 else 0.0+ rr = (s[2] - prev[2]) / dt if dt > 0 else 0.0+ rates.append((s[0], lr, rr))+ print(f"{s[0]:6.2f} {s[1] - l0:7d} {s[2] - r0:7d} {lr:7.0f} {rr:7.0f} {str(s[3] or ''):>7s}")+ prev = s+ edge += BIN_S+ top = max((max(lr, rr) for _, lr, rr in rates), default=0.0)+ t90 = next((t for t, lr, rr in rates if max(lr, rr) >= 0.9 * top), None)+ tot_l, tot_r = samples[-1][1] - l0, samples[-1][2] - r0+ print(f"total {tot_l} / {tot_r} ticks in {a.seconds:.1f} s at {a.pct} %; top rate {top:.0f} ticks/s"+ + (f", reached 90 % of it at {t90:.1f} s" if t90 is not None else ""))+ return 0+++if __name__ == "__main__":+ sys.exit(main())