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