Hiromasa Oku
/
RE_Decorder
Rotary Encoder Decoder on mbed
Revision 0:665fbfcccab8, committed 2013-03-19
- Comitter:
- hiromasaoku
- Date:
- Tue Mar 19 05:34:46 2013 +0000
- Commit message:
- Rotary Encoder Decoder on mbed.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 665fbfcccab8 main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Mar 19 05:34:46 2013 +0000 @@ -0,0 +1,50 @@ +#include "mbed.h" +#define PI 3.1415 + +InterruptIn clockEncoderPin(p5); +DigitalIn directionPin(p6); + + +Serial pc(USBTX, USBRX); + +unsigned int encoderPos = 0; +float velo_rad = 0; +float angle_pos=0; + +unsigned long time_before = 0; + +bool newSpeed = false; +bool directionBool = true; + +float deltaAngle = PI / 32; +Timer timer; + +void encoder() { + time_before = timer.read_us(); + directionBool = directionPin; + if (directionBool) { + angle_pos += PI / 32.0; + encoderPos++; + } + else { + angle_pos -= PI / 32.0; + encoderPos--; + } + newSpeed = true; + } + +int main() { + clockEncoderPin.rise(&encoder); + timer.start(); + while(1) { + if(newSpeed){ + unsigned long deltaT = timer.read_us(); + timer.reset(); + velo_rad = 1000000.0 * (float)deltaAngle / (float)deltaT * (directionBool? 1: -1); + pc.printf("%f " , velo_rad); + newSpeed = false; + //timer.reset(); + } + + } +}
diff -r 000000000000 -r 665fbfcccab8 mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Tue Mar 19 05:34:46 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/5e5da4a5990b \ No newline at end of file