Nicholas Dart
/
M3pi_logo_principle
M3pi logo character recognition principle
main.cpp@0:33bfb6a726ba, 2011-07-08 (annotated)
- Committer:
- Nicholas
- Date:
- Fri Jul 08 12:12:56 2011 +0000
- Revision:
- 0:33bfb6a726ba
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nicholas | 0:33bfb6a726ba | 1 | #include "mbed.h" |
Nicholas | 0:33bfb6a726ba | 2 | #include "m3pi.h" |
Nicholas | 0:33bfb6a726ba | 3 | #include "MSCFileSystem.h" |
Nicholas | 0:33bfb6a726ba | 4 | |
Nicholas | 0:33bfb6a726ba | 5 | DigitalOut ledone(LED1); |
Nicholas | 0:33bfb6a726ba | 6 | DigitalOut ledtwo(LED2); |
Nicholas | 0:33bfb6a726ba | 7 | DigitalOut ledthree(LED3); |
Nicholas | 0:33bfb6a726ba | 8 | DigitalOut ledfour(LED4); |
Nicholas | 0:33bfb6a726ba | 9 | MSCFileSystem msc("msc"); // allows read/write to a removable USB device through the m3pi's USB interface |
Nicholas | 0:33bfb6a726ba | 10 | Serial pc(USBTX, USBRX); |
Nicholas | 0:33bfb6a726ba | 11 | m3pi m3pi; |
Nicholas | 0:33bfb6a726ba | 12 | |
Nicholas | 0:33bfb6a726ba | 13 | float speed_max; //the maximum speed allowed. used in fwd/bck under recognition |
Nicholas | 0:33bfb6a726ba | 14 | float speed_mod; //the modifiable speed varible used for cornering. may be used to adjust cornering values in future versions |
Nicholas | 0:33bfb6a726ba | 15 | float speed_user; |
Nicholas | 0:33bfb6a726ba | 16 | |
Nicholas | 0:33bfb6a726ba | 17 | 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) |
Nicholas | 0:33bfb6a726ba | 18 | |
Nicholas | 0:33bfb6a726ba | 19 | FILE *CMD = fopen("/msc/Commands.txt", "r"); //where all the commands should be stored |
Nicholas | 0:33bfb6a726ba | 20 | |
Nicholas | 0:33bfb6a726ba | 21 | //this will be run in the event that something goes wrong like a character cant be recognised or a file is missing |
Nicholas | 0:33bfb6a726ba | 22 | void ERR() { |
Nicholas | 0:33bfb6a726ba | 23 | while (1) { |
Nicholas | 0:33bfb6a726ba | 24 | ledtwo = 0; |
Nicholas | 0:33bfb6a726ba | 25 | ledthree = 0; |
Nicholas | 0:33bfb6a726ba | 26 | ledone = 1; |
Nicholas | 0:33bfb6a726ba | 27 | ledfour = 1; |
Nicholas | 0:33bfb6a726ba | 28 | wait(0.2); |
Nicholas | 0:33bfb6a726ba | 29 | ledfour = 0; |
Nicholas | 0:33bfb6a726ba | 30 | ledone = 0; |
Nicholas | 0:33bfb6a726ba | 31 | ledtwo = 1; |
Nicholas | 0:33bfb6a726ba | 32 | ledthree = 1; |
Nicholas | 0:33bfb6a726ba | 33 | wait(0.2); |
Nicholas | 0:33bfb6a726ba | 34 | } |
Nicholas | 0:33bfb6a726ba | 35 | } |
Nicholas | 0:33bfb6a726ba | 36 | |
Nicholas | 0:33bfb6a726ba | 37 | //run on completion of the scrip |
Nicholas | 0:33bfb6a726ba | 38 | void FIN() { |
Nicholas | 0:33bfb6a726ba | 39 | while (1) { |
Nicholas | 0:33bfb6a726ba | 40 | ledone = 1; |
Nicholas | 0:33bfb6a726ba | 41 | ledtwo = 1; |
Nicholas | 0:33bfb6a726ba | 42 | ledthree = 1; |
Nicholas | 0:33bfb6a726ba | 43 | ledfour = 1; |
Nicholas | 0:33bfb6a726ba | 44 | wait(0.5); |
Nicholas | 0:33bfb6a726ba | 45 | ledone = 0; |
Nicholas | 0:33bfb6a726ba | 46 | ledtwo = 0; |
Nicholas | 0:33bfb6a726ba | 47 | ledthree = 0; |
Nicholas | 0:33bfb6a726ba | 48 | ledfour = 0; |
Nicholas | 0:33bfb6a726ba | 49 | wait(0.5); |
Nicholas | 0:33bfb6a726ba | 50 | } |
Nicholas | 0:33bfb6a726ba | 51 | } |
Nicholas | 0:33bfb6a726ba | 52 | //this will check that all the files are present and accounted for. |
Nicholas | 0:33bfb6a726ba | 53 | void opening() { |
Nicholas | 0:33bfb6a726ba | 54 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 55 | m3pi.locate(0,0); |
Nicholas | 0:33bfb6a726ba | 56 | m3pi.printf(" LOGO"); |
Nicholas | 0:33bfb6a726ba | 57 | m3pi.locate(0,1); |
Nicholas | 0:33bfb6a726ba | 58 | m3pi.printf(" RACE"); |
Nicholas | 0:33bfb6a726ba | 59 | FILE *CMD = fopen("/msc/Commands.txt", "r"); |
Nicholas | 0:33bfb6a726ba | 60 | //if it isnt is there, we have an ERROR, Run ERR(); |
Nicholas | 0:33bfb6a726ba | 61 | if (CMD == NULL) { |
Nicholas | 0:33bfb6a726ba | 62 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 63 | m3pi.printf("CMD ERR"); |
Nicholas | 0:33bfb6a726ba | 64 | fclose(CMD); |
Nicholas | 0:33bfb6a726ba | 65 | while (1) { |
Nicholas | 0:33bfb6a726ba | 66 | ERR(); |
Nicholas | 0:33bfb6a726ba | 67 | } |
Nicholas | 0:33bfb6a726ba | 68 | fclose(CMD); |
Nicholas | 0:33bfb6a726ba | 69 | } |
Nicholas | 0:33bfb6a726ba | 70 | } |
Nicholas | 0:33bfb6a726ba | 71 | |
Nicholas | 0:33bfb6a726ba | 72 | //getting everything from the text document |
Nicholas | 0:33bfb6a726ba | 73 | void grabbing() { |
Nicholas | 0:33bfb6a726ba | 74 | fopen("/msc/Commands.txt", "r"); |
Nicholas | 0:33bfb6a726ba | 75 | while (!feof(CMD)) { |
Nicholas | 0:33bfb6a726ba | 76 | ledone = 1; |
Nicholas | 0:33bfb6a726ba | 77 | //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. |
Nicholas | 0:33bfb6a726ba | 78 | fscanf (CMD, "%s", Buffer); |
Nicholas | 0:33bfb6a726ba | 79 | } |
Nicholas | 0:33bfb6a726ba | 80 | fclose(CMD); |
Nicholas | 0:33bfb6a726ba | 81 | ledone = 0; |
Nicholas | 0:33bfb6a726ba | 82 | //add on to the end of Buffer a * so that the end can be recognised |
Nicholas | 0:33bfb6a726ba | 83 | sprintf(Buffer + strlen(Buffer), "*"); |
Nicholas | 0:33bfb6a726ba | 84 | } |
Nicholas | 0:33bfb6a726ba | 85 | |
Nicholas | 0:33bfb6a726ba | 86 | //sets the speed |
Nicholas | 0:33bfb6a726ba | 87 | void variables () { |
Nicholas | 0:33bfb6a726ba | 88 | if (Buffer[0] == 'M') { |
Nicholas | 0:33bfb6a726ba | 89 | //as everything is in a string, numbers /begin at 48 (0, 1 etc) so anything after 48 should be a number |
Nicholas | 0:33bfb6a726ba | 90 | if (Buffer[1] >= 49) { |
Nicholas | 0:33bfb6a726ba | 91 | speed_max = ((Buffer[1] - 48) / 10); |
Nicholas | 0:33bfb6a726ba | 92 | } else { |
Nicholas | 0:33bfb6a726ba | 93 | // if it isnt a number, it can be used, say ERR |
Nicholas | 0:33bfb6a726ba | 94 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 95 | m3pi.printf("BAD"); |
Nicholas | 0:33bfb6a726ba | 96 | m3pi.locate(0,1); |
Nicholas | 0:33bfb6a726ba | 97 | m3pi.printf("SPEED"); |
Nicholas | 0:33bfb6a726ba | 98 | ERR(); |
Nicholas | 0:33bfb6a726ba | 99 | } |
Nicholas | 0:33bfb6a726ba | 100 | speed_user = speed_max; |
Nicholas | 0:33bfb6a726ba | 101 | } else { |
Nicholas | 0:33bfb6a726ba | 102 | //if nothing is there, there has been no speed defined! |
Nicholas | 0:33bfb6a726ba | 103 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 104 | m3pi.printf("NO SPEED"); |
Nicholas | 0:33bfb6a726ba | 105 | m3pi.locate(0,1); |
Nicholas | 0:33bfb6a726ba | 106 | m3pi.printf("DEFINED"); |
Nicholas | 0:33bfb6a726ba | 107 | } |
Nicholas | 0:33bfb6a726ba | 108 | } |
Nicholas | 0:33bfb6a726ba | 109 | |
Nicholas | 0:33bfb6a726ba | 110 | |
Nicholas | 0:33bfb6a726ba | 111 | void recognition() { |
Nicholas | 0:33bfb6a726ba | 112 | //this shall be used to incriment what character we are looking at |
Nicholas | 0:33bfb6a726ba | 113 | int N=2; |
Nicholas | 0:33bfb6a726ba | 114 | //repeat until the end of the document |
Nicholas | 0:33bfb6a726ba | 115 | while (!feof(CMD)) { |
Nicholas | 0:33bfb6a726ba | 116 | if (Buffer[N] == 'F' || Buffer[N] == 'f') { |
Nicholas | 0:33bfb6a726ba | 117 | N++; |
Nicholas | 0:33bfb6a726ba | 118 | //Because it's a string, the conversion will turn the number 1 into 49, as it is the 49th character that can be recognised. |
Nicholas | 0:33bfb6a726ba | 119 | while (Buffer[N] >= 48) { |
Nicholas | 0:33bfb6a726ba | 120 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 121 | m3pi.printf("FWD %c", Buffer[N]); // F represents forward and |
Nicholas | 0:33bfb6a726ba | 122 | m3pi.forward(speed_max); // a number after it represents how long |
Nicholas | 0:33bfb6a726ba | 123 | wait(Buffer[N] - 48); //to go forwards for. this senario |
Nicholas | 0:33bfb6a726ba | 124 | m3pi.stop(); //only includes the posibilty of |
Nicholas | 0:33bfb6a726ba | 125 | N++; // a one digit number. |
Nicholas | 0:33bfb6a726ba | 126 | } |
Nicholas | 0:33bfb6a726ba | 127 | } else if (Buffer[N] == 'B' || Buffer[N] == 'b') { |
Nicholas | 0:33bfb6a726ba | 128 | N++; |
Nicholas | 0:33bfb6a726ba | 129 | while (Buffer[N] >= 48) { |
Nicholas | 0:33bfb6a726ba | 130 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 131 | m3pi.printf("BACK %c", Buffer[N]); |
Nicholas | 0:33bfb6a726ba | 132 | m3pi.backward(speed_max); |
Nicholas | 0:33bfb6a726ba | 133 | wait(Buffer[N] - 48); |
Nicholas | 0:33bfb6a726ba | 134 | N++; |
Nicholas | 0:33bfb6a726ba | 135 | } |
Nicholas | 0:33bfb6a726ba | 136 | } else if (Buffer[N] == 'R' || Buffer[N] == 'r') { |
Nicholas | 0:33bfb6a726ba | 137 | N++; |
Nicholas | 0:33bfb6a726ba | 138 | while (Buffer[N] >= 48) { |
Nicholas | 0:33bfb6a726ba | 139 | int A=Buffer[N] - 48; |
Nicholas | 0:33bfb6a726ba | 140 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 141 | m3pi.printf("RGHT %d", A); |
Nicholas | 0:33bfb6a726ba | 142 | m3pi.left( - speed_max); |
Nicholas | 0:33bfb6a726ba | 143 | m3pi.right(speed_max); |
Nicholas | 0:33bfb6a726ba | 144 | wait(((speed_mod * 0.28)/9)*A); //a number after R or L represents |
Nicholas | 0:33bfb6a726ba | 145 | m3pi.stop(); //how much to turn. the algorithm |
Nicholas | 0:33bfb6a726ba | 146 | N++; //above converts that to wait times for motors |
Nicholas | 0:33bfb6a726ba | 147 | } |
Nicholas | 0:33bfb6a726ba | 148 | } else if (Buffer[N] == 'L' || Buffer[N] == 'l') { |
Nicholas | 0:33bfb6a726ba | 149 | N++; |
Nicholas | 0:33bfb6a726ba | 150 | while (Buffer[N] >= 48) { |
Nicholas | 0:33bfb6a726ba | 151 | int A=Buffer[N] - 48; |
Nicholas | 0:33bfb6a726ba | 152 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 153 | m3pi.printf("LFT %d", A); |
Nicholas | 0:33bfb6a726ba | 154 | m3pi.right( - speed_max); |
Nicholas | 0:33bfb6a726ba | 155 | m3pi.left(speed_max); |
Nicholas | 0:33bfb6a726ba | 156 | wait(((speed_mod * 0.28)/9)*A); |
Nicholas | 0:33bfb6a726ba | 157 | m3pi.stop(); |
Nicholas | 0:33bfb6a726ba | 158 | N++; |
Nicholas | 0:33bfb6a726ba | 159 | } |
Nicholas | 0:33bfb6a726ba | 160 | } else if (Buffer[N] == 'S' || Buffer[N] == 's') { |
Nicholas | 0:33bfb6a726ba | 161 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 162 | m3pi.printf("STOP"); |
Nicholas | 0:33bfb6a726ba | 163 | m3pi.stop(); |
Nicholas | 0:33bfb6a726ba | 164 | FIN(); |
Nicholas | 0:33bfb6a726ba | 165 | N++; |
Nicholas | 0:33bfb6a726ba | 166 | //if there is a * then we have reached the end of what is in Buffer |
Nicholas | 0:33bfb6a726ba | 167 | } else if (Buffer[N] == '*') { |
Nicholas | 0:33bfb6a726ba | 168 | m3pi.stop(); |
Nicholas | 0:33bfb6a726ba | 169 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 170 | m3pi.printf("FIN!"); |
Nicholas | 0:33bfb6a726ba | 171 | FIN(); |
Nicholas | 0:33bfb6a726ba | 172 | //if there is a character that is not listed above, it is probebly an error |
Nicholas | 0:33bfb6a726ba | 173 | } else { |
Nicholas | 0:33bfb6a726ba | 174 | m3pi.stop(); |
Nicholas | 0:33bfb6a726ba | 175 | m3pi.cls(); |
Nicholas | 0:33bfb6a726ba | 176 | m3pi.printf(" ERR "); |
Nicholas | 0:33bfb6a726ba | 177 | ERR(); |
Nicholas | 0:33bfb6a726ba | 178 | } |
Nicholas | 0:33bfb6a726ba | 179 | } |
Nicholas | 0:33bfb6a726ba | 180 | } |
Nicholas | 0:33bfb6a726ba | 181 | |
Nicholas | 0:33bfb6a726ba | 182 | int main() { |
Nicholas | 0:33bfb6a726ba | 183 | opening(); |
Nicholas | 0:33bfb6a726ba | 184 | grabbing(); |
Nicholas | 0:33bfb6a726ba | 185 | variables(); |
Nicholas | 0:33bfb6a726ba | 186 | speed_mod = speed_max; |
Nicholas | 0:33bfb6a726ba | 187 | wait(2.0); |
Nicholas | 0:33bfb6a726ba | 188 | recognition(); |
Nicholas | 0:33bfb6a726ba | 189 | } |