converting encoder data

Hi you all,

I'm trying to convert encoder data into mechanical degrees, the value is an unsigned int data type and the encoder has a resolution of 2500 ppr (pulses per revolution) it is an incremental encoder in quadrature the resolution turns in 10000 ppr (4x decoding).

But I haven't be able to get the angle in mechanical degrees, I used this factor (360/2500*4) or this (360.0/2500.0*4.0), but seems doesn't work.

"seems doesn't work" in what way?

in the way that I don't see the value in mechanical degrees (0º...180º...360º on) just values from (0 to 65535) the unsigned data type

But more than one rotation is more than 360 degrees - you need to take the modulus if you want angles in the 0..360 range. Also check you have enough bits to do the calculation - perhaps use long int.

I don’t get pretty well what you mean with modules. I stored the pulses in a unsigned int data type due to the fact that IC (hctl-2020) sends data in two bytes (first the higher byte and then the lower one). So what I expect is that if I turn with my hand let say 180º or whichever it be the Serial shows me a value according to the rotation and later use that value in degrees.

The quadrature decoder/counter chip will give you a 16-bit number. You can’t just take that number and convert it to a angle because the counter covers 6.5536 revolutions. You will need to keep track of the CHANGE in the counter. You can then use that CHANGE to adjust the angle.

int change = newvalue - previousvalue;

int anglecount = anglecount + change;

if (anglecount > 10000) anglecount -= 10000;
if (anglecount < 0) anglecount += 10000;

unsigned int angleDegrees = (angleCount * 360UL) / 10000;