falta verificar cambio de joystick a accel

Dependencies:   mbed MPU6050

Committer:
jiuk
Date:
Sun Nov 18 20:49:38 2018 +0000
Revision:
3:81dc55b0a8d7
Parent:
2:484928b9a5b5
Child:
4:3426b2472f99
prueba velocidad accelerometro, falta visualizar mas manzanas

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jiuk 0:2dabb9782f24 1 // Read from I2C slave at address 0x62
jiuk 0:2dabb9782f24 2
jiuk 0:2dabb9782f24 3 #include "mbed.h"
jiuk 0:2dabb9782f24 4
jiuk 0:2dabb9782f24 5 #include "MPU6050.h"
jiuk 0:2dabb9782f24 6
jiuk 0:2dabb9782f24 7 Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:2dabb9782f24 8 MPU6050 Wire(PB_9 , PB_8 );
jiuk 0:2dabb9782f24 9
jiuk 0:2dabb9782f24 10 SPI deviceM(PB_15, PB_14, PB_13);
jiuk 0:2dabb9782f24 11 DigitalOut ssel (PB_12);
jiuk 0:2dabb9782f24 12 //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
jiuk 0:2dabb9782f24 13 //DigitalOut ssel (PB_9); //Chip Select para el controlador.
jiuk 0:2dabb9782f24 14 //Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:2dabb9782f24 15 Serial com_tar(PC_10,PC_11); //master f446R
jiuk 0:2dabb9782f24 16 //Serial com_tar(PA_15,PB_7); //slave f411R //habilitar la comunicacion serial a traves del puerto usb.
jiuk 2:484928b9a5b5 17 Ticker timer;
jiuk 0:2dabb9782f24 18
jiuk 0:2dabb9782f24 19 #define VEL 200 //Velocidad de actualizacion de dato en el controlador.
jiuk 0:2dabb9782f24 20 #define MIN 1
jiuk 0:2dabb9782f24 21 #define MAX 8
jiuk 0:2dabb9782f24 22 #define MINC 128
jiuk 0:2dabb9782f24 23 #define MAXC 1
jiuk 0:2dabb9782f24 24 #define DEL 0.1
jiuk 3:81dc55b0a8d7 25 #define GIRO 2
jiuk 3:81dc55b0a8d7 26 #define G_DIAG 2
jiuk 0:2dabb9782f24 27
jiuk 2:484928b9a5b5 28 int columna=1,manzanas=0;
jiuk 0:2dabb9782f24 29 int fila=1, fil=1, colum=1;
jiuk 2:484928b9a5b5 30 void blink();
jiuk 0:2dabb9782f24 31
jiuk 2:484928b9a5b5 32 //--------------------------------------------------
jiuk 2:484928b9a5b5 33 //-------------------TICKER------------------------
jiuk 2:484928b9a5b5 34 //--------------------------------------------------
jiuk 2:484928b9a5b5 35 void attime() {
jiuk 2:484928b9a5b5 36 blink();
jiuk 2:484928b9a5b5 37 };
jiuk 2:484928b9a5b5 38
jiuk 2:484928b9a5b5 39 //--------------------------------------------------
jiuk 2:484928b9a5b5 40 //-------------------SPI------------------------
jiuk 2:484928b9a5b5 41 //--------------------------------------------------
jiuk 0:2dabb9782f24 42 void sendSPI(uint8_t d1, uint8_t d2)
jiuk 0:2dabb9782f24 43 {
jiuk 0:2dabb9782f24 44 deviceM.unlock();
jiuk 0:2dabb9782f24 45 ssel=0;
jiuk 0:2dabb9782f24 46 deviceM.write(d1);
jiuk 0:2dabb9782f24 47 deviceM.write(d2);
jiuk 0:2dabb9782f24 48 ssel=1;
jiuk 0:2dabb9782f24 49 deviceM.lock();
jiuk 2:484928b9a5b5 50 };
jiuk 2:484928b9a5b5 51
jiuk 0:2dabb9782f24 52
jiuk 2:484928b9a5b5 53 //--------------------------------------------------
jiuk 2:484928b9a5b5 54 //-------------------TEST------------------------
jiuk 2:484928b9a5b5 55 //--------------------------------------------------
jiuk 0:2dabb9782f24 56 void test() //test
jiuk 0:2dabb9782f24 57 {
jiuk 0:2dabb9782f24 58 sendSPI(0x09,0); //no decodificacion
jiuk 0:2dabb9782f24 59 sendSPI(0x0A,0x00); //intensidad
jiuk 0:2dabb9782f24 60 sendSPI(0x0B,0x07); //usa 7 leds
jiuk 0:2dabb9782f24 61 sendSPI(0x0C,1); //no apaga
jiuk 0:2dabb9782f24 62 sendSPI(0x0F,0); //operacion normal
jiuk 0:2dabb9782f24 63 }
jiuk 0:2dabb9782f24 64
jiuk 2:484928b9a5b5 65
jiuk 2:484928b9a5b5 66 //--------------------------------------------------
jiuk 2:484928b9a5b5 67 //-------------------BORRAR------------------------
jiuk 2:484928b9a5b5 68 //--------------------------------------------------
jiuk 0:2dabb9782f24 69 void borrar() //borrar toda la matriz;
jiuk 0:2dabb9782f24 70 {
jiuk 0:2dabb9782f24 71 int i;
jiuk 0:2dabb9782f24 72 for(i=0;i<=8;i++)
jiuk 0:2dabb9782f24 73 {
jiuk 0:2dabb9782f24 74 sendSPI(i,0);
jiuk 0:2dabb9782f24 75 }
jiuk 0:2dabb9782f24 76 }
jiuk 0:2dabb9782f24 77
jiuk 2:484928b9a5b5 78
jiuk 2:484928b9a5b5 79 //--------------------------------------------------
jiuk 2:484928b9a5b5 80 //-------------------GENERAR_PUNTO------------------------
jiuk 2:484928b9a5b5 81 //--------------------------------------------------
jiuk 0:2dabb9782f24 82 void generar_punto()
jiuk 0:2dabb9782f24 83 {
jiuk 2:484928b9a5b5 84 int col[8]={1,2,4,8,16,32,64,128};
jiuk 0:2dabb9782f24 85 //int fil=0;
jiuk 0:2dabb9782f24 86 borrar();
jiuk 2:484928b9a5b5 87 int _fil=fil;
jiuk 2:484928b9a5b5 88 fil= rand() % 7+1;
jiuk 0:2dabb9782f24 89 int y= rand() % 7+1;
jiuk 0:2dabb9782f24 90 command.printf("\n\n Fila: %d",fil);
jiuk 0:2dabb9782f24 91 command.printf("\n Columna:%d",col[y]);
jiuk 0:2dabb9782f24 92 //int time=0;
jiuk 0:2dabb9782f24 93 //while(time<4)
jiuk 0:2dabb9782f24 94 // {
jiuk 2:484928b9a5b5 95 if(_fil==fil)
jiuk 2:484928b9a5b5 96 colum=colum+col[y];
jiuk 2:484928b9a5b5 97 colum=col[y];
jiuk 0:2dabb9782f24 98 if(fil==fila){
jiuk 0:2dabb9782f24 99 int guarda_linea=columna+colum;
jiuk 0:2dabb9782f24 100 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 101 }
jiuk 0:2dabb9782f24 102 else
jiuk 0:2dabb9782f24 103 sendSPI(fil,col[y]);
jiuk 2:484928b9a5b5 104
jiuk 0:2dabb9782f24 105 // wait_ms(VEL);
jiuk 0:2dabb9782f24 106 // sendSPI(fil,0);
jiuk 0:2dabb9782f24 107 // wait_ms(VEL);
jiuk 0:2dabb9782f24 108 // time++;
jiuk 0:2dabb9782f24 109 // }
jiuk 0:2dabb9782f24 110
jiuk 0:2dabb9782f24 111 }
jiuk 0:2dabb9782f24 112
jiuk 2:484928b9a5b5 113 //--------------------------------------------------
jiuk 2:484928b9a5b5 114 //-------------------BLINK------------------------
jiuk 2:484928b9a5b5 115 //--------------------------------------------------
jiuk 2:484928b9a5b5 116 void blink(){
jiuk 2:484928b9a5b5 117 sendSPI(fil,0);
jiuk 2:484928b9a5b5 118 if(fil==fila)
jiuk 2:484928b9a5b5 119 sendSPI(fila,columna);
jiuk 2:484928b9a5b5 120 }
jiuk 2:484928b9a5b5 121
jiuk 2:484928b9a5b5 122 //--------------------------------------------------
jiuk 2:484928b9a5b5 123 //-------------------REVISAR_MOV------------------------
jiuk 2:484928b9a5b5 124 //--------------------------------------------------
jiuk 0:2dabb9782f24 125 void revisar_mov(uint8_t correr){
jiuk 0:2dabb9782f24 126 switch (correr){
jiuk 3:81dc55b0a8d7 127
jiuk 3:81dc55b0a8d7 128 case 2:
jiuk 3:81dc55b0a8d7 129 columna=columna<<1;
jiuk 3:81dc55b0a8d7 130 fila--;
jiuk 3:81dc55b0a8d7 131 if (columna>MINC)
jiuk 3:81dc55b0a8d7 132 columna=MAXC;
jiuk 3:81dc55b0a8d7 133 if (fila<MIN){
jiuk 3:81dc55b0a8d7 134 fila=MAX;
jiuk 3:81dc55b0a8d7 135 sendSPI(MIN,0);
jiuk 3:81dc55b0a8d7 136 }
jiuk 3:81dc55b0a8d7 137 sendSPI(fila+1,0);
jiuk 3:81dc55b0a8d7 138 break;
jiuk 3:81dc55b0a8d7 139
jiuk 3:81dc55b0a8d7 140 case 4:
jiuk 3:81dc55b0a8d7 141 columna=columna<<1;
jiuk 3:81dc55b0a8d7 142 fila++;
jiuk 3:81dc55b0a8d7 143 if (fila>MAX){
jiuk 3:81dc55b0a8d7 144 fila=MIN;
jiuk 3:81dc55b0a8d7 145 sendSPI(MAX,0);
jiuk 3:81dc55b0a8d7 146 }
jiuk 3:81dc55b0a8d7 147 if (columna>MINC)
jiuk 3:81dc55b0a8d7 148 columna=MAXC;
jiuk 3:81dc55b0a8d7 149 sendSPI(fila-1,0);
jiuk 3:81dc55b0a8d7 150 break;
jiuk 3:81dc55b0a8d7 151
jiuk 3:81dc55b0a8d7 152 case 1:
jiuk 3:81dc55b0a8d7 153 columna=columna>>1;
jiuk 3:81dc55b0a8d7 154 fila--;
jiuk 3:81dc55b0a8d7 155 if (columna<MAXC)
jiuk 3:81dc55b0a8d7 156 columna=MINC;
jiuk 3:81dc55b0a8d7 157 if (fila<MIN){
jiuk 3:81dc55b0a8d7 158 fila=MAX;
jiuk 3:81dc55b0a8d7 159 sendSPI(MIN,0);
jiuk 3:81dc55b0a8d7 160 }
jiuk 3:81dc55b0a8d7 161 sendSPI(fila+1,0);
jiuk 3:81dc55b0a8d7 162 //command.printf("\n case1 Col:%d",columna);
jiuk 3:81dc55b0a8d7 163 //command.printf("\n FIL:%d",fila);
jiuk 3:81dc55b0a8d7 164 break;
jiuk 3:81dc55b0a8d7 165
jiuk 3:81dc55b0a8d7 166 case 3:
jiuk 3:81dc55b0a8d7 167 columna=columna>>1;
jiuk 3:81dc55b0a8d7 168 fila++;
jiuk 3:81dc55b0a8d7 169 if (columna<MAXC)
jiuk 3:81dc55b0a8d7 170 columna=MINC;
jiuk 3:81dc55b0a8d7 171 if (fila>MAX){
jiuk 3:81dc55b0a8d7 172 fila=MIN;
jiuk 3:81dc55b0a8d7 173 sendSPI(MAX,0);
jiuk 3:81dc55b0a8d7 174 }
jiuk 3:81dc55b0a8d7 175 sendSPI(fila-1,0);
jiuk 3:81dc55b0a8d7 176 //command.printf("\n case3 Col:%d",columna);
jiuk 3:81dc55b0a8d7 177 //command.printf("\n FIL:%d",fila);
jiuk 3:81dc55b0a8d7 178 break;
jiuk 3:81dc55b0a8d7 179
jiuk 0:2dabb9782f24 180 case 28:
jiuk 0:2dabb9782f24 181 columna=columna<<1;
jiuk 0:2dabb9782f24 182 if (columna>MINC)
jiuk 0:2dabb9782f24 183 columna=MAXC;
jiuk 0:2dabb9782f24 184 break;
jiuk 0:2dabb9782f24 185
jiuk 0:2dabb9782f24 186 case 29:
jiuk 0:2dabb9782f24 187 columna=columna>>1;
jiuk 0:2dabb9782f24 188 if (columna<MAXC)
jiuk 0:2dabb9782f24 189 columna=MINC;
jiuk 0:2dabb9782f24 190 break;
jiuk 0:2dabb9782f24 191
jiuk 0:2dabb9782f24 192 case 30:
jiuk 0:2dabb9782f24 193 fila--;
jiuk 3:81dc55b0a8d7 194 if (fila<MIN){
jiuk 3:81dc55b0a8d7 195 fila=MAX;
jiuk 3:81dc55b0a8d7 196 sendSPI(MIN,0);
jiuk 3:81dc55b0a8d7 197 }
jiuk 0:2dabb9782f24 198 sendSPI(fila+1,0);
jiuk 0:2dabb9782f24 199 break;
jiuk 0:2dabb9782f24 200
jiuk 0:2dabb9782f24 201 case 31:
jiuk 0:2dabb9782f24 202 fila++;
jiuk 3:81dc55b0a8d7 203 if (fila>MAX){
jiuk 3:81dc55b0a8d7 204 fila=MIN;
jiuk 3:81dc55b0a8d7 205 sendSPI(MAX,0);
jiuk 3:81dc55b0a8d7 206 }
jiuk 0:2dabb9782f24 207 sendSPI(fila-1,0);
jiuk 0:2dabb9782f24 208 break;
jiuk 1:efcead3fb5eb 209
jiuk 1:efcead3fb5eb 210 case 0:
jiuk 1:efcead3fb5eb 211 break;
jiuk 0:2dabb9782f24 212 }
jiuk 0:2dabb9782f24 213 }
jiuk 2:484928b9a5b5 214
jiuk 3:81dc55b0a8d7 215 //--------------------------------------------------
jiuk 3:81dc55b0a8d7 216 //-------------------VELOCIDAD-----------------------
jiuk 3:81dc55b0a8d7 217 //--------------------------------------------------
jiuk 3:81dc55b0a8d7 218 int velocidad(float _acc[3])
jiuk 3:81dc55b0a8d7 219 {
jiuk 3:81dc55b0a8d7 220 uint8_t vel;
jiuk 3:81dc55b0a8d7 221 int acc[3];
jiuk 3:81dc55b0a8d7 222 acc[0]=_acc[0];
jiuk 3:81dc55b0a8d7 223 acc[1]=_acc[1];
jiuk 3:81dc55b0a8d7 224 if((acc[0]>GIRO) || (acc[0]<-GIRO) || (acc[1]>GIRO) || (acc[1]<-GIRO))
jiuk 3:81dc55b0a8d7 225 {
jiuk 3:81dc55b0a8d7 226 if(acc[0]>GIRO)
jiuk 3:81dc55b0a8d7 227 vel=acc[0];
jiuk 3:81dc55b0a8d7 228 else if(acc[0]<-GIRO)
jiuk 3:81dc55b0a8d7 229 vel=-acc[0];
jiuk 3:81dc55b0a8d7 230 else if(acc[1]>GIRO)
jiuk 3:81dc55b0a8d7 231 vel=acc[1];
jiuk 3:81dc55b0a8d7 232 else if(acc[1]<-GIRO)
jiuk 3:81dc55b0a8d7 233 vel=-acc[1];
jiuk 3:81dc55b0a8d7 234 command.printf("---Velocidad if %d \n", vel);
jiuk 3:81dc55b0a8d7 235 wait(3/vel);
jiuk 3:81dc55b0a8d7 236 }
jiuk 3:81dc55b0a8d7 237 int v=1/vel;
jiuk 3:81dc55b0a8d7 238
jiuk 3:81dc55b0a8d7 239 command.printf("Velocidad %d \n", v);
jiuk 3:81dc55b0a8d7 240 command.printf("x %d \n", acc[1]);
jiuk 3:81dc55b0a8d7 241 command.printf("y %d \n", acc[0]);
jiuk 3:81dc55b0a8d7 242 return vel;
jiuk 3:81dc55b0a8d7 243 }
jiuk 2:484928b9a5b5 244
jiuk 2:484928b9a5b5 245 //--------------------------------------------------
jiuk 2:484928b9a5b5 246 //-------------------BUSCADOR------------------------
jiuk 2:484928b9a5b5 247 //--------------------------------------------------
jiuk 0:2dabb9782f24 248 void buscador()
jiuk 0:2dabb9782f24 249 {
jiuk 0:2dabb9782f24 250 uint8_t correr;
jiuk 0:2dabb9782f24 251 //float gyro[3]; Wire.getGyro(gyro);
jiuk 0:2dabb9782f24 252 float acc[3]; Wire.getAccelero(acc);
jiuk 2:484928b9a5b5 253 //command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]);
jiuk 0:2dabb9782f24 254 //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]);
jiuk 3:81dc55b0a8d7 255 velocidad(acc);
jiuk 3:81dc55b0a8d7 256 //command.printf("Velocidad %d \n", vel);
jiuk 3:81dc55b0a8d7 257 if(acc[0]>GIRO) //eje x derecha
jiuk 3:81dc55b0a8d7 258 {
jiuk 3:81dc55b0a8d7 259 if(acc[1]>G_DIAG) //eje y arriba
jiuk 3:81dc55b0a8d7 260 correr=1;
jiuk 3:81dc55b0a8d7 261 else if(acc[1]<-G_DIAG)//eje y abajo
jiuk 3:81dc55b0a8d7 262 correr=2;
jiuk 3:81dc55b0a8d7 263 else
jiuk 3:81dc55b0a8d7 264 correr=30;
jiuk 3:81dc55b0a8d7 265 }
jiuk 3:81dc55b0a8d7 266 else if(acc[0]<-GIRO) //eje x izquierda
jiuk 3:81dc55b0a8d7 267 {
jiuk 3:81dc55b0a8d7 268 if(acc[1]>G_DIAG) //eje y arriba
jiuk 3:81dc55b0a8d7 269 correr=3;
jiuk 3:81dc55b0a8d7 270 else if(acc[1]<-G_DIAG)//eje y abajo
jiuk 3:81dc55b0a8d7 271 correr=4;
jiuk 3:81dc55b0a8d7 272 else
jiuk 3:81dc55b0a8d7 273 correr=31;
jiuk 3:81dc55b0a8d7 274 }
jiuk 3:81dc55b0a8d7 275 else if(acc[1]>GIRO) //eje y arriba
jiuk 3:81dc55b0a8d7 276 correr=29;
jiuk 3:81dc55b0a8d7 277 else if(acc[1]<-GIRO) //eje y abajo
jiuk 3:81dc55b0a8d7 278 correr=28;
jiuk 3:81dc55b0a8d7 279 else
jiuk 3:81dc55b0a8d7 280 correr=0; //
jiuk 0:2dabb9782f24 281 revisar_mov(correr);
jiuk 0:2dabb9782f24 282 //wait (DEL);
jiuk 2:484928b9a5b5 283 //command.printf("\n correr:\n %d",correr);
jiuk 0:2dabb9782f24 284 }
jiuk 0:2dabb9782f24 285
jiuk 2:484928b9a5b5 286
jiuk 2:484928b9a5b5 287 //--------------------------------------------------
jiuk 2:484928b9a5b5 288 //-------------------COMER------------------------
jiuk 2:484928b9a5b5 289 //--------------------------------------------------
jiuk 0:2dabb9782f24 290 int comer ()
jiuk 0:2dabb9782f24 291 {
jiuk 0:2dabb9782f24 292 int x=0;
jiuk 2:484928b9a5b5 293 if((fil==fila)&&((colum & columna) != 0))
jiuk 2:484928b9a5b5 294 {
jiuk 2:484928b9a5b5 295 manzanas++;
jiuk 2:484928b9a5b5 296 sendSPI(fila,columna);
jiuk 2:484928b9a5b5 297 for(int i=0;i<manzanas;i++)
jiuk 2:484928b9a5b5 298 {
jiuk 2:484928b9a5b5 299 int guarda_linea=columna+colum;
jiuk 2:484928b9a5b5 300 sendSPI(fila,guarda_linea);
jiuk 2:484928b9a5b5 301 generar_punto();
jiuk 2:484928b9a5b5 302 sendSPI(fil,colum);
jiuk 2:484928b9a5b5 303 //com_tar.putc(x);
jiuk 2:484928b9a5b5 304 }
jiuk 0:2dabb9782f24 305 x=1;
jiuk 2:484928b9a5b5 306 }
jiuk 0:2dabb9782f24 307 return x;
jiuk 2:484928b9a5b5 308 }
jiuk 2:484928b9a5b5 309
jiuk 0:2dabb9782f24 310
jiuk 2:484928b9a5b5 311 //--------------------------------------------------
jiuk 2:484928b9a5b5 312 //-------------------VALIDAR_CONTRINCANTE------------------------
jiuk 2:484928b9a5b5 313 //--------------------------------------------------
jiuk 0:2dabb9782f24 314 int validar_contrincante(){
jiuk 0:2dabb9782f24 315 int x=com_tar.getc();
jiuk 0:2dabb9782f24 316 return x;
jiuk 0:2dabb9782f24 317 }
jiuk 0:2dabb9782f24 318
jiuk 0:2dabb9782f24 319 int main ()
jiuk 0:2dabb9782f24 320 {
jiuk 0:2dabb9782f24 321 int guarda_linea,x=0;
jiuk 2:484928b9a5b5 322 timer.attach(&attime, 0.3);
jiuk 0:2dabb9782f24 323 test();
jiuk 0:2dabb9782f24 324 borrar();
jiuk 0:2dabb9782f24 325 sendSPI(fila,columna);
jiuk 0:2dabb9782f24 326 generar_punto();
jiuk 0:2dabb9782f24 327 while(1)
jiuk 0:2dabb9782f24 328 {
jiuk 0:2dabb9782f24 329 buscador();
jiuk 0:2dabb9782f24 330 //validar_contrincante();
jiuk 0:2dabb9782f24 331 x=comer();
jiuk 2:484928b9a5b5 332 //command.printf("\n comer:\n %d",x);
jiuk 0:2dabb9782f24 333 if (x==0)
jiuk 0:2dabb9782f24 334 {
jiuk 0:2dabb9782f24 335 if(fil==fila){
jiuk 0:2dabb9782f24 336 guarda_linea=columna+colum;
jiuk 0:2dabb9782f24 337 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 338 }
jiuk 0:2dabb9782f24 339 else
jiuk 0:2dabb9782f24 340 {
jiuk 0:2dabb9782f24 341 guarda_linea=columna;
jiuk 0:2dabb9782f24 342 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 343 sendSPI(fil,colum);
jiuk 0:2dabb9782f24 344 }
jiuk 0:2dabb9782f24 345 }
jiuk 0:2dabb9782f24 346 else{
jiuk 0:2dabb9782f24 347 sendSPI(fila,columna);
jiuk 0:2dabb9782f24 348 }
jiuk 0:2dabb9782f24 349 //generar_punto();
jiuk 0:2dabb9782f24 350 }
jiuk 0:2dabb9782f24 351 }