I’m doing a project where I have a robot that needs to move and stop at a specific position. I’m using an encoder to track the position then a motor controller to move the motor. The problem I’m facing is that setting a pwm of 0 to the enable pin does not actually brake the motor. It just doesn’t receive any current but the momentum keeps it moving hence the robot stopping a bit further than necessary. Is there a way to dynamically brake both motors at the same time?
edit Ignore my writing and jump to MarkT's post below!
The L298 can at its best use some "magnetic" force to do the braking. It's better than nothing and all depends on the motor itself and the magnitude of the momentum. I think the braking mechanism is something like shortcutting the input terminals to the motor. When freewheeling, the motor produces voltage and when shortcutting, the produced braking energy dissipates in heat.
If you know in advance the position when to stop, you have to start braking in advance. Measure how long it takes to reach zero speed from any speed. If the magnetic braking takes too long, you have to add other braking devices, like disk or drum brakes.
Close a PID loop for position control around the motor code, you'll get braking for free. You
have an encoder already so all the hardware is there for this.