Test code for wheel encoders to calculate distance travelled by robot

Dependencies:   Motorslam QEI mbed

Fork of QEI_HelloWorld by Aaron Berk

Committer:
aminomar
Date:
Wed May 04 18:57:41 2016 +0000
Revision:
2:18287291c192
Parent:
1:30696e4d196b
Test code for wheel encoder to calculate distance travelled by robot;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aminomar 2:18287291c192 1 #include "Motorslam.h"
aberk 0:bcff39fac858 2 #include "QEI.h"
aminomar 2:18287291c192 3 #include "mbed.h"
aminomar 2:18287291c192 4
aminomar 2:18287291c192 5 #define PULSES_PER_REV 24
aminomar 2:18287291c192 6 #define WHEEL_DIAMETER 42.00 //mm
aminomar 2:18287291c192 7 #define CIRCUMFERENCE 131.95 //mm
aberk 0:bcff39fac858 8
aberk 0:bcff39fac858 9 Serial pc(USBTX, USBRX);
aberk 1:30696e4d196b 10 //Use X4 encoding.
aberk 1:30696e4d196b 11 //QEI wheel(p29, p30, NC, 624, QEI::X4_ENCODING);
aberk 1:30696e4d196b 12 //Use X2 encoding by default.
aminomar 2:18287291c192 13 QEI wheelLeft (p10, p9, NC, 24);//chanA, chan B
aminomar 2:18287291c192 14 QEI wheelRight (p30, p29, NC, 24);
aminomar 2:18287291c192 15 Motorslam motor( p21, p5, p6, p22, p8, p7, p18 ); //pwmLeft(A), leftIn1, leftIn2, pwmRight(B), rightIn1, rightIn2, standby
aminomar 2:18287291c192 16
aminomar 2:18287291c192 17 float distance;
aminomar 2:18287291c192 18 float fPwmPeriodLeft;
aminomar 2:18287291c192 19 float fPwmPulsewidthLeft;
aminomar 2:18287291c192 20 float fPwmPeriodRight;
aminomar 2:18287291c192 21 float fPwmPulsewidthRight;
aberk 0:bcff39fac858 22
aberk 0:bcff39fac858 23 int main() {
aminomar 2:18287291c192 24
aminomar 2:18287291c192 25 fPwmPeriodLeft = 0.001f; // 1KHz
aminomar 2:18287291c192 26 fPwmPulsewidthLeft = 0.50; // 49% duty cycle: slower, to make robot straight
aminomar 2:18287291c192 27 fPwmPeriodRight = 0.001f; // 1KHz
aminomar 2:18287291c192 28 fPwmPulsewidthRight = 0.50; // 51% duty cycle: faster, to make it straight
aminomar 2:18287291c192 29 motor.setPwmLeftperiod(fPwmPeriodLeft);
aminomar 2:18287291c192 30 motor.setPwmRightperiod(fPwmPeriodRight);
aminomar 2:18287291c192 31 motor.setPwmLeftpulsewidth(fPwmPulsewidthLeft);
aminomar 2:18287291c192 32 motor.setPwmRightpulsewidth(fPwmPulsewidthRight);
aberk 0:bcff39fac858 33
aminomar 2:18287291c192 34 while(1)
aminomar 2:18287291c192 35 {
aminomar 2:18287291c192 36 motor.moveForward();
aminomar 2:18287291c192 37
aminomar 2:18287291c192 38 pc.printf("Pulses on left: %i\n", wheelLeft.getPulses());//LEFT
aminomar 2:18287291c192 39 distance = ((wheelLeft.getPulses() / 24.00) * (131.95));
aminomar 2:18287291c192 40 pc.printf("distance on left: %f\n\n", distance);
aminomar 2:18287291c192 41
aminomar 2:18287291c192 42 pc.printf("Pulses on right: %i\n", wheelRight.getPulses());//RIGHT
aminomar 2:18287291c192 43 distance = ((wheelRight.getPulses() / 24.00) * (131.95));
aminomar 2:18287291c192 44 pc.printf("distance on right: %f\n\n", distance);
aminomar 2:18287291c192 45
aminomar 2:18287291c192 46 wait(1);
aberk 0:bcff39fac858 47 }
aminomar 2:18287291c192 48 //motor.moveStop();
aberk 0:bcff39fac858 49 }