Controls the rovot with pot meters and button, fall back for if the filters fail

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial pc(USBTX, USBRX);
00004 
00005 
00006 // Define pins for motor control
00007 //horizontal motor
00008 DigitalOut directionPin(D4);
00009 PwmOut PWM(D5);
00010 //keypress motor
00011 DigitalOut directionPin_key(D7);
00012 PwmOut PWM_key(D6);
00013 
00014 //button to press the key
00015 DigitalIn button_key(PTC6);
00016 DigitalIn button_song(PTA4);
00017 
00018 //pot meters to act as emg filter substitutes
00019 AnalogIn left_pot(A0);
00020 AnalogIn right_pot(A1);
00021 
00022 //variables to hold the values of the pot meters
00023 double emg_left;
00024 double emg_right;
00025 
00026 //int to check if the button is pressed
00027 const int Button_move_pressed = 0;
00028 const int Button_song_pressed = 0;
00029 
00030 //values for clockwise and counter clockwise movement
00031 const int cw = 1;
00032 const int ccw = 0;
00033 
00034 
00035 
00036 int main()
00037 {
00038     while(1) {
00039         pc.printf("at the begin");
00040         
00041         //read the 'emg' values
00042         emg_left = left_pot.read();
00043         emg_right = right_pot.read();
00044         
00045         //if left above threashold and right below; move left
00046         while (emg_left > 0.5 and emg_right < 0.5)
00047         {
00048             directionPin.write(cw);
00049             PWM.write(1);
00050             //new left value to check (in while loop condition) if it has fallen below threashold
00051             emg_left = left_pot.read();
00052             pc.printf("left \n");
00053         }
00054         
00055         while (emg_right > 0.5 and emg_left < 0.5)
00056         {
00057             directionPin.write(ccw);
00058             PWM.write(1);
00059             //new right value, to check in while loop condition
00060             emg_right = right_pot.read();
00061             pc.printf("right \n");
00062         }
00063         if (button_key.read() == Button_move_pressed)
00064         {
00065             //move to key
00066             directionPin_key.write(cw);
00067             PWM_key.write(1);
00068             wait(0.14); //time it takes pwm of one to reach the key
00069             //pause at the key for one second
00070             PWM_key.write(0);
00071             wait(1);
00072             //move bakc to the starting position
00073             directionPin_key.write(ccw);
00074             PWM_key.write(1);
00075             //time needs to be slightly longer than moving forward (no answer why)
00076             wait(0.165f);
00077             pc.printf("keypress \n");
00078             
00079         }
00080         
00081         /// set spped values of both motors to zero so if no muscles are contracted, no motors move
00082         //more of a safety catch than actually necessary
00083         PWM_key.write(0);
00084         PWM.write(0);
00085         
00086         //cw to the right
00087          if (button_song.read() == Button_song_pressed){
00088              
00089                 //pressing va
00090             directionPin_key.write(cw);
00091             PWM_key.write(1);
00092             wait(0.14); //time it takes pwm of one to reach the key
00093             //pause at the key for one second
00094             PWM_key.write(0);
00095             wait(1);
00096             //move bakc to the starting position
00097             directionPin_key.write(ccw);
00098             PWM_key.write(1);
00099             //time needs to be slightly longer than moving forward (no answer why)
00100             wait(0.165f);
00101             PWM_key.write(0);
00102              
00103              // go 1 key to right
00104              directionPin.write(cw);
00105              PWM.write(1);
00106              wait(2.2);
00107              PWM.write(0);
00108               
00109                //pressing der
00110             directionPin_key.write(cw);
00111             PWM_key.write(1);
00112             wait(0.14); //time it takes pwm of one to reach the key
00113             //pause at the key for one second
00114             PWM_key.write(0);
00115             wait(1);
00116             //move bakc to the starting position
00117             directionPin_key.write(ccw);
00118             PWM_key.write(1);
00119             //time needs to be slightly longer than moving forward (no answer why)
00120             wait(0.165f);
00121             PWM_key.write(0);
00122              
00123              // go 1 key to right
00124              directionPin.write(cw);
00125              PWM.write(1);
00126              wait(2.2);
00127              PWM.write(0);
00128              
00129                 //pressing ja 
00130             directionPin_key.write(cw);
00131             PWM_key.write(1);
00132             wait(0.14); //time it takes pwm of one to reach the key
00133             //pause at the key for one second
00134             PWM_key.write(0);
00135             wait(1);
00136             //move bakc to the starting position
00137             directionPin_key.write(ccw);
00138             PWM_key.write(1);
00139             //time needs to be slightly longer than moving forward (no answer why)
00140             wait(0.165f);
00141             PWM_key.write(0);
00142             
00143              // go 2 key to left
00144              directionPin.write(ccw);
00145              PWM.write(1);
00146              wait(4.5);
00147              PWM.write(0);
00148              
00149                    //pressing cob 
00150             directionPin_key.write(cw);
00151             PWM_key.write(1);
00152             wait(0.14); //time it takes pwm of one to reach the key
00153             //pause at the key for one second
00154             PWM_key.write(0);
00155             wait(1);
00156             //move bakc to the starting position
00157             directionPin_key.write(ccw);
00158             PWM_key.write(1);
00159             //time needs to be slightly longer than moving forward (no answer why)
00160             wait(0.165f);
00161             PWM_key.write(0);
00162             
00163              wait(1.5);
00164              
00165                   //pressing va
00166             directionPin_key.write(cw);
00167             PWM_key.write(1);
00168             wait(0.14); //time it takes pwm of one to reach the key
00169             //pause at the key for one second
00170             PWM_key.write(0);
00171             wait(1);
00172             //move bakc to the starting position
00173             directionPin_key.write(ccw);
00174             PWM_key.write(1);
00175             //time needs to be slightly longer than moving forward (no answer why)
00176             wait(0.165f);
00177             PWM_key.write(0);
00178              
00179              // go 1 key to right
00180              directionPin.write(cw);
00181              PWM.write(1);
00182              wait(2.2);
00183              PWM.write(0);
00184               
00185                //pressing der
00186             directionPin_key.write(cw);
00187             PWM_key.write(1);
00188             wait(0.14); //time it takes pwm of one to reach the key
00189             //pause at the key for one second
00190             PWM_key.write(0);
00191             wait(1);
00192             //move bakc to the starting position
00193             directionPin_key.write(ccw);
00194             PWM_key.write(1);
00195             //time needs to be slightly longer than moving forward (no answer why)
00196             wait(0.165f);
00197             PWM_key.write(0);
00198              
00199              // go 1 key to right
00200              directionPin.write(cw);
00201              PWM.write(1);
00202              wait(2.2);
00203              PWM.write(0);
00204              
00205                 //pressing ja 
00206             directionPin_key.write(cw);
00207             PWM_key.write(1);
00208             wait(0.14); //time it takes pwm of one to reach the key
00209             //pause at the key for one second
00210             PWM_key.write(0);
00211             wait(1);
00212             //move bakc to the starting position
00213             directionPin_key.write(ccw);
00214             PWM_key.write(1);
00215             //time needs to be slightly longer than moving forward (no answer why)
00216             wait(0.165f);
00217             PWM_key.write(0);
00218             
00219              // go 2 key to left
00220              directionPin.write(ccw);
00221              PWM.write(1);
00222              wait(4.5);
00223              PWM.write(0);
00224              
00225                    //pressing cob 
00226             directionPin_key.write(cw);
00227             PWM_key.write(1);
00228             wait(0.14); //time it takes pwm of one to reach the key
00229             //pause at the key for one second
00230             PWM_key.write(0);
00231             wait(1);
00232             //move bakc to the starting position
00233             directionPin_key.write(ccw);
00234             PWM_key.write(1);
00235             //time needs to be slightly longer than moving forward (no answer why)
00236             wait(0.165f);
00237             PWM_key.write(0);
00238              }
00239     }
00240 }
00241