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
Diff: encoder.cpp
- Revision:
- 4:69be34f39c9f
- Parent:
- 2:0998a8fd7727
- Parent:
- 3:dcb7bdc73882
- Child:
- 5:04c4a90c7a0a
--- a/encoder.cpp Mon Nov 04 09:56:06 2013 +0000 +++ b/encoder.cpp Mon Sep 29 15:51:28 2014 +0000 @@ -1,13 +1,12 @@ #include "encoder.h" -Encoder::Encoder(PinName pos_a, PinName pos_b) : pin_a(pos_a), pin_b(pos_b) + +Encoder::Encoder(PinName int_a, PinName int_b, bool speed=false) : pin_a(int_a), pin_b(int_b) { - DigitalOut rled(LED_RED); - rled = 0; - EncoderTimer.start(); - pin_a.mode(PullUp); - pin_b.mode(PullUp); + m_speed_enabled = speed; + if(m_speed_enabled) + EncoderTimer.start(); pin_a.fall(this,&Encoder::encoderFalling); pin_a.rise(this,&Encoder::encoderRising); m_position = 0; @@ -18,16 +17,20 @@ 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; + float temp_speed=0; + int motortime_now + if(m_speed_enabled) + { + 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++; @@ -43,16 +46,20 @@ 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; + + float temp_speed=0; + int motortime_now; + if(m_speed_enabled) + { + 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--;