buggy code as of
Dependencies: mbed
main.cpp@0:aca11cc796fd, 2015-10-13 (annotated)
- Committer:
- benparkes
- Date:
- Tue Oct 13 12:09:44 2015 +0000
- Revision:
- 0:aca11cc796fd
12/10/15
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benparkes | 0:aca11cc796fd | 1 | #include "mbed.h" |
benparkes | 0:aca11cc796fd | 2 | |
benparkes | 0:aca11cc796fd | 3 | #define wheelc 0.1759292 |
benparkes | 0:aca11cc796fd | 4 | //Status LED |
benparkes | 0:aca11cc796fd | 5 | DigitalOut led(LED1); |
benparkes | 0:aca11cc796fd | 6 | |
benparkes | 0:aca11cc796fd | 7 | //Motor PWM (speed) |
benparkes | 0:aca11cc796fd | 8 | PwmOut PWMA(PA_8); |
benparkes | 0:aca11cc796fd | 9 | PwmOut PWMB(PB_4); |
benparkes | 0:aca11cc796fd | 10 | |
benparkes | 0:aca11cc796fd | 11 | //Motor Direction |
benparkes | 0:aca11cc796fd | 12 | DigitalOut DIRA(PA_9); |
benparkes | 0:aca11cc796fd | 13 | DigitalOut DIRB(PB_10); |
benparkes | 0:aca11cc796fd | 14 | |
benparkes | 0:aca11cc796fd | 15 | //Hall-Effect Sensor Inputs |
benparkes | 0:aca11cc796fd | 16 | DigitalIn HEA1(PB_2); |
benparkes | 0:aca11cc796fd | 17 | DigitalIn HEA2(PB_1); |
benparkes | 0:aca11cc796fd | 18 | DigitalIn HEB1(PB_15); |
benparkes | 0:aca11cc796fd | 19 | DigitalIn HEB2(PB_14); |
benparkes | 0:aca11cc796fd | 20 | |
benparkes | 0:aca11cc796fd | 21 | //On board switch |
benparkes | 0:aca11cc796fd | 22 | DigitalIn SW1(USER_BUTTON); |
benparkes | 0:aca11cc796fd | 23 | |
benparkes | 0:aca11cc796fd | 24 | //Use the serial object so we can use higher speeds |
benparkes | 0:aca11cc796fd | 25 | Serial terminal(USBTX, USBRX); |
benparkes | 0:aca11cc796fd | 26 | |
benparkes | 0:aca11cc796fd | 27 | //Timer used for measuring speeds |
benparkes | 0:aca11cc796fd | 28 | Timer timer; |
benparkes | 0:aca11cc796fd | 29 | |
benparkes | 0:aca11cc796fd | 30 | //Enumerated types |
benparkes | 0:aca11cc796fd | 31 | enum DIRECTION {FORWARD=0, REVERSE}; |
benparkes | 0:aca11cc796fd | 32 | enum PULSE {NOPULSE=0, PULSE}; |
benparkes | 0:aca11cc796fd | 33 | enum SWITCHSTATE {PRESSED=0, RELEASED}; |
benparkes | 0:aca11cc796fd | 34 | |
benparkes | 0:aca11cc796fd | 35 | //Debug GPIO |
benparkes | 0:aca11cc796fd | 36 | DigitalOut probe(D10); |
benparkes | 0:aca11cc796fd | 37 | |
benparkes | 0:aca11cc796fd | 38 | //Duty cycles |
benparkes | 0:aca11cc796fd | 39 | float dutyA = 1.0f; //100% |
benparkes | 0:aca11cc796fd | 40 | float dutyB = 1.0f; //100% |
benparkes | 0:aca11cc796fd | 41 | |
benparkes | 0:aca11cc796fd | 42 | int forward(float distance, int speed) |
benparkes | 0:aca11cc796fd | 43 | { |
benparkes | 0:aca11cc796fd | 44 | //Set motor period to 100Hz |
benparkes | 0:aca11cc796fd | 45 | dutyA = 5; |
benparkes | 0:aca11cc796fd | 46 | dutyB = 5; |
benparkes | 0:aca11cc796fd | 47 | PWMA.period_ms(speed); |
benparkes | 0:aca11cc796fd | 48 | PWMB.period_ms(speed); |
benparkes | 0:aca11cc796fd | 49 | |
benparkes | 0:aca11cc796fd | 50 | //Set initial motor speed to stop |
benparkes | 0:aca11cc796fd | 51 | PWMA.write(dutyA); //Set duty cycle (%) |
benparkes | 0:aca11cc796fd | 52 | PWMB.write(dutyB); //Set duty cycle (%) |
benparkes | 0:aca11cc796fd | 53 | |
benparkes | 0:aca11cc796fd | 54 | |
benparkes | 0:aca11cc796fd | 55 | } |
benparkes | 0:aca11cc796fd | 56 | |
benparkes | 0:aca11cc796fd | 57 | int turn(int degrees, bool direction) |
benparkes | 0:aca11cc796fd | 58 | { |
benparkes | 0:aca11cc796fd | 59 | //Set motor period to 100Hz |
benparkes | 0:aca11cc796fd | 60 | dutyA = 10; |
benparkes | 0:aca11cc796fd | 61 | dutyB = 10; |
benparkes | 0:aca11cc796fd | 62 | PWMA.period_ms(200); |
benparkes | 0:aca11cc796fd | 63 | PWMB.period_ms(0); |
benparkes | 0:aca11cc796fd | 64 | |
benparkes | 0:aca11cc796fd | 65 | //Set initial motor speed to stop |
benparkes | 0:aca11cc796fd | 66 | PWMA.write(dutyA); //Set duty cycle (%) |
benparkes | 0:aca11cc796fd | 67 | PWMB.write(dutyB); //Set duty cycle (%) |
benparkes | 0:aca11cc796fd | 68 | wait(0.676); |
benparkes | 0:aca11cc796fd | 69 | dutyA = 0; |
benparkes | 0:aca11cc796fd | 70 | dutyB = 0; |
benparkes | 0:aca11cc796fd | 71 | PWMA.write(dutyA); //Set duty cycle (%) |
benparkes | 0:aca11cc796fd | 72 | PWMB.write(dutyB); //Set duty cycle (%) |
benparkes | 0:aca11cc796fd | 73 | } |
benparkes | 0:aca11cc796fd | 74 | int main() |
benparkes | 0:aca11cc796fd | 75 | { |
benparkes | 0:aca11cc796fd | 76 | //Configure the terminal to high speed |
benparkes | 0:aca11cc796fd | 77 | terminal.baud(115200); |
benparkes | 0:aca11cc796fd | 78 | |
benparkes | 0:aca11cc796fd | 79 | //Set initial motor direction |
benparkes | 0:aca11cc796fd | 80 | DIRA = FORWARD; |
benparkes | 0:aca11cc796fd | 81 | DIRB = FORWARD; |
benparkes | 0:aca11cc796fd | 82 | |
benparkes | 0:aca11cc796fd | 83 | //Set initial motor speed to stop |
benparkes | 0:aca11cc796fd | 84 | PWMA.write(0.0f); //0% duty cycle |
benparkes | 0:aca11cc796fd | 85 | PWMB.write(0.0f); //0% duty cycle |
benparkes | 0:aca11cc796fd | 86 | while(1) { |
benparkes | 0:aca11cc796fd | 87 | while (SW1 == PRESSED) { |
benparkes | 0:aca11cc796fd | 88 | wait(0.01); |
benparkes | 0:aca11cc796fd | 89 | while(SW1 == RELEASED) { |
benparkes | 0:aca11cc796fd | 90 | |
benparkes | 0:aca11cc796fd | 91 | for (int i = 0; i<2; i++) { |
benparkes | 0:aca11cc796fd | 92 | forward(1,1); |
benparkes | 0:aca11cc796fd | 93 | wait(1.5); |
benparkes | 0:aca11cc796fd | 94 | turn(90,1); |
benparkes | 0:aca11cc796fd | 95 | forward(1,1); |
benparkes | 0:aca11cc796fd | 96 | wait(0.75); |
benparkes | 0:aca11cc796fd | 97 | turn(90,1); |
benparkes | 0:aca11cc796fd | 98 | |
benparkes | 0:aca11cc796fd | 99 | } |
benparkes | 0:aca11cc796fd | 100 | sleep(); |
benparkes | 0:aca11cc796fd | 101 | } |
benparkes | 0:aca11cc796fd | 102 | } |
benparkes | 0:aca11cc796fd | 103 | } |
benparkes | 0:aca11cc796fd | 104 | //Set initial motor speed to stop |
benparkes | 0:aca11cc796fd | 105 | PWMA.write(0); //Set duty cycle (%) |
benparkes | 0:aca11cc796fd | 106 | PWMB.write(0); //Set duty cycle (%) |
benparkes | 0:aca11cc796fd | 107 | |
benparkes | 0:aca11cc796fd | 108 | |
benparkes | 0:aca11cc796fd | 109 | |
benparkes | 0:aca11cc796fd | 110 | } |