With the control tick and CAN working, the next piece of the pan tracker was the motor: a goBILDA 5203 gearmotor (26.9:1, with an encoder) driven by a Pololu VNH5019 carrier. The STM32H723 sends PWM, reads the encoder and current sense, and monitors the driver’s fault line. I hadn’t touched motors since university, so it’s been a fun time in the lab!

Background

H-bridge
Four switches around the motor. Turning on different pairs drives it forward or backward. The VNH5019 is a single-chip H-bridge with current sense and a fault output.
PWM duty
The fraction of each PWM period the bridge drives the motor, which sets the average voltage. Here the period is 50 µs (20 kHz).
Quadrature encoder
Two square waves, A and B, a quarter cycle apart. Which one leads gives the direction. Counting every edge on both (x4) gives the position.
Coast vs. brake
Coast leaves the motor leads floating, so it spins down on friction alone. Brake ties both leads to the same rail, so the motor’s own back-EMF drives a current that slows it down.
Deadband
The smallest duty that gets the motor moving from rest.
Open drain
A pin that can only pull low. Something else pulls it high, so either side can pull the line down.

Setup

  • PWM: TIM1 CH1 at 20 kHz (PSC 0, ARR 12999 on the 260 MHz timer clock), so 13,000 duty steps. The VNH5019 does its own bridge timing, so no complementary output.
  • Encoder: TIM4 in encoder mode on PD12/PD13, x4, with the input filter on both channels.
  • Direction: INA/INB on two GPIOs.
  • EN/DIAG: open drain on PE13. The driver pulls it low on a fault, so if the firmware has it released but the pin reads low, that’s a fault.
  • Current sense: ADC3 on PF4, through 1 kΩ and 100 nF (about a 1.6 kHz corner) to smooth the 20 kHz ripple.

motor.c drives it open loop: a motor CLI command with percent duty and a run deadline (1 s default, 5 s max), and a fault interrupt on EN/DIAG that latches until motor clear and puts the supervisor in FAULT.

First spin

  • The encoder counted down for positive duty… unexpected, but flipping TIM4 CH1’s polarity in CubeMX fixed it quickly. Positive is now counterclockwise from the shaft end, which matches ROS REP 103 with the motor shaft-up.
  • One output revolution by hand yields 751 counts (751.8 expected). (This was with simple tape markers!!)
  • Full duty: max speed in about 0.1 s (time constant roughly 25 ms), then flat at 2,760 counts/s, which is 220 RPM (rating is 223).
  • Deadband from rest is about +7% and -8% to -10%, and it moves with gear and commutator position. The control loop will need a friction feed-forward. I’ll probably need to characterize this with more samples in the future.

Stopping

The first stop coasted: 0.4 s and about 500 counts after the deadline, which is 240° of output shaft. This is bad news for the camera application!

The VNH5019 datasheet (section 2.4) luckily had an explanation. With INA = INB = 0, a 0 on PWM also turns off both low-side switches, so the motor floats. Braking to ground needs PWM held high.

The fix:

  • bridge_brake(): INA = INB = 0 with PWM held high (CCR1 = ARR + 1). Used for stops and reversals.
  • bridge_coast(): everything off. Used for faults, so a faulted driver isn’t carrying braking current.

With the brake, the motor stops in about 37 ms and 30 counts (14°), vs. 0.4 s coasting.

Output shaft speed around the stop at full duty. The coasting run takes about 0.4 s to reach zero. The braking run stops in about 37 ms.

Current sense

At full duty, current_ma read 528-585 mA against 490 mA peak on the supply’s display, and 7 mA stopped. This is within the VNH5019’s sense ratio spread (about -34% to +42%), so it’ll be ok for relative and stall checks, but not absolute current. Single samples move about 60 mA with commutator ripple.

The fault line

To test the fault interrupt, I jumpered the EN row to GND during a 30% run. Shorting wires is my least favorite kind of bench test on fragile at-home setups, but it was important to check.

Upon short, it latched, the motor stopped, the supervisor went from IDLE to FAULT, and motor clear refused while the short remained (correct). After removing it, clearing the fault via the CLI worked and the motor ran again.

When checking the status, the first test run accumulated 164 faults for one short. The jumper bounced -> ISR counted and posted a message on every falling edge -> overflowed the log queue. Now the ISR re-safes the bridge on every edge but only counts and posts the first one. Retested it (forcing pin bounce): fault_count 1.

Next

Next up: closing the loop! Handling velocity, then position. I’ll be moving the motor update from the supervisor task into the 1 kHz control tick.