OX V1

Dependencies:   mbed

Committer:
arthicha
Date:
Mon Dec 05 16:11:19 2016 +0000
Revision:
2:84a84429750a
Parent:
1:6514e03fe62f
hhh;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
arthicha 0:6182212860fb 1 #include "mbed.h"
arthicha 0:6182212860fb 2 #include "Moop.h"
arthicha 0:6182212860fb 3 #include "bmu.h"
arthicha 0:6182212860fb 4 #include "Latrix.h"
arthicha 0:6182212860fb 5 #include "zmu9250.h"
arthicha 0:6182212860fb 6 #include "MPU9250.h"
arthicha 1:6514e03fe62f 7 #include "time.h"
arthicha 0:6182212860fb 8
arthicha 0:6182212860fb 9 InterruptIn lim(PinName D3);
arthicha 0:6182212860fb 10 InterruptIn rotate(PinName D4);
arthicha 0:6182212860fb 11 InterruptIn choose(PinName D5);
arthicha 0:6182212860fb 12 Ticker flipper;
arthicha 0:6182212860fb 13 Ticker Update;
arthicha 0:6182212860fb 14
arthicha 0:6182212860fb 15 Moop map;
arthicha 0:6182212860fb 16
arthicha 0:6182212860fb 17 Serial pc(PinName USBTX,PinName USBRX);
arthicha 0:6182212860fb 18 Serial mas(PinName D8,PinName D2);
arthicha 0:6182212860fb 19
arthicha 0:6182212860fb 20 bmuimu imu;
arthicha 0:6182212860fb 21 Latrix ledMatrix;
arthicha 0:6182212860fb 22
arthicha 1:6514e03fe62f 23 Timer t;
arthicha 0:6182212860fb 24
arthicha 1:6514e03fe62f 25 bool anach ;
arthicha 1:6514e03fe62f 26 bool buttonState ; // button limit switchm rotate and choose state respectively.
arthicha 1:6514e03fe62f 27 unsigned int count; // count down.
arthicha 0:6182212860fb 28 char data[50];
arthicha 0:6182212860fb 29 char elem;
arthicha 1:6514e03fe62f 30 bool error ; // store error in ambigous imu data.
arthicha 1:6514e03fe62f 31 int helperRoll , helperPitch,helperYaw;
arthicha 1:6514e03fe62f 32 unsigned int indx ;
arthicha 1:6514e03fe62f 33 unsigned int state ;
arthicha 0:6182212860fb 34
arthicha 0:6182212860fb 35 // state 0 = initial / start.
arthicha 0:6182212860fb 36 // 1 = waiting for limit switch.
arthicha 0:6182212860fb 37
arthicha 1:6514e03fe62f 38 unsigned int turn ; // turn.
arthicha 0:6182212860fb 39
arthicha 1:6514e03fe62f 40 unsigned int x , y , z ,lx,ly,lz;
arthicha 1:6514e03fe62f 41 int winStatus ;
arthicha 0:6182212860fb 42
arthicha 0:6182212860fb 43 void Rise(int but)
arthicha 0:6182212860fb 44 {
arthicha 0:6182212860fb 45 buttonState[but-3] = 1;
arthicha 0:6182212860fb 46 }
arthicha 0:6182212860fb 47
arthicha 0:6182212860fb 48 void Fall(int but)
arthicha 0:6182212860fb 49 {
arthicha 0:6182212860fb 50 buttonState[but-3] = 0;
arthicha 0:6182212860fb 51 }
arthicha 0:6182212860fb 52
arthicha 0:6182212860fb 53 void Update()
arthicha 0:6182212860fb 54 {
arthicha 0:6182212860fb 55 imu.Update();
arthicha 0:6182212860fb 56 }
arthicha 0:6182212860fb 57
arthicha 0:6182212860fb 58 void Sent(char arrayi[],float it,int siz)
arthicha 0:6182212860fb 59 {
arthicha 0:6182212860fb 60 for (int i = 0;i<siz;i++)
arthicha 0:6182212860fb 61 {
arthicha 0:6182212860fb 62 mas.putc(arrayi[i]);
arthicha 0:6182212860fb 63 wait(it);
arthicha 0:6182212860fb 64 }
arthicha 0:6182212860fb 65 }
arthicha 0:6182212860fb 66
arthicha 0:6182212860fb 67 void getStr()
arthicha 0:6182212860fb 68 {
arthicha 0:6182212860fb 69 while (mas.readable())
arthicha 0:6182212860fb 70 {
arthicha 0:6182212860fb 71
arthicha 0:6182212860fb 72 elem = mas.getc();
arthicha 0:6182212860fb 73 pc.printf("char is %d\n\r",elem);
arthicha 0:6182212860fb 74 if ((elem == '\n'))
arthicha 0:6182212860fb 75 {
arthicha 0:6182212860fb 76 pc.printf("string is %s\n\r",data);
arthicha 0:6182212860fb 77 for(int i=0;i<indx;i++)
arthicha 0:6182212860fb 78 {
arthicha 0:6182212860fb 79 if (indx > 2)
arthicha 0:6182212860fb 80 {
arthicha 0:6182212860fb 81 dete[i] = data[i];
arthicha 0:6182212860fb 82 }
arthicha 0:6182212860fb 83 }
arthicha 0:6182212860fb 84 for(int i=0;i<30;i++)
arthicha 0:6182212860fb 85 {
arthicha 0:6182212860fb 86 data[i] = '\0';
arthicha 0:6182212860fb 87 }
arthicha 0:6182212860fb 88 indx = 0;
arthicha 0:6182212860fb 89 break;
arthicha 0:6182212860fb 90 }else
arthicha 0:6182212860fb 91 {
arthicha 0:6182212860fb 92 data[indx] = elem;
arthicha 0:6182212860fb 93 indx += 1;
arthicha 0:6182212860fb 94 }
arthicha 0:6182212860fb 95
arthicha 0:6182212860fb 96 }
arthicha 0:6182212860fb 97 }
arthicha 0:6182212860fb 98
arthicha 0:6182212860fb 99 void ShowMap()
arthicha 0:6182212860fb 100 {
arthicha 0:6182212860fb 101 int mmapx[5][5][5] = {0};
arthicha 0:6182212860fb 102 int mmapy[5][5][5] = {0};
arthicha 0:6182212860fb 103 for(int i=0;i<5;i++)
arthicha 0:6182212860fb 104 {
arthicha 0:6182212860fb 105 for(int j = 0;j<5;j++)
arthicha 0:6182212860fb 106 {
arthicha 0:6182212860fb 107 for (int k =0;k<5;k++)
arthicha 0:6182212860fb 108 {
arthicha 0:6182212860fb 109 mmapx[i][j] = map.check1[i][j][k]*pow(2,k);
arthicha 0:6182212860fb 110 Sent(char(mmapx[i]+64)[j],0.1,1);
arthicha 0:6182212860fb 111 Sent("\n",0.1,1);
arthicha 0:6182212860fb 112 mmapy[i][j] = map.check2[i][j][k]*pow(2,k);
arthicha 0:6182212860fb 113 Sent(char(mmapy[i]+64)[j],0.1,1);
arthicha 0:6182212860fb 114 Sent("\n",0.1,1);
arthicha 0:6182212860fb 115 }
arthicha 0:6182212860fb 116 }
arthicha 0:6182212860fb 117 }
arthicha 0:6182212860fb 118 int page = 0;
arthicha 0:6182212860fb 119 while(1)
arthicha 0:6182212860fb 120 {
arthicha 0:6182212860fb 121 if (page == 5)
arthicha 0:6182212860fb 122 {
arthicha 0:6182212860fb 123 page = 4;
arthicha 0:6182212860fb 124 }else if(page == -1)
arthicha 0:6182212860fb 125 {
arthicha 0:6182212860fb 126 page = 0;
arthicha 0:6182212860fb 127 }
arthicha 0:6182212860fb 128 ledMatrix.Show(mmapx[page],mmapy[page]);
arthicha 0:6182212860fb 129 if(buttonState[2]==1)
arthicha 0:6182212860fb 130 {
arthicha 0:6182212860fb 131 while(buttonState[2]==1)
arthicha 0:6182212860fb 132 {
arthicha 0:6182212860fb 133
arthicha 0:6182212860fb 134 }
arthicha 0:6182212860fb 135 page += 1;
arthicha 0:6182212860fb 136 }
arthicha 0:6182212860fb 137 else if(buttonState[1]==1)
arthicha 0:6182212860fb 138 {
arthicha 0:6182212860fb 139 while(buttonState[1]==1)
arthicha 0:6182212860fb 140 {
arthicha 0:6182212860fb 141
arthicha 0:6182212860fb 142 }
arthicha 0:6182212860fb 143 page -= 1;
arthicha 0:6182212860fb 144 }
arthicha 0:6182212860fb 145 }
arthicha 0:6182212860fb 146 }
arthicha 0:6182212860fb 147
arthicha 0:6182212860fb 148 void Loose()
arthicha 0:6182212860fb 149 {
arthicha 0:6182212860fb 150 ledMatrix.Loose();
arthicha 0:6182212860fb 151 Sent("los\n",0.1,4);
arthicha 0:6182212860fb 152 ShowMap();
arthicha 0:6182212860fb 153 }
arthicha 0:6182212860fb 154
arthicha 0:6182212860fb 155 void Wiin()
arthicha 0:6182212860fb 156 {
arthicha 0:6182212860fb 157 ledMatrix.Wiin();
arthicha 0:6182212860fb 158 Sent("lwin\n",0.1,4);
arthicha 0:6182212860fb 159 ShowMap();
arthicha 0:6182212860fb 160 }
arthicha 0:6182212860fb 161
arthicha 0:6182212860fb 162
arthicha 0:6182212860fb 163
arthicha 0:6182212860fb 164 bool chooseAnother()
arthicha 0:6182212860fb 165 {
arthicha 0:6182212860fb 166 Sent('cho\n',0.1,4);
arthicha 0:6182212860fb 167 while(1)
arthicha 0:6182212860fb 168 {
arthicha 0:6182212860fb 169 if ((dete[0]='c')&&(dete[1]='h')&&(dete[2]='0'))
arthicha 0:6182212860fb 170 {
arthicha 0:6182212860fb 171 return true;
arthicha 0:6182212860fb 172 }else if ((dete[0]='c')&&(dete[1]='h')&&(dete[2]='x'))
arthicha 0:6182212860fb 173 {
arthicha 0:6182212860fb 174 return false;
arthicha 0:6182212860fb 175 }
arthicha 0:6182212860fb 176 }
arthicha 0:6182212860fb 177 }
arthicha 0:6182212860fb 178
arthicha 0:6182212860fb 179 bool anotherRotate()
arthicha 0:6182212860fb 180 {
arthicha 0:6182212860fb 181 Sent('rot\n',0.1,4);
arthicha 0:6182212860fb 182 while(1)
arthicha 0:6182212860fb 183 {
arthicha 0:6182212860fb 184 if ((dete[0]='r')&&(dete[1]='o')&&(dete[2]='t'))
arthicha 0:6182212860fb 185 {
arthicha 0:6182212860fb 186 return true;
arthicha 0:6182212860fb 187 }else if ((dete[0]='r')&&(dete[1]='o')&&(dete[2]='x'))
arthicha 0:6182212860fb 188 {
arthicha 0:6182212860fb 189 return false;
arthicha 0:6182212860fb 190 }
arthicha 0:6182212860fb 191 }
arthicha 0:6182212860fb 192 }
arthicha 0:6182212860fb 193
arthicha 0:6182212860fb 194 int main()// main program of
arthicha 1:6514e03fe62f 195 {
arthicha 1:6514e03fe62f 196 anach = false;
arthicha 1:6514e03fe62f 197 buttonState = {0,0,0};
arthicha 1:6514e03fe62f 198 count = 10;
arthicha 1:6514e03fe62f 199 error = false;
arthicha 1:6514e03fe62f 200 indx = 0
arthicha 1:6514e03fe62f 201 helperRoll = 0
arthicha 1:6514e03fe62f 202 state = 0;
arthicha 1:6514e03fe62f 203 winStatus = 0;
arthicha 1:6514e03fe62f 204 turn = 0;
arthicha 1:6514e03fe62f 205 helperPitch=0,helperYaw=0;
arthicha 1:6514e03fe62f 206
arthicha 0:6182212860fb 207 lim.rise(&Rise(3));
arthicha 0:6182212860fb 208 lim.fall(&Fall(3));
arthicha 0:6182212860fb 209 rotate.rise(&Rise(4));
arthicha 0:6182212860fb 210 rotate.fall(&Fall(4));
arthicha 0:6182212860fb 211 choose.rise(&Rise(5));
arthicha 0:6182212860fb 212 choose.fall(&Fall(5));
arthicha 1:6514e03fe62f 213 x = 0, y = 0, z = 0,lx=0,ly=0,lz=0;
arthicha 0:6182212860fb 214
arthicha 0:6182212860fb 215 ledMatrix.Initial(); // set initial pattern in led matrix.
arthicha 0:6182212860fb 216 wait(5); // wait 5 second for putting the box down to set initial position of imu.
arthicha 0:6182212860fb 217 //imu.Initial(); // set initial for imu.
arthicha 0:6182212860fb 218 Update.attach(&Update,1.0); //timer interrupt for recieving bluetooth.
arthicha 0:6182212860fb 219 while((dete[0]!='i')||(dete[1]!='n')||(dete[2]!='i')||(dete[3]!='t'))
arthicha 0:6182212860fb 220 {
arthicha 0:6182212860fb 221 }
arthicha 0:6182212860fb 222 ledMatrix.Wbox(); // set led to tell the player that the game is ready and this is the woman-box.
arthicha 0:6182212860fb 223 t.start();
arthicha 0:6182212860fb 224 while (1) // void loop.
arthicha 0:6182212860fb 225 {
arthicha 0:6182212860fb 226 state = 1;
arthicha 0:6182212860fb 227
arthicha 0:6182212860fb 228 if (buttonState[0] == 1) // if the boxes are connected.
arthicha 0:6182212860fb 229 {
arthicha 0:6182212860fb 230 x = map.GetCoordinateX();
arthicha 0:6182212860fb 231 y = map.GetCoordinateY();
arthicha 0:6182212860fb 232 z = map.GetCoordinateZ(); // get the position of main box which linking with map or the box not be choosen.
arthicha 0:6182212860fb 233 /*Sent("helr\n",0.1,5); // tell the other box, that we want helper roll.
arthicha 0:6182212860fb 234 while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='r'))
arthicha 0:6182212860fb 235 {
arthicha 0:6182212860fb 236 pc.printf("srt %s\n\r",dete);
arthicha 0:6182212860fb 237 }
arthicha 0:6182212860fb 238 helperRoll = int( ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48) ); //get roll from another box.
arthicha 0:6182212860fb 239 Sent("help\n",0.1,5); // tell the other box, that we want helper roll.
arthicha 0:6182212860fb 240 while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='p'))
arthicha 0:6182212860fb 241 {
arthicha 0:6182212860fb 242 pc.printf("srt %s\n\r",dete);
arthicha 0:6182212860fb 243 }
arthicha 0:6182212860fb 244 helperPitch = int( ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48) ); //get roll from another box.
arthicha 0:6182212860fb 245 Sent("hely\n",0.1,5); // tell the other box, that we want helper roll.
arthicha 0:6182212860fb 246 while((dete[0]!='h')||(dete[1]!='e')||(dete[2]!='l')||(dete[3]!='y'))
arthicha 0:6182212860fb 247 {
arthicha 0:6182212860fb 248 pc.printf("srt %s\n\r",dete);
arthicha 0:6182212860fb 249 }
arthicha 0:6182212860fb 250 helperYaw = int( ((int(dete[4+ii])-48)*100)+((int(dete[5+ii])-48)*10)+(int(dete[6+ii])-48) ); //get roll from another box.*/
arthicha 0:6182212860fb 251 //Sent("ok\n",0.1,3); //free the otherbox.
arthicha 0:6182212860fb 252
arthicha 0:6182212860fb 253 error = imu.getposition(x,y,z)//,helperRoll,helperPitch,helperYaw); // calculate position from imu.
arthicha 0:6182212860fb 254 if (error)
arthicha 0:6182212860fb 255 {
arthicha 0:6182212860fb 256 ledMatrix.Error()
arthicha 0:6182212860fb 257 while(buttonState[0] == 1)
arthicha 0:6182212860fb 258 {
arthicha 0:6182212860fb 259
arthicha 0:6182212860fb 260 }
arthicha 0:6182212860fb 261 }else
arthicha 0:6182212860fb 262 {
arthicha 0:6182212860fb 263 lx = x;
arthicha 0:6182212860fb 264 ly = y;
arthicha 0:6182212860fb 265 lz = z;
arthicha 0:6182212860fb 266 x = imu.getX();
arthicha 0:6182212860fb 267 y = imu.getY();
arthicha 0:6182212860fb 268 z = imu.getZ(); // get new possition.
arthicha 0:6182212860fb 269
arthicha 0:6182212860fb 270 winStatus = map.Update(x,y,z)//,helperRoll); //update new position in map.
arthicha 0:6182212860fb 271
arthicha 0:6182212860fb 272 map.ChangeOX([x,y,z,lx,ly,lz])
arthicha 0:6182212860fb 273 if (winStatus == 1)
arthicha 0:6182212860fb 274 {
arthicha 0:6182212860fb 275 Wiin(); // win 3 in a row.
arthicha 0:6182212860fb 276 }else if (winStatus == -1)
arthicha 0:6182212860fb 277 {
arthicha 0:6182212860fb 278 Loose();// lose out of map or un valid position.
arthicha 0:6182212860fb 279 }else
arthicha 0:6182212860fb 280 {
arthicha 0:6182212860fb 281 Sent("mapc\n",0.1,4); //map complete.
arthicha 0:6182212860fb 282 ledMatrix.MapComplete(); // show that this position is ok.
arthicha 0:6182212860fb 283 while(1)
arthicha 0:6182212860fb 284 {
arthicha 0:6182212860fb 285 anach = chooseAnother() // wheather thr others box is choosen.
arthicha 0:6182212860fb 286 if ((buttonState[2]==1) && (!anach))
arthicha 0:6182212860fb 287 {
arthicha 0:6182212860fb 288 Sent("ch1\n",0.1,4);
arthicha 0:6182212860fb 289
arthicha 0:6182212860fb 290 map.Choose(1); // choose main box.
arthicha 0:6182212860fb 291 }else if((buttonState[2]==0) && (anach))
arthicha 0:6182212860fb 292 {
arthicha 0:6182212860fb 293 map.Choose(0); // choose another box.
arthicha 0:6182212860fb 294
arthicha 0:6182212860fb 295 }else if(buttonState[2]==0)
arthicha 0:6182212860fb 296 {
arthicha 0:6182212860fb 297 if((buttonState[1]==0)||(anotherRotate())) // click buutton some rotate box.
arthicha 0:6182212860fb 298 {
arthicha 0:6182212860fb 299 state = 3;
arthicha 0:6182212860fb 300 while((buttonState[1]==0)&&(anotherRotate()))
arthicha 0:6182212860fb 301 {
arthicha 0:6182212860fb 302
arthicha 0:6182212860fb 303 }
arthicha 0:6182212860fb 304
arthicha 0:6182212860fb 305 int yyaaww = imu.Rotate(); // check wheater rotating is error.
arthicha 0:6182212860fb 306 if (false)
arthicha 0:6182212860fb 307 {
arthicha 0:6182212860fb 308 ledMatrix.Error();
arthicha 0:6182212860fb 309
arthicha 0:6182212860fb 310 }else
arthicha 0:6182212860fb 311 {
arthicha 0:6182212860fb 312 map.Rotate(0,0,yyaaww)//imu.Rotate_Yaw); // private variable/arttribute in imu class.
arthicha 0:6182212860fb 313 ledMatrix.RotateComplete();
arthicha 0:6182212860fb 314 }
arthicha 0:6182212860fb 315 }
arthicha 0:6182212860fb 316 } //close choose or rotate.
arthicha 0:6182212860fb 317 } //close while true.
arthicha 0:6182212860fb 318 turn += 1;
arthicha 0:6182212860fb 319 Sent("T++\n",0.1,4);
arthicha 0:6182212860fb 320 t.start();
arthicha 0:6182212860fb 321 state = 2;
arthicha 0:6182212860fb 322 ledMatrix.ChooseComplete();
arthicha 0:6182212860fb 323 } // close winState.
arthicha 0:6182212860fb 324 } // close not error.
arthicha 0:6182212860fb 325
arthicha 0:6182212860fb 326 }else // if the boxes aren't connect.
arthicha 0:6182212860fb 327 {
arthicha 0:6182212860fb 328 if (count == 0) // time out.
arthicha 0:6182212860fb 329 {
arthicha 0:6182212860fb 330 t.stop();
arthicha 0:6182212860fb 331 Loose(); // lose state
arthicha 0:6182212860fb 332 }else if(turn != 0)
arthicha 0:6182212860fb 333 {
arthicha 0:6182212860fb 334 t.stop();
arthicha 0:6182212860fb 335 if (t.read() > 1.0);
arthicha 0:6182212860fb 336 {
arthicha 0:6182212860fb 337 t.start();
arthicha 0:6182212860fb 338 count -= 1; // time decreasing.
arthicha 0:6182212860fb 339 }
arthicha 0:6182212860fb 340 } // close check time
arthicha 0:6182212860fb 341 ledMatrix.CountDown(count);.
arthicha 0:6182212860fb 342 Sent("Tur\n",0.1,4)
arthicha 0:6182212860fb 343 } // close boxes aren't connect.
arthicha 0:6182212860fb 344
arthicha 0:6182212860fb 345 } // close loop.
arthicha 0:6182212860fb 346
arthicha 0:6182212860fb 347
arthicha 0:6182212860fb 348
arthicha 0:6182212860fb 349 } // close main.*/