Electrical – Calculate speed from encoder counts

encodermotorspeed

I'm using the Arduino Encoder library to keep track of my motor's encoder counts. I need to calculate the motor's speed so it can be compared to a target speed and subjected to PID.

I've thought of one approach:

1) Calculate counts per timestep – Let's say we have a timestep of 100ms and we desire a speed of 50cm/sec. Determine speed in timestep units, 50/10.

To calculate speed motor is traveling at, simply calculate how many counts since last timestep, which will give speed in timestep units, which can be directly compared with desired speed;

speed = counts - prevCounts;
prevCounts = counts;

The advantage I can see with this approach is that it doesn't require us perform division (i.e. speed = (counts – prevCounts) / 0.1 ) in each timestep.

Is this a sensible? Are there any flaws in my method?

Best Answer

It seems to be correct as long as you are calculating everything in unit step time because all it does is scaling of units.

Related Topic