Our Pi sample cart always veers right even with the same PWM on both motors
Plant manager, so this is a maintenance team side project I’ve been asked to help unstick. They built a small cart to carry QC samples from the line to the lab along a straight 30 m corridor. Raspberry Pi 4, an L298N driver, two of the yellow TT gear motors, a USB power bank, and it reports battery voltage and trip count to our Hermetry dashboard so we know when to charge it.
The cart never goes straight. Same PWM duty on both sides, 70 %, and it drifts right, about a metre over 10 m. We swapped the motors left to right and it still drifts right. The team wants to know whether to buy better motors or whether it’s a code problem.
Could you just give the right motor a bit more PWM until it tracks straight? Like 70 vs 74?
They tried that. It’s straight for a few runs, then it drifts the other way when the battery drops.
That’s typical with those TT motors. No two run at the same speed for the same PWM, the L298N drops a couple of volts differently per channel, the floor and the wheel diameters differ, and all of it changes with battery voltage. A fixed trim only fits one battery level, as you saw. Swapping the motors and still drifting right points at something besides the motors too (wheel diameter, a caster, the floor).
Better motors won’t fix open loop. What fixes it is measuring the wheels: TT motors with encoder discs and slotted optical sensors cost a few pounds. Count ticks on both wheels and correct the PWM so the counts stay equal. A simple proportional correction is enough for a corridor:
from gpiozero import Motor, RotaryEncoder # or DigitalInputDevice per sensor
import time
left, right = Motor(17, 18), Motor(22, 23)
enc_l, enc_r = RotaryEncoder(5, 6), RotaryEncoder(13, 19)
base, kp = 0.7, 0.002
while not arrived():
err = enc_l.steps - enc_r.steps # +: left ahead
left.forward(max(0, min(1, base - kp * err)))
right.forward(max(0, min(1, base + kp * err)))
time.sleep(0.02)
If one cheap optical sensor per wheel (no direction) is all you have, count pulses with a DigitalInputDevice callback instead, direction is known anyway. For really straight runs, add an MPU6050 gyro and hold the heading. And if they’re rebuilding, swap the L298N for a TB6612FNG: much smaller voltage drop, so the battery lasts longer too.
While they’re at it, send the encoder counts per trip to Hermetry. A rising left/right difference on the same route is an early sign of a wheel or gearbox going.
Encoders fitted Friday. With the correction it stays within about 5 cm over the 30 m, full or low battery. The swap test result turned out to be a slightly squashed right wheel, so Ciaran was right that it wasn’t only the motors. Trip counts and encoder difference now go to the dashboard.
- Vous devez être connecté pour répondre à ce sujet.
Rejoignez la discussion
Connectez-vous pour poser et répondre à des questions, aimer les messages utiles et marquer la réponse qui a résolu votre problème. Chaque contribution rapporte des points et des niveaux.

