How to brake a motor

Hi guys,

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?

Thank you

Motor: Pololu - 70:1 Metal Gearmotor 37Dx70L mm 12V with 64 CPR Encoder (Spur Pinion)
Motor controller: L298N such as this one https://m.gearbest.com/other-accessories/pp_226466.html?wid=1433363&currency=AUD&vip=15877875&gclid=Cj0KCQjwxbzdBRCoARIsACzIK2nA36_eG8s4OQg8u-ZLiBFxSJ1unuYnQ8G-hqcBKEkBcnV0yjtbZfQaApEzEALw_wcB

To brake with the L298, you have to keep the EN pin HIGH and set both IN pins to the same state (HIGH or LOW), see FIG.6 in datasheet.
http://www.st.com/content/ccc/resource/technical/document/datasheet/82/cc/3f/39/0a/29/4d/f0/CD00000240.pdf/files/CD00000240.pdf/jcr:content/translations/en.CD00000240.pdf

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.

Is there a way to dynamically brake both motors at the same time?

Actually you can use LOW; command, can you need to show your code.

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.