Funciona con acelero-metro, falta comunicación y blink.

Dependencies:   mbed MPU6050

Committer:
jiuk
Date:
Sun Nov 18 03:37:38 2018 +0000
Revision:
2:484928b9a5b5
Parent:
1:efcead3fb5eb
Recoge 1 manzana+ blink, funciona aceleroscopio

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 0:2dabb9782f24 25
jiuk 2:484928b9a5b5 26 int columna=1,manzanas=0;
jiuk 0:2dabb9782f24 27 int fila=1, fil=1, colum=1;
jiuk 2:484928b9a5b5 28 void blink();
jiuk 0:2dabb9782f24 29
jiuk 2:484928b9a5b5 30 //--------------------------------------------------
jiuk 2:484928b9a5b5 31 //-------------------TICKER------------------------
jiuk 2:484928b9a5b5 32 //--------------------------------------------------
jiuk 2:484928b9a5b5 33 void attime() {
jiuk 2:484928b9a5b5 34 blink();
jiuk 2:484928b9a5b5 35 };
jiuk 2:484928b9a5b5 36
jiuk 2:484928b9a5b5 37 //--------------------------------------------------
jiuk 2:484928b9a5b5 38 //-------------------SPI------------------------
jiuk 2:484928b9a5b5 39 //--------------------------------------------------
jiuk 0:2dabb9782f24 40 void sendSPI(uint8_t d1, uint8_t d2)
jiuk 0:2dabb9782f24 41 {
jiuk 0:2dabb9782f24 42 deviceM.unlock();
jiuk 0:2dabb9782f24 43 ssel=0;
jiuk 0:2dabb9782f24 44 deviceM.write(d1);
jiuk 0:2dabb9782f24 45 deviceM.write(d2);
jiuk 0:2dabb9782f24 46 ssel=1;
jiuk 0:2dabb9782f24 47 deviceM.lock();
jiuk 2:484928b9a5b5 48 };
jiuk 2:484928b9a5b5 49
jiuk 0:2dabb9782f24 50
jiuk 2:484928b9a5b5 51 //--------------------------------------------------
jiuk 2:484928b9a5b5 52 //-------------------TEST------------------------
jiuk 2:484928b9a5b5 53 //--------------------------------------------------
jiuk 0:2dabb9782f24 54 void test() //test
jiuk 0:2dabb9782f24 55 {
jiuk 0:2dabb9782f24 56 sendSPI(0x09,0); //no decodificacion
jiuk 0:2dabb9782f24 57 sendSPI(0x0A,0x00); //intensidad
jiuk 0:2dabb9782f24 58 sendSPI(0x0B,0x07); //usa 7 leds
jiuk 0:2dabb9782f24 59 sendSPI(0x0C,1); //no apaga
jiuk 0:2dabb9782f24 60 sendSPI(0x0F,0); //operacion normal
jiuk 0:2dabb9782f24 61 }
jiuk 0:2dabb9782f24 62
jiuk 2:484928b9a5b5 63
jiuk 2:484928b9a5b5 64 //--------------------------------------------------
jiuk 2:484928b9a5b5 65 //-------------------BORRAR------------------------
jiuk 2:484928b9a5b5 66 //--------------------------------------------------
jiuk 0:2dabb9782f24 67 void borrar() //borrar toda la matriz;
jiuk 0:2dabb9782f24 68 {
jiuk 0:2dabb9782f24 69 int i;
jiuk 0:2dabb9782f24 70 for(i=0;i<=8;i++)
jiuk 0:2dabb9782f24 71 {
jiuk 0:2dabb9782f24 72 sendSPI(i,0);
jiuk 0:2dabb9782f24 73 }
jiuk 0:2dabb9782f24 74 }
jiuk 0:2dabb9782f24 75
jiuk 2:484928b9a5b5 76
jiuk 2:484928b9a5b5 77 //--------------------------------------------------
jiuk 2:484928b9a5b5 78 //-------------------GENERAR_PUNTO------------------------
jiuk 2:484928b9a5b5 79 //--------------------------------------------------
jiuk 0:2dabb9782f24 80 void generar_punto()
jiuk 0:2dabb9782f24 81 {
jiuk 2:484928b9a5b5 82 int col[8]={1,2,4,8,16,32,64,128};
jiuk 0:2dabb9782f24 83 //int fil=0;
jiuk 0:2dabb9782f24 84 borrar();
jiuk 2:484928b9a5b5 85 int _fil=fil;
jiuk 2:484928b9a5b5 86 fil= rand() % 7+1;
jiuk 0:2dabb9782f24 87 int y= rand() % 7+1;
jiuk 0:2dabb9782f24 88 command.printf("\n\n Fila: %d",fil);
jiuk 0:2dabb9782f24 89 command.printf("\n Columna:%d",col[y]);
jiuk 0:2dabb9782f24 90 //int time=0;
jiuk 0:2dabb9782f24 91 //while(time<4)
jiuk 0:2dabb9782f24 92 // {
jiuk 2:484928b9a5b5 93 if(_fil==fil)
jiuk 2:484928b9a5b5 94 colum=colum+col[y];
jiuk 2:484928b9a5b5 95 colum=col[y];
jiuk 0:2dabb9782f24 96 if(fil==fila){
jiuk 0:2dabb9782f24 97 int guarda_linea=columna+colum;
jiuk 0:2dabb9782f24 98 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 99 }
jiuk 0:2dabb9782f24 100 else
jiuk 0:2dabb9782f24 101 sendSPI(fil,col[y]);
jiuk 2:484928b9a5b5 102
jiuk 0:2dabb9782f24 103 // wait_ms(VEL);
jiuk 0:2dabb9782f24 104 // sendSPI(fil,0);
jiuk 0:2dabb9782f24 105 // wait_ms(VEL);
jiuk 0:2dabb9782f24 106 // time++;
jiuk 0:2dabb9782f24 107 // }
jiuk 0:2dabb9782f24 108
jiuk 0:2dabb9782f24 109 }
jiuk 0:2dabb9782f24 110
jiuk 2:484928b9a5b5 111 //--------------------------------------------------
jiuk 2:484928b9a5b5 112 //-------------------BLINK------------------------
jiuk 2:484928b9a5b5 113 //--------------------------------------------------
jiuk 2:484928b9a5b5 114 void blink(){
jiuk 2:484928b9a5b5 115 sendSPI(fil,0);
jiuk 2:484928b9a5b5 116 if(fil==fila)
jiuk 2:484928b9a5b5 117 sendSPI(fila,columna);
jiuk 2:484928b9a5b5 118 }
jiuk 2:484928b9a5b5 119
jiuk 2:484928b9a5b5 120 //--------------------------------------------------
jiuk 2:484928b9a5b5 121 //-------------------REVISAR_MOV------------------------
jiuk 2:484928b9a5b5 122 //--------------------------------------------------
jiuk 0:2dabb9782f24 123 void revisar_mov(uint8_t correr){
jiuk 0:2dabb9782f24 124 switch (correr){
jiuk 0:2dabb9782f24 125 case 28:
jiuk 0:2dabb9782f24 126 columna=columna<<1;
jiuk 0:2dabb9782f24 127 if (columna>MINC)
jiuk 0:2dabb9782f24 128 columna=MAXC;
jiuk 0:2dabb9782f24 129 break;
jiuk 0:2dabb9782f24 130
jiuk 0:2dabb9782f24 131 case 29:
jiuk 0:2dabb9782f24 132 columna=columna>>1;
jiuk 0:2dabb9782f24 133 if (columna<MAXC)
jiuk 0:2dabb9782f24 134 columna=MINC;
jiuk 0:2dabb9782f24 135 break;
jiuk 0:2dabb9782f24 136
jiuk 0:2dabb9782f24 137 case 30:
jiuk 0:2dabb9782f24 138 fila--;
jiuk 0:2dabb9782f24 139 sendSPI(fila+1,0);
jiuk 0:2dabb9782f24 140 if (fila<MIN)
jiuk 0:2dabb9782f24 141 fila=MAX;
jiuk 0:2dabb9782f24 142 sendSPI(fila+1,0);
jiuk 0:2dabb9782f24 143 break;
jiuk 0:2dabb9782f24 144
jiuk 0:2dabb9782f24 145 case 31:
jiuk 0:2dabb9782f24 146 fila++;
jiuk 0:2dabb9782f24 147 sendSPI(fila-1,0);
jiuk 0:2dabb9782f24 148 if (fila>MAX)
jiuk 0:2dabb9782f24 149 fila=MIN;
jiuk 0:2dabb9782f24 150 sendSPI(fila-1,0);
jiuk 0:2dabb9782f24 151 break;
jiuk 1:efcead3fb5eb 152
jiuk 1:efcead3fb5eb 153 case 0:
jiuk 1:efcead3fb5eb 154 break;
jiuk 0:2dabb9782f24 155 }
jiuk 0:2dabb9782f24 156 }
jiuk 2:484928b9a5b5 157
jiuk 2:484928b9a5b5 158
jiuk 2:484928b9a5b5 159 //--------------------------------------------------
jiuk 2:484928b9a5b5 160 //-------------------BUSCADOR------------------------
jiuk 2:484928b9a5b5 161 //--------------------------------------------------
jiuk 0:2dabb9782f24 162 void buscador()
jiuk 0:2dabb9782f24 163 {
jiuk 0:2dabb9782f24 164 uint8_t correr;
jiuk 0:2dabb9782f24 165 //float gyro[3]; Wire.getGyro(gyro);
jiuk 0:2dabb9782f24 166 float acc[3]; Wire.getAccelero(acc);
jiuk 2:484928b9a5b5 167 //command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]);
jiuk 0:2dabb9782f24 168 //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]);
jiuk 0:2dabb9782f24 169
jiuk 0:2dabb9782f24 170 wait(DEL);
jiuk 0:2dabb9782f24 171
jiuk 0:2dabb9782f24 172 int giro=3;
jiuk 0:2dabb9782f24 173 if(acc[0]>giro) //eje x derecha
jiuk 2:484928b9a5b5 174 correr=30;
jiuk 0:2dabb9782f24 175 else if(acc[0]<-giro) //eje x izquierda
jiuk 2:484928b9a5b5 176 correr=31;
jiuk 0:2dabb9782f24 177 else if(acc[1]>giro) //eje y arriba
jiuk 2:484928b9a5b5 178 correr=29;
jiuk 0:2dabb9782f24 179 else if(acc[1]<-giro) //eje y abajo
jiuk 2:484928b9a5b5 180 correr=28;
jiuk 1:efcead3fb5eb 181 else
jiuk 1:efcead3fb5eb 182 correr=0; //
jiuk 0:2dabb9782f24 183 revisar_mov(correr);
jiuk 0:2dabb9782f24 184 //wait (DEL);
jiuk 2:484928b9a5b5 185 //command.printf("\n correr:\n %d",correr);
jiuk 0:2dabb9782f24 186 }
jiuk 0:2dabb9782f24 187
jiuk 2:484928b9a5b5 188
jiuk 2:484928b9a5b5 189 //--------------------------------------------------
jiuk 2:484928b9a5b5 190 //-------------------COMER------------------------
jiuk 2:484928b9a5b5 191 //--------------------------------------------------
jiuk 0:2dabb9782f24 192 int comer ()
jiuk 0:2dabb9782f24 193 {
jiuk 0:2dabb9782f24 194 int x=0;
jiuk 2:484928b9a5b5 195 if((fil==fila)&&((colum & columna) != 0))
jiuk 2:484928b9a5b5 196 {
jiuk 2:484928b9a5b5 197 manzanas++;
jiuk 2:484928b9a5b5 198 sendSPI(fila,columna);
jiuk 2:484928b9a5b5 199 for(int i=0;i<manzanas;i++)
jiuk 2:484928b9a5b5 200 {
jiuk 2:484928b9a5b5 201 int guarda_linea=columna+colum;
jiuk 2:484928b9a5b5 202 sendSPI(fila,guarda_linea);
jiuk 2:484928b9a5b5 203 generar_punto();
jiuk 2:484928b9a5b5 204 sendSPI(fil,colum);
jiuk 2:484928b9a5b5 205 //com_tar.putc(x);
jiuk 2:484928b9a5b5 206 }
jiuk 0:2dabb9782f24 207 x=1;
jiuk 2:484928b9a5b5 208 }
jiuk 0:2dabb9782f24 209 return x;
jiuk 2:484928b9a5b5 210 }
jiuk 2:484928b9a5b5 211
jiuk 0:2dabb9782f24 212
jiuk 2:484928b9a5b5 213 //--------------------------------------------------
jiuk 2:484928b9a5b5 214 //-------------------VALIDAR_CONTRINCANTE------------------------
jiuk 2:484928b9a5b5 215 //--------------------------------------------------
jiuk 0:2dabb9782f24 216 int validar_contrincante(){
jiuk 0:2dabb9782f24 217 int x=com_tar.getc();
jiuk 0:2dabb9782f24 218 return x;
jiuk 0:2dabb9782f24 219 }
jiuk 0:2dabb9782f24 220
jiuk 0:2dabb9782f24 221 int main ()
jiuk 0:2dabb9782f24 222 {
jiuk 0:2dabb9782f24 223 int guarda_linea,x=0;
jiuk 2:484928b9a5b5 224 timer.attach(&attime, 0.3);
jiuk 0:2dabb9782f24 225 test();
jiuk 0:2dabb9782f24 226 borrar();
jiuk 0:2dabb9782f24 227 sendSPI(fila,columna);
jiuk 0:2dabb9782f24 228 generar_punto();
jiuk 0:2dabb9782f24 229 while(1)
jiuk 0:2dabb9782f24 230 {
jiuk 0:2dabb9782f24 231 buscador();
jiuk 0:2dabb9782f24 232 //validar_contrincante();
jiuk 0:2dabb9782f24 233 x=comer();
jiuk 2:484928b9a5b5 234 //command.printf("\n comer:\n %d",x);
jiuk 0:2dabb9782f24 235 if (x==0)
jiuk 0:2dabb9782f24 236 {
jiuk 0:2dabb9782f24 237 if(fil==fila){
jiuk 0:2dabb9782f24 238 guarda_linea=columna+colum;
jiuk 0:2dabb9782f24 239 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 240 }
jiuk 0:2dabb9782f24 241 else
jiuk 0:2dabb9782f24 242 {
jiuk 0:2dabb9782f24 243 guarda_linea=columna;
jiuk 0:2dabb9782f24 244 sendSPI(fila,guarda_linea);
jiuk 0:2dabb9782f24 245 sendSPI(fil,colum);
jiuk 0:2dabb9782f24 246 }
jiuk 0:2dabb9782f24 247 }
jiuk 0:2dabb9782f24 248 else{
jiuk 0:2dabb9782f24 249 sendSPI(fila,columna);
jiuk 0:2dabb9782f24 250 }
jiuk 0:2dabb9782f24 251 //generar_punto();
jiuk 0:2dabb9782f24 252 }
jiuk 0:2dabb9782f24 253 }