![](/media/cache/profiles/vlcsnap-2021-10-25-16h54m29s128.png.50x50_q85.jpg)
DE upgrade of Crabrace code. Note you still need to add switches and (external) LEDs.
main.cpp@0:4d82538f5640, 2018-10-22 (annotated)
- Committer:
- evangeli
- Date:
- Mon Oct 22 15:50:15 2018 +0000
- Revision:
- 0:4d82538f5640
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
evangeli | 0:4d82538f5640 | 1 | /* |
evangeli | 0:4d82538f5640 | 2 | ES200 3321 Project 2 Team Crab Race |
evangeli | 0:4d82538f5640 | 3 | MIDN 3/c Pak, Tamas, etc |
evangeli | 0:4d82538f5640 | 4 | */ |
evangeli | 0:4d82538f5640 | 5 | #include "mbed.h" |
evangeli | 0:4d82538f5640 | 6 | #include "stdio.h" |
evangeli | 0:4d82538f5640 | 7 | #include "Motor.h" |
evangeli | 0:4d82538f5640 | 8 | #include "Servo.h" |
evangeli | 0:4d82538f5640 | 9 | |
evangeli | 0:4d82538f5640 | 10 | // declare global hardware objects |
evangeli | 0:4d82538f5640 | 11 | Serial pc(USBTX,USBRX); // for printing out stuff during debugging |
evangeli | 0:4d82538f5640 | 12 | Motor knife_motor(p26, p30, p29); |
evangeli | 0:4d82538f5640 | 13 | Servo finish_servo(p22); |
evangeli | 0:4d82538f5640 | 14 | Servo start_servo(p21); |
evangeli | 0:4d82538f5640 | 15 | |
evangeli | 0:4d82538f5640 | 16 | // where are your switches? you need a few |
evangeli | 0:4d82538f5640 | 17 | DigitalIn start_sw(p19); |
evangeli | 0:4d82538f5640 | 18 | |
evangeli | 0:4d82538f5640 | 19 | // need LEDs (they're supposed to be external LEDs!) |
evangeli | 0:4d82538f5640 | 20 | DigitalOut led[4]= {LED1, LED2, LED3, LED4}; |
evangeli | 0:4d82538f5640 | 21 | |
evangeli | 0:4d82538f5640 | 22 | |
evangeli | 0:4d82538f5640 | 23 | |
evangeli | 0:4d82538f5640 | 24 | |
evangeli | 0:4d82538f5640 | 25 | |
evangeli | 0:4d82538f5640 | 26 | int main(void) |
evangeli | 0:4d82538f5640 | 27 | { |
evangeli | 0:4d82538f5640 | 28 | int x; // for random outcome of race |
evangeli | 0:4d82538f5640 | 29 | int i; // a counter variable |
evangeli | 0:4d82538f5640 | 30 | |
evangeli | 0:4d82538f5640 | 31 | // setup |
evangeli | 0:4d82538f5640 | 32 | pc.printf("ES200 3321 Project 2 Team Crab Race\r\n"); |
evangeli | 0:4d82538f5640 | 33 | pc.printf("main() thread running\r\n"); |
evangeli | 0:4d82538f5640 | 34 | pc.printf("main() thread calibrating servos\r\n"); |
evangeli | 0:4d82538f5640 | 35 | start_servo.calibrate(0.0009, 90.0); // HiTec HS422 servos for ES200/202 |
evangeli | 0:4d82538f5640 | 36 | finish_servo.calibrate(0.0009, 90.0); |
evangeli | 0:4d82538f5640 | 37 | start_servo.write(0.0); // set start gate initial position |
evangeli | 0:4d82538f5640 | 38 | finish_servo.write(0.5); // set finish gate initial position |
evangeli | 0:4d82538f5640 | 39 | knife_motor.speed(0.0); // stop knife motor |
evangeli | 0:4d82538f5640 | 40 | |
evangeli | 0:4d82538f5640 | 41 | // main loop |
evangeli | 0:4d82538f5640 | 42 | while(1) { |
evangeli | 0:4d82538f5640 | 43 | |
evangeli | 0:4d82538f5640 | 44 | // STATE: IDLE |
evangeli | 0:4d82538f5640 | 45 | pc.printf("main() thread idle state\r\n"); |
evangeli | 0:4d82538f5640 | 46 | start_servo.write(0.0); |
evangeli | 0:4d82538f5640 | 47 | finish_servo.write(0.5); |
evangeli | 0:4d82538f5640 | 48 | while (!start_sw.read()) { |
evangeli | 0:4d82538f5640 | 49 | ThisThread::sleep_for(200); // works like wait() |
evangeli | 0:4d82538f5640 | 50 | } |
evangeli | 0:4d82538f5640 | 51 | |
evangeli | 0:4d82538f5640 | 52 | |
evangeli | 0:4d82538f5640 | 53 | // STATE: START RACE |
evangeli | 0:4d82538f5640 | 54 | x = (rand()%10)+1; |
evangeli | 0:4d82538f5640 | 55 | pc.printf("main() thread starting a race, x = %d\r\n",x); |
evangeli | 0:4d82538f5640 | 56 | start_servo.write(0.25); // lower start gate to begin race |
evangeli | 0:4d82538f5640 | 57 | ThisThread::sleep_for(1000); //gives time for crab to get down track |
evangeli | 0:4d82538f5640 | 58 | |
evangeli | 0:4d82538f5640 | 59 | |
evangeli | 0:4d82538f5640 | 60 | // STATE: END RACE depending on value of x |
evangeli | 0:4d82538f5640 | 61 | pc.printf("main() thread preparing to end race\r\n"); |
evangeli | 0:4d82538f5640 | 62 | start_servo.write(0.0); //set initial start gate position |
evangeli | 0:4d82538f5640 | 63 | if (x <= 7) { |
evangeli | 0:4d82538f5640 | 64 | pc.printf("main() thread for x = %d, victory display\r\n",x); |
evangeli | 0:4d82538f5640 | 65 | finish_servo = 1.0; //gate has a 70% chance to go one way |
evangeli | 0:4d82538f5640 | 66 | pc.printf("main() thread waiting for start_sw to stop display\r\n"); |
evangeli | 0:4d82538f5640 | 67 | while (start_sw.read()) { |
evangeli | 0:4d82538f5640 | 68 | for (i=0; i<5; i++) { |
evangeli | 0:4d82538f5640 | 69 | // blink lights for victory display |
evangeli | 0:4d82538f5640 | 70 | led[i] = 1; |
evangeli | 0:4d82538f5640 | 71 | ThisThread::sleep_for(150); |
evangeli | 0:4d82538f5640 | 72 | led[i]=0; |
evangeli | 0:4d82538f5640 | 73 | } // for |
evangeli | 0:4d82538f5640 | 74 | } // while(start_sw.read()) |
evangeli | 0:4d82538f5640 | 75 | } // if (x<=7) |
evangeli | 0:4d82538f5640 | 76 | else { //30% chance the gate will go the other way |
evangeli | 0:4d82538f5640 | 77 | pc.printf("main() thread for x = %d, fatality display\r\n",x); |
evangeli | 0:4d82538f5640 | 78 | finish_servo.write(0.0); |
evangeli | 0:4d82538f5640 | 79 | pc.printf("main() thread waiting for start_sw to stop display\r\n"); |
evangeli | 0:4d82538f5640 | 80 | while (start_sw.read()) { |
evangeli | 0:4d82538f5640 | 81 | knife_motor.speed (0.5); |
evangeli | 0:4d82538f5640 | 82 | ThisThread::sleep_for(500); |
evangeli | 0:4d82538f5640 | 83 | knife_motor.speed (-0.5); |
evangeli | 0:4d82538f5640 | 84 | ThisThread::sleep_for(500); |
evangeli | 0:4d82538f5640 | 85 | } // while(start_sw.read()) |
evangeli | 0:4d82538f5640 | 86 | knife_motor.speed(0); // stop knife |
evangeli | 0:4d82538f5640 | 87 | } |
evangeli | 0:4d82538f5640 | 88 | pc.printf("main() thread returning to idle state, reset the racer positions\r\n"); |
evangeli | 0:4d82538f5640 | 89 | finish_servo.write(0.5); //set finish gate to halfway point |
evangeli | 0:4d82538f5640 | 90 | |
evangeli | 0:4d82538f5640 | 91 | |
evangeli | 0:4d82538f5640 | 92 | } // while(1) |
evangeli | 0:4d82538f5640 | 93 | } // main() |