mbedbidos
/
recoge_manzanas_7
Funciona con acelero-metro, falta comunicación y blink.
recoge_manzanas.cpp@5:1292da7b7c6f, 2018-11-20 (annotated)
- Committer:
- jiuk
- Date:
- Tue Nov 20 08:30:45 2018 +0000
- Revision:
- 5:1292da7b7c6f
- Parent:
- 4:3426b2472f99
- Child:
- 6:eadec7d43859
cambio de funciones de movimiento.
Who changed what in which revision?
User | Revision | Line number | New 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 | 4:3426b2472f99 | 18 | DigitalIn pulsador(PH_1); |
jiuk | 4:3426b2472f99 | 19 | AnalogIn in1(PC_2); |
jiuk | 4:3426b2472f99 | 20 | AnalogIn in2(PC_3); |
jiuk | 5:1292da7b7c6f | 21 | DigitalOut led(LED1); |
jiuk | 0:2dabb9782f24 | 22 | |
jiuk | 0:2dabb9782f24 | 23 | #define VEL 200 //Velocidad de actualizacion de dato en el controlador. |
jiuk | 0:2dabb9782f24 | 24 | #define MIN 1 |
jiuk | 0:2dabb9782f24 | 25 | #define MAX 8 |
jiuk | 0:2dabb9782f24 | 26 | #define MINC 128 |
jiuk | 0:2dabb9782f24 | 27 | #define MAXC 1 |
jiuk | 0:2dabb9782f24 | 28 | #define DEL 0.1 |
jiuk | 5:1292da7b7c6f | 29 | #define GIRO 3 |
jiuk | 5:1292da7b7c6f | 30 | #define G_DIAG 3 |
jiuk | 0:2dabb9782f24 | 31 | |
jiuk | 5:1292da7b7c6f | 32 | int columna=1, manzanas=0, fila=1, fil=1, colum=0; |
jiuk | 5:1292da7b7c6f | 33 | uint8_t _guarda_manzanas[8]={0,0,0,0,0,0,0,0}; |
jiuk | 4:3426b2472f99 | 34 | bool _boton=0; |
jiuk | 2:484928b9a5b5 | 35 | void blink(); |
jiuk | 4:3426b2472f99 | 36 | void boton(); |
jiuk | 5:1292da7b7c6f | 37 | void guarda_manzana(); |
jiuk | 5:1292da7b7c6f | 38 | void imprime_manzanas(); |
jiuk | 5:1292da7b7c6f | 39 | |
jiuk | 5:1292da7b7c6f | 40 | //-------------------------------------------------- |
jiuk | 5:1292da7b7c6f | 41 | //-------------------GUARDA_MANZANAS------------------------ |
jiuk | 5:1292da7b7c6f | 42 | //-------------------------------------------------- |
jiuk | 5:1292da7b7c6f | 43 | void guarda_manzana() |
jiuk | 5:1292da7b7c6f | 44 | { |
jiuk | 5:1292da7b7c6f | 45 | _guarda_manzanas[fil]=_guarda_manzanas[fil]+colum; |
jiuk | 5:1292da7b7c6f | 46 | } |
jiuk | 0:2dabb9782f24 | 47 | |
jiuk | 2:484928b9a5b5 | 48 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 49 | //-------------------TICKER------------------------ |
jiuk | 2:484928b9a5b5 | 50 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 51 | void attime() { |
jiuk | 2:484928b9a5b5 | 52 | blink(); |
jiuk | 5:1292da7b7c6f | 53 | boton(); |
jiuk | 2:484928b9a5b5 | 54 | }; |
jiuk | 4:3426b2472f99 | 55 | |
jiuk | 4:3426b2472f99 | 56 | //-------------------------------------------------- |
jiuk | 4:3426b2472f99 | 57 | //-------------------BOTON------------------------ |
jiuk | 4:3426b2472f99 | 58 | //-------------------------------------------------- |
jiuk | 4:3426b2472f99 | 59 | void boton() |
jiuk | 4:3426b2472f99 | 60 | { |
jiuk | 5:1292da7b7c6f | 61 | if (pulsador == 0) |
jiuk | 4:3426b2472f99 | 62 | { |
jiuk | 5:1292da7b7c6f | 63 | command.printf("Button pressed %b\n", _boton); |
jiuk | 5:1292da7b7c6f | 64 | _boton= !_boton; |
jiuk | 4:3426b2472f99 | 65 | } |
jiuk | 4:3426b2472f99 | 66 | } |
jiuk | 5:1292da7b7c6f | 67 | |
jiuk | 2:484928b9a5b5 | 68 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 69 | //-------------------SPI------------------------ |
jiuk | 2:484928b9a5b5 | 70 | //-------------------------------------------------- |
jiuk | 0:2dabb9782f24 | 71 | void sendSPI(uint8_t d1, uint8_t d2) |
jiuk | 0:2dabb9782f24 | 72 | { |
jiuk | 0:2dabb9782f24 | 73 | deviceM.unlock(); |
jiuk | 0:2dabb9782f24 | 74 | ssel=0; |
jiuk | 0:2dabb9782f24 | 75 | deviceM.write(d1); |
jiuk | 0:2dabb9782f24 | 76 | deviceM.write(d2); |
jiuk | 0:2dabb9782f24 | 77 | ssel=1; |
jiuk | 0:2dabb9782f24 | 78 | deviceM.lock(); |
jiuk | 2:484928b9a5b5 | 79 | }; |
jiuk | 2:484928b9a5b5 | 80 | |
jiuk | 2:484928b9a5b5 | 81 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 82 | //-------------------TEST------------------------ |
jiuk | 2:484928b9a5b5 | 83 | //-------------------------------------------------- |
jiuk | 0:2dabb9782f24 | 84 | void test() //test |
jiuk | 0:2dabb9782f24 | 85 | { |
jiuk | 0:2dabb9782f24 | 86 | sendSPI(0x09,0); //no decodificacion |
jiuk | 4:3426b2472f99 | 87 | sendSPI(0x0A,0x9); //intensidad |
jiuk | 0:2dabb9782f24 | 88 | sendSPI(0x0B,0x07); //usa 7 leds |
jiuk | 0:2dabb9782f24 | 89 | sendSPI(0x0C,1); //no apaga |
jiuk | 0:2dabb9782f24 | 90 | sendSPI(0x0F,0); //operacion normal |
jiuk | 0:2dabb9782f24 | 91 | } |
jiuk | 0:2dabb9782f24 | 92 | |
jiuk | 2:484928b9a5b5 | 93 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 94 | //-------------------BORRAR------------------------ |
jiuk | 2:484928b9a5b5 | 95 | //-------------------------------------------------- |
jiuk | 0:2dabb9782f24 | 96 | void borrar() //borrar toda la matriz; |
jiuk | 0:2dabb9782f24 | 97 | { |
jiuk | 0:2dabb9782f24 | 98 | int i; |
jiuk | 0:2dabb9782f24 | 99 | for(i=0;i<=8;i++) |
jiuk | 0:2dabb9782f24 | 100 | { |
jiuk | 0:2dabb9782f24 | 101 | sendSPI(i,0); |
jiuk | 0:2dabb9782f24 | 102 | } |
jiuk | 0:2dabb9782f24 | 103 | } |
jiuk | 0:2dabb9782f24 | 104 | |
jiuk | 5:1292da7b7c6f | 105 | //-------------------------------------------------- |
jiuk | 5:1292da7b7c6f | 106 | //-------------------IMPRIME_MANZANAS------------------------ |
jiuk | 5:1292da7b7c6f | 107 | //-------------------------------------------------- |
jiuk | 5:1292da7b7c6f | 108 | void imprime_manzanas() |
jiuk | 5:1292da7b7c6f | 109 | { |
jiuk | 5:1292da7b7c6f | 110 | for(int i=0;i<9;i++) |
jiuk | 5:1292da7b7c6f | 111 | { |
jiuk | 5:1292da7b7c6f | 112 | if(i==fila) |
jiuk | 5:1292da7b7c6f | 113 | { |
jiuk | 5:1292da7b7c6f | 114 | uint8_t x=_guarda_manzanas[i]+columna; |
jiuk | 5:1292da7b7c6f | 115 | sendSPI(i,x); |
jiuk | 5:1292da7b7c6f | 116 | command.printf("if=fila.imprime_manzanas: X=%d\n", x); |
jiuk | 5:1292da7b7c6f | 117 | } |
jiuk | 5:1292da7b7c6f | 118 | else |
jiuk | 5:1292da7b7c6f | 119 | { |
jiuk | 5:1292da7b7c6f | 120 | sendSPI(i,_guarda_manzanas[i]); |
jiuk | 5:1292da7b7c6f | 121 | command.printf("else.imprime_manzanas: %d\n", _guarda_manzanas[i]); |
jiuk | 5:1292da7b7c6f | 122 | } |
jiuk | 5:1292da7b7c6f | 123 | } |
jiuk | 5:1292da7b7c6f | 124 | } |
jiuk | 2:484928b9a5b5 | 125 | |
jiuk | 2:484928b9a5b5 | 126 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 127 | //-------------------GENERAR_PUNTO------------------------ |
jiuk | 2:484928b9a5b5 | 128 | //-------------------------------------------------- |
jiuk | 0:2dabb9782f24 | 129 | void generar_punto() |
jiuk | 0:2dabb9782f24 | 130 | { |
jiuk | 2:484928b9a5b5 | 131 | int col[8]={1,2,4,8,16,32,64,128}; |
jiuk | 2:484928b9a5b5 | 132 | int _fil=fil; |
jiuk | 2:484928b9a5b5 | 133 | fil= rand() % 7+1; |
jiuk | 0:2dabb9782f24 | 134 | int y= rand() % 7+1; |
jiuk | 5:1292da7b7c6f | 135 | int colum=col[y]; |
jiuk | 2:484928b9a5b5 | 136 | if(_fil==fil) |
jiuk | 5:1292da7b7c6f | 137 | _guarda_manzanas[fil]=colum+_guarda_manzanas[fil]; |
jiuk | 0:2dabb9782f24 | 138 | else |
jiuk | 5:1292da7b7c6f | 139 | _guarda_manzanas[fil]=colum; |
jiuk | 5:1292da7b7c6f | 140 | command.printf("Generar_punto: %d\n", _guarda_manzanas[fil]); |
jiuk | 5:1292da7b7c6f | 141 | imprime_manzanas(); |
jiuk | 0:2dabb9782f24 | 142 | } |
jiuk | 0:2dabb9782f24 | 143 | |
jiuk | 2:484928b9a5b5 | 144 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 145 | //-------------------BLINK------------------------ |
jiuk | 2:484928b9a5b5 | 146 | //-------------------------------------------------- |
jiuk | 5:1292da7b7c6f | 147 | void blink() |
jiuk | 5:1292da7b7c6f | 148 | { |
jiuk | 5:1292da7b7c6f | 149 | borrar(); |
jiuk | 5:1292da7b7c6f | 150 | sendSPI(fila,columna); |
jiuk | 5:1292da7b7c6f | 151 | wait(0.05); |
jiuk | 5:1292da7b7c6f | 152 | } |
jiuk | 2:484928b9a5b5 | 153 | |
jiuk | 2:484928b9a5b5 | 154 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 155 | //-------------------REVISAR_MOV------------------------ |
jiuk | 2:484928b9a5b5 | 156 | //-------------------------------------------------- |
jiuk | 0:2dabb9782f24 | 157 | void revisar_mov(uint8_t correr){ |
jiuk | 5:1292da7b7c6f | 158 | //uint8_t guardar_linea=0; |
jiuk | 5:1292da7b7c6f | 159 | switch (correr) |
jiuk | 5:1292da7b7c6f | 160 | { |
jiuk | 3:81dc55b0a8d7 | 161 | |
jiuk | 3:81dc55b0a8d7 | 162 | case 2: |
jiuk | 5:1292da7b7c6f | 163 | columna=columna<<1; |
jiuk | 5:1292da7b7c6f | 164 | fila--; |
jiuk | 5:1292da7b7c6f | 165 | if (columna>MINC) |
jiuk | 5:1292da7b7c6f | 166 | columna=MAXC; |
jiuk | 5:1292da7b7c6f | 167 | if (fila<MIN) |
jiuk | 5:1292da7b7c6f | 168 | { |
jiuk | 5:1292da7b7c6f | 169 | fila=MAX; |
jiuk | 5:1292da7b7c6f | 170 | uint8_t x=_guarda_manzanas[MIN]; |
jiuk | 5:1292da7b7c6f | 171 | sendSPI(MIN,0); |
jiuk | 5:1292da7b7c6f | 172 | } |
jiuk | 5:1292da7b7c6f | 173 | sendSPI(fila+1,0); |
jiuk | 3:81dc55b0a8d7 | 174 | break; |
jiuk | 3:81dc55b0a8d7 | 175 | |
jiuk | 3:81dc55b0a8d7 | 176 | case 4: |
jiuk | 5:1292da7b7c6f | 177 | columna=columna<<1; |
jiuk | 5:1292da7b7c6f | 178 | fila++; |
jiuk | 5:1292da7b7c6f | 179 | if (fila>MAX){ |
jiuk | 5:1292da7b7c6f | 180 | fila=MIN; |
jiuk | 5:1292da7b7c6f | 181 | sendSPI(MAX,0); |
jiuk | 5:1292da7b7c6f | 182 | } |
jiuk | 5:1292da7b7c6f | 183 | if (columna>MINC) |
jiuk | 5:1292da7b7c6f | 184 | columna=MAXC; |
jiuk | 5:1292da7b7c6f | 185 | sendSPI(fila-1,0); |
jiuk | 3:81dc55b0a8d7 | 186 | break; |
jiuk | 3:81dc55b0a8d7 | 187 | |
jiuk | 3:81dc55b0a8d7 | 188 | case 1: |
jiuk | 5:1292da7b7c6f | 189 | columna=columna>>1; |
jiuk | 5:1292da7b7c6f | 190 | fila--; |
jiuk | 5:1292da7b7c6f | 191 | if (columna<MAXC) |
jiuk | 5:1292da7b7c6f | 192 | columna=MINC; |
jiuk | 5:1292da7b7c6f | 193 | if (fila<MIN){ |
jiuk | 5:1292da7b7c6f | 194 | fila=MAX; |
jiuk | 5:1292da7b7c6f | 195 | sendSPI(MIN,0); |
jiuk | 5:1292da7b7c6f | 196 | } |
jiuk | 5:1292da7b7c6f | 197 | sendSPI(fila+1,0); |
jiuk | 3:81dc55b0a8d7 | 198 | break; |
jiuk | 3:81dc55b0a8d7 | 199 | |
jiuk | 3:81dc55b0a8d7 | 200 | case 3: |
jiuk | 5:1292da7b7c6f | 201 | columna=columna>>1; |
jiuk | 5:1292da7b7c6f | 202 | fila++; |
jiuk | 5:1292da7b7c6f | 203 | if (columna<MAXC) |
jiuk | 5:1292da7b7c6f | 204 | columna=MINC; |
jiuk | 5:1292da7b7c6f | 205 | if (fila>MAX){ |
jiuk | 5:1292da7b7c6f | 206 | fila=MIN; |
jiuk | 5:1292da7b7c6f | 207 | sendSPI(MAX,0); |
jiuk | 5:1292da7b7c6f | 208 | } |
jiuk | 5:1292da7b7c6f | 209 | sendSPI(fila-1,0); |
jiuk | 3:81dc55b0a8d7 | 210 | break; |
jiuk | 3:81dc55b0a8d7 | 211 | |
jiuk | 0:2dabb9782f24 | 212 | case 28: |
jiuk | 5:1292da7b7c6f | 213 | columna=columna<<1; |
jiuk | 5:1292da7b7c6f | 214 | if (columna>MINC) |
jiuk | 5:1292da7b7c6f | 215 | columna=MAXC; |
jiuk | 0:2dabb9782f24 | 216 | break; |
jiuk | 0:2dabb9782f24 | 217 | |
jiuk | 0:2dabb9782f24 | 218 | case 29: |
jiuk | 5:1292da7b7c6f | 219 | columna=columna>>1; |
jiuk | 5:1292da7b7c6f | 220 | if (columna<MAXC) |
jiuk | 5:1292da7b7c6f | 221 | columna=MINC; |
jiuk | 0:2dabb9782f24 | 222 | break; |
jiuk | 0:2dabb9782f24 | 223 | |
jiuk | 0:2dabb9782f24 | 224 | case 30: |
jiuk | 5:1292da7b7c6f | 225 | fila--; |
jiuk | 5:1292da7b7c6f | 226 | if (fila<MIN){ |
jiuk | 5:1292da7b7c6f | 227 | fila=MAX; |
jiuk | 5:1292da7b7c6f | 228 | sendSPI(MIN,0); |
jiuk | 5:1292da7b7c6f | 229 | } |
jiuk | 5:1292da7b7c6f | 230 | sendSPI(fila+1,0); |
jiuk | 0:2dabb9782f24 | 231 | break; |
jiuk | 0:2dabb9782f24 | 232 | |
jiuk | 0:2dabb9782f24 | 233 | case 31: |
jiuk | 5:1292da7b7c6f | 234 | fila++; |
jiuk | 5:1292da7b7c6f | 235 | if (fila>MAX){ |
jiuk | 5:1292da7b7c6f | 236 | fila=MIN; |
jiuk | 5:1292da7b7c6f | 237 | sendSPI(MAX,0); |
jiuk | 5:1292da7b7c6f | 238 | } |
jiuk | 5:1292da7b7c6f | 239 | sendSPI(fila-1,0); |
jiuk | 0:2dabb9782f24 | 240 | break; |
jiuk | 1:efcead3fb5eb | 241 | |
jiuk | 1:efcead3fb5eb | 242 | case 0: |
jiuk | 1:efcead3fb5eb | 243 | break; |
jiuk | 0:2dabb9782f24 | 244 | } |
jiuk | 0:2dabb9782f24 | 245 | } |
jiuk | 2:484928b9a5b5 | 246 | |
jiuk | 3:81dc55b0a8d7 | 247 | //-------------------------------------------------- |
jiuk | 3:81dc55b0a8d7 | 248 | //-------------------VELOCIDAD----------------------- |
jiuk | 3:81dc55b0a8d7 | 249 | //-------------------------------------------------- |
jiuk | 3:81dc55b0a8d7 | 250 | int velocidad(float _acc[3]) |
jiuk | 3:81dc55b0a8d7 | 251 | { |
jiuk | 5:1292da7b7c6f | 252 | int vel=0,x=2,y=4; // |
jiuk | 3:81dc55b0a8d7 | 253 | int acc[3]; |
jiuk | 4:3426b2472f99 | 254 | acc[0]=(int)_acc[0]; |
jiuk | 4:3426b2472f99 | 255 | acc[1]=(int)_acc[1]; |
jiuk | 3:81dc55b0a8d7 | 256 | if((acc[0]>GIRO) || (acc[0]<-GIRO) || (acc[1]>GIRO) || (acc[1]<-GIRO)) |
jiuk | 3:81dc55b0a8d7 | 257 | { |
jiuk | 4:3426b2472f99 | 258 | if((acc[0]>(GIRO+x)) || (acc[0]<-(GIRO+x)) || (acc[1]>(GIRO+x)) || (acc[1]<-(GIRO+x))) |
jiuk | 4:3426b2472f99 | 259 | { |
jiuk | 4:3426b2472f99 | 260 | vel=300; |
jiuk | 4:3426b2472f99 | 261 | if((acc[0]>(GIRO+y)) || (acc[0]<-(GIRO+y)) || (acc[1]>(GIRO+y)) || (acc[1]<-(GIRO+y))) |
jiuk | 4:3426b2472f99 | 262 | vel=100; |
jiuk | 4:3426b2472f99 | 263 | } |
jiuk | 4:3426b2472f99 | 264 | else |
jiuk | 4:3426b2472f99 | 265 | vel=500; |
jiuk | 3:81dc55b0a8d7 | 266 | } |
jiuk | 3:81dc55b0a8d7 | 267 | return vel; |
jiuk | 3:81dc55b0a8d7 | 268 | } |
jiuk | 2:484928b9a5b5 | 269 | |
jiuk | 2:484928b9a5b5 | 270 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 271 | //-------------------BUSCADOR------------------------ |
jiuk | 2:484928b9a5b5 | 272 | //-------------------------------------------------- |
jiuk | 5:1292da7b7c6f | 273 | //command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]); |
jiuk | 5:1292da7b7c6f | 274 | //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]); |
jiuk | 5:1292da7b7c6f | 275 | //float gyro[3]; Wire.getGyro(gyro); |
jiuk | 5:1292da7b7c6f | 276 | //command.printf("Accelerometer: \t X= %d, \t Y= %d, \n", acc[0],acc[1]); |
jiuk | 5:1292da7b7c6f | 277 | |
jiuk | 0:2dabb9782f24 | 278 | void buscador() |
jiuk | 0:2dabb9782f24 | 279 | { |
jiuk | 0:2dabb9782f24 | 280 | uint8_t correr; |
jiuk | 4:3426b2472f99 | 281 | int y=0,x=0,acc[3],v; |
jiuk | 4:3426b2472f99 | 282 | float _acc[3]; |
jiuk | 5:1292da7b7c6f | 283 | |
jiuk | 5:1292da7b7c6f | 284 | if(_boton==1) |
jiuk | 5:1292da7b7c6f | 285 | { |
jiuk | 5:1292da7b7c6f | 286 | Wire.getAccelero(_acc); |
jiuk | 5:1292da7b7c6f | 287 | acc[0]=((int)_acc[0])+2; |
jiuk | 5:1292da7b7c6f | 288 | acc[1]=((int)_acc[1])+2; |
jiuk | 5:1292da7b7c6f | 289 | } |
jiuk | 5:1292da7b7c6f | 290 | else |
jiuk | 4:3426b2472f99 | 291 | { |
jiuk | 4:3426b2472f99 | 292 | x=((int)(in1.read()*-650))+13; |
jiuk | 4:3426b2472f99 | 293 | y=((int)(in2.read()*-650))+13; |
jiuk | 4:3426b2472f99 | 294 | acc[0]=x; |
jiuk | 4:3426b2472f99 | 295 | acc[1]=y; |
jiuk | 5:1292da7b7c6f | 296 | //command.printf("Joystick: \t X= %d, \t Y= %d, \n", x,y); |
jiuk | 4:3426b2472f99 | 297 | } |
jiuk | 4:3426b2472f99 | 298 | |
jiuk | 5:1292da7b7c6f | 299 | //command.printf("Velocidad %d \n", v); |
jiuk | 5:1292da7b7c6f | 300 | if(acc[0]>GIRO) //eje x derecha |
jiuk | 3:81dc55b0a8d7 | 301 | { |
jiuk | 5:1292da7b7c6f | 302 | if(acc[1]>G_DIAG) //eje y arriba |
jiuk | 3:81dc55b0a8d7 | 303 | correr=1; |
jiuk | 3:81dc55b0a8d7 | 304 | else if(acc[1]<-G_DIAG)//eje y abajo |
jiuk | 3:81dc55b0a8d7 | 305 | correr=2; |
jiuk | 3:81dc55b0a8d7 | 306 | else |
jiuk | 3:81dc55b0a8d7 | 307 | correr=30; |
jiuk | 5:1292da7b7c6f | 308 | } |
jiuk | 5:1292da7b7c6f | 309 | else if(acc[0]<-GIRO) //eje x izquierda |
jiuk | 3:81dc55b0a8d7 | 310 | { |
jiuk | 3:81dc55b0a8d7 | 311 | if(acc[1]>G_DIAG) //eje y arriba |
jiuk | 3:81dc55b0a8d7 | 312 | correr=3; |
jiuk | 3:81dc55b0a8d7 | 313 | else if(acc[1]<-G_DIAG)//eje y abajo |
jiuk | 3:81dc55b0a8d7 | 314 | correr=4; |
jiuk | 3:81dc55b0a8d7 | 315 | else |
jiuk | 3:81dc55b0a8d7 | 316 | correr=31; |
jiuk | 5:1292da7b7c6f | 317 | } |
jiuk | 5:1292da7b7c6f | 318 | else if(acc[1]>GIRO) //eje y arriba |
jiuk | 3:81dc55b0a8d7 | 319 | correr=29; |
jiuk | 5:1292da7b7c6f | 320 | else if(acc[1]<-GIRO) //eje y abajo |
jiuk | 3:81dc55b0a8d7 | 321 | correr=28; |
jiuk | 5:1292da7b7c6f | 322 | else |
jiuk | 5:1292da7b7c6f | 323 | correr=0; |
jiuk | 4:3426b2472f99 | 324 | revisar_mov(correr); |
jiuk | 4:3426b2472f99 | 325 | v=velocidad(_acc); |
jiuk | 4:3426b2472f99 | 326 | wait_ms(v); |
jiuk | 0:2dabb9782f24 | 327 | } |
jiuk | 0:2dabb9782f24 | 328 | |
jiuk | 2:484928b9a5b5 | 329 | |
jiuk | 2:484928b9a5b5 | 330 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 331 | //-------------------COMER------------------------ |
jiuk | 2:484928b9a5b5 | 332 | //-------------------------------------------------- |
jiuk | 0:2dabb9782f24 | 333 | int comer () |
jiuk | 0:2dabb9782f24 | 334 | { |
jiuk | 0:2dabb9782f24 | 335 | int x=0; |
jiuk | 2:484928b9a5b5 | 336 | if((fil==fila)&&((colum & columna) != 0)) |
jiuk | 2:484928b9a5b5 | 337 | { |
jiuk | 2:484928b9a5b5 | 338 | manzanas++; |
jiuk | 2:484928b9a5b5 | 339 | sendSPI(fila,columna); |
jiuk | 2:484928b9a5b5 | 340 | for(int i=0;i<manzanas;i++) |
jiuk | 2:484928b9a5b5 | 341 | { |
jiuk | 2:484928b9a5b5 | 342 | int guarda_linea=columna+colum; |
jiuk | 2:484928b9a5b5 | 343 | sendSPI(fila,guarda_linea); |
jiuk | 2:484928b9a5b5 | 344 | generar_punto(); |
jiuk | 2:484928b9a5b5 | 345 | sendSPI(fil,colum); |
jiuk | 2:484928b9a5b5 | 346 | //com_tar.putc(x); |
jiuk | 2:484928b9a5b5 | 347 | } |
jiuk | 0:2dabb9782f24 | 348 | x=1; |
jiuk | 2:484928b9a5b5 | 349 | } |
jiuk | 0:2dabb9782f24 | 350 | return x; |
jiuk | 2:484928b9a5b5 | 351 | } |
jiuk | 2:484928b9a5b5 | 352 | |
jiuk | 0:2dabb9782f24 | 353 | |
jiuk | 2:484928b9a5b5 | 354 | //-------------------------------------------------- |
jiuk | 2:484928b9a5b5 | 355 | //-------------------VALIDAR_CONTRINCANTE------------------------ |
jiuk | 2:484928b9a5b5 | 356 | //-------------------------------------------------- |
jiuk | 0:2dabb9782f24 | 357 | int validar_contrincante(){ |
jiuk | 0:2dabb9782f24 | 358 | int x=com_tar.getc(); |
jiuk | 0:2dabb9782f24 | 359 | return x; |
jiuk | 0:2dabb9782f24 | 360 | } |
jiuk | 0:2dabb9782f24 | 361 | |
jiuk | 4:3426b2472f99 | 362 | |
jiuk | 4:3426b2472f99 | 363 | //-------------------------------------------------- |
jiuk | 4:3426b2472f99 | 364 | //-------------------MAIN------------------------ |
jiuk | 4:3426b2472f99 | 365 | //-------------------------------------------------- |
jiuk | 0:2dabb9782f24 | 366 | int main () |
jiuk | 0:2dabb9782f24 | 367 | { |
jiuk | 0:2dabb9782f24 | 368 | int guarda_linea,x=0; |
jiuk | 2:484928b9a5b5 | 369 | timer.attach(&attime, 0.3); |
jiuk | 0:2dabb9782f24 | 370 | test(); |
jiuk | 0:2dabb9782f24 | 371 | borrar(); |
jiuk | 0:2dabb9782f24 | 372 | sendSPI(fila,columna); |
jiuk | 0:2dabb9782f24 | 373 | generar_punto(); |
jiuk | 0:2dabb9782f24 | 374 | while(1) |
jiuk | 0:2dabb9782f24 | 375 | { |
jiuk | 0:2dabb9782f24 | 376 | buscador(); |
jiuk | 0:2dabb9782f24 | 377 | //validar_contrincante(); |
jiuk | 0:2dabb9782f24 | 378 | x=comer(); |
jiuk | 2:484928b9a5b5 | 379 | //command.printf("\n comer:\n %d",x); |
jiuk | 0:2dabb9782f24 | 380 | if (x==0) |
jiuk | 0:2dabb9782f24 | 381 | { |
jiuk | 0:2dabb9782f24 | 382 | if(fil==fila){ |
jiuk | 0:2dabb9782f24 | 383 | guarda_linea=columna+colum; |
jiuk | 0:2dabb9782f24 | 384 | sendSPI(fila,guarda_linea); |
jiuk | 0:2dabb9782f24 | 385 | } |
jiuk | 0:2dabb9782f24 | 386 | else |
jiuk | 0:2dabb9782f24 | 387 | { |
jiuk | 0:2dabb9782f24 | 388 | guarda_linea=columna; |
jiuk | 0:2dabb9782f24 | 389 | sendSPI(fila,guarda_linea); |
jiuk | 0:2dabb9782f24 | 390 | sendSPI(fil,colum); |
jiuk | 0:2dabb9782f24 | 391 | } |
jiuk | 0:2dabb9782f24 | 392 | } |
jiuk | 0:2dabb9782f24 | 393 | else{ |
jiuk | 0:2dabb9782f24 | 394 | sendSPI(fila,columna); |
jiuk | 0:2dabb9782f24 | 395 | } |
jiuk | 0:2dabb9782f24 | 396 | //generar_punto(); |
jiuk | 0:2dabb9782f24 | 397 | } |
jiuk | 0:2dabb9782f24 | 398 | } |