summaryrefslogtreecommitdiff
path: root/RobotFirmware/opmodes/team205_mech.h
diff options
context:
space:
mode:
authorBEAST <macbook@Macbooks-MacBook-Pro-2.local>2026-11-17 09:41:29 +0530
committerBEAST <macbook@Macbooks-MacBook-Pro-2.local>2026-11-17 09:41:29 +0530
commit0ec829abd912a765c65889f8e39efc4ae9773160 (patch)
tree6c825fbac2a140bc65c3de50dde25f84be975a4b /RobotFirmware/opmodes/team205_mech.h
parentd358423b15bbd89ca8b34bb4ffc787811774105a (diff)
parent6cf74a023fba53afc80cf2ca15c7d6a60ea10cba (diff)
Merge branch 'main' of https://github.com/gigagamerchaddhruv-spec/nrl-pixel-pokersHEADmain
Diffstat (limited to 'RobotFirmware/opmodes/team205_mech.h')
-rw-r--r--RobotFirmware/opmodes/team205_mech.h600
1 files changed, 399 insertions, 201 deletions
diff --git a/RobotFirmware/opmodes/team205_mech.h b/RobotFirmware/opmodes/team205_mech.h
index 6b6d279..3959fc5 100644
--- a/RobotFirmware/opmodes/team205_mech.h
+++ b/RobotFirmware/opmodes/team205_mech.h
@@ -1,274 +1,472 @@
#pragma once
+
#include "NRL.h"
#include <Preferences.h>
-#include <esp_system.h>
+#include <cmath>
+#include <cstring>
// ============================================================================
-// TEAM 205 (PIXEL POKER) - SHARED MECHANISM & POWER LIBRARY (team205_mech.h)
-// Supports: drivemode.cpp, normaldrive.cpp, tiltsteering.cpp, pixelpoker_auto.cpp
+// TEAM 205 (PIXEL POKERS) — SHARED MECHANISM HELPERS
+// ============================================================================
+//
+// This is a HELPER HEADER, not an OpMode. It contains no REGISTER_OPMODE
+// and adds nothing to the Controller's OpMode list. Every OpMode that needs
+// it just adds one #include line.
+//
+//
+// WHY THIS FILE EXISTS
+// ---------------------------------------------------------------------------
+// HexaServo::setPosition() does TWO things at once:
+//
+// 1. It sets the TARGET angle the servo should reach.
+// 2. If a ramp rate is configured, it advances the servo ONE ramp step
+// toward that target, by (elapsed_ms / 1000) * rate degrees.
+//
+// The ramp ONLY advances when setPosition() is called. The driver's 250 ms
+// heartbeat (HexaServo::tick) re-writes the PWM duty for the CURRENT angle
+// but never moves the mechanism.
+//
+// Four consequences, ALL of which bit this robot on the field:
+//
+// (a) Calling setPosition() ONCE with a new angle moves the servo by ONE
+// ramp step and then it stops there. It does NOT travel to the target.
+// A payload gate toggled on a button edge therefore moved ~2 degrees
+// and stopped — the gate "never opens".
+//
+// (b) A mechanism only travels if setPosition() is called REPEATEDLY.
+// The ramp IS the motion.
+//
+// (c) getPosition() reports the driver's RAMP position, not the real servo
+// angle. The driver has no idea that the jaws are jammed on a piece,
+// so it happily reports that a straining claw has "reached" 168.
+//
+// (d) The PWM duty is refreshed every tick when setPosition() is called
+// every tick. A servo that is only refreshed every 250 ms can drop
+// its pulses if the ESP32 stalls or the battery rail sags — which is
+// exactly what "the servo just stops working" looks like.
+//
+// ServoAxis makes one rule impossible to get wrong:
+//
+// Every mechanism has exactly ONE owner. The owner sets the TARGET.
+// apply() is called EVERY tick and pushes the target to the driver.
+//
+// The target lives in the axis, not in the driver, so an action can move a
+// mechanism while loop() keeps re-asserting the target every tick without
+// the two fighting each other.
// ============================================================================
-class ServoAxis {
-public:
- void bind(HexaServo& driver, float homeDeg, float poseADeg, float poseBDeg) {
- drv = &driver;
- homeAngle = homeDeg;
- minAngle = fminf(poseADeg, poseBDeg);
- maxAngle = fmaxf(poseADeg, poseBDeg);
- targetDeg = homeDeg;
- lastMoveMs = millis();
+
+// ============================================================================
+// ServoAxis — one mechanism on one servo
+// ============================================================================
+struct ServoAxis {
+ HexaServo* servo = nullptr;
+
+ float target = 0.0f; // the travel pose we are heading for
+ float lo = 0.0f; // travel limits
+ float hi = 180.0f;
+
+ // Optional PRESS override. A servo only applies force while it is still
+ // trying to REACH its commanded angle. Commanding exactly the closed
+ // angle means the servo arrives, stops pushing, and holds nothing — the
+ // piece is then held by friction alone and falls out the moment the arm
+ // moves. So while gripping, the servo is commanded PAST the closed angle:
+ // the piece blocks the jaws short of it, the servo never arrives, and it
+ // keeps straining and pressing.
+ float pressAngle = -1.0f; // < 0 = press disabled
+ bool pressing = false;
+
+ // Attach this axis to a driver. `start` is the mechanism's home pose.
+ void bind(HexaServo& s, float start, float travelLo, float travelHi) {
+ servo = &s;
+ lo = travelLo;
+ hi = travelHi;
+ target = constrain(start, travelLo, travelHi);
+ pressing = false;
}
- void command(float deg, bool allowOvertravel = false) {
- if (!allowOvertravel) {
- deg = constrain(deg, minAngle, maxAngle);
- } else {
- deg = constrain(deg, 0.0f, 180.0f);
- }
+ // Set the travel target. This does NOT move the servo — apply() does.
+ // While `pressing` the press angle wins, so call stopPressing() first if
+ // the mechanism needs to travel.
+ void command(float angle) {
+ target = constrain(angle, lo, hi);
+ }
- if (fabsf(deg - targetDeg) > 0.25f) {
- targetDeg = deg;
- lastMoveMs = millis();
- }
+ // Freeze where the mechanism is RIGHT NOW. This is what "release the
+ // trigger and it stops" actually requires: the ramp chases a target, so
+ // the target has to become the current position.
+ //
+ // A straining servo cannot be frozen — it is held by the piece, and the
+ // driver does not know where the jaws really are.
+ void hold() {
+ if (pressing) return;
+ target = actual();
}
- void press(float gripDeg) {
- command(gripDeg, true);
+ // Strain past the jaws and press.
+ void press(float angle) {
+ pressAngle = angle;
+ pressing = true;
}
+ // Stop pressing. The mechanism goes back to holding `target`.
void stopPressing() {
- targetDeg = constrain(targetDeg, minAngle, maxAngle);
+ pressing = false;
}
- void apply() {
- if (drv != nullptr) {
- drv->setPosition(targetDeg);
- }
- }
+ bool isPressing() const { return pressing; }
- float actual() const {
- return (drv != nullptr) ? drv->getPosition() : targetDeg;
+ // The angle actually sent to the servo.
+ float commanded() const {
+ return (pressing && pressAngle >= 0.0f) ? pressAngle : target;
}
- float target() const {
- return targetDeg;
+ // Push the commanded angle to the driver. Call this EVERY tick — this is
+ // what actually moves the mechanism AND what keeps its PWM alive.
+ void apply() {
+ if (servo == nullptr) return;
+ servo->setPosition(commanded());
}
- bool travelArrived(float tolDeg = 0.5f) const {
- return fabsf(actual() - targetDeg) <= tolDeg;
- }
+ // Where the driver thinks the servo is (the ramp position). When the servo
+ // is straining against a piece this runs AHEAD of the real jaw angle.
+ float actual() const { return servo ? servo->getPosition() : commanded(); }
- bool isSettled(float tolDeg = 1.0f) const {
- return travelArrived(tolDeg);
+ // True once the driver has been ramped to the commanded angle.
+ bool arrived(float tol = 1.0f) const {
+ return fabsf(actual() - commanded()) <= tol;
}
- uint32_t msSinceCommand() const {
- return millis() - lastMoveMs;
+ // True once the driver has been ramped to the TRAVEL target (ignoring any
+ // press override).
+ bool travelArrived(float tol = 1.0f) const {
+ return fabsf(actual() - target) <= tol;
}
-private:
- HexaServo* drv = nullptr;
- float homeAngle = 90.0f;
- float minAngle = 0.0f;
- float maxAngle = 180.0f;
- float targetDeg = 90.0f;
- uint32_t lastMoveMs = 0;
+ // True while the mechanism is still travelling toward its commanded
+ // angle. A straining (pressing) servo is NOT travelling — it is jammed
+ // against a piece — so it never blocks another mechanism.
+ bool travelling(float tol = 0.75f) const {
+ return fabsf(actual() - commanded()) > tol;
+ }
};
-// Non-blocking motion arbiter with 650ms auto-release timeout
-static ServoAxis* activeMotionOwner = nullptr;
-static uint32_t motionLockStartMs = 0;
-static constexpr uint32_t MOTION_LOCK_TIMEOUT_MS = 650;
+// ============================================================================
+// MotionArbiter — never ramp two servos at the same time
+// ============================================================================
+// "The servos share the battery rail. There is NO BEC and no separate servo
+// regulator." (HexaServo.h)
+//
+// Two servos ramping at once double the current the rail must supply. On a
+// loaded rail that sag is what makes one servo stop responding.
+//
+// The arbiter serialises mechanism travel: while one mechanism is moving,
+// another mechanism's request is DEFERRED (the caller simply keeps its old
+// target and asks again next tick). Nothing is dropped — the deferred move
+// starts as soon as the rail is quiet again.
+//
+// Pressing (holding a strain against a piece) is NOT travel: a straining
+// servo is not moving, so it never blocks anybody.
+//
+// The owner is a function-local static, so every OpMode that includes this
+// header shares one arbiter per program — the arm and the claw arbitrate
+// against each other without any extra wiring.
inline bool motionRequest(ServoAxis& axis) {
- uint32_t now = millis();
-
- if (activeMotionOwner == &axis) {
- return true;
+ static ServoAxis* owner = nullptr;
+ if (owner != nullptr && owner != &axis && owner->travelling()) {
+ return false; // rail is busy — defer this move
}
-
- if (activeMotionOwner == nullptr ||
- activeMotionOwner->travelArrived(1.0f) ||
- (now - motionLockStartMs) >= MOTION_LOCK_TIMEOUT_MS) {
- activeMotionOwner = &axis;
- motionLockStartMs = now;
- return true;
- }
-
- return false;
+ owner = &axis;
+ return true;
}
+// Release the rail. Call when a mechanism is idle.
inline void motionRelease(ServoAxis& axis) {
- if (activeMotionOwner == &axis) {
- activeMotionOwner = nullptr;
- }
+ static ServoAxis* owner = nullptr;
+ if (owner == &axis) owner = nullptr;
}
-enum class ClawDir { NONE, EXPAND, CONTRACT };
-class ClawControl {
-public:
- ClawDir dir = ClawDir::NONE;
-
- void bind(ServoAxis& clawAxisRef, float openDeg, float closedDeg, float gripDeg) {
- axis = &clawAxisRef;
- openAngle = openDeg;
- closeAngle = closedDeg;
- gripAngle = gripDeg;
- gripping = false;
- dir = ClawDir::NONE;
- }
+// ============================================================================
+// PowerGuard — watch the rail so a brownout never surprises you
+// ============================================================================
+// A brownout (the ESP32 resetting because the battery rail sagged) is the
+// classic explanation for "the servo just stops working" and for "the claw
+// opens by itself": init() runs again and puts the claw back to its open
+// angle.
+//
+// PowerGuard does two things:
+//
+// 1. DERATE. When the pack sags, the drive output is scaled back so the
+// robot crawls instead of pulling the rail under. This is what stops
+// "drive while gripping" from resetting the board.
+//
+// 2. REPORT. The boot counter and reset reason go to the Controller, so a
+// brownout is visible instead of mysterious.
+//
+// THRESHOLDS: measure your own pack. Run the Power Test OpMode, watch the
+// voltage with the servos moving and the motors driving, and set these two
+// numbers either side of what you saw.
+struct PowerGuard {
+ // Start backing off below this pack voltage (V).
+ float sagVoltage = 9.80f;
+ // Resume full power above this pack voltage (V).
+ float recoverVoltage = 10.60f;
+ // Drive output scale at and below sagVoltage.
+ float minDerate = 0.25f;
+
+ float minVoltage = 99.0f; // lowest pack voltage seen this run
+ float peakServoA = 0.0f; // peak servo-rail current seen this run
+ bool protecting = false;
+
+ // Call once per tick. Returns the drive scale factor to use this tick.
+ float update() {
+ float v = power.getBatteryVoltage();
+ float a = power.getServoCurrent();
- void update(ClawDir desired) {
- dir = desired;
- if (axis == nullptr) return;
+ if (v > 0.0f && v < minVoltage) minVoltage = v;
+ if (a > peakServoA) peakServoA = a;
- if (desired == ClawDir::EXPAND) {
- gripping = false;
- if (motionRequest(*axis)) {
- axis->command(openAngle);
- }
- } else if (desired == ClawDir::CONTRACT) {
- if (motionRequest(*axis)) {
- axis->command(closeAngle);
- }
- } else {
- if (!gripping && prevDir != ClawDir::NONE) {
- axis->command(axis->actual());
- }
- motionRelease(*axis);
+ if (v <= 0.0f) return 1.0f; // sensor not ready — do not interfere
+
+ if (!protecting && v < sagVoltage) {
+ protecting = true;
+ } else if (protecting && v > recoverVoltage) {
+ protecting = false;
}
- prevDir = desired;
- }
+ if (!protecting) return 1.0f;
- void tryClose() {
- if (axis == nullptr) return;
- gripping = false;
- axis->command(closeAngle);
+ // Linear ramp from minDerate at sagVoltage up to 1.0 at recoverVoltage.
+ float span = recoverVoltage - sagVoltage;
+ if (span <= 0.0f) return minDerate;
+ float t = constrain((v - sagVoltage) / span, 0.0f, 1.0f);
+ return minDerate + t * (1.0f - minDerate);
}
- void tryOpen() {
- if (axis == nullptr) return;
- gripping = false;
- axis->command(openAngle);
- }
+ // True while the pack is sagging and the drive is being backed off.
+ bool isProtecting() const { return protecting; }
+};
- void engageNow() {
- if (axis == nullptr) return;
- gripping = true;
- axis->press(gripAngle);
- }
- void releaseNow() {
- gripping = false;
- if (axis != nullptr) {
- axis->stopPressing();
- motionRelease(*axis);
- }
- }
+// ============================================================================
+// Reset-reason helpers. Declared up here because BrownoutBeacon uses them.
+// ============================================================================
- bool isGripping() const {
- return gripping;
- }
+// Number of times the MCU has booted since power-up. If this climbs while
+// you are driving, the board is resetting — that is a brownout, not firmware.
+inline uint32_t team205BootCount() { return nrlBootCount(); }
-private:
- ServoAxis* axis = nullptr;
- ClawDir prevDir = ClawDir::NONE;
- float openAngle = 125.0f;
- float closeAngle = 155.0f;
- float gripAngle = 160.5f;
- bool gripping = false;
-};
+// Short token for the last reset: "BROWNOUT", "POWERON", "TASK_WDT", ...
+inline const char* team205ResetReason() { return nrlLastResetReasonShort(); }
-class PowerGuard {
-public:
- float update() {
- if (power.isBatteryLow()) {
- lowSagActive = true;
- scale = fmaxf(0.65f, scale - 0.02f);
- } else {
- lowSagActive = false;
- scale = fminf(1.00f, scale + 0.01f);
- }
- return scale;
- }
+// True if the last reset was a brownout.
+inline bool team205LastResetWasBrownout() {
+ const char* r = team205ResetReason();
+ return (r != nullptr) && (strcmp(r, "BROWNOUT") == 0);
+}
- bool isSagging() const { return lowSagActive; }
- float currentScale() const { return scale; }
-private:
- float scale = 1.0f;
- bool lowSagActive = false;
-};
+// ============================================================================
+// BrownoutBeacon — make a reset visible without a laptop
+// ============================================================================
+// It OWNS the user LED while active — skip your normal LED logic when
+// service() returns true.
+struct BrownoutBeacon {
+ bool active = false;
+ uint32_t untilMs = 0;
-class BrownoutBeacon {
-public:
+ // Call once in init().
void arm() {
- wasBrownout = (esp_reset_reason() == ESP_RST_BROWNOUT);
- if (wasBrownout) {
- Preferences prefs;
- if (prefs.begin("t205_pwr", false)) {
- bootCount = prefs.getUInt("bo_cnt", 0) + 1;
- prefs.putUInt("bo_cnt", bootCount);
- prefs.end();
- }
- beaconEndMs = millis() + 3000;
- } else {
- Preferences prefs;
- if (prefs.begin("t205_pwr", true)) {
- bootCount = prefs.getUInt("bo_cnt", 0);
- prefs.end();
- }
- beaconEndMs = 0;
- }
+ if (!team205LastResetWasBrownout()) return;
+ active = true;
+ untilMs = millis() + 2000;
}
+ // Call every tick in loop(). Returns true while it owns the LED.
bool service() {
- if (!wasBrownout || millis() >= beaconEndMs) {
- return false;
- }
- bool on = ((millis() / 150) % 2) == 0;
- if (on) {
- userLed.setSolid(255, 0, 0);
- } else {
+ if (!active) return false;
+ if ((int32_t)(millis() - untilMs) >= 0) {
+ active = false;
userLed.setOff();
+ return false;
}
+ userLed.setSolid(255, 0, 0);
return true;
}
+};
- uint32_t brownoutCount() const { return bootCount; }
+// Publish the power / brownout state to the Controller. Call from loop().
+inline void team205ReportPower(NRLTelemetry& tel, const PowerGuard& g) {
+ tel.addData("Boot #", (float)team205BootCount());
+ tel.addData("Reset", team205ResetReason());
+ tel.addData("Servo A", power.getServoCurrent());
+ tel.addData("Min V", g.minVoltage);
+ tel.addData("Peak A", g.peakServoA);
+}
-private:
- bool wasBrownout = false;
- uint32_t beaconEndMs = 0;
- uint32_t bootCount = 0;
-};
-class GripMemory {
-public:
+// ============================================================================
+// GripMemory — survive a brownout reset
+// ============================================================================
+// If the board browns out mid-match, RAM is gone and the claw would be
+// re-initialised OPEN — the piece drops on the floor and the driver has no
+// idea why.
+//
+// Storing the grip state in NVS (flash) and re-applying it on the next boot
+// means a brownout costs you a second of driving, not the piece.
+//
+// Writes happen only when the grip state CHANGES (a button press), so the
+// flash wear is negligible.
+struct GripMemory {
bool engaged = false;
+ // Call in init(). Restores the last known grip state.
void begin() {
- Preferences prefs;
- if (prefs.begin("t205_grip", true)) {
- engaged = prefs.getBool("held", false);
- prefs.end();
+ Preferences p;
+ if (!p.begin("team205", false)) return;
+ engaged = p.getBool("grip", false);
+ p.end();
+ }
+
+ // Call when the grip state changes.
+ void set(bool on) {
+ if (engaged == on) return;
+ engaged = on;
+ Preferences p;
+ if (!p.begin("team205", false)) return;
+ p.putBool("grip", on);
+ p.end();
+ }
+};
+
+
+// ============================================================================
+// ClawControl — the claw's state machine
+// ============================================================================
+// The claw is the one mechanism with real state (open / closing / closed /
+// gripping), and getting that state wrong is what makes it "open by itself".
+// It lives here so every OpMode gets the same correct behaviour.
+//
+// THE STATES
+// ---------------------------------------------------------------------------
+// NONE jaws hold wherever they are. The TARGET is frozen to the
+// current position, so the servo stops where it is instead of
+// creeping on toward its last commanded pose.
+// EXPAND the jaws open. The grip is released first — but only once the
+// rail is free, so the claw never relaxes-and-recloses while
+// another mechanism is moving.
+// CONTRACT the jaws close. The grip engages only after the jaws have
+// REALLY arrived closed.
+// GRIPPING the servo is commanded PAST the closed angle so it strains
+// against the piece and presses. The grip is a LATCH: it
+// survives releasing the trigger, and it is re-asserted every
+// tick so the squeeze never fades.
+//
+// Why "really arrived closed" matters: the driver reports its own RAMP
+// position, and it has no idea the jaws are blocked by a piece. If the grip
+// engaged on a half-open jaw the servo would strain against nothing.
+// ============================================================================
+
+// Which way the jaws are being driven.
+enum class ClawDir { NONE, EXPAND, CONTRACT };
+
+struct ClawControl {
+ ServoAxis* axis = nullptr;
+
+ float openAngle = 125.0f;
+ float closedAngle = 155.0f;
+ float gripAngle = 168.0f;
+
+ ClawDir dir = ClawDir::NONE;
+ bool gripping = false; // latched: the servo is straining on a piece
+ bool closing = false; // we have commanded closed and are waiting
+
+ void bind(ServoAxis& a, float open, float closed, float grip) {
+ axis = &a;
+ openAngle = open;
+ closedAngle = closed;
+ gripAngle = grip;
+ dir = ClawDir::NONE;
+ gripping = false;
+ closing = false;
+ }
+
+ bool isGripping() const { return gripping; }
+ bool isClosing() const { return closing; }
+
+ // --- transitions ---------------------------------------------------
+
+ // Start closing. Returns false if the rail is busy — the caller should
+ // ask again next tick. Nothing is lost by waiting.
+ bool tryClose() {
+ if (!motionRequest(*axis)) return false;
+ if (!gripping) {
+ axis->command(closedAngle);
+ closing = true;
}
+ return true;
}
- void set(bool state) {
- if (engaged == state) return;
- engaged = state;
- Preferences prefs;
- if (prefs.begin("t205_grip", false)) {
- prefs.putBool("held", state);
- prefs.end();
+ // Start opening. Returns false if the rail is busy.
+ bool tryOpen() {
+ if (!motionRequest(*axis)) return false;
+ // Let go FIRST, then open. If we opened first while still pressing,
+ // the servo would relax back toward the closed angle and re-clamp.
+ if (gripping) {
+ gripping = false;
+ axis->stopPressing();
}
+ closing = false;
+ axis->command(openAngle);
+ return true;
}
-};
-template <typename TelemetryT>
-inline void team205ReportPower(TelemetryT& telem, const PowerGuard& pg) {
- telem.addData("Rail", pg.isSagging() ? "LOW SAG" : "OK");
-}
+ // Engage the grip now. Used by the auto-grab macro and by BTN_A.
+ void engageNow() {
+ gripping = true;
+ closing = false;
+ axis->press(gripAngle);
+ }
+
+ // Release the grip now.
+ void releaseNow() {
+ gripping = false;
+ closing = false;
+ axis->stopPressing();
+ }
+
+ // --- per-tick update -----------------------------------------------
+
+ // Call every tick with the direction the driver is asking for.
+ void update(ClawDir requested) {
+ dir = requested;
+
+ if (requested == ClawDir::EXPAND) {
+ // Deferring is safe: the claw simply holds its current command
+ // (still gripping, still at the grip angle) until the rail frees
+ // up. The piece is NOT dropped.
+ tryOpen();
+
+ } else if (requested == ClawDir::CONTRACT) {
+ tryClose();
+
+ // Engage the grip only once the jaws have REALLY arrived closed.
+ // The driver reports its ramp position, so this is the best
+ // available signal that the jaws are shut.
+ if (closing && axis->travelArrived(1.0f)) {
+ engageNow();
+ }
+
+ } else {
+ // No trigger engaged: the jaws stop where they are. Because the
+ // servo ramps toward a TARGET, "stop" means the target must
+ // become the current position — otherwise the claw keeps creeping
+ // toward its last commanded pose.
+ closing = false;
+ motionRelease(*axis);
+ if (!gripping) axis->hold();
+ }
+ }
+};