Skip to content

Fix disable_servos() so servos actually go limp - #14

Merged
twells46 merged 3 commits into
masterfrom
oscar/servo-power-off
Oct 28, 2026
Merged

twells46 merged 3 commits into
masterfrom
oscar/servo-power-off

Conversation

@objorkman

@objorkman objorkman commented Oct 13, 2026 •

Copy link
Copy Markdown
Collaborator

What and why

disable_servo() and disable_servos() only stop a port's control pulses (the firmware sets the pulse width to 0). The 6 V servo rail stays powered. Analog servos go limp without pulses, but digital servos, including the ones we use, keep holding their last position. So disable_servos() had no visible effect on them, unlike off() for motors.

The board can already cut servo power. In docs/Schematics/, PE10 (SRV_DISABLE in wallaby_r2.h) connects through R66 (0 Ω) to the enable pin of U72, the AP65502 regulator that makes VCC_6V0. That rail feeds only the four servo ports, through the fuses U68–U71. R52 (100 kΩ) pulls the enable up to the battery. The firmware never configured PE10, so the rail was always on.

Changes

  • Firmware/src/wallaby_init.c: configure PE10 as an open-drain output, driven low, at the very start of init(). Open-drain because the enable is pulled up to the battery: low turns servo power off, and released (the pin's state until now) turns it on. Servos boot disabled, so power starts off. It runs before init180MHz() waits for the crystal, because the rail is on from reset until the pin is driven.
  • Firmware/src/wallaby_servo.c / .h: add update_servo_power(). It cuts power 40 ms after all four servo disable bits (4–7 of REG_RW_MOT_SRV_ALLSTOP) are set, and restores it as soon as any is cleared. The 40 ms wait is there because the timer ISRs zero a port's pulse at its next 20 ms update, and compare preload applies that one period later. Cutting power sooner would drive the last pulse into an unpowered servo.
  • Firmware/src/main.c: call update_servo_power() once per loop pass. It runs in the main loop rather than the servo timer interrupts because init() starts those interrupts before init_tx_buffer() sets the disable bits, so an interrupt would briefly see every servo as enabled and switch the rail on at boot.
  • AGENTS.md: add PE10 to the peripheral table, and the servo power check to the main-loop description.

The register map is unchanged, so no libwallaby change or SPI version bump is needed: disable_servos() already sets the bits the firmware now checks.

Behavior changes

  • disable_servos(), or disabling all four servos one by one, cuts servo power 40 ms later, and the servos go limp.
  • When a program ends, the servos go limp. libwallaby's cleanup calls disable_servos(), including from shut_down_in(), so an arm holding a load drops at the end of a run. This is intended, and judges are aware of it.
  • Disabling the last enabled servo also cuts power to servos disabled earlier that were still holding.
  • Servo power is off at boot until a program enables a servo. Servos already got no pulses at boot. Anything powered from a servo port's 6 V pin is also unpowered whenever all four servos are disabled.
  • Disabling fewer than four servos works as before: those ports stop getting pulses, and the rail stays on. One regulator feeds every port, so a single servo can't be made limp on its own.
  • When power comes back, all four servos move to their last commanded positions at once.

Verification

  • Clean Docker build (with build/ deleted first) passes. The changed C files compile with no warnings.
  • Disassembly: init() enables the GPIOE clock, writes bit 10 of BSRRH and calls GPIO_Init before init180MHz(). update_servo_power() reads register 61, masks 0xF0, and writes BSRRH only once usCount - since >= 40000, or writes BSRRL right away when any servo is enabled.
  • Each file keeps its line endings (CRLF in Firmware/src/wallaby_init.c, Firmware/src/wallaby_servo.c and Firmware/include/wallaby_servo.h, LF in main.c).
  • Traced PE10 → R66 → U72 enable, and VCC_6V0 → U68–U71 → servo connectors, in the Eagle schematic. R66 is on the rev 4 BOM.
  • Not yet tested on hardware. To test on a Wombat:
    1. After boot, with no program running: the servo port power pin reads about 0 V and servos turn freely by hand. Watch for a twitch at power-up or reset.
    2. enable_servos() and a position command: the servos get power (about 6 V) and move.
    3. disable_servos(): all four go limp. Enabling them again works, with no brownout or reset when four loaded servos power up together.
    4. disable_servo(0) only: the other three keep working, and servo 0 behaves as it does today.
    5. Stopping a program, or shut_down_in(), goes limp.
    6. Quick regression: motors and sensors behave as before.

disable_servo() only stops a port's control pulses; the 6 V servo rail
stays on. Analog servos go limp without pulses, but digital servos,
including the ones we use, keep holding their last position, so
disable_servos() had no visible effect on them.

PE10 drives the enable of U72, the one regulator for all four servo
ports, but the firmware never configured it, so R52 kept the rail on.
PE10 is now an open-drain output, and the main loop pulls it low when
all four servo disable bits in REG_RW_MOT_SRV_ALLSTOP are set and
releases it otherwise. Disabling a single servo still only stops its
pulses, since the ports share the regulator.

Servo power is now off at boot until a program enables a servo, and
when a program exits, because libwallaby's cleanup disables all servos.
PE10 is now driven low at the top of init(), before init180MHz() waits
for the crystal. The servo rail is on from reset until then, so this
shortens the on/off blip after every reset.

Servo power is now cut 40 ms after all four servos are disabled. The
timer ISRs zero a port's pulse at its next 20 ms update, and compare
preload applies that one period later, so cutting power within 1 ms
drove up to one more pulse into an unpowered servo.

AGENTS.md now lists the servo power check in the main loop's window.
@objorkman
objorkman marked this pull request as ready for review October 13, 2026 06:26
@objorkman
objorkman requested a review from twells46 October 13, 2026 06:26
Resolve the main.c conflict with PR #13: call update_servo_power() after
spi2_dma_check_sync().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@twells46

Copy link
Copy Markdown
Contributor

I tested this on a Wombat. The firmware change does what it says, and disabled servos now go limp. I found four things worth knowing before merging. Two are libwallaby bugs that change what this PR delivers, and two are behaviour changes worth documenting.

Setup

Master now has #13, which adds a call at the same spot in main.c, so I tested this PR merged onto master b2ff45e with update_servo_power() placed after spi2_dma_check_sync(). A second build added diagnostics: the PE10 pin state and counts of rail on/off transitions, published in unused reply bytes. I drove it with raw spidev frames shaped like libwallaby's, with botui stopped, and with programs using the deployed libkipr 1.2.4. Servos were on all four ports, and for the battery tests motors were on ports 0, 1 and 3.

What works

Test Result
Boot state, 5 resets PE10 open-drain output, driven low, rail off, every time
Rail on after enabling any servo within 1.1 ms
Rail off after disabling all four 40.0–41.3 ms as measured by the firmware, 50 of 50
Each of the 15 partial patterns, and the motor bits rail never cut
Re-enable 5, 20 or 35 ms into the 40 ms window rail never cut
20,000 healthy frames, and link slips, with all servos disabled rail never on
STM32 reset while the rail is on rail off in the first frame after the reset
disable_servos() with a servo holding a position servo goes limp (on master it keeps holding)
#13's healthy soak, slow clocks and boot while polling, on the merge pass

What to know

1. With libkipr 1.2.4, servos don't go limp when a program ends. Return, exit(1), SIGINT, SIGTERM and shut_down_in() all leave register 61 at 0x00, so the servos stay enabled and the rail stays on. Master behaves the same, so this predates the PR, but it means the "servos go limp when a program ends" part needs a libwallaby fix too.

In gdb, register_servo_cleanup() (module/servo/src/init.cpp, constructor(2)) does call cleanup_add(). At exit, though, cleanup() prints "Cleaning up 1" and calls only ao(), so the servo entry is lost. The likely cause is initialisation order. cleanup_functions is a namespace-scope std::vector in module/core/src/cleanup.cpp, constructed at default priority, so its constructor runs after the constructor(2) push and wipes it. A function-local static vector, or dropping the priority, should fix it.

2. A misread over the SPI link can now cut the servo rail mid-run. set_servo_enabled() is a read-modify-write of register 61 (servo_p.cpp:21–35), and r8 returns the reply byte even when the transfer failed. If a read lands one byte off, it returns register 60 instead. Its high nibble is the count of rejected packets, which reads 0xF_ at 15, and that's all four disable bits set.

To test this, I enabled servos 1–3 and had a loop call disable_servo(0) the way libwallaby does, with a wrong-length transfer every 40 rounds. In 20 s there were 667 slips, 280 write-backs had all four disable bits set, and the rail was cut 5 times, dropping every arm. On master the same write-backs only stop the pulses, and the digital servos keep holding.

It needs a link slip; there were none in 20,000 healthy frames. The fix belongs in libwallaby: check the result of transfer() in r8/r16/r32, and don't write back a failed read. It's the same root as the write-back I noted on #13.

3. A re-enabled servo snaps at full speed to its last commanded position. Positions stay in the STM32 until it's reset, and enabling a servo writes only register 61. Before, a held digital servo was already at that position. Now it has gone limp and may have been moved by hand or by gravity. I confirmed it with a servo at 2300 µs: disabled it, moved the horn by hand, re-enabled it, and it snapped back at normal servo speed.

The same happens across programs. The next program's enable_servo() drives the servo to wherever the previous program left it, and get_servo_position() returns that stale value. Calling set_servo_position() before enabling avoids it. This is worth a line in the docs.

4. The moment of highest undervoltage risk moves later. I ran three motors at raw PWM up to 400, all four servos swinging the full 700↔2300 µs, and CPU load on the Pi, and polled vcgencmd get_throttled for the undervoltage flag. The flag trips when the servos' start-up current lands at the same moment as a motor reversal.

On master the servos are already powered, so the risky window is a reversal within about 50 ms of enable_servos(). With this PR the servos start drawing only about 100–200 ms after the enable (U72's soft-start, then the servos' own boot), and the window moves there. One older pack, at the threshold, master and this PR back to back:

Motor reversal after enable_servos() master this PR
0 or 50 ms 22/24 0/24
100–300 ms 1/48 10/48
200 ms only 0/12 6/12
All six delays together 23/72 10/72
Control: servos already enabled, new position + reversal 12/12 12/12

So the peak risk is the same, and over the delays I tried this PR tripped less often. A program that starts or reverses motors 100–200 ms after enabling the servos can now trip the flag where it didn't before. It only shows on a weak pack: a fresh one never tripped in 840 cycles at full motor power on either build. There were no STM32 resets and no Pi reboots on either build. I'd add one sentence to the description: "Servo start-up current now arrives about 100–200 ms after enable_servos(), not straight away."

Not tested: anything that needs a meter or a scope (the rail voltage at idle, the U72 EN voltage), and heavily loaded or stalled servos.

@twells46
twells46 merged commit 9773317 into master Oct 28, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants