Library to read out speed and position from a quadrature encoder. This library uses X2 decoding.
Dependents: BMT-K9_encoder BMT-K9-Regelaar K9motoraansturing_copy EMGverwerking ... more
encoder.cpp.orig
- Committer:
- vsluiter
- Date:
- 2014-09-29
- Revision:
- 4:69be34f39c9f
File content as of revision 4:69be34f39c9f:
#include "encoder.h" Encoder::Encoder(PinName pos_a, PinName pos_b) : pin_a(pos_a), pin_b(pos_b) { DigitalOut rled(LED_RED); rled = 0; EncoderTimer.start(); pin_a.mode(PullUp); pin_b.mode(PullUp); pin_a.fall(this,&Encoder::encoderFalling); pin_a.rise(this,&Encoder::encoderRising); m_position = 0; m_speed = 0; zero_speed = false; } void Encoder::encoderFalling(void) { //temporary speed storage, in case higher interrupt level does stuff float temp_speed; //int motortime_now = EncoderTimer.read_us(); //EncoderTimer.reset(); //EncoderTimeout.attach(this,&Encoder::timeouthandler,0.1); /*calculate as ticks per second*/ //if(zero_speed) // temp_speed = 0; //else // temp_speed = 1000000./motortime_now; //zero_speed = false; if(pin_b) { m_position++; m_speed = temp_speed; } else { m_position--; m_speed = -temp_speed; //negative speed } } void Encoder::encoderRising(void) { //temporary speed storage, in case higher interrupt level does stuff float temp_speed; //int motortime_now = EncoderTimer.read_us(); //EncoderTimer.reset(); //EncoderTimeout.attach(this,&Encoder::encoderFalling,0.1); /*calculate as ticks per second*/ //if(zero_speed) // temp_speed = 0; //else // temp_speed = 1000000./motortime_now; //zero_speed = false; if(pin_b) { m_position--; m_speed = -temp_speed; //negative speed } else { m_position++; m_speed = temp_speed; } } void Encoder::timeouthandler(void) { m_speed = 0; zero_speed = true; }