インクリメント方式エンコーダ用ライブラリ

Dependents:   RobotControl_Sample2022 sotsuken_mecha

初期化 IncEncoder name(pinA,pinB,分解能); 分解能 : x2_resolution x4_resolution

リセット name.reset();

IncEncoder.cpp

Committer:
koki_konishi
Date:
23 months ago
Revision:
0:c40c29a063c0

File content as of revision 0:c40c29a063c0:

#include "IncEncoder.h"
#include "mbed.h"

IncEncoder::IncEncoder(PinName ENC_A,PinName ENC_B,Encoding encoding) : ENC_A_(ENC_A),ENC_B_(ENC_B){
    pulses = 0;
    
    ENC_A_.rise(this, &IncEncoder::A_RISE);
    ENC_A_.fall(this, &IncEncoder::A_FALL);
    
    if(encoding == x4_Resolution){
        ENC_B_.rise(this, &IncEncoder::B_RISE);
        ENC_B_.fall(this, &IncEncoder::B_FALL);
    }
}

int IncEncoder::GetIncPulses(){
    return pulses;
}

void IncEncoder::reset(){
    pulses = 0;
}

void IncEncoder::A_RISE(){
    if(ENC_B_) pulses++;
    else       pulses--;
}

void IncEncoder::A_FALL(){
    if(ENC_B_) pulses--;
    else       pulses++;
}

void IncEncoder::B_RISE(){
    if(ENC_A_) pulses--;
    else       pulses++;
}

void IncEncoder::B_FALL(){
    if(ENC_A_) pulses++;
    else       pulses--;
}