zhouhang shao
/
test
test
Embed:
(wiki syntax)
Show/hide line numbers
encoder.cpp
00001 /* 00002 #include "encoder.h" 00003 00004 00005 void resetEncoders(){ 00006 leftEncoder.reset(); 00007 } 00008 00009 int getEncoderDistance(){ 00010 return (leftEncoder) >> 1; 00011 } 00012 00013 Encoder::Encoder(PinName channelA, PinName channelB) : 00014 channelA_(channelA), 00015 channelB_(channelB){ 00016 pulses = 0; 00017 00018 //Workout what the current state is. 00019 int chanA = channelA_.read(); 00020 int chanB = channelB_.read(); 00021 00022 //2-bit state. 00023 currState_ = (chanA << 1) | (chanB); 00024 prevState_ = currState_; 00025 00026 channelA_.rise(this, &Encoder::encode); 00027 channelA_.fall(this, &Encoder::encode); 00028 } 00029 00030 //Internally updates the pulse count for the encoder. 00031 void Encoder::encode(void) { 00032 int chanA = channelA_.read(); 00033 int chanB = channelB_.read(); 00034 00035 //2-bit state. 00036 currState_ = (chanA << 1) | (chanB); 00037 00038 //11->00->11->00 is counter clockwise rotation or "forward". 00039 if ((prevState_ == 0x3 && currState_ == 0x0) || 00040 (prevState_ == 0x0 && currState_ == 0x3)) { 00041 pulses+=2; 00042 } 00043 //10->01->10->01 is clockwise rotation or "backward". 00044 else if ((prevState_ == 0x2 && currState_ == 0x1) || 00045 (prevState_ == 0x1 && currState_ == 0x2)) { 00046 pulses-=2; 00047 } 00048 00049 prevState_ = currState_; 00050 } 00051 00052 //Resets the encoder 00053 void Encoder::reset(void) { 00054 pulses = 0; 00055 }*/
Generated on Sun Sep 11 2022 08:09:41 by 1.7.2