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
Revision 6:18b000b443af, committed 2014-09-29
- Comitter:
- vsluiter
- Date:
- Mon Sep 29 20:47:37 2014 +0000
- Parent:
- 5:04c4a90c7a0a
- Commit message:
- Updated documentation
Changed in this revision
diff -r 04c4a90c7a0a -r 18b000b443af encoder.cpp.orig --- a/encoder.cpp.orig Mon Sep 29 20:43:45 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ - -#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; -} \ No newline at end of file
diff -r 04c4a90c7a0a -r 18b000b443af encoder.h --- a/encoder.h Mon Sep 29 20:43:45 2014 +0000 +++ b/encoder.h Mon Sep 29 20:47:37 2014 +0000 @@ -2,6 +2,7 @@ #define _ENCODER_H_ #include "mbed.h" + /** Encoder class. * Used to read out incremental position encoder. Decodes position in X2 configuration. * @@ -27,6 +28,7 @@ /** Create Encoder instance @param int_a Pin to be used as InterruptIn! Be careful, as not all pins on all platforms may be used as InterruptIn. @param int_b second encoder pin, used as DigitalIn. Can be any DigitalIn pin, not necessarily on InterruptIn location + @param speed boolean value to determine whether speed calculation is done in interrupt routine. Default false (no speed calculation) */ Encoder(PinName int_a, PinName int_b, bool speed=false);
diff -r 04c4a90c7a0a -r 18b000b443af encoder.h.orig --- a/encoder.h.orig Mon Sep 29 20:43:45 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ -#ifndef _ENCODER_H_ -#define _ENCODER_H_ - -#include "mbed.h" -/** Encoder class. - * Used to read out incremental position encoder. Decodes position in X2 configuration. - * - * Example: - * @code - * #include "mbed.h" - * #include "Encoder.h" - * - * Encoder motor1(PTD0,PTC9); - * Serial pc(USBTX,USBRX); - * pc.baud(115200); - * while(1) { - * wait(0.2); - * pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); - * } - * @endcode - */ -class Encoder -{ - public: - /** Create Encoder instance - @param int_a Pin to be used as InterruptIn! Be careful, as not all pins on all platforms may be used as InterruptIn. - @param int_b second encoder pin, used as DigitalIn. Can be any DigitalIn pin, not necessarily on InterruptIn location - */ - Encoder(PinName pos_a, PinName pos_b); - /** Request position - @returns current position in encoder counts - */ - int32_t getPosition(){return m_position;} - /** Overwrite position - @param pos position to be written - */ - void setPosition(int32_t pos){m_position = pos;} - /** Request speed - @returns current speed - */ - float getSpeed(){return m_speed;} - private: - void encoderFalling(void); - void encoderRising(void); - Timer EncoderTimer; - //Timeout EncoderTimeout; - InterruptIn pin_a; - DigitalIn pin_b; - int32_t m_position; - float m_speed; - void timeouthandler(void); - bool zero_speed; -}; - - -#endif //_ENCODER_H_ \ No newline at end of file