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@4:69be34f39c9f, 2014-09-29 (annotated)
- Committer:
- vsluiter
- Date:
- Mon Sep 29 15:51:28 2014 +0000
- Revision:
- 4:69be34f39c9f
Merged versions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 4:69be34f39c9f | 1 | |
vsluiter | 4:69be34f39c9f | 2 | #include "encoder.h" |
vsluiter | 4:69be34f39c9f | 3 | |
vsluiter | 4:69be34f39c9f | 4 | Encoder::Encoder(PinName pos_a, PinName pos_b) : pin_a(pos_a), pin_b(pos_b) |
vsluiter | 4:69be34f39c9f | 5 | { |
vsluiter | 4:69be34f39c9f | 6 | DigitalOut rled(LED_RED); |
vsluiter | 4:69be34f39c9f | 7 | rled = 0; |
vsluiter | 4:69be34f39c9f | 8 | EncoderTimer.start(); |
vsluiter | 4:69be34f39c9f | 9 | pin_a.mode(PullUp); |
vsluiter | 4:69be34f39c9f | 10 | pin_b.mode(PullUp); |
vsluiter | 4:69be34f39c9f | 11 | pin_a.fall(this,&Encoder::encoderFalling); |
vsluiter | 4:69be34f39c9f | 12 | pin_a.rise(this,&Encoder::encoderRising); |
vsluiter | 4:69be34f39c9f | 13 | m_position = 0; |
vsluiter | 4:69be34f39c9f | 14 | m_speed = 0; |
vsluiter | 4:69be34f39c9f | 15 | zero_speed = false; |
vsluiter | 4:69be34f39c9f | 16 | } |
vsluiter | 4:69be34f39c9f | 17 | |
vsluiter | 4:69be34f39c9f | 18 | void Encoder::encoderFalling(void) |
vsluiter | 4:69be34f39c9f | 19 | { |
vsluiter | 4:69be34f39c9f | 20 | //temporary speed storage, in case higher interrupt level does stuff |
vsluiter | 4:69be34f39c9f | 21 | float temp_speed; |
vsluiter | 4:69be34f39c9f | 22 | //int motortime_now = EncoderTimer.read_us(); |
vsluiter | 4:69be34f39c9f | 23 | //EncoderTimer.reset(); |
vsluiter | 4:69be34f39c9f | 24 | //EncoderTimeout.attach(this,&Encoder::timeouthandler,0.1); |
vsluiter | 4:69be34f39c9f | 25 | /*calculate as ticks per second*/ |
vsluiter | 4:69be34f39c9f | 26 | //if(zero_speed) |
vsluiter | 4:69be34f39c9f | 27 | // temp_speed = 0; |
vsluiter | 4:69be34f39c9f | 28 | //else |
vsluiter | 4:69be34f39c9f | 29 | // temp_speed = 1000000./motortime_now; |
vsluiter | 4:69be34f39c9f | 30 | //zero_speed = false; |
vsluiter | 4:69be34f39c9f | 31 | if(pin_b) |
vsluiter | 4:69be34f39c9f | 32 | { |
vsluiter | 4:69be34f39c9f | 33 | m_position++; |
vsluiter | 4:69be34f39c9f | 34 | m_speed = temp_speed; |
vsluiter | 4:69be34f39c9f | 35 | } |
vsluiter | 4:69be34f39c9f | 36 | else |
vsluiter | 4:69be34f39c9f | 37 | { |
vsluiter | 4:69be34f39c9f | 38 | m_position--; |
vsluiter | 4:69be34f39c9f | 39 | m_speed = -temp_speed; //negative speed |
vsluiter | 4:69be34f39c9f | 40 | } |
vsluiter | 4:69be34f39c9f | 41 | } |
vsluiter | 4:69be34f39c9f | 42 | |
vsluiter | 4:69be34f39c9f | 43 | void Encoder::encoderRising(void) |
vsluiter | 4:69be34f39c9f | 44 | { |
vsluiter | 4:69be34f39c9f | 45 | //temporary speed storage, in case higher interrupt level does stuff |
vsluiter | 4:69be34f39c9f | 46 | float temp_speed; |
vsluiter | 4:69be34f39c9f | 47 | //int motortime_now = EncoderTimer.read_us(); |
vsluiter | 4:69be34f39c9f | 48 | //EncoderTimer.reset(); |
vsluiter | 4:69be34f39c9f | 49 | //EncoderTimeout.attach(this,&Encoder::encoderFalling,0.1); |
vsluiter | 4:69be34f39c9f | 50 | /*calculate as ticks per second*/ |
vsluiter | 4:69be34f39c9f | 51 | //if(zero_speed) |
vsluiter | 4:69be34f39c9f | 52 | // temp_speed = 0; |
vsluiter | 4:69be34f39c9f | 53 | //else |
vsluiter | 4:69be34f39c9f | 54 | // temp_speed = 1000000./motortime_now; |
vsluiter | 4:69be34f39c9f | 55 | //zero_speed = false; |
vsluiter | 4:69be34f39c9f | 56 | if(pin_b) |
vsluiter | 4:69be34f39c9f | 57 | { |
vsluiter | 4:69be34f39c9f | 58 | m_position--; |
vsluiter | 4:69be34f39c9f | 59 | m_speed = -temp_speed; //negative speed |
vsluiter | 4:69be34f39c9f | 60 | } |
vsluiter | 4:69be34f39c9f | 61 | else |
vsluiter | 4:69be34f39c9f | 62 | { |
vsluiter | 4:69be34f39c9f | 63 | m_position++; |
vsluiter | 4:69be34f39c9f | 64 | m_speed = temp_speed; |
vsluiter | 4:69be34f39c9f | 65 | } |
vsluiter | 4:69be34f39c9f | 66 | } |
vsluiter | 4:69be34f39c9f | 67 | |
vsluiter | 4:69be34f39c9f | 68 | void Encoder::timeouthandler(void) |
vsluiter | 4:69be34f39c9f | 69 | { |
vsluiter | 4:69be34f39c9f | 70 | m_speed = 0; |
vsluiter | 4:69be34f39c9f | 71 | zero_speed = true; |
vsluiter | 4:69be34f39c9f | 72 | } |