simple QEI library. no return speed, only return angle.

Dependents:   WRS_mechanamu_test

QEI.cpp

Committer:
sgrsn
Date:
2018-08-20
Revision:
0:bffc97496048
Child:
1:6fa863d09d45
Child:
2:62821df48957

File content as of revision 0:bffc97496048:

#include "QEI.h"

const int8_t encodeTable[] = {0, -1,  1,  0, 1,  0,  0, -1, -1,  0,  0,  1, 0,  1, -1,  0 };

QEI::QEI(PinName A, PinName B, int ppr) : channelA(A), channelB(B)
{
    channelA.rise(this, &QEI::encode);
    channelB.rise(this, &QEI::encode);
    channelA.fall(this, &QEI::encode);
    channelB.fall(this, &QEI::encode);
    _ppr = ppr;
    currState = 0;
    prevState = 0;
    position = 0;
}

float QEI::getAngle()
{
    return float(position) * 360.0/(_ppr*4.0);
}

void QEI::encode(void)
{
    int8_t chanA  = channelA.read();
    int8_t chanB  = channelB.read();
    currState = chanA | (chanB << 1);
    
    if (prevState != currState) {
        position += encodeTable[currState | (prevState<<2)];
        prevState = currState;
    }
}