I've seen many motors having capacitors attached in parallel in bots. Apparently, this is for the "safety" of the motor. As I understand it, all these will do is smoothen any fluctuations--and I doubt that fluctuations can have any adverse effects on a motor. Apparently these protect the motor if the shaft is being slowed/blocked, but I fail to see how.

What exactly is the function of such a capacitor? What does it prevent, and how?

Capacitors are used with motors in two different ways. Sometimes the same motor will have both techniques applied, and be associated with two significantly different-looking capacitors.


When motors with brushes are running normally, the motor brushes produce sparks, which cause noise "from DC to daylight". This has nothing to do with PWM -- it happens even when these motors are connected directly across a battery, without any PWM.
If we did nothing, the cable running from the electronics board (or directly from the battery) to the motor would act like an antenna, radiating TV and other radio interference.
One way people fix that problem is to attach small ceramic capacitors directly to the motor to absorb much of that noise. b c d e
When using PWM to drive the motor, when the transistors turn "on", the motor may pull a current spike / surge current -- the above noise-filtering capacitors make that current spike worse. When the transistors turn "off", the motor inductance may cause voltage spikes from the motor inductance -- the above noise-filtering capacitors help a little. More complex filters attached directly to the motor can help these two problems. a b
When a motor -- even a motor that doesn't have brushes -- is first turned on at a dead stop, and also when the robot hits an obstruction and stalls the motor, the motor pulls much higher currents than it does in normal operation -- currents that may last for several seconds. This high current may pull down the battery power rail enough to reset all the digital electronics in the system (or perhaps reset just some of the digital electronics, causing half-brain syndrome).

One work-around has 2 parts:


add large electrolytic capacitors directly across the battery (or across the battery input to the PWM motor driver, or across the battery input to the digital electronics, or often capacitors in all three locations) -- these capacitors work better at supplying high currents for a few milliseconds than the battery does.
In the few milliseconds we have before the stalled motor pulls all the energy from those big capacitors and then pulls the power rails low enough to start resetting things, program the digital system to somehow recognize that the motor has stalled and kill the power to that motor. Then that motor no longer drags down the power rail, and the digital electronics and all the other motors continue to operate normally. ("soft-start", "current-limiting", "torque-limiting", etc. are more sophisticated forms of this idea). (Those big capacitors, also absorb some of the energy that comes out of the motor when the PWM turns "off", and later put that energy back into the motor when the PWM turns "on").



The above capacitors protect other things from the motor's electrical interference.
I suppose one could argue that step (2) above prevents a stalled motor from eventually, after many seconds, overheating and failing -- but that's not really its primary purpose.
