123
Fork of Encoder by
encoder.cpp@6:18b000b443af, 2014-09-29 (annotated)
- Committer:
- vsluiter
- Date:
- Mon Sep 29 20:47:37 2014 +0000
- Revision:
- 6:18b000b443af
- Parent:
- 5:04c4a90c7a0a
Updated documentation
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 | 4:69be34f39c9f | 4 | |
vsluiter | 5:04c4a90c7a0a | 5 | Encoder::Encoder(PinName int_a, PinName int_b, bool speed) : pin_a(int_a), pin_b(int_b) |
vsluiter | 0:c90b36abcbf8 | 6 | { |
vsluiter | 3:dcb7bdc73882 | 7 | m_speed_enabled = speed; |
vsluiter | 3:dcb7bdc73882 | 8 | if(m_speed_enabled) |
vsluiter | 3:dcb7bdc73882 | 9 | EncoderTimer.start(); |
vsluiter | 0:c90b36abcbf8 | 10 | pin_a.fall(this,&Encoder::encoderFalling); |
vsluiter | 0:c90b36abcbf8 | 11 | pin_a.rise(this,&Encoder::encoderRising); |
vsluiter | 0:c90b36abcbf8 | 12 | m_position = 0; |
vsluiter | 0:c90b36abcbf8 | 13 | m_speed = 0; |
vsluiter | 0:c90b36abcbf8 | 14 | zero_speed = false; |
vsluiter | 0:c90b36abcbf8 | 15 | } |
vsluiter | 0:c90b36abcbf8 | 16 | |
vsluiter | 0:c90b36abcbf8 | 17 | void Encoder::encoderFalling(void) |
vsluiter | 0:c90b36abcbf8 | 18 | { |
vsluiter | 0:c90b36abcbf8 | 19 | //temporary speed storage, in case higher interrupt level does stuff |
vsluiter | 3:dcb7bdc73882 | 20 | float temp_speed=0; |
vsluiter | 5:04c4a90c7a0a | 21 | int motortime_now; |
vsluiter | 3:dcb7bdc73882 | 22 | if(m_speed_enabled) |
vsluiter | 3:dcb7bdc73882 | 23 | { |
vsluiter | 3:dcb7bdc73882 | 24 | motortime_now = EncoderTimer.read_us(); |
vsluiter | 3:dcb7bdc73882 | 25 | EncoderTimer.reset(); |
vsluiter | 3:dcb7bdc73882 | 26 | EncoderTimeout.attach(this,&Encoder::timeouthandler,0.1); |
vsluiter | 3:dcb7bdc73882 | 27 | /*calculate as ticks per second*/ |
vsluiter | 3:dcb7bdc73882 | 28 | if(zero_speed) |
vsluiter | 3:dcb7bdc73882 | 29 | temp_speed = 0; |
vsluiter | 3:dcb7bdc73882 | 30 | else |
vsluiter | 3:dcb7bdc73882 | 31 | temp_speed = 1000000./motortime_now; |
vsluiter | 3:dcb7bdc73882 | 32 | zero_speed = false; |
vsluiter | 3:dcb7bdc73882 | 33 | } |
vsluiter | 0:c90b36abcbf8 | 34 | if(pin_b) |
vsluiter | 0:c90b36abcbf8 | 35 | { |
vsluiter | 0:c90b36abcbf8 | 36 | m_position++; |
vsluiter | 0:c90b36abcbf8 | 37 | m_speed = temp_speed; |
vsluiter | 0:c90b36abcbf8 | 38 | } |
vsluiter | 0:c90b36abcbf8 | 39 | else |
vsluiter | 0:c90b36abcbf8 | 40 | { |
vsluiter | 0:c90b36abcbf8 | 41 | m_position--; |
vsluiter | 0:c90b36abcbf8 | 42 | m_speed = -temp_speed; //negative speed |
vsluiter | 0:c90b36abcbf8 | 43 | } |
vsluiter | 0:c90b36abcbf8 | 44 | } |
vsluiter | 0:c90b36abcbf8 | 45 | |
vsluiter | 0:c90b36abcbf8 | 46 | void Encoder::encoderRising(void) |
vsluiter | 0:c90b36abcbf8 | 47 | { |
vsluiter | 0:c90b36abcbf8 | 48 | //temporary speed storage, in case higher interrupt level does stuff |
vsluiter | 4:69be34f39c9f | 49 | |
vsluiter | 3:dcb7bdc73882 | 50 | float temp_speed=0; |
vsluiter | 3:dcb7bdc73882 | 51 | int motortime_now; |
vsluiter | 3:dcb7bdc73882 | 52 | if(m_speed_enabled) |
vsluiter | 3:dcb7bdc73882 | 53 | { |
vsluiter | 3:dcb7bdc73882 | 54 | EncoderTimer.reset(); |
vsluiter | 3:dcb7bdc73882 | 55 | EncoderTimeout.attach(this,&Encoder::encoderFalling,0.1); |
vsluiter | 3:dcb7bdc73882 | 56 | /*calculate as ticks per second*/ |
vsluiter | 3:dcb7bdc73882 | 57 | if(zero_speed) |
vsluiter | 3:dcb7bdc73882 | 58 | temp_speed = 0; |
vsluiter | 3:dcb7bdc73882 | 59 | else |
vsluiter | 3:dcb7bdc73882 | 60 | temp_speed = 1000000./motortime_now; |
vsluiter | 3:dcb7bdc73882 | 61 | zero_speed = false; |
vsluiter | 3:dcb7bdc73882 | 62 | } |
vsluiter | 0:c90b36abcbf8 | 63 | if(pin_b) |
vsluiter | 0:c90b36abcbf8 | 64 | { |
vsluiter | 0:c90b36abcbf8 | 65 | m_position--; |
vsluiter | 0:c90b36abcbf8 | 66 | m_speed = -temp_speed; //negative speed |
vsluiter | 0:c90b36abcbf8 | 67 | } |
vsluiter | 0:c90b36abcbf8 | 68 | else |
vsluiter | 0:c90b36abcbf8 | 69 | { |
vsluiter | 0:c90b36abcbf8 | 70 | m_position++; |
vsluiter | 0:c90b36abcbf8 | 71 | m_speed = temp_speed; |
vsluiter | 0:c90b36abcbf8 | 72 | } |
vsluiter | 0:c90b36abcbf8 | 73 | } |
vsluiter | 0:c90b36abcbf8 | 74 | |
vsluiter | 0:c90b36abcbf8 | 75 | void Encoder::timeouthandler(void) |
vsluiter | 0:c90b36abcbf8 | 76 | { |
vsluiter | 0:c90b36abcbf8 | 77 | m_speed = 0; |
vsluiter | 0:c90b36abcbf8 | 78 | zero_speed = true; |
vsluiter | 0:c90b36abcbf8 | 79 | } |