Fork van Victor Sluiters library. Output van speed is nu een double i.p.v. float.
Fork of Encoder by
encoder.cpp@0:c90b36abcbf8, 2013-09-25 (annotated)
- Committer:
- vsluiter
- Date:
- Wed Sep 25 14:55:08 2013 +0000
- Revision:
- 0:c90b36abcbf8
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vsluiter | 0:c90b36abcbf8 | 1 | |
vsluiter | 0:c90b36abcbf8 | 2 | #include "encoder.h" |
vsluiter | 0:c90b36abcbf8 | 3 | |
vsluiter | 0:c90b36abcbf8 | 4 | Encoder::Encoder(PinName int_a, PinName int_b) : pin_a(int_a), pin_b(int_b) |
vsluiter | 0:c90b36abcbf8 | 5 | { |
vsluiter | 0:c90b36abcbf8 | 6 | EncoderTimer.start(); |
vsluiter | 0:c90b36abcbf8 | 7 | pin_a.fall(this,&Encoder::encoderFalling); |
vsluiter | 0:c90b36abcbf8 | 8 | pin_a.rise(this,&Encoder::encoderRising); |
vsluiter | 0:c90b36abcbf8 | 9 | m_position = 0; |
vsluiter | 0:c90b36abcbf8 | 10 | m_speed = 0; |
vsluiter | 0:c90b36abcbf8 | 11 | zero_speed = false; |
vsluiter | 0:c90b36abcbf8 | 12 | } |
vsluiter | 0:c90b36abcbf8 | 13 | |
vsluiter | 0:c90b36abcbf8 | 14 | void Encoder::encoderFalling(void) |
vsluiter | 0:c90b36abcbf8 | 15 | { |
vsluiter | 0:c90b36abcbf8 | 16 | //temporary speed storage, in case higher interrupt level does stuff |
vsluiter | 0:c90b36abcbf8 | 17 | float temp_speed; |
vsluiter | 0:c90b36abcbf8 | 18 | int motortime_now = EncoderTimer.read_us(); |
vsluiter | 0:c90b36abcbf8 | 19 | EncoderTimer.reset(); |
vsluiter | 0:c90b36abcbf8 | 20 | EncoderTimeout.attach(this,&Encoder::timeouthandler,0.1); |
vsluiter | 0:c90b36abcbf8 | 21 | /*calculate as ticks per second*/ |
vsluiter | 0:c90b36abcbf8 | 22 | if(zero_speed) |
vsluiter | 0:c90b36abcbf8 | 23 | temp_speed = 0; |
vsluiter | 0:c90b36abcbf8 | 24 | else |
vsluiter | 0:c90b36abcbf8 | 25 | temp_speed = 1000000./motortime_now; |
vsluiter | 0:c90b36abcbf8 | 26 | zero_speed = false; |
vsluiter | 0:c90b36abcbf8 | 27 | if(pin_b) |
vsluiter | 0:c90b36abcbf8 | 28 | { |
vsluiter | 0:c90b36abcbf8 | 29 | m_position++; |
vsluiter | 0:c90b36abcbf8 | 30 | m_speed = temp_speed; |
vsluiter | 0:c90b36abcbf8 | 31 | } |
vsluiter | 0:c90b36abcbf8 | 32 | else |
vsluiter | 0:c90b36abcbf8 | 33 | { |
vsluiter | 0:c90b36abcbf8 | 34 | m_position--; |
vsluiter | 0:c90b36abcbf8 | 35 | m_speed = -temp_speed; //negative speed |
vsluiter | 0:c90b36abcbf8 | 36 | } |
vsluiter | 0:c90b36abcbf8 | 37 | } |
vsluiter | 0:c90b36abcbf8 | 38 | |
vsluiter | 0:c90b36abcbf8 | 39 | void Encoder::encoderRising(void) |
vsluiter | 0:c90b36abcbf8 | 40 | { |
vsluiter | 0:c90b36abcbf8 | 41 | //temporary speed storage, in case higher interrupt level does stuff |
vsluiter | 0:c90b36abcbf8 | 42 | float temp_speed; |
vsluiter | 0:c90b36abcbf8 | 43 | int motortime_now = EncoderTimer.read_us(); |
vsluiter | 0:c90b36abcbf8 | 44 | EncoderTimer.reset(); |
vsluiter | 0:c90b36abcbf8 | 45 | EncoderTimeout.attach(this,&Encoder::encoderFalling,0.1); |
vsluiter | 0:c90b36abcbf8 | 46 | /*calculate as ticks per second*/ |
vsluiter | 0:c90b36abcbf8 | 47 | if(zero_speed) |
vsluiter | 0:c90b36abcbf8 | 48 | temp_speed = 0; |
vsluiter | 0:c90b36abcbf8 | 49 | else |
vsluiter | 0:c90b36abcbf8 | 50 | temp_speed = 1000000./motortime_now; |
vsluiter | 0:c90b36abcbf8 | 51 | zero_speed = false; |
vsluiter | 0:c90b36abcbf8 | 52 | if(pin_b) |
vsluiter | 0:c90b36abcbf8 | 53 | { |
vsluiter | 0:c90b36abcbf8 | 54 | m_position--; |
vsluiter | 0:c90b36abcbf8 | 55 | m_speed = -temp_speed; //negative speed |
vsluiter | 0:c90b36abcbf8 | 56 | } |
vsluiter | 0:c90b36abcbf8 | 57 | else |
vsluiter | 0:c90b36abcbf8 | 58 | { |
vsluiter | 0:c90b36abcbf8 | 59 | m_position++; |
vsluiter | 0:c90b36abcbf8 | 60 | m_speed = temp_speed; |
vsluiter | 0:c90b36abcbf8 | 61 | } |
vsluiter | 0:c90b36abcbf8 | 62 | } |
vsluiter | 0:c90b36abcbf8 | 63 | |
vsluiter | 0:c90b36abcbf8 | 64 | void Encoder::timeouthandler(void) |
vsluiter | 0:c90b36abcbf8 | 65 | { |
vsluiter | 0:c90b36abcbf8 | 66 | m_speed = 0; |
vsluiter | 0:c90b36abcbf8 | 67 | zero_speed = true; |
vsluiter | 0:c90b36abcbf8 | 68 | } |