旋转编码器测速c语言程序,官方例程中编码器测速程序高低速怎么区分
如下是官方編碼器測速的程序:
//**** High Speed Calculation using QEP Position counter ****//
// Check unit Time out-event for speed calculation:
// Unit Timer is configured for 100Hz in INIT function
if(EQep1Regs.QFLG.bit.UTO==1) // If unit timeout (one 100Hz period)
{
/** Differentiator **/
// The following lines calculate position = (x2-x1)/4000 (position in 1 revolution)
pos16bval=(unsigned int)EQep1Regs.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;
if (p->DirectionQep==0) // POSCNT is counting down
{
if (newp>oldp)
Tmp1 = - (_IQ(1) - newp + oldp); // x2-x1 should be negative
else
Tmp1 = newp -oldp;
}
else if (p->DirectionQep==1) // POSCNT is counting up
{
if (newp
Tmp1 = _IQ(1) + newp - oldp;
else
Tmp1 = newp - oldp; // x2-x1 should be positive
}
if (Tmp1>_IQ(1))
p->Speed_fr = _IQ(1);
else if (Tmp1<_iq>
p->Speed_fr = _IQ(-1);
else
p->Speed_fr = Tmp1;
// Update the electrical angle
p->oldpos = newp;
// Change motor speed from pu value to rpm value (Q15 -> Q0)
// Q0 = Q0*GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
p->SpeedRpm_fr = _IQmpy(p->BaseRpm,p->Speed_fr);
//=======================================
EQep1Regs.QCLR.bit.UTO=1; // Clear interrupt flag
}
//**** Low-speed computation using QEP capture counter ****//
if(EQep1Regs.QEPSTS.bit.UPEVNT==1) // Unit position event
{
if(EQep1Regs.QEPSTS.bit.COEF==0) // No Capture overflow
temp1=(unsigned long)EQep1Regs.QCPRDLAT; // temp1 = t2-t1
else // Capture overflow, saturate the result
temp1=0xFFFF;
p->Speed_pr = _IQdiv(p->SpeedScaler,temp1); // p->Speed_pr = p->SpeedScaler/temp1
Tmp1=p->Speed_pr;
if (Tmp1>_IQ(1))
p->Speed_pr = _IQ(1);
else
p->Speed_pr = Tmp1;
// Convert p->Speed_pr to RPM
if (p->DirectionQep==0) // Reverse direction = negative
p->SpeedRpm_pr = -_IQmpy(p->BaseRpm,p->Speed_pr); // Q0 = Q0*GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
else // Forward direction = positive
p->SpeedRpm_pr = _IQmpy(p->BaseRpm,p->Speed_pr); // Q0 = Q0*GLOBAL_Q => _IQXmpy(), X = GLOBAL_Q
EQep1Regs.QEPSTS.all=0x88; // Clear Unit position event flag
// Clear overflow error flag
}
高速是采用M法測量速度 ?低速采用T法測速
問題是怎么判斷是用M法還是T法呢?在程序中沒有進行判斷啊,僅靠這兩個狀態標志位夠用用嗎?不需要人為在程序中添加其他判斷條件嗎?
如果不是,那么是不是在eQEP初始化的時候做了哪些小技巧?多謝指教!
if(EQep1Regs.QFLG.bit.UTO==1) // If unit timeout (one 100Hz period)
if(EQep1Regs.QEPSTS.bit.UPEVNT==1) // Unit position event
總結
以上是生活随笔為你收集整理的旋转编码器测速c语言程序,官方例程中编码器测速程序高低速怎么区分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 条形码入门指南(七):线性条形码
- 下一篇: 虚幻引擎中文教学福利:使用 C++ 开发