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 8:78b1ff957cba, committed 2014-02-22
- Comitter:
- DavidEGrayson
- Date:
- Sat Feb 22 02:23:21 2014 +0000
- Parent:
- 7:85b8b5acfb22
- Child:
- 9:9734347b5756
- Commit message:
- refactored into many files
Changed in this revision
--- a/Pacer.lib Fri Feb 21 00:00:43 2014 +0000 +++ b/Pacer.lib Sat Feb 22 02:23:21 2014 +0000 @@ -1,1 +1,1 @@ -Pacer#ffaf699a4eea +Pacer#415086687bce
--- a/PololuEncoder.lib Fri Feb 21 00:00:43 2014 +0000 +++ b/PololuEncoder.lib Sat Feb 22 02:23:21 2014 +0000 @@ -1,1 +1,1 @@ -PololuEncoder#22ac2b4a8012 +PololuEncoder#a2dd8d8bde4c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/encoders.cpp Sat Feb 22 02:23:21 2014 +0000 @@ -0,0 +1,21 @@ +#include "encoders.h" + +const PinName encoderPin1A = p6, + encoderPin1B = p7, + encoderPin2A = p8, + encoderPin2B = p9; + +PololuEncoderBuffer encoderBuffer; +PololuEncoder encoder1(encoderPin1A, encoderPin1B, &encoderBuffer, ENCODER1); +PololuEncoder encoder2(encoderPin2A, encoderPin2B, &encoderBuffer, ENCODER2); + +void encoders_init() +{ + DigitalIn(encoderPin1A).mode(PullUp); + DigitalIn(encoderPin1B).mode(PullUp); + DigitalIn(encoderPin2A).mode(PullUp); + DigitalIn(encoderPin2B).mode(PullUp); + wait_us(50); + encoder1.init(); + encoder2.init(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/encoders.h Sat Feb 22 02:23:21 2014 +0000 @@ -0,0 +1,11 @@ +#pragma once + +#include <PololuEncoder.h> + +#define ENCODER1 0x00 +#define ENCODER2 0x01 + +extern PololuEncoderBuffer encoderBuffer; +extern PololuEncoder encoder1, encoder2; + +void encoders_init(); \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/leds.cpp Sat Feb 22 02:23:21 2014 +0000 @@ -0,0 +1,3 @@ +#include "leds.h" + +DigitalOut led1(LED1), led2(LED2), led3(LED3), led4(LED4);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/leds.h Sat Feb 22 02:23:21 2014 +0000 @@ -0,0 +1,4 @@ +#pragma once + +#include <mbed.h> +extern DigitalOut led1, led2, led3, led4; \ No newline at end of file
--- a/main.cpp Fri Feb 21 00:00:43 2014 +0000 +++ b/main.cpp Sat Feb 22 02:23:21 2014 +0000 @@ -1,69 +1,36 @@ #include <mbed.h> -#include "PololuEncoder.h" -#include "Pacer.h" -#include "motors.h" - -DigitalOut led1(LED1), led2(LED2); - -Serial pc(USBTX, USBRX); +#include <Pacer.h> -#define ENCODER1 0x00 -#define ENCODER2 0x01 - -const PinName encoderPin1A = p6, - encoderPin1B = p7, - encoderPin2A = p8, - encoderPin2B = p9; - -PololuEncoderBuffer encoderBuffer; -PololuEncoder encoder1(encoderPin1A, encoderPin1B, &encoderBuffer, ENCODER1); -PololuEncoder encoder2(encoderPin2A, encoderPin2B, &encoderBuffer, ENCODER2); +#include "motors.h" +#include "encoders.h" +#include "pc_serial.h" +#include "leds.h" int main() { pc.baud(115200); // Enable pull-ups on encoder pins and give them a chance to settle. - DigitalIn(encoderPin1A).mode(PullUp); - DigitalIn(encoderPin1B).mode(PullUp); - DigitalIn(encoderPin2A).mode(PullUp); - DigitalIn(encoderPin2B).mode(PullUp); - wait_us(50); - encoder1.init(); - encoder2.init(); - + encoders_init(); motors_init(); - motors_speed_set(600, 1195); + // Test routines + motors_test(); + encoders_test(); Pacer reportPacer(500000); Pacer blinkPacer(200000); - uint32_t eventCount = 0; - uint32_t count = 0; while(1) { while(encoderBuffer.hasEvents()) { PololuEncoderEvent event = encoderBuffer.readEvent(); - eventCount += 1; - if (event == POLOLU_ENCODER_EVENT_ERR | ENCODER1) - { - pc.puts("error\n"); - } - else if (event == POLOLU_ENCODER_EVENT_INC | ENCODER1) - { - count += 1; - } - else if (event == POLOLU_ENCODER_EVENT_DEC | ENCODER1) - { - count -= 1; - } } if(reportPacer.pace()) { led2 = 1; - pc.printf("%8x\n", LPC_PWM1->MCR); + pc.printf("%8d %8d\n", encoder1.getCount(), encoder2.getCount()); led2 = 0; }
--- a/motors.cpp Fri Feb 21 00:00:43 2014 +0000 +++ b/motors.cpp Sat Feb 22 02:23:21 2014 +0000 @@ -4,18 +4,29 @@ // Application mbed pin LPC1768 // Motor 1 PWM p26 P2[0]/PWM1[1] // Motor 1 dir p25 -// Motor 2 PWM p27 P2[2]/PWM1[3] -// Motor 2 dir p28 +// Motor 2 PWM p24 P2[2]/PWM1[3] +// Motor 2 dir p23 -static PwmOut motor1Pwm(p26); +// 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); - -static PwmOut motor2Pwm(p24); DigitalOut motor2Dir(p23); void motors_init() { - motor1Pwm.period_us(100); + //PwmOut(p26).period_us(100); + + // Power the PWM module by setting PCPWM1 bit in PCONP register. (Table 46). + LPC_SC->PCONP |= (1 << 6); + + // In PCLKSEL0 register, set the clock for PWM1 to be equal to CCLK/4 (96/4 = 24 MHz). + LPC_SC->PCLKSEL0 &= ~(3 << 12); + + // Select the functions of P2.0 and P2.2 as PWM. (Table 83). + LPC_PINCON->PINSEL4 = (LPC_PINCON->PINSEL4 & ~((3 << 0) | (3 << 4))) | ((1 << 0) | (1 << 4)); // Set most parts of the PWM module to their defaults. LPC_PWM1->TCR = 0; @@ -32,12 +43,31 @@ motors_speed_set(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) { + if (motor1_speed < 0) + { + motor1_speed = -motor1_speed; + motor1Dir = 0; + } + else + { + motor1Dir = 1; + } LPC_PWM1->MR1 = motor1_speed; + + if (motor2_speed < 0) + { + motor2_speed = -motor2_speed; + motor2Dir = 0; + } + else + { + motor2Dir = 1; + } LPC_PWM1->MR3 = motor2_speed; + LPC_PWM1->LER |= (1<<1) | (1<<3); }
--- a/motors.h Fri Feb 21 00:00:43 2014 +0000 +++ b/motors.h Sat Feb 22 02:23:21 2014 +0000 @@ -1,2 +1,4 @@ +#pragma once + void motors_init(); void motors_speed_set(int16_t motor1_speed, int16_t motor2_speed);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pc_serial.cpp Sat Feb 22 02:23:21 2014 +0000 @@ -0,0 +1,3 @@ +#include "pc_serial.h" + +Serial pc(USBTX, USBRX);
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pc_serial.h Sat Feb 22 02:23:21 2014 +0000 @@ -0,0 +1,4 @@ +#pragma once + +#include <mbed.h> +extern Serial pc;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test.cpp Sat Feb 22 02:23:21 2014 +0000 @@ -0,0 +1,40 @@ +// A file for testing routines that will not be used in the final firmware. + +#include <mbed.h> +#include "motors.h" +#include "leds.h" +#include "pc_serial.h" + +void testMotors() +{ + led1 = 1; + led2 = 0; + led3 = 0; + while(1) + { + motors_speed_set(0, 0); + led2 = 0; + led3 = 0; + wait(2); + + motors_speed_set(300, 300); + wait(2); + + motors_speed_set(-300, 300); + wait(2); + + motors_speed_set(0, 0); + led2 = 1; + wait(2); + + motors_speed_set(600, 600); + wait(2); + + motors_speed_set(0, 0); + led3 = 1; + wait(2); + + motors_speed_set(1200, 1200); + wait(2); + } +} \ No newline at end of file