Nicholas Dart / Mbed 2 deprecated M3pi_logo_principle

Dependencies:   mbed m3pi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "m3pi.h"
00003 #include "MSCFileSystem.h"
00004 
00005 DigitalOut ledone(LED1);
00006 DigitalOut ledtwo(LED2);
00007 DigitalOut ledthree(LED3);
00008 DigitalOut ledfour(LED4);
00009 MSCFileSystem msc("msc");   // allows read/write to a removable USB device through the m3pi's USB interface
00010 Serial pc(USBTX, USBRX);
00011 m3pi m3pi;
00012 
00013 float speed_max;            //the maximum speed allowed. used in fwd/bck under recognition
00014 float speed_mod;            //the modifiable speed varible used for cornering. may be used to adjust cornering values in future versions
00015 float speed_user;
00016 
00017 char Buffer[512];   //will house all of the data in the Commands.txt file. has space for 510 commands + one speed variable at begining (M5)
00018 
00019 FILE *CMD = fopen("/msc/Commands.txt", "r");    //where all the commands should be stored
00020 
00021 //this will be run in the event that something goes wrong like a character cant be recognised or a file is missing
00022 void ERR() {
00023     while (1) {
00024         ledtwo = 0;
00025         ledthree = 0;
00026         ledone = 1;
00027         ledfour = 1;
00028         wait(0.2);
00029         ledfour = 0;
00030         ledone = 0;
00031         ledtwo = 1;
00032         ledthree = 1;
00033         wait(0.2);
00034     }
00035 }
00036 
00037 //run on completion of the scrip
00038 void FIN() {
00039     while (1) {
00040         ledone = 1;
00041         ledtwo = 1;
00042         ledthree = 1;
00043         ledfour = 1;
00044         wait(0.5);
00045         ledone = 0;
00046         ledtwo = 0;
00047         ledthree = 0;
00048         ledfour = 0;
00049         wait(0.5);
00050     }
00051 }
00052 //this will check that all the files are present and accounted for.
00053 void opening() {
00054     m3pi.cls();
00055     m3pi.locate(0,0);
00056     m3pi.printf("  LOGO");
00057     m3pi.locate(0,1);
00058     m3pi.printf("  RACE");
00059     FILE *CMD = fopen("/msc/Commands.txt", "r");
00060     //if it isnt is there, we have an ERROR, Run ERR();
00061     if (CMD == NULL) {
00062         m3pi.cls();
00063         m3pi.printf("CMD ERR");
00064         fclose(CMD);
00065         while (1) {
00066             ERR();
00067         }
00068         fclose(CMD);
00069     }
00070 }
00071 
00072 //getting everything from the text document
00073 void grabbing() {
00074     fopen("/msc/Commands.txt", "r");
00075     while (!feof(CMD)) {
00076         ledone = 1;
00077         //put everything in Buffer as a string it would be easier to have it in seperate containers but for simplicity to understand for nivices like my, i have done it like this.
00078         fscanf (CMD, "%s", Buffer);
00079     }
00080     fclose(CMD);
00081     ledone = 0;
00082     //add on to the end of Buffer a *  so that the end can be recognised
00083     sprintf(Buffer + strlen(Buffer), "*");
00084 }
00085 
00086 //sets the speed
00087 void variables () {
00088     if (Buffer[0] == 'M') {
00089         //as everything is in a string, numbers /begin at 48 (0, 1 etc) so anything after 48 should be a number
00090         if (Buffer[1] >= 49) {
00091             speed_max = ((Buffer[1] - 48) / 10);
00092         } else {
00093             // if it isnt a number, it can be used, say ERR
00094             m3pi.cls();
00095             m3pi.printf("BAD");
00096             m3pi.locate(0,1);
00097             m3pi.printf("SPEED");
00098             ERR();
00099         }
00100         speed_user = speed_max;
00101     } else {
00102         //if nothing is there, there has been no speed defined!
00103         m3pi.cls();
00104         m3pi.printf("NO SPEED");
00105         m3pi.locate(0,1);
00106         m3pi.printf("DEFINED");
00107     }
00108 }
00109 
00110 
00111 void recognition() {
00112     //this shall be used to incriment what character we are looking at
00113     int N=2;
00114     //repeat until the end of the document
00115     while (!feof(CMD)) {
00116         if (Buffer[N] == 'F' || Buffer[N] == 'f') {
00117             N++;
00118             //Because it's a  string, the conversion will turn the number 1 into 49, as it is the 49th character that can be recognised.
00119             while (Buffer[N] >= 48) {
00120                 m3pi.cls();
00121                 m3pi.printf("FWD %c", Buffer[N]);   // F represents forward and
00122                 m3pi.forward(speed_max);            // a number after it represents how long
00123                 wait(Buffer[N] - 48);               //to go forwards for. this senario
00124                 m3pi.stop();                        //only includes the posibilty of
00125                 N++;                                // a one digit number.
00126             }
00127         } else if (Buffer[N] == 'B' || Buffer[N] == 'b') {
00128             N++;
00129             while (Buffer[N] >= 48) {
00130                 m3pi.cls();
00131                 m3pi.printf("BACK %c", Buffer[N]);
00132                 m3pi.backward(speed_max);
00133                 wait(Buffer[N] - 48);
00134                 N++;
00135             }
00136         } else if (Buffer[N] == 'R' || Buffer[N] == 'r') {
00137             N++;
00138             while (Buffer[N] >= 48) {
00139                 int A=Buffer[N] - 48;
00140                 m3pi.cls();
00141                 m3pi.printf("RGHT %d", A);
00142                 m3pi.left( - speed_max);
00143                 m3pi.right(speed_max);
00144                 wait(((speed_mod * 0.28)/9)*A); //a number after R or L represents
00145                 m3pi.stop();                    //how much to turn. the algorithm
00146                 N++;                            //above converts that to wait times for motors
00147             }
00148         } else if (Buffer[N] == 'L' || Buffer[N] == 'l') {
00149             N++;
00150             while (Buffer[N] >= 48) {
00151                 int A=Buffer[N] - 48;
00152                 m3pi.cls();
00153                 m3pi.printf("LFT %d", A);
00154                 m3pi.right( - speed_max);
00155                 m3pi.left(speed_max);
00156                 wait(((speed_mod * 0.28)/9)*A);
00157                 m3pi.stop();
00158                 N++;
00159             }
00160         } else if (Buffer[N] == 'S' || Buffer[N] == 's') {
00161             m3pi.cls();
00162             m3pi.printf("STOP");
00163             m3pi.stop();
00164             FIN();
00165             N++;
00166             //if there is a * then we have reached the end of what is in Buffer
00167         } else if (Buffer[N] == '*') {
00168             m3pi.stop();
00169             m3pi.cls();
00170             m3pi.printf("FIN!");
00171             FIN();
00172             //if there is a character that is not listed above, it is probebly an error
00173         } else {
00174             m3pi.stop();
00175             m3pi.cls();
00176             m3pi.printf("  ERR  ");
00177             ERR();
00178         }
00179     }
00180 }
00181 
00182 int main() {
00183     opening();
00184     grabbing();
00185     variables();
00186     speed_mod = speed_max;
00187     wait(2.0);
00188     recognition();
00189 }