Giroscopio, recoge manzanas

Dependencies:   mbed MPU6050

Committer:
jiuk
Date:
Mon Nov 19 02:07:02 2018 +0000
Revision:
0:8c4cd4b858b4
alguna version giroscopio

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jiuk 0:8c4cd4b858b4 1 // Read from I2C slave at address 0x62
jiuk 0:8c4cd4b858b4 2
jiuk 0:8c4cd4b858b4 3 #include "mbed.h"
jiuk 0:8c4cd4b858b4 4
jiuk 0:8c4cd4b858b4 5 #include "MPU6050.h"
jiuk 0:8c4cd4b858b4 6
jiuk 0:8c4cd4b858b4 7 Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:8c4cd4b858b4 8 MPU6050 Wire(PB_9 , PB_8 );
jiuk 0:8c4cd4b858b4 9
jiuk 0:8c4cd4b858b4 10 SPI deviceM(PB_15, PB_14, PB_13);
jiuk 0:8c4cd4b858b4 11 DigitalOut ssel (PB_12);
jiuk 0:8c4cd4b858b4 12 //SPI deviceM(PB_5, PB_4, PB_3); //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
jiuk 0:8c4cd4b858b4 13 //DigitalOut ssel (PB_9); //Chip Select para el controlador.
jiuk 0:8c4cd4b858b4 14 //Serial command(USBTX,USBRX); //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:8c4cd4b858b4 15 Serial com_tar(PC_10,PC_11); //master f446R
jiuk 0:8c4cd4b858b4 16 //Serial com_tar(PA_15,PB_7); //slave f411R //habilitar la comunicacion serial a traves del puerto usb.
jiuk 0:8c4cd4b858b4 17
jiuk 0:8c4cd4b858b4 18 #define VEL 200 //Velocidad de actualizacion de dato en el controlador.
jiuk 0:8c4cd4b858b4 19 #define MIN 1
jiuk 0:8c4cd4b858b4 20 #define MAX 8
jiuk 0:8c4cd4b858b4 21 #define MINC 128
jiuk 0:8c4cd4b858b4 22 #define MAXC 1
jiuk 0:8c4cd4b858b4 23
jiuk 0:8c4cd4b858b4 24 int columna=1;
jiuk 0:8c4cd4b858b4 25 int fila=1, fil=1, colum=1;
jiuk 0:8c4cd4b858b4 26
jiuk 0:8c4cd4b858b4 27 void sendSPI(uint8_t d1, uint8_t d2)
jiuk 0:8c4cd4b858b4 28 {
jiuk 0:8c4cd4b858b4 29 deviceM.unlock();
jiuk 0:8c4cd4b858b4 30 ssel=0;
jiuk 0:8c4cd4b858b4 31 deviceM.write(d1);
jiuk 0:8c4cd4b858b4 32 deviceM.write(d2);
jiuk 0:8c4cd4b858b4 33 ssel=1;
jiuk 0:8c4cd4b858b4 34 deviceM.lock();
jiuk 0:8c4cd4b858b4 35 }
jiuk 0:8c4cd4b858b4 36
jiuk 0:8c4cd4b858b4 37 void test() //test
jiuk 0:8c4cd4b858b4 38 {
jiuk 0:8c4cd4b858b4 39 sendSPI(0x09,0); //no decodificacion
jiuk 0:8c4cd4b858b4 40 sendSPI(0x0A,0x00); //intensidad
jiuk 0:8c4cd4b858b4 41 sendSPI(0x0B,0x07); //usa 7 leds
jiuk 0:8c4cd4b858b4 42 sendSPI(0x0C,1); //no apaga
jiuk 0:8c4cd4b858b4 43 sendSPI(0x0F,0); //operacion normal
jiuk 0:8c4cd4b858b4 44 }
jiuk 0:8c4cd4b858b4 45
jiuk 0:8c4cd4b858b4 46 void borrar() //borrar toda la matriz;
jiuk 0:8c4cd4b858b4 47 {
jiuk 0:8c4cd4b858b4 48 int i;
jiuk 0:8c4cd4b858b4 49 for(i=0;i<=8;i++)
jiuk 0:8c4cd4b858b4 50 {
jiuk 0:8c4cd4b858b4 51 sendSPI(i,0);
jiuk 0:8c4cd4b858b4 52 }
jiuk 0:8c4cd4b858b4 53 }
jiuk 0:8c4cd4b858b4 54
jiuk 0:8c4cd4b858b4 55 void generar_punto()
jiuk 0:8c4cd4b858b4 56 {
jiuk 0:8c4cd4b858b4 57 int col[8]={0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000};
jiuk 0:8c4cd4b858b4 58 //int fil=0;
jiuk 0:8c4cd4b858b4 59 borrar();
jiuk 0:8c4cd4b858b4 60 fil= rand() % 7+1;
jiuk 0:8c4cd4b858b4 61 int y= rand() % 7+1;
jiuk 0:8c4cd4b858b4 62 command.printf("\n\n Fila: %d",fil);
jiuk 0:8c4cd4b858b4 63 command.printf("\n Columna:%d",col[y]);
jiuk 0:8c4cd4b858b4 64 //int time=0;
jiuk 0:8c4cd4b858b4 65 //while(time<4)
jiuk 0:8c4cd4b858b4 66 // {
jiuk 0:8c4cd4b858b4 67 if(fil==fila){
jiuk 0:8c4cd4b858b4 68 int guarda_linea=columna+colum;
jiuk 0:8c4cd4b858b4 69 sendSPI(fila,guarda_linea);
jiuk 0:8c4cd4b858b4 70 }
jiuk 0:8c4cd4b858b4 71 else
jiuk 0:8c4cd4b858b4 72 sendSPI(fil,col[y]);
jiuk 0:8c4cd4b858b4 73 colum=col[y];
jiuk 0:8c4cd4b858b4 74 // wait_ms(VEL);
jiuk 0:8c4cd4b858b4 75 // sendSPI(fil,0);
jiuk 0:8c4cd4b858b4 76 // wait_ms(VEL);
jiuk 0:8c4cd4b858b4 77 // time++;
jiuk 0:8c4cd4b858b4 78 // }
jiuk 0:8c4cd4b858b4 79
jiuk 0:8c4cd4b858b4 80 }
jiuk 0:8c4cd4b858b4 81
jiuk 0:8c4cd4b858b4 82 void buscador()
jiuk 0:8c4cd4b858b4 83 {
jiuk 0:8c4cd4b858b4 84
jiuk 0:8c4cd4b858b4 85 uint8_t correr;
jiuk 0:8c4cd4b858b4 86 float gyro[3]; Wire.getGyro(gyro);
jiuk 0:8c4cd4b858b4 87 float acc[3]; Wire.getAccelero(acc);
jiuk 0:8c4cd4b858b4 88 command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]);
jiuk 0:8c4cd4b858b4 89 //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]);
jiuk 0:8c4cd4b858b4 90
jiuk 0:8c4cd4b858b4 91 wait(0.5);
jiuk 0:8c4cd4b858b4 92 char t=1;
jiuk 0:8c4cd4b858b4 93
jiuk 0:8c4cd4b858b4 94 while (t==1 && command.readable()==0){
jiuk 0:8c4cd4b858b4 95 wait_ms(0.1);
jiuk 0:8c4cd4b858b4 96 t=0;
jiuk 0:8c4cd4b858b4 97 }
jiuk 0:8c4cd4b858b4 98 if (command.readable()){
jiuk 0:8c4cd4b858b4 99 if(acc[0]<4)
jiuk 0:8c4cd4b858b4 100 correr=28;
jiuk 0:8c4cd4b858b4 101 if(acc[0]<-4)
jiuk 0:8c4cd4b858b4 102 correr=29;
jiuk 0:8c4cd4b858b4 103 if(acc[1]<4)
jiuk 0:8c4cd4b858b4 104 correr=30;
jiuk 0:8c4cd4b858b4 105 if(acc[1]<-4)
jiuk 0:8c4cd4b858b4 106 correr=31;
jiuk 0:8c4cd4b858b4 107
jiuk 0:8c4cd4b858b4 108 //com_tar.putc(correr);
jiuk 0:8c4cd4b858b4 109 wait_ms(100);
jiuk 0:8c4cd4b858b4 110 }
jiuk 0:8c4cd4b858b4 111
jiuk 0:8c4cd4b858b4 112
jiuk 0:8c4cd4b858b4 113 correr=command.getc();
jiuk 0:8c4cd4b858b4 114
jiuk 0:8c4cd4b858b4 115 command.printf("\n correr:\n %d",correr);
jiuk 0:8c4cd4b858b4 116
jiuk 0:8c4cd4b858b4 117 if(correr==30) //Arriba.
jiuk 0:8c4cd4b858b4 118 {
jiuk 0:8c4cd4b858b4 119 fila--;
jiuk 0:8c4cd4b858b4 120 sendSPI(fila+1,0);
jiuk 0:8c4cd4b858b4 121 if (fila<MIN)
jiuk 0:8c4cd4b858b4 122 fila=MAX;
jiuk 0:8c4cd4b858b4 123 sendSPI(fila+1,0);
jiuk 0:8c4cd4b858b4 124 }
jiuk 0:8c4cd4b858b4 125
jiuk 0:8c4cd4b858b4 126 else if(correr==28) //izquierda, correr la figura.
jiuk 0:8c4cd4b858b4 127 {
jiuk 0:8c4cd4b858b4 128 columna=columna<<1;
jiuk 0:8c4cd4b858b4 129 if (columna>MINC){
jiuk 0:8c4cd4b858b4 130 columna=MAXC;
jiuk 0:8c4cd4b858b4 131 }
jiuk 0:8c4cd4b858b4 132 //command.printf("\n columna:\n %d",columna);
jiuk 0:8c4cd4b858b4 133 }
jiuk 0:8c4cd4b858b4 134
jiuk 0:8c4cd4b858b4 135 else if(correr==29) //Derecha correr la figura.
jiuk 0:8c4cd4b858b4 136 {
jiuk 0:8c4cd4b858b4 137 columna=columna>>1;
jiuk 0:8c4cd4b858b4 138 if (columna<MAXC){
jiuk 0:8c4cd4b858b4 139 columna=MINC;
jiuk 0:8c4cd4b858b4 140 }
jiuk 0:8c4cd4b858b4 141 //command.printf("\n columna:\n %d",columna);
jiuk 0:8c4cd4b858b4 142 }
jiuk 0:8c4cd4b858b4 143
jiuk 0:8c4cd4b858b4 144
jiuk 0:8c4cd4b858b4 145 else if(correr==31) //Bajar la figura.
jiuk 0:8c4cd4b858b4 146 {
jiuk 0:8c4cd4b858b4 147 fila++;
jiuk 0:8c4cd4b858b4 148 sendSPI(fila-1,0);
jiuk 0:8c4cd4b858b4 149 if (fila>MAX)
jiuk 0:8c4cd4b858b4 150 fila=MIN;
jiuk 0:8c4cd4b858b4 151 sendSPI(fila-1,0);
jiuk 0:8c4cd4b858b4 152 }
jiuk 0:8c4cd4b858b4 153 }
jiuk 0:8c4cd4b858b4 154
jiuk 0:8c4cd4b858b4 155 int comer ()
jiuk 0:8c4cd4b858b4 156 {
jiuk 0:8c4cd4b858b4 157 int x=0;
jiuk 0:8c4cd4b858b4 158 if(fil==fila){
jiuk 0:8c4cd4b858b4 159 if((colum & columna) != 0)
jiuk 0:8c4cd4b858b4 160 {
jiuk 0:8c4cd4b858b4 161 generar_punto();
jiuk 0:8c4cd4b858b4 162 x=1;
jiuk 0:8c4cd4b858b4 163 sendSPI(fil,colum);
jiuk 0:8c4cd4b858b4 164 //com_tar.putc(x);
jiuk 0:8c4cd4b858b4 165 }
jiuk 0:8c4cd4b858b4 166 }
jiuk 0:8c4cd4b858b4 167 return x;
jiuk 0:8c4cd4b858b4 168 }
jiuk 0:8c4cd4b858b4 169
jiuk 0:8c4cd4b858b4 170 int validar_contrincante(){
jiuk 0:8c4cd4b858b4 171 int x=com_tar.getc();
jiuk 0:8c4cd4b858b4 172 return x;
jiuk 0:8c4cd4b858b4 173 }
jiuk 0:8c4cd4b858b4 174
jiuk 0:8c4cd4b858b4 175 int main ()
jiuk 0:8c4cd4b858b4 176 {
jiuk 0:8c4cd4b858b4 177 int guarda_linea,x=0;
jiuk 0:8c4cd4b858b4 178 test();
jiuk 0:8c4cd4b858b4 179 borrar();
jiuk 0:8c4cd4b858b4 180 sendSPI(fila,columna);
jiuk 0:8c4cd4b858b4 181 generar_punto();
jiuk 0:8c4cd4b858b4 182 while(1)
jiuk 0:8c4cd4b858b4 183 {
jiuk 0:8c4cd4b858b4 184 buscador();
jiuk 0:8c4cd4b858b4 185 //validar_contrincante();
jiuk 0:8c4cd4b858b4 186 x=comer();
jiuk 0:8c4cd4b858b4 187 command.printf("\n comer:\n %d",x);
jiuk 0:8c4cd4b858b4 188 if (x==0)
jiuk 0:8c4cd4b858b4 189 {
jiuk 0:8c4cd4b858b4 190 if(fil==fila){
jiuk 0:8c4cd4b858b4 191 guarda_linea=columna+colum;
jiuk 0:8c4cd4b858b4 192 sendSPI(fila,guarda_linea);
jiuk 0:8c4cd4b858b4 193 }
jiuk 0:8c4cd4b858b4 194 else
jiuk 0:8c4cd4b858b4 195 {
jiuk 0:8c4cd4b858b4 196 guarda_linea=columna;
jiuk 0:8c4cd4b858b4 197 sendSPI(fila,guarda_linea);
jiuk 0:8c4cd4b858b4 198 sendSPI(fil,colum);
jiuk 0:8c4cd4b858b4 199 }
jiuk 0:8c4cd4b858b4 200 }
jiuk 0:8c4cd4b858b4 201 else{
jiuk 0:8c4cd4b858b4 202 sendSPI(fila,columna);
jiuk 0:8c4cd4b858b4 203 }
jiuk 0:8c4cd4b858b4 204 //generar_punto();
jiuk 0:8c4cd4b858b4 205 }
jiuk 0:8c4cd4b858b4 206 }
jiuk 0:8c4cd4b858b4 207