David's dead reckoning code for the LVBots competition on March 6th. Uses the mbed LPC1768, DRV8835, QTR-3RC, and two DC motors with encoders.
Dependencies: PololuEncoder Pacer mbed GeneralDebouncer
Revision 9:9734347b5756, committed 2014-02-22
- Comitter:
- DavidEGrayson
- Date:
- Sat Feb 22 03:03:37 2014 +0000
- Parent:
- 8:78b1ff957cba
- Child:
- 10:e4dd36148539
- Commit message:
- Made verything use CamelCase.;
Changed in this revision
--- a/encoders.cpp Sat Feb 22 02:23:21 2014 +0000 +++ b/encoders.cpp Sat Feb 22 03:03:37 2014 +0000 @@ -1,21 +1,21 @@ #include "encoders.h" -const PinName encoderPin1A = p6, - encoderPin1B = p7, - encoderPin2A = p8, - encoderPin2B = p9; +const PinName encoderPinLeftA = p6, // Gray wire + encoderPinLeftB = p7, // White wire + encoderPinRightA = p30, // White wire + encoderPinRightB = p29; // Gray wire PololuEncoderBuffer encoderBuffer; -PololuEncoder encoder1(encoderPin1A, encoderPin1B, &encoderBuffer, ENCODER1); -PololuEncoder encoder2(encoderPin2A, encoderPin2B, &encoderBuffer, ENCODER2); +PololuEncoder encoderLeft(encoderPinLeftA, encoderPinLeftB, &encoderBuffer, ENCODER1); +PololuEncoder encoderRight(encoderPinRightA, encoderPinRightB, &encoderBuffer, ENCODER2); -void encoders_init() +void encodersInit() { - DigitalIn(encoderPin1A).mode(PullUp); - DigitalIn(encoderPin1B).mode(PullUp); - DigitalIn(encoderPin2A).mode(PullUp); - DigitalIn(encoderPin2B).mode(PullUp); + DigitalIn(encoderPinLeftA).mode(PullNone); + DigitalIn(encoderPinLeftB).mode(PullNone); + DigitalIn(encoderPinRightA).mode(PullNone); + DigitalIn(encoderPinRightB).mode(PullNone); wait_us(50); - encoder1.init(); - encoder2.init(); + encoderLeft.init(); + encoderRight.init(); } \ No newline at end of file
--- a/encoders.h Sat Feb 22 02:23:21 2014 +0000 +++ b/encoders.h Sat Feb 22 03:03:37 2014 +0000 @@ -6,6 +6,6 @@ #define ENCODER2 0x01 extern PololuEncoderBuffer encoderBuffer; -extern PololuEncoder encoder1, encoder2; +extern PololuEncoder encoderLeft, encoderRight; -void encoders_init(); \ No newline at end of file +void encodersInit(); \ No newline at end of file
--- a/main.cpp Sat Feb 22 02:23:21 2014 +0000 +++ b/main.cpp Sat Feb 22 03:03:37 2014 +0000 @@ -3,40 +3,24 @@ #include "motors.h" #include "encoders.h" +#include "leds.h" #include "pc_serial.h" -#include "leds.h" +#include "test.h" int main() { pc.baud(115200); // Enable pull-ups on encoder pins and give them a chance to settle. - encoders_init(); - motors_init(); + encodersInit(); + motorsInit(); // Test routines - motors_test(); - encoders_test(); + //testMotors(); + testEncoders(); - Pacer reportPacer(500000); - Pacer blinkPacer(200000); while(1) { - while(encoderBuffer.hasEvents()) - { - PololuEncoderEvent event = encoderBuffer.readEvent(); - } - - if(reportPacer.pace()) - { - led2 = 1; - pc.printf("%8d %8d\n", encoder1.getCount(), encoder2.getCount()); - led2 = 0; - } - - if (blinkPacer.pace()) - { - led1 = !led1; - } + } }
--- a/motors.cpp Sat Feb 22 02:23:21 2014 +0000 +++ b/motors.cpp Sat Feb 22 03:03:37 2014 +0000 @@ -2,20 +2,20 @@ #include "motors.h" // Application mbed pin LPC1768 -// Motor 1 PWM p26 P2[0]/PWM1[1] -// Motor 1 dir p25 -// Motor 2 PWM p24 P2[2]/PWM1[3] -// Motor 2 dir p23 +// Motor L PWM p26 P2[0]/PWM1[1] +// Motor L dir p25 +// Motor R PWM p24 P2[2]/PWM1[3] +// Motor R dir p23 // Clock structure: // System clock: 96 MHz // In LPC_SC->PCLKSEL0, PWM is assigned to system clock / 4 by default, so it ticks at 24 MHz. // This allows us to have 1200 possible speeds at 20 kHz. -DigitalOut motor1Dir(p25); -DigitalOut motor2Dir(p23); +DigitalOut motorLeftDir(p25); +DigitalOut motorRightDir(p23); -void motors_init() +void motorsInit() { //PwmOut(p26).period_us(100); @@ -40,34 +40,34 @@ LPC_PWM1->MR0 = 1200; // Set the period. This must be done before enabling PWM. LPC_PWM1->LER = (1 << 0); - motors_speed_set(0, 0); + motorsSpeedSet(0, 0); LPC_PWM1->TCR = (1 << 0) | (1 << 3); // Enable the PWM counter and enable PWM. } -void motors_speed_set(int16_t motor1_speed, int16_t motor2_speed) +void motorsSpeedSet(int16_t motorLeftSpeed, int16_t motorRightSpeed) { - if (motor1_speed < 0) + if (motorLeftSpeed < 0) { - motor1_speed = -motor1_speed; - motor1Dir = 0; + motorLeftSpeed = -motorLeftSpeed; + motorLeftDir = 0; } else { - motor1Dir = 1; + motorLeftDir = 1; } - LPC_PWM1->MR1 = motor1_speed; + LPC_PWM1->MR1 = motorLeftSpeed; - if (motor2_speed < 0) + if (motorRightSpeed < 0) { - motor2_speed = -motor2_speed; - motor2Dir = 0; + motorRightSpeed = -motorRightSpeed; + motorRightDir = 0; } else { - motor2Dir = 1; + motorRightDir = 1; } - LPC_PWM1->MR3 = motor2_speed; + LPC_PWM1->MR3 = motorRightSpeed; LPC_PWM1->LER |= (1<<1) | (1<<3); }
--- a/motors.h Sat Feb 22 02:23:21 2014 +0000 +++ b/motors.h Sat Feb 22 03:03:37 2014 +0000 @@ -1,4 +1,4 @@ #pragma once -void motors_init(); -void motors_speed_set(int16_t motor1_speed, int16_t motor2_speed); +void motorsInit(); +void motorsSpeedSet(int16_t motorLeftSpeed, int16_t motorRightSpeed);
--- a/test.cpp Sat Feb 22 02:23:21 2014 +0000 +++ b/test.cpp Sat Feb 22 03:03:37 2014 +0000 @@ -2,9 +2,38 @@ #include <mbed.h> #include "motors.h" +#include <Pacer.h> + +#include "test.h" #include "leds.h" +#include "encoders.h" #include "pc_serial.h" +void testEncoders() +{ + Pacer reportPacer(500000); + Pacer blinkPacer(200000); + while(1) + { + while(encoderBuffer.hasEvents()) + { + PololuEncoderEvent event = encoderBuffer.readEvent(); + } + + if(reportPacer.pace()) + { + led2 = 1; + pc.printf("%8d %8d\n", encoderLeft.getCount(), encoderRight.getCount()); + led2 = 0; + } + + if (blinkPacer.pace()) + { + led1 = !led1; + } + } +} + void testMotors() { led1 = 1; @@ -12,29 +41,29 @@ led3 = 0; while(1) { - motors_speed_set(0, 0); + motorsSpeedSet(0, 0); led2 = 0; led3 = 0; wait(2); - motors_speed_set(300, 300); + motorsSpeedSet(300, 300); wait(2); - motors_speed_set(-300, 300); + motorsSpeedSet(-300, 300); wait(2); - motors_speed_set(0, 0); + motorsSpeedSet(0, 0); led2 = 1; wait(2); - motors_speed_set(600, 600); + motorsSpeedSet(600, 600); wait(2); - motors_speed_set(0, 0); + motorsSpeedSet(0, 0); led3 = 1; wait(2); - motors_speed_set(1200, 1200); + motorsSpeedSet(1200, 1200); wait(2); } } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.h Sat Feb 22 03:03:37 2014 +0000 @@ -0,0 +1,4 @@ +#pragma once + +void testEncoders(); +void testMotors(); \ No newline at end of file