Interrupt pwm motor control to read ultrasonic distance

27 Mar 2012

If I have a motor ( driving a robot chassis ) using pwm what happens if I use an interrupt to read distance from ultrasonic module ? Will it make the pwm control stutter ? Is this the best way to do it anyway ? I'd like to control the motors according to the distance from an obstacle.

28 Mar 2012

if your PWM is in hardware, then there should not be any interruption whatsoever.

28 Mar 2012

With the mbed PWM library, the PWM output is driven by a timer (ie hardware peripheral). Interrupts should have no effect on the timer setting. However, I have found that sometimes, the PWM duty has to be rewritten when changing scope (switching into a different C++ function). I have tried declaring the float duty variable as volatile, but could only fix it by refreshing the duty within the new scope.

One to watch out for - but other than that, yes, use PWM duty to control a motor's speed, and get feedback from the US detector.