Motor is operated by keyboard commands, encoder reads out position but not yet speed
Dependencies: HIDScope
Dependents: Project_motor Project_motor Motor_poscontrol Conceptcontroller_v_1_0
Fork of Encoder by
encoder.cpp@2:0998a8fd7727, 2013-11-04 (annotated)
- Committer:
- vsluiter
- Date:
- Mon Nov 04 09:56:06 2013 +0000
- Revision:
- 2:0998a8fd7727
- Parent:
- 0:c90b36abcbf8
- Child:
- 4:69be34f39c9f
Changed
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 | 2:0998a8fd7727 | 4 | Encoder::Encoder(PinName pos_a, PinName pos_b) : pin_a(pos_a), pin_b(pos_b) |
vsluiter | 0:c90b36abcbf8 | 5 | { |
vsluiter | 2:0998a8fd7727 | 6 | DigitalOut rled(LED_RED); |
vsluiter | 2:0998a8fd7727 | 7 | rled = 0; |
vsluiter | 0:c90b36abcbf8 | 8 | EncoderTimer.start(); |
vsluiter | 2:0998a8fd7727 | 9 | pin_a.mode(PullUp); |
vsluiter | 2:0998a8fd7727 | 10 | pin_b.mode(PullUp); |
vsluiter | 0:c90b36abcbf8 | 11 | pin_a.fall(this,&Encoder::encoderFalling); |
vsluiter | 0:c90b36abcbf8 | 12 | pin_a.rise(this,&Encoder::encoderRising); |
vsluiter | 0:c90b36abcbf8 | 13 | m_position = 0; |
vsluiter | 0:c90b36abcbf8 | 14 | m_speed = 0; |
vsluiter | 0:c90b36abcbf8 | 15 | zero_speed = false; |
vsluiter | 0:c90b36abcbf8 | 16 | } |
vsluiter | 0:c90b36abcbf8 | 17 | |
vsluiter | 0:c90b36abcbf8 | 18 | void Encoder::encoderFalling(void) |
vsluiter | 0:c90b36abcbf8 | 19 | { |
vsluiter | 0:c90b36abcbf8 | 20 | //temporary speed storage, in case higher interrupt level does stuff |
vsluiter | 0:c90b36abcbf8 | 21 | float temp_speed; |
vsluiter | 2:0998a8fd7727 | 22 | //int motortime_now = EncoderTimer.read_us(); |
vsluiter | 2:0998a8fd7727 | 23 | //EncoderTimer.reset(); |
vsluiter | 2:0998a8fd7727 | 24 | //EncoderTimeout.attach(this,&Encoder::timeouthandler,0.1); |
vsluiter | 0:c90b36abcbf8 | 25 | /*calculate as ticks per second*/ |
vsluiter | 2:0998a8fd7727 | 26 | //if(zero_speed) |
vsluiter | 2:0998a8fd7727 | 27 | // temp_speed = 0; |
vsluiter | 2:0998a8fd7727 | 28 | //else |
vsluiter | 2:0998a8fd7727 | 29 | // temp_speed = 1000000./motortime_now; |
vsluiter | 2:0998a8fd7727 | 30 | //zero_speed = false; |
vsluiter | 0:c90b36abcbf8 | 31 | if(pin_b) |
vsluiter | 0:c90b36abcbf8 | 32 | { |
vsluiter | 0:c90b36abcbf8 | 33 | m_position++; |
vsluiter | 0:c90b36abcbf8 | 34 | m_speed = temp_speed; |
vsluiter | 0:c90b36abcbf8 | 35 | } |
vsluiter | 0:c90b36abcbf8 | 36 | else |
vsluiter | 0:c90b36abcbf8 | 37 | { |
vsluiter | 0:c90b36abcbf8 | 38 | m_position--; |
vsluiter | 0:c90b36abcbf8 | 39 | m_speed = -temp_speed; //negative speed |
vsluiter | 0:c90b36abcbf8 | 40 | } |
vsluiter | 0:c90b36abcbf8 | 41 | } |
vsluiter | 0:c90b36abcbf8 | 42 | |
vsluiter | 0:c90b36abcbf8 | 43 | void Encoder::encoderRising(void) |
vsluiter | 0:c90b36abcbf8 | 44 | { |
vsluiter | 0:c90b36abcbf8 | 45 | //temporary speed storage, in case higher interrupt level does stuff |
vsluiter | 0:c90b36abcbf8 | 46 | float temp_speed; |
vsluiter | 2:0998a8fd7727 | 47 | //int motortime_now = EncoderTimer.read_us(); |
vsluiter | 2:0998a8fd7727 | 48 | //EncoderTimer.reset(); |
vsluiter | 2:0998a8fd7727 | 49 | //EncoderTimeout.attach(this,&Encoder::encoderFalling,0.1); |
vsluiter | 0:c90b36abcbf8 | 50 | /*calculate as ticks per second*/ |
vsluiter | 2:0998a8fd7727 | 51 | //if(zero_speed) |
vsluiter | 2:0998a8fd7727 | 52 | // temp_speed = 0; |
vsluiter | 2:0998a8fd7727 | 53 | //else |
vsluiter | 2:0998a8fd7727 | 54 | // temp_speed = 1000000./motortime_now; |
vsluiter | 2:0998a8fd7727 | 55 | //zero_speed = false; |
vsluiter | 0:c90b36abcbf8 | 56 | if(pin_b) |
vsluiter | 0:c90b36abcbf8 | 57 | { |
vsluiter | 0:c90b36abcbf8 | 58 | m_position--; |
vsluiter | 0:c90b36abcbf8 | 59 | m_speed = -temp_speed; //negative speed |
vsluiter | 0:c90b36abcbf8 | 60 | } |
vsluiter | 0:c90b36abcbf8 | 61 | else |
vsluiter | 0:c90b36abcbf8 | 62 | { |
vsluiter | 0:c90b36abcbf8 | 63 | m_position++; |
vsluiter | 0:c90b36abcbf8 | 64 | m_speed = temp_speed; |
vsluiter | 0:c90b36abcbf8 | 65 | } |
vsluiter | 0:c90b36abcbf8 | 66 | } |
vsluiter | 0:c90b36abcbf8 | 67 | |
vsluiter | 0:c90b36abcbf8 | 68 | void Encoder::timeouthandler(void) |
vsluiter | 0:c90b36abcbf8 | 69 | { |
vsluiter | 0:c90b36abcbf8 | 70 | m_speed = 0; |
vsluiter | 0:c90b36abcbf8 | 71 | zero_speed = true; |
vsluiter | 0:c90b36abcbf8 | 72 | } |