mbedbidos
/
recoge_manzanas_5
Funciona con acelero-metro, falta comunicación y blink.
recoge_manzanas.cpp@0:2dabb9782f24, 2018-11-09 (annotated)
- Committer:
- jiuk
- Date:
- Fri Nov 09 12:49:54 2018 +0000
- Revision:
- 0:2dabb9782f24
- Child:
- 1:efcead3fb5eb
Funciona con acelero metro falta blink de la manzana.
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 | 0:2dabb9782f24 | 17 | |
jiuk | 0:2dabb9782f24 | 18 | #define VEL 200 //Velocidad de actualizacion de dato en el controlador. |
jiuk | 0:2dabb9782f24 | 19 | #define MIN 1 |
jiuk | 0:2dabb9782f24 | 20 | #define MAX 8 |
jiuk | 0:2dabb9782f24 | 21 | #define MINC 128 |
jiuk | 0:2dabb9782f24 | 22 | #define MAXC 1 |
jiuk | 0:2dabb9782f24 | 23 | #define DEL 0.1 |
jiuk | 0:2dabb9782f24 | 24 | |
jiuk | 0:2dabb9782f24 | 25 | int columna=1; |
jiuk | 0:2dabb9782f24 | 26 | int fila=1, fil=1, colum=1; |
jiuk | 0:2dabb9782f24 | 27 | |
jiuk | 0:2dabb9782f24 | 28 | void sendSPI(uint8_t d1, uint8_t d2) |
jiuk | 0:2dabb9782f24 | 29 | { |
jiuk | 0:2dabb9782f24 | 30 | deviceM.unlock(); |
jiuk | 0:2dabb9782f24 | 31 | ssel=0; |
jiuk | 0:2dabb9782f24 | 32 | deviceM.write(d1); |
jiuk | 0:2dabb9782f24 | 33 | deviceM.write(d2); |
jiuk | 0:2dabb9782f24 | 34 | ssel=1; |
jiuk | 0:2dabb9782f24 | 35 | deviceM.lock(); |
jiuk | 0:2dabb9782f24 | 36 | } |
jiuk | 0:2dabb9782f24 | 37 | |
jiuk | 0:2dabb9782f24 | 38 | void test() //test |
jiuk | 0:2dabb9782f24 | 39 | { |
jiuk | 0:2dabb9782f24 | 40 | sendSPI(0x09,0); //no decodificacion |
jiuk | 0:2dabb9782f24 | 41 | sendSPI(0x0A,0x00); //intensidad |
jiuk | 0:2dabb9782f24 | 42 | sendSPI(0x0B,0x07); //usa 7 leds |
jiuk | 0:2dabb9782f24 | 43 | sendSPI(0x0C,1); //no apaga |
jiuk | 0:2dabb9782f24 | 44 | sendSPI(0x0F,0); //operacion normal |
jiuk | 0:2dabb9782f24 | 45 | } |
jiuk | 0:2dabb9782f24 | 46 | |
jiuk | 0:2dabb9782f24 | 47 | void borrar() //borrar toda la matriz; |
jiuk | 0:2dabb9782f24 | 48 | { |
jiuk | 0:2dabb9782f24 | 49 | int i; |
jiuk | 0:2dabb9782f24 | 50 | for(i=0;i<=8;i++) |
jiuk | 0:2dabb9782f24 | 51 | { |
jiuk | 0:2dabb9782f24 | 52 | sendSPI(i,0); |
jiuk | 0:2dabb9782f24 | 53 | } |
jiuk | 0:2dabb9782f24 | 54 | } |
jiuk | 0:2dabb9782f24 | 55 | |
jiuk | 0:2dabb9782f24 | 56 | void generar_punto() |
jiuk | 0:2dabb9782f24 | 57 | { |
jiuk | 0:2dabb9782f24 | 58 | int col[8]={0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000}; |
jiuk | 0:2dabb9782f24 | 59 | //int fil=0; |
jiuk | 0:2dabb9782f24 | 60 | borrar(); |
jiuk | 0:2dabb9782f24 | 61 | fil= rand() % 7+1; |
jiuk | 0:2dabb9782f24 | 62 | int y= rand() % 7+1; |
jiuk | 0:2dabb9782f24 | 63 | command.printf("\n\n Fila: %d",fil); |
jiuk | 0:2dabb9782f24 | 64 | command.printf("\n Columna:%d",col[y]); |
jiuk | 0:2dabb9782f24 | 65 | //int time=0; |
jiuk | 0:2dabb9782f24 | 66 | //while(time<4) |
jiuk | 0:2dabb9782f24 | 67 | // { |
jiuk | 0:2dabb9782f24 | 68 | if(fil==fila){ |
jiuk | 0:2dabb9782f24 | 69 | int guarda_linea=columna+colum; |
jiuk | 0:2dabb9782f24 | 70 | sendSPI(fila,guarda_linea); |
jiuk | 0:2dabb9782f24 | 71 | } |
jiuk | 0:2dabb9782f24 | 72 | else |
jiuk | 0:2dabb9782f24 | 73 | sendSPI(fil,col[y]); |
jiuk | 0:2dabb9782f24 | 74 | colum=col[y]; |
jiuk | 0:2dabb9782f24 | 75 | // wait_ms(VEL); |
jiuk | 0:2dabb9782f24 | 76 | // sendSPI(fil,0); |
jiuk | 0:2dabb9782f24 | 77 | // wait_ms(VEL); |
jiuk | 0:2dabb9782f24 | 78 | // time++; |
jiuk | 0:2dabb9782f24 | 79 | // } |
jiuk | 0:2dabb9782f24 | 80 | |
jiuk | 0:2dabb9782f24 | 81 | } |
jiuk | 0:2dabb9782f24 | 82 | |
jiuk | 0:2dabb9782f24 | 83 | void revisar_mov(uint8_t correr){ |
jiuk | 0:2dabb9782f24 | 84 | switch (correr){ |
jiuk | 0:2dabb9782f24 | 85 | case 28: |
jiuk | 0:2dabb9782f24 | 86 | columna=columna<<1; |
jiuk | 0:2dabb9782f24 | 87 | if (columna>MINC) |
jiuk | 0:2dabb9782f24 | 88 | columna=MAXC; |
jiuk | 0:2dabb9782f24 | 89 | break; |
jiuk | 0:2dabb9782f24 | 90 | |
jiuk | 0:2dabb9782f24 | 91 | case 29: |
jiuk | 0:2dabb9782f24 | 92 | columna=columna>>1; |
jiuk | 0:2dabb9782f24 | 93 | if (columna<MAXC) |
jiuk | 0:2dabb9782f24 | 94 | columna=MINC; |
jiuk | 0:2dabb9782f24 | 95 | break; |
jiuk | 0:2dabb9782f24 | 96 | |
jiuk | 0:2dabb9782f24 | 97 | case 30: |
jiuk | 0:2dabb9782f24 | 98 | fila--; |
jiuk | 0:2dabb9782f24 | 99 | sendSPI(fila+1,0); |
jiuk | 0:2dabb9782f24 | 100 | if (fila<MIN) |
jiuk | 0:2dabb9782f24 | 101 | fila=MAX; |
jiuk | 0:2dabb9782f24 | 102 | sendSPI(fila+1,0); |
jiuk | 0:2dabb9782f24 | 103 | break; |
jiuk | 0:2dabb9782f24 | 104 | |
jiuk | 0:2dabb9782f24 | 105 | case 31: |
jiuk | 0:2dabb9782f24 | 106 | fila++; |
jiuk | 0:2dabb9782f24 | 107 | sendSPI(fila-1,0); |
jiuk | 0:2dabb9782f24 | 108 | if (fila>MAX) |
jiuk | 0:2dabb9782f24 | 109 | fila=MIN; |
jiuk | 0:2dabb9782f24 | 110 | sendSPI(fila-1,0); |
jiuk | 0:2dabb9782f24 | 111 | break; |
jiuk | 0:2dabb9782f24 | 112 | } |
jiuk | 0:2dabb9782f24 | 113 | } |
jiuk | 0:2dabb9782f24 | 114 | |
jiuk | 0:2dabb9782f24 | 115 | void buscador() |
jiuk | 0:2dabb9782f24 | 116 | { |
jiuk | 0:2dabb9782f24 | 117 | |
jiuk | 0:2dabb9782f24 | 118 | uint8_t correr; |
jiuk | 0:2dabb9782f24 | 119 | //float gyro[3]; Wire.getGyro(gyro); |
jiuk | 0:2dabb9782f24 | 120 | float acc[3]; Wire.getAccelero(acc); |
jiuk | 0:2dabb9782f24 | 121 | command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]); |
jiuk | 0:2dabb9782f24 | 122 | //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]); |
jiuk | 0:2dabb9782f24 | 123 | |
jiuk | 0:2dabb9782f24 | 124 | wait(DEL); |
jiuk | 0:2dabb9782f24 | 125 | /* |
jiuk | 0:2dabb9782f24 | 126 | char t=1; |
jiuk | 0:2dabb9782f24 | 127 | while (t==1 && command.readable()==0){ |
jiuk | 0:2dabb9782f24 | 128 | wait_ms(0.1); |
jiuk | 0:2dabb9782f24 | 129 | t=0; |
jiuk | 0:2dabb9782f24 | 130 | } |
jiuk | 0:2dabb9782f24 | 131 | if (command.readable()){ |
jiuk | 0:2dabb9782f24 | 132 | if(acc[0]<4) |
jiuk | 0:2dabb9782f24 | 133 | correr=28; |
jiuk | 0:2dabb9782f24 | 134 | if(acc[0]<-4) |
jiuk | 0:2dabb9782f24 | 135 | correr=29; |
jiuk | 0:2dabb9782f24 | 136 | if(acc[1]<4) |
jiuk | 0:2dabb9782f24 | 137 | correr=30; |
jiuk | 0:2dabb9782f24 | 138 | if(acc[1]<-4) |
jiuk | 0:2dabb9782f24 | 139 | correr=31; |
jiuk | 0:2dabb9782f24 | 140 | |
jiuk | 0:2dabb9782f24 | 141 | //com_tar.putc(correr); |
jiuk | 0:2dabb9782f24 | 142 | wait_ms(100); |
jiuk | 0:2dabb9782f24 | 143 | } |
jiuk | 0:2dabb9782f24 | 144 | |
jiuk | 0:2dabb9782f24 | 145 | |
jiuk | 0:2dabb9782f24 | 146 | correr=command.getc(); |
jiuk | 0:2dabb9782f24 | 147 | */ |
jiuk | 0:2dabb9782f24 | 148 | int giro=3; |
jiuk | 0:2dabb9782f24 | 149 | if(acc[0]>giro) //eje x derecha |
jiuk | 0:2dabb9782f24 | 150 | correr=29; |
jiuk | 0:2dabb9782f24 | 151 | else if(acc[0]<-giro) //eje x izquierda |
jiuk | 0:2dabb9782f24 | 152 | correr=28; |
jiuk | 0:2dabb9782f24 | 153 | else if(acc[1]>giro) //eje y arriba |
jiuk | 0:2dabb9782f24 | 154 | correr=30; |
jiuk | 0:2dabb9782f24 | 155 | else if(acc[1]<-giro) //eje y abajo |
jiuk | 0:2dabb9782f24 | 156 | correr=31; |
jiuk | 0:2dabb9782f24 | 157 | revisar_mov(correr); |
jiuk | 0:2dabb9782f24 | 158 | //wait (DEL); |
jiuk | 0:2dabb9782f24 | 159 | command.printf("\n correr:\n %d",correr); |
jiuk | 0:2dabb9782f24 | 160 | } |
jiuk | 0:2dabb9782f24 | 161 | |
jiuk | 0:2dabb9782f24 | 162 | int comer () |
jiuk | 0:2dabb9782f24 | 163 | { |
jiuk | 0:2dabb9782f24 | 164 | int x=0; |
jiuk | 0:2dabb9782f24 | 165 | if(fil==fila){ |
jiuk | 0:2dabb9782f24 | 166 | if((colum & columna) != 0) |
jiuk | 0:2dabb9782f24 | 167 | { |
jiuk | 0:2dabb9782f24 | 168 | generar_punto(); |
jiuk | 0:2dabb9782f24 | 169 | x=1; |
jiuk | 0:2dabb9782f24 | 170 | sendSPI(fil,colum); |
jiuk | 0:2dabb9782f24 | 171 | //com_tar.putc(x); |
jiuk | 0:2dabb9782f24 | 172 | } |
jiuk | 0:2dabb9782f24 | 173 | } |
jiuk | 0:2dabb9782f24 | 174 | return x; |
jiuk | 0:2dabb9782f24 | 175 | } |
jiuk | 0:2dabb9782f24 | 176 | |
jiuk | 0:2dabb9782f24 | 177 | int validar_contrincante(){ |
jiuk | 0:2dabb9782f24 | 178 | int x=com_tar.getc(); |
jiuk | 0:2dabb9782f24 | 179 | return x; |
jiuk | 0:2dabb9782f24 | 180 | } |
jiuk | 0:2dabb9782f24 | 181 | |
jiuk | 0:2dabb9782f24 | 182 | int main () |
jiuk | 0:2dabb9782f24 | 183 | { |
jiuk | 0:2dabb9782f24 | 184 | int guarda_linea,x=0; |
jiuk | 0:2dabb9782f24 | 185 | test(); |
jiuk | 0:2dabb9782f24 | 186 | borrar(); |
jiuk | 0:2dabb9782f24 | 187 | sendSPI(fila,columna); |
jiuk | 0:2dabb9782f24 | 188 | generar_punto(); |
jiuk | 0:2dabb9782f24 | 189 | while(1) |
jiuk | 0:2dabb9782f24 | 190 | { |
jiuk | 0:2dabb9782f24 | 191 | buscador(); |
jiuk | 0:2dabb9782f24 | 192 | //validar_contrincante(); |
jiuk | 0:2dabb9782f24 | 193 | x=comer(); |
jiuk | 0:2dabb9782f24 | 194 | command.printf("\n comer:\n %d",x); |
jiuk | 0:2dabb9782f24 | 195 | if (x==0) |
jiuk | 0:2dabb9782f24 | 196 | { |
jiuk | 0:2dabb9782f24 | 197 | if(fil==fila){ |
jiuk | 0:2dabb9782f24 | 198 | guarda_linea=columna+colum; |
jiuk | 0:2dabb9782f24 | 199 | sendSPI(fila,guarda_linea); |
jiuk | 0:2dabb9782f24 | 200 | } |
jiuk | 0:2dabb9782f24 | 201 | else |
jiuk | 0:2dabb9782f24 | 202 | { |
jiuk | 0:2dabb9782f24 | 203 | guarda_linea=columna; |
jiuk | 0:2dabb9782f24 | 204 | sendSPI(fila,guarda_linea); |
jiuk | 0:2dabb9782f24 | 205 | sendSPI(fil,colum); |
jiuk | 0:2dabb9782f24 | 206 | } |
jiuk | 0:2dabb9782f24 | 207 | } |
jiuk | 0:2dabb9782f24 | 208 | else{ |
jiuk | 0:2dabb9782f24 | 209 | sendSPI(fila,columna); |
jiuk | 0:2dabb9782f24 | 210 | } |
jiuk | 0:2dabb9782f24 | 211 | //generar_punto(); |
jiuk | 0:2dabb9782f24 | 212 | } |
jiuk | 0:2dabb9782f24 | 213 | } |