Wheel encoders and setting the speed

I have wheel encoders on my 2 wheel robot car. They work fine through interrupts. Reading some values I get from the interrupts gives a perfect measurement of the speed. But what would be the best method to actually control the speed?

Is a PID system overkill? I haven't done that, but the net is full of examples.

Before diving into PID, I found myself inventing the wheel (I'm the founder of The Wheel Re-Inventors' Society). But perhaps this is overkill, too. In my main loop, I continuously update the motor power as long as I want to remain one speed, until I want to change the speed. My algo goes like this:

  1. On the first loop with a new target_speed value, the motor_power is set according to a lookup table.
  2. On the second loop with the new target_speed value, the measured_speed/motor_power ratio is used to adjust the motor_power to fit the target_speed. Simple line going through zero.
  3. On the following loops with the same target_speed, new (measured_speed, motor_power) points are recorded. To adjust the motor_power, the two last points are used for interpolation or extrapolation (same difference). When a fairly stable speed is reached, the lookup table used in step 1 is updated.

Won't work without PID.

Say that I want a speed of 300 mm/s. I set motor power to 150 (let's just assume I have a very bad lookup table). I get a speed measure of 224 mm/s. My second step calculates a new power using a linear model with two points (0, 0) and (224, 150). That model would give motor power 201. So I set that as the motor power and measure the speed in my third step. Has my algo gone completely wrong already?

I don't expect motor power 201 to give the final 300 mm/s yet. Say I measured 312 mm/s. Or I measured 278 mm/s. Whatever I measured, I have a new pair of points. Let's go with the 312 mm/s. I have points (224, 150) and (312, 201). These two points would tell me that 300 mm/s would require motor power 194. I set motor power to 194 and measure the speed again.

What I hope is that looping the last step, always using the last two measurements would get me to the target speed. Possible oscillation would maybe stay in a very small intervall.

Hi @Johan_Ha,

if you want to dig deeper this might be of interest for you

https://projecthub.arduino.cc/tareqwaleed1996/pid-speed-controller-for-dc-motor-c9f9b9

And there are libs available you might want to check out

https://github.com/natnqweb/Motor_PID

or

https://www.arduino.cc/reference/en/libraries/arduinomotorcarrier/pid/

Good luck!

You're probably right. Without load, my motors seemed to run with a very constant speed. But as soon as I put the car on the floor, it went absolutely crazy. I shall run a few tests before I close the case, then I switch to full PID.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.