final, faltan ganas

Dependencies:   mbed MPU6050

Committer:
jiuk
Date:
Wed Nov 14 22:10:24 2018 +0000
Revision:
1:efcead3fb5eb
Parent:
0:2dabb9782f24
Child:
2:484928b9a5b5
Funcional

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 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 1:efcead3fb5eb 112
jiuk 1:efcead3fb5eb 113 case 0:
jiuk 1:efcead3fb5eb 114 fila;
jiuk 1:efcead3fb5eb 115 break;
jiuk 0:2dabb9782f24 116 }
jiuk 0:2dabb9782f24 117 }
jiuk 0:2dabb9782f24 118
jiuk 0:2dabb9782f24 119 void buscador()
jiuk 0:2dabb9782f24 120 {
jiuk 0:2dabb9782f24 121
jiuk 0:2dabb9782f24 122 uint8_t correr;
jiuk 0:2dabb9782f24 123 //float gyro[3]; Wire.getGyro(gyro);
jiuk 0:2dabb9782f24 124 float acc[3]; Wire.getAccelero(acc);
jiuk 0:2dabb9782f24 125 command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]);
jiuk 0:2dabb9782f24 126 //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]);
jiuk 0:2dabb9782f24 127
jiuk 0:2dabb9782f24 128 wait(DEL);
jiuk 0:2dabb9782f24 129
jiuk 0:2dabb9782f24 130 int giro=3;
jiuk 0:2dabb9782f24 131 if(acc[0]>giro) //eje x derecha
jiuk 0:2dabb9782f24 132 correr=29;
jiuk 0:2dabb9782f24 133 else if(acc[0]<-giro) //eje x izquierda
jiuk 0:2dabb9782f24 134 correr=28;
jiuk 0:2dabb9782f24 135 else if(acc[1]>giro) //eje y arriba
jiuk 0:2dabb9782f24 136 correr=30;
jiuk 0:2dabb9782f24 137 else if(acc[1]<-giro) //eje y abajo
jiuk 0:2dabb9782f24 138 correr=31;
jiuk 1:efcead3fb5eb 139 else
jiuk 1:efcead3fb5eb 140 correr=0; //
jiuk 0:2dabb9782f24 141 revisar_mov(correr);
jiuk 0:2dabb9782f24 142 //wait (DEL);
jiuk 0:2dabb9782f24 143 command.printf("\n correr:\n %d",correr);
jiuk 0:2dabb9782f24 144 }
jiuk 0:2dabb9782f24 145
jiuk 0:2dabb9782f24 146 int comer ()
jiuk 0:2dabb9782f24 147 {
jiuk 0:2dabb9782f24 148 int x=0;
jiuk 0:2dabb9782f24 149 if(fil==fila){
jiuk 0:2dabb9782f24 150 if((colum & columna) != 0)
jiuk 0:2dabb9782f24 151 {
jiuk 0:2dabb9782f24 152 generar_punto();
jiuk 0:2dabb9782f24 153 x=1;
jiuk 0:2dabb9782f24 154 sendSPI(fil,colum);
jiuk 0:2dabb9782f24 155 //com_tar.putc(x);
jiuk 0:2dabb9782f24 156 }
jiuk 0:2dabb9782f24 157 }
jiuk 0:2dabb9782f24 158 return x;
jiuk 0:2dabb9782f24 159 }
jiuk 0:2dabb9782f24 160
jiuk 0:2dabb9782f24 161 int validar_contrincante(){
jiuk 0:2dabb9782f24 162 int x=com_tar.getc();
jiuk 0:2dabb9782f24 163 return x;
jiuk 0:2dabb9782f24 164 }
jiuk 0:2dabb9782f24 165
jiuk 0:2dabb9782f24 166 int main ()
jiuk 0:2dabb9782f24 167 {
jiuk 0:2dabb9782f24 168 int guarda_linea,x=0;
jiuk 0:2dabb9782f24 169 test();
jiuk 0:2dabb9782f24 170 borrar();
jiuk 0:2dabb9782f24 171 sendSPI(fila,columna);
jiuk 0:2dabb9782f24 172 generar_punto();
jiuk 0:2dabb9782f24 173 while(1)
jiuk 0:2dabb9782f24 174 {
jiuk 0:2dabb9782f24 175 buscador();
jiuk 0:2dabb9782f24 176 //validar_contrincante();
jiuk 0:2dabb9782f24 177 x=comer();
jiuk 0:2dabb9782f24 178 command.printf("\n comer:\n %d",x);
jiuk 0:2dabb9782f24 179 if (x==0)
jiuk 0:2dabb9782f24 180 {
jiuk 0:2dabb9782f24 181 if(fil==fila){
jiuk 0:2dabb9782f24 182 guarda_linea=columna+colum;
jiuk 0:2dabb9782f24 183 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 184 }
jiuk 0:2dabb9782f24 185 else
jiuk 0:2dabb9782f24 186 {
jiuk 0:2dabb9782f24 187 guarda_linea=columna;
jiuk 0:2dabb9782f24 188 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 189 sendSPI(fil,colum);
jiuk 0:2dabb9782f24 190 }
jiuk 0:2dabb9782f24 191 }
jiuk 0:2dabb9782f24 192 else{
jiuk 0:2dabb9782f24 193 sendSPI(fila,columna);
jiuk 0:2dabb9782f24 194 }
jiuk 0:2dabb9782f24 195 //generar_punto();
jiuk 0:2dabb9782f24 196 }
jiuk 0:2dabb9782f24 197 }