This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi,
I just wonder about algorithm used in the example code - below you can find a part which calculates the position difference for speed calculation.
Let's walk through the calculation:
If the QPOSLAT = 1000 and the mech_scaler = 4000, we get 4e6 in the tmp. Then it is masked and shifted to an integer range, so we get 1952. A mask of 0x7FFF does not make any difference.
The tricky part is newp=_IQ15toIQ(tmp); which should convert IQ15 to IQ (float). But the conversion doesn't go per-unit and we get newp = 1952.0F.
Let's assume that we've got the oldp = 1852.0F. If we do a substraction of Tmpl = newp - oldp, it would give us Tmpl = 100.0F, which (based on the peroid of the calculation) results in the rotor speed.
But the issue here is the Speed_fr saturation done as if (Tmp1>_IQ(1)) etc. It always gets saturated and I'm always getting per-unit speed of +1.0 or -1.0. What am I missing?
Thanks
pos16bval=(unsigned int)EQep2Regs.QPOSLAT; // Latched POSCNT value tmp = (long)((long)pos16bval*(long)p->mech_scaler); // Q0*Q26 = Q26 tmp &= 0x03FFF000; tmp = (int)(tmp>>11); // Q26 -> Q15 tmp &= 0x7FFF; newp=_IQ15toIQ(tmp); oldp=p->oldpos; //.. Tmp1 = newp -oldp; //.. if (Tmp1>_IQ(1)) { p->Speed_fr = _IQ(1); } else if (Tmp1<_IQ(-1)) { p->Speed_fr = _IQ(-1); } else { p->Speed_fr = Tmp1; }