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

arm sweep: --random (a random walk over the allowed cells) and --clearance (grid steps kept clear of any forbidden point, required above speed 400)

metrox55d3bab5fad3
diff --git a/benches/bench_arm_sweep_cold.py b/benches/bench_arm_sweep_cold.py
index dc145f7..c097ddc 100644
--- a/benches/bench_arm_sweep_cold.py
+++ b/benches/bench_arm_sweep_cold.py
@@ -494,6 +494,27 @@ check(f"a forbidden waypoint slipped in at {k}: refused before it is written, ex
forbidden not in gs and code == 2 and "refuses the move to waypoint" in text and P.parked(lines[-1]["ticks"]),
text.strip().splitlines()[0][:110])
+# ── G. the random walk and the clearance ──
+print("G. the random walk and the clearance")
+pr = W.Planner(GUARD, CAL_RUN, M, TOL).plan(25.0, 3, 0.25, "random")
+rt = [w.ticks for w in pr.waypoints]
+check("random: starts and ends at the drive pose, visits at least three cells",
+ rt[0] == DRIVE and rt[-1] == DRIVE and len({tuple(sorted(t.items())) for t in rt[1:-1]}) >= 3, f"{len(rt)} waypoints")
+check("random: every waypoint is allowed by the guard", all(GUARD.allowed_ticks(t) for t in rt))
+check("random: every move passes the segment check", all(not W.Planner.move_refused(P, rt[i - 1], rt[i]) for i in range(1, len(rt))))
+check("random: two seeds give two different tours",
+ [w.ticks for w in W.Planner(GUARD, CAL_RUN, M, TOL).plan(25.0, 4, 0.25, "random").waypoints] != rt)
+try:
+ pc = W.Planner(GUARD, CAL_RUN, M, TOL, clearance=1)
+ cells_c = [w.cell for w in pc.plan(25.0, 3, 0.25, "random").waypoints if w.cell is not None]
+ check("clearance 1: no waypoint cell touches a forbidden grid point of the table",
+ cells_c and all(not near_forbidden(GUARD, c) for c in cells_c), f"{len(cells_c)} cells")
+except W.Refused as e:
+ check("clearance 1: the drive pose stays plannable", False, str(e)[:100])
+r = run(["--random", "--speed", "1500", "--plan-only", "--table", str(table_path), "--calib", str(calib_path),
+ "--duration", "10", "--log", str(TMP / "g.jsonl")])
+check("speed 1500 without --clearance is refused", r[0] == 1 and "needs --clearance" in r[1], r[1].strip().splitlines()[0][:100])
+
# ── F. the start ──
print("F. the start")
clk = W.VirtualClock()
diff --git a/tools/moss_arm_sweep.py b/tools/moss_arm_sweep.py
index 760892c..c7e77fd 100644
--- a/tools/moss_arm_sweep.py
+++ b/tools/moss_arm_sweep.py
@@ -24,6 +24,10 @@ Options
--tolerance ticks: a waypoint is reached when all five are this close (25)
--timeout seconds per waypoint before it counts as a fault (12)
--seed the random spread's seed: the same inputs give the same tour (0)
+ --random a random walk over the allowed cells (any direction, unvisited cells
+ first, the way home always kept) instead of the boundary tour
+ --clearance grid steps kept clear of any forbidden point (0); a run above speed
+ 400 needs 1 or more, because the table knows poses, not overshoot
--spread how much the random spread weighs against the nearest move (0.25)
--log the JSONL log (arm_sweep.jsonl)
@@ -125,7 +129,8 @@ TICKS_PER_SPEED_UNIT = 1.0 # Goal_Speed: steps per second
TICKS_PER_ACC_UNIT = 100.0 # Acc: 100 steps per second squared per unit
SETTLE_S = 0.05 # per move: one poll and the next write
POLL_S = 0.04
-MAX_SPEED = 400 # the tour hugs the rover: slow or not at all
+SLOW_SPEED = 400 # above it, --clearance 1 or more is required: a servo overshoots its goal at speed
+MAX_SPEED = 2000 # Goal_Speed ceiling this tool accepts (steps per second)
BUS_SILENT_S = 1.0 # no complete reading for this long is a fault
REVERSE_S = 0.4 # the penalty, in seconds, for each joint a move turns back
SEARCH_POPS = 50000 # the way to the nearest frontier cell gives up after this many cells
@@ -243,8 +248,9 @@ def _shifted(arr: np.ndarray, o) -> np.ndarray:
class Planner:
"""The keep-out table as a graph of free cells, and the tour on it."""
- def __init__(self, guard: ArmGuard, calibration: dict, model: SpeedModel, tolerance: int = 25):
+ def __init__(self, guard: ArmGuard, calibration: dict, model: SpeedModel, tolerance: int = 25, clearance: int = 0):
self.guard, self.model, self.tol = guard, model, int(tolerance)
+ self.clearance = int(clearance)
self.joints = tuple(guard.joints)
unknown = [j for j in self.joints if j not in SERVO_IDS]
if unknown:
@@ -271,6 +277,15 @@ class Planner:
raise Refused(f"the drive pose {self._fmt(self.drive)} is not allowed: {guard.why(q_drive)}")
occ = guard.occ
+ for _ in range(self.clearance): # clearance: every grid point within that many steps of a forbidden one counts as forbidden
+ grown = occ.copy()
+ for k in range(occ.ndim):
+ lo = tuple(slice(0, -1) if i == k else slice(None) for i in range(occ.ndim))
+ hi = tuple(slice(1, None) if i == k else slice(None) for i in range(occ.ndim))
+ grown[lo] |= occ[hi]
+ grown[hi] |= occ[lo]
+ occ = grown
+ self.occ_used = occ
d = occ.ndim
cells, near = ~occ, occ.copy()
for k in range(d): # near: forbidden, or one grid step from a forbidden point along one joint
@@ -455,9 +470,14 @@ class Planner:
heapq.heappush(heap, (dm, m))
return []
- def plan(self, duration: float, seed: int = 0, spread: float = 0.25) -> Plan:
+ def plan(self, duration: float, seed: int = 0, spread: float = 0.25, mode: str = "frontier") -> Plan:
+ """mode "frontier": the tour hugs the forbidden region (the default);
+ mode "random": a random walk over the allowed cells, any direction,
+ unvisited cells first, the time to come home always kept."""
budget = float(duration)
rng = np.random.default_rng(seed)
+ if mode == "random":
+ return self._plan_random(budget, rng)
visited = np.zeros(self.N, dtype=bool)
cells = [] # the tour's cells, in order
t, prev_o = 0.0, np.zeros(len(self.joints), dtype=np.int64)
@@ -517,6 +537,55 @@ class Planner:
raise Refused(f"the planned move to waypoint {i} fails its check ({why})")
return plan
+ def _plan_random(self, budget: float, rng) -> Plan:
+ visited = np.zeros(self.N, dtype=bool)
+ cells, t, cur = [], 0.0, None
+ ok_f = self.cell_ok.ravel()
+ while True:
+ if cur is None:
+ nbr = np.array(sorted(self.drive_next), dtype=np.int64)
+ if not len(nbr):
+ break
+ costs = np.array([self.to_drive_s(int(n)) for n in nbr])
+ ok = np.ones(len(nbr), dtype=bool)
+ else:
+ nbr = np.clip(cur + self.shift, 0, self.N - 1)
+ costs, ok = self.cost, self.edge[:, cur]
+ m = ok & ok_f[nbr] & (t + costs + self.dist[nbr] <= budget + 1e-9)
+ if not m.any():
+ break
+ idx = np.flatnonzero(m)
+ fresh = idx[~visited[nbr[idx]]]
+ pool = fresh if len(fresh) else idx
+ k = int(pool[int(rng.integers(len(pool)))])
+ n = int(nbr[k])
+ t += float(costs[k])
+ visited[n] = True
+ cells.append(n)
+ cur = n
+ if len(set(cells)) < 3:
+ raise Refused(f"the random walk finds fewer than three allowed cells in reach of the drive pose "
+ f"in {budget:g} s at speed {self.model.speed}")
+ cells += self.home_cells(cells[-1])
+ wps = [Waypoint(dict(self.drive), "drive")]
+ for n in cells:
+ if n is None:
+ w = Waypoint(dict(self.drive), "drive")
+ else:
+ w = Waypoint(self.centre_ticks(n), "frontier" if self.frontier_f[n] else "pass",
+ tuple(int(x) for x in np.unravel_index(n, self.cshape)))
+ w.move_s = self.model.move_time(wps[-1].ticks, w.ticks)
+ wps.append(w)
+ for w in wps:
+ if w.kind == "frontier":
+ self._skirt(w)
+ plan = Plan(wps, budget, self.model, self.tol, 0)
+ for i in range(1, len(wps)):
+ why = self.move_refused(wps[i - 1].ticks, wps[i].ticks)
+ if why:
+ raise Refused(f"the planned move to waypoint {i} fails its check ({why})")
+ return plan
+
def _skirt(self, w: Waypoint) -> None:
"""The forbidden grid point nearest the waypoint (grid steps, the most
off joint first), why() there, and the part it names."""
@@ -967,6 +1036,8 @@ def main(argv=None, bus_hook=None, say=print) -> int:
ap.add_argument("--timeout", type=float, default=12.0)
ap.add_argument("--seed", type=int, default=0)
ap.add_argument("--spread", type=float, default=0.25)
+ ap.add_argument("--random", action="store_true", help="a random walk over the allowed cells instead of the boundary tour")
+ ap.add_argument("--clearance", type=int, default=0, help="grid steps kept clear of any forbidden point (0); at least 1 above speed 400")
ap.add_argument("--log", default="arm_sweep.jsonl")
ap.add_argument("--dry-run", action="store_true", help="a fake bus instead of the arm")
ap.add_argument("--fast", action="store_true", help="with --dry-run: a virtual clock")
@@ -975,13 +1046,19 @@ def main(argv=None, bus_hook=None, say=print) -> int:
if not 1 <= a.speed <= MAX_SPEED:
say(f"refused: --speed {a.speed}: 1 to {MAX_SPEED}")
return 1
+ if a.speed > SLOW_SPEED and a.clearance < 1:
+ say(f"refused: --speed {a.speed} above {SLOW_SPEED} needs --clearance 1 or more (overshoot at speed is not in the table)")
+ return 1
+ if a.clearance < 0:
+ say("refused: --clearance below 0")
+ return 1
if not 1 <= a.acc <= 254:
say(f"refused: --acc {a.acc}: 1 to 254")
return 1
try:
guard, calibration = load(a.table, a.calib)
- p = Planner(guard, calibration, SpeedModel(a.speed, a.acc, a.tolerance), a.tolerance)
- plan = p.plan(a.duration, a.seed, a.spread)
+ p = Planner(guard, calibration, SpeedModel(a.speed, a.acc, a.tolerance), a.tolerance, a.clearance)
+ plan = p.plan(a.duration, a.seed, a.spread, "random" if a.random else "frontier")
except Refused as e:
say(f"refused: {e}")
return 1