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
motors.cpp@7:85b8b5acfb22, 2014-02-21 (annotated)
- Committer:
- DavidEGrayson
- Date:
- Fri Feb 21 00:00:43 2014 +0000
- Revision:
- 7:85b8b5acfb22
- Parent:
- 6:89a39870e23d
- Child:
- 8:78b1ff957cba
Setting more PWM registers now, but the call to period_us is still necessary for some reason.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DavidEGrayson | 4:1b20a11765c8 | 1 | #include <mbed.h> |
DavidEGrayson | 7:85b8b5acfb22 | 2 | #include "motors.h" |
DavidEGrayson | 7:85b8b5acfb22 | 3 | |
DavidEGrayson | 7:85b8b5acfb22 | 4 | // Application mbed pin LPC1768 |
DavidEGrayson | 7:85b8b5acfb22 | 5 | // Motor 1 PWM p26 P2[0]/PWM1[1] |
DavidEGrayson | 7:85b8b5acfb22 | 6 | // Motor 1 dir p25 |
DavidEGrayson | 7:85b8b5acfb22 | 7 | // Motor 2 PWM p27 P2[2]/PWM1[3] |
DavidEGrayson | 7:85b8b5acfb22 | 8 | // Motor 2 dir p28 |
DavidEGrayson | 4:1b20a11765c8 | 9 | |
DavidEGrayson | 4:1b20a11765c8 | 10 | static PwmOut motor1Pwm(p26); |
DavidEGrayson | 4:1b20a11765c8 | 11 | DigitalOut motor1Dir(p25); |
DavidEGrayson | 4:1b20a11765c8 | 12 | |
DavidEGrayson | 4:1b20a11765c8 | 13 | static PwmOut motor2Pwm(p24); |
DavidEGrayson | 4:1b20a11765c8 | 14 | DigitalOut motor2Dir(p23); |
DavidEGrayson | 4:1b20a11765c8 | 15 | |
DavidEGrayson | 4:1b20a11765c8 | 16 | void motors_init() |
DavidEGrayson | 4:1b20a11765c8 | 17 | { |
DavidEGrayson | 7:85b8b5acfb22 | 18 | motor1Pwm.period_us(100); |
DavidEGrayson | 7:85b8b5acfb22 | 19 | |
DavidEGrayson | 7:85b8b5acfb22 | 20 | // Set most parts of the PWM module to their defaults. |
DavidEGrayson | 7:85b8b5acfb22 | 21 | LPC_PWM1->TCR = 0; |
DavidEGrayson | 7:85b8b5acfb22 | 22 | LPC_PWM1->CTCR = 0; |
DavidEGrayson | 7:85b8b5acfb22 | 23 | LPC_PWM1->CCR = 0; |
DavidEGrayson | 5:01ad080dc4fa | 24 | |
DavidEGrayson | 7:85b8b5acfb22 | 25 | // Enable PWM output 1 and PWM output 3. |
DavidEGrayson | 7:85b8b5acfb22 | 26 | LPC_PWM1->PCR = (1 << 9) | (1 << 11); |
DavidEGrayson | 7:85b8b5acfb22 | 27 | |
DavidEGrayson | 5:01ad080dc4fa | 28 | LPC_PWM1->MCR = (1 << 1); // Reset PWMTC when it is equal to MR0. |
DavidEGrayson | 7:85b8b5acfb22 | 29 | |
DavidEGrayson | 7:85b8b5acfb22 | 30 | LPC_PWM1->MR0 = 1200; // Set the period. This must be done before enabling PWM. |
DavidEGrayson | 7:85b8b5acfb22 | 31 | LPC_PWM1->LER = (1 << 0); |
DavidEGrayson | 7:85b8b5acfb22 | 32 | motors_speed_set(0, 0); |
DavidEGrayson | 7:85b8b5acfb22 | 33 | |
DavidEGrayson | 7:85b8b5acfb22 | 34 | LPC_PWM1->TCR = (1 << 0) | (1 << 3); // Enable the PWM counter and enable PWM. |
DavidEGrayson | 7:85b8b5acfb22 | 35 | |
DavidEGrayson | 4:1b20a11765c8 | 36 | } |
DavidEGrayson | 4:1b20a11765c8 | 37 | |
DavidEGrayson | 4:1b20a11765c8 | 38 | void motors_speed_set(int16_t motor1_speed, int16_t motor2_speed) |
DavidEGrayson | 4:1b20a11765c8 | 39 | { |
DavidEGrayson | 6:89a39870e23d | 40 | LPC_PWM1->MR1 = motor1_speed; |
DavidEGrayson | 6:89a39870e23d | 41 | LPC_PWM1->MR3 = motor2_speed; |
DavidEGrayson | 7:85b8b5acfb22 | 42 | LPC_PWM1->LER |= (1<<1) | (1<<3); |
DavidEGrayson | 4:1b20a11765c8 | 43 | } |