cancel
Showing results for 
Search instead for 
Did you mean: 

how to calculate speed from quadrature encoder - stm32f103

domanski0karol
Associate III
Posted on September 26, 2016 at 12:57

Hi, I use quadrature encoder connect to stm32f103 timer which I set to encoder mode and its work properly. I count to 1632 - its full wheel rotary, but I dont know how to calculate a current speed from this data. How I should do this?

2 REPLIES 2
Posted on September 26, 2016 at 13:54

If you understand the distance covered by a full rotation, you can measure the time it takes to do that, and thus the velocity. You can also measure the time between pulses, and thus a fractional rotation.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
frankfrank956
Associate III
Posted on September 27, 2016 at 00:59 I sample the encoders' counter every millisecond with a call from systick. Prior calibration tells me how many mm of travel per encoder count (MM_PER_COUNT). Since I read the encoder counter 1000 times per second, I set a constant (

ENCODER_READ_FREQUENCY)

to 1000.0f. You could sample at any frequency so long as the counters do not overflow between reads.

The code looks a bit like this sample extracted from a larger application. Note there will be some quantisation effects at low speed but it works out just fine for me.

float
encodersRead (
void
)
{
 // return speed in mm per second
static
int16_t Encoder;
static
int16_t oldEncoder;
oldEncoder = Encoder;
Encoder = TIM_GetCounter (ENC_TIMER) ;
int16_t Count = Encoder - oldEncoder;
return
(
float
) Count * ENCODER_READ_FREQUENCY * MM_PER_COUNT;
}