DE refactored as state machine

Dependencies:   Motor Servo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 ES200 3321 Project 2 
00003 MIDN 3/C Dietrich, Schwind, Silver
00004 */
00005 
00006 #include "mbed.h"
00007 #include "rtos.h"
00008 #include "stdlib.h"
00009 #include "Motor.h"
00010 #include "Servo.h"
00011 
00012 // global hardware objects
00013 Serial pc(USBTX,USBRX); 
00014 
00015 Servo servo_1(p24);//rotates hand between palm facing up and palm facing in
00016 Servo servo_2(p21); //on jar to rotate it in hand
00017 Motor motor(p29, p30, p26); // drive arm from 90 degrees to forehead
00018 DigitalIn switch_1(p17); //on protoboard - hit to start 
00019 DigitalIn switch_2(p18); //on "hand" - hits forehead to turn motor off
00020 DigitalIn switch_3(p12); //on protoboard - returns arm to start position
00021 DigitalIn switch_4(p10); //on "hand" - hits desk when going down to turn motor off
00022 
00023 
00024 
00025 int main() {
00026     // setup
00027     pc.printf("ES200 3321 Team Peanut Butter Beat Army\r\n");
00028     servo_1.calibrate(0.0009, 45.0); //Calibrates the servo_1 timing by setting the pulse width and range of motion (45*2 = 90 degrees)
00029     servo_2.calibrate(0.0009, 90.0); //Calibrates the servo_2 timing by setting the pulse width and range of motion (45*2 = 90 degrees)
00030     
00031     // state machines start here
00032     while(1){
00033         
00034         // idle state
00035         do {
00036             pc.printf("main() in idle state, awaiting sw1\r\n");
00037             servo_1.write(0.25); //Sets the servo in the initial position - palm facing in
00038             servo_2.write(0.0); //Sets the servo in the initial position - Does not matter where
00039             ThisThread::sleep_for(200); 
00040         } while (!switch_1.read());
00041             
00042         // peanut butter rotation 
00043         pc.printf("main() in peanut butter rotation state LATER\r\n");
00044         
00045         
00046         // palm up state
00047         pc.printf("main() going to palm up state\r\n");
00048         servo_1.write(.75); //turn the hand to the "palm up" position
00049         ThisThread::sleep_for(2000);           //wait for hand to get into "palm up" position
00050         
00051         // smash state
00052         pc.printf("main() smash PB against head state\r\n");
00053         motor.speed(0.7);    //turn motor on to drive arm up
00054         while (!switch_2.read()){
00055             pc.printf("main() awaiting palm switch 2 to finish PB head smash\r\n");
00056             ThisThread::sleep_for(100); 
00057             }
00058         motor.speed(0.0); 
00059             
00060         // play sound
00061         pc.printf("main() in play sound state LATER\r\n");
00062         while (!switch_3.read()){
00063               pc.printf("main() awaiting switch_3 to recycle\r\n");
00064               ThisThread::sleep_for(200); 
00065               }; 
00066               
00067         // lower arm
00068         pc.printf("main() lowering arm\r\n"); 
00069         motor.speed(-0.5); //turns the motor on to return the arm to the original position      
00070         while (!switch_4.read()) {
00071             pc.printf("main() awaiting switch_4 during recycle\r\n"); 
00072             ThisThread::sleep_for(200);
00073         } 
00074         motor.speed(0.0); // motor will turn off
00075         ThisThread::sleep_for(1000);        //wait 1 second for arm to level out
00076         
00077         // palm to side
00078         pc.printf("main() palm to side position\r\n");
00079         servo_1.write(0.25); //Servo 1 will turn on returning the hand to the original "palm in" position
00080                 
00081         pc.printf("main() returning to idle state\r\n");
00082         } // while(1)
00083 } // main()
00084 
00085 
00086 
00087 
00088 
00089 
00090 
00091 
00092 
00093 
00094 
00095 
00096 
00097 
00098 
00099 if (switch_1.read() == 1) //User hits switch 1 (button on protoboard)
00100 {
00101 
00102 
00103 
00104 /*
00105 {motor.speed(0);   //turn motor off
00106 int a = rand()%1;} //Generate random integer 0 or 1 to determine if the jar will open or not - what sound will play and if jar opens
00107 
00108 if (a == 1) {
00109     servo_1.write(...); //Servo on wrist turns to allow pb to come out
00110     pc.printf("1\n"); // send signal to Matlab to play Beat Army sound
00111     wait(10.0); // wait for Matlab to finish playing the sound NATHAN'S CODE???
00112       
00113 else if (a == 0)
00114 {
00115     pc.printf("2\n"); // send signal to Matlab to play boo sound
00116     wait(10.0); // wait for Matlab to finish playing the sound NATHAN'S CODE???
00117 }
00118 */
00119