Giroscopio, recoge manzanas

Dependencies:   mbed MPU6050

Files at this revision

API Documentation at this revision

Comitter:
jiuk
Date:
Mon Nov 19 02:07:02 2018 +0000
Commit message:
alguna version giroscopio

Changed in this revision

MI2C_giroscopy.cpp Show annotated file Show diff for this revision Revisions of this file
MPU6050.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
moggo.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MI2C_giroscopy.cpp	Mon Nov 19 02:07:02 2018 +0000
@@ -0,0 +1,207 @@
+// Read from I2C slave at address 0x62
+
+#include "mbed.h"
+
+#include "MPU6050.h"
+
+Serial command(USBTX,USBRX);        //habilitar la comunicacion serial a traves del puerto usb.
+MPU6050 Wire(PB_9 , PB_8 ); 
+
+SPI deviceM(PB_15, PB_14, PB_13);
+DigitalOut ssel (PB_12);
+//SPI deviceM(PB_5, PB_4, PB_3);    //define el Clock, Dato salida (miso) y Dato de entrada (mosi).
+//DigitalOut ssel (PB_9);           //Chip Select para el controlador.
+//Serial command(USBTX,USBRX);        //habilitar la comunicacion serial a traves del puerto usb.
+Serial com_tar(PC_10,PC_11);        //master f446R
+//Serial com_tar(PA_15,PB_7);       //slave f411R        //habilitar la comunicacion serial a traves del puerto usb.
+
+#define  VEL 200                     //Velocidad de actualizacion de dato en el controlador.
+#define  MIN 1
+#define  MAX 8
+#define  MINC 128
+#define  MAXC 1
+
+int columna=1;
+int fila=1, fil=1, colum=1;
+
+void sendSPI(uint8_t d1, uint8_t d2)
+{
+    deviceM.unlock();
+    ssel=0;
+    deviceM.write(d1); 
+    deviceM.write(d2);
+    ssel=1;
+    deviceM.lock();
+}
+
+void test()                 //test
+{
+    sendSPI(0x09,0);        //no decodificacion
+    sendSPI(0x0A,0x00);     //intensidad
+    sendSPI(0x0B,0x07);     //usa 7 leds                     
+    sendSPI(0x0C,1);        //no apaga
+    sendSPI(0x0F,0);        //operacion normal     
+}
+
+void borrar()            //borrar toda la matriz;
+{
+    int i;
+    for(i=0;i<=8;i++)
+    {
+        sendSPI(i,0);
+    }
+}
+
+void generar_punto()
+{
+    int col[8]={0b00000001,0b00000010,0b00000100,0b00001000,0b00010000,0b00100000,0b01000000,0b10000000};
+    //int fil=0;
+    borrar();
+    fil= rand() % 7+1; 
+    int y= rand() % 7+1;
+    command.printf("\n\n Fila: %d",fil); 
+    command.printf("\n Columna:%d",col[y]);
+    //int time=0;
+    //while(time<4)
+    //    {
+    if(fil==fila){
+                    int guarda_linea=columna+colum;
+                    sendSPI(fila,guarda_linea);
+                    }
+    else
+    sendSPI(fil,col[y]);
+    colum=col[y];
+    //    wait_ms(VEL);
+    //    sendSPI(fil,0);
+    //    wait_ms(VEL);
+    //    time++;
+    //    }
+    
+}
+
+void buscador()
+{
+
+    uint8_t correr;
+    float gyro[3]; Wire.getGyro(gyro);
+    float acc[3]; Wire.getAccelero(acc);     
+    command.printf("Accelerometer: \t X= %f, \t Y= %f, \t Z=%f \n", acc[0],acc[1],acc[2]);
+  //command.printf("Gyroscope: \t X= %f, \t Y= %f, \t Z=%f \n", gyro[0],gyro[1],gyro[2]);
+ 
+    wait(0.5);
+    char t=1;    
+    
+    while (t==1 && command.readable()==0){ 
+        wait_ms(0.1);
+        t=0;
+        }
+    if (command.readable()){
+        if(acc[0]<4)   
+        correr=28;
+        if(acc[0]<-4)
+        correr=29;
+        if(acc[1]<4)   
+        correr=30;
+        if(acc[1]<-4)
+        correr=31;
+        
+        //com_tar.putc(correr);
+        wait_ms(100);
+        }
+
+    
+    correr=command.getc();
+    
+    command.printf("\n correr:\n %d",correr);
+       
+    if(correr==30)  //Arriba.
+    {
+        fila--;
+        sendSPI(fila+1,0);
+        if (fila<MIN)
+            fila=MAX;
+            sendSPI(fila+1,0);
+            }
+    
+    else if(correr==28)  //izquierda, correr la figura.
+    {
+        columna=columna<<1;
+        if (columna>MINC){
+            columna=MAXC;
+            }
+            //command.printf("\n columna:\n %d",columna);
+            }
+            
+    else if(correr==29)  //Derecha correr la figura.
+    {
+        columna=columna>>1;
+        if (columna<MAXC){
+            columna=MINC;
+            }
+        //command.printf("\n columna:\n %d",columna);
+            }
+            
+    
+    else if(correr==31)  //Bajar la figura.
+    {
+        fila++;
+        sendSPI(fila-1,0);
+        if (fila>MAX)
+            fila=MIN;
+            sendSPI(fila-1,0);
+            }
+}
+
+int comer ()
+{
+            int x=0;
+            if(fil==fila){
+                if((colum & columna) != 0)
+                    {
+                        generar_punto();
+                        x=1;
+                        sendSPI(fil,colum);
+                        //com_tar.putc(x);
+                        }
+                }
+                return x;
+    }
+
+int validar_contrincante(){
+    int x=com_tar.getc();
+    return x;
+    }
+
+int main ()
+{
+    int guarda_linea,x=0;
+    test();
+    borrar();
+    sendSPI(fila,columna);
+    generar_punto();
+    while(1)
+        {
+        buscador();
+        //validar_contrincante();
+            x=comer();
+            command.printf("\n comer:\n %d",x);
+            if (x==0)
+                {
+                if(fil==fila){
+                        guarda_linea=columna+colum;
+                        sendSPI(fila,guarda_linea);
+                        }
+                else
+                    {
+                        guarda_linea=columna;
+                        sendSPI(fila,guarda_linea);
+                        sendSPI(fil,colum);  
+                    }
+                }
+            else{
+                sendSPI(fila,columna);
+                }
+        //generar_punto();
+        }
+}
+    
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MPU6050.lib	Mon Nov 19 02:07:02 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/teams/mbedbidos/code/MPU6050/#0e4b60792553
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Nov 19 02:07:02 2018 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/mbed_official/code/mbed/builds/e95d10626187
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/moggo.h	Mon Nov 19 02:07:02 2018 +0000
@@ -0,0 +1,10 @@
+#ifndef MOGGO_H
+#define MOGGO_H
+
+#include "mbed.h"
+
+#define ALL_TYPE 1
+
+ uint8_t FIG_ALL[7]={0b10000000,0b11000000,0b11100000,0b01000000,0b01100000,0,0b00100000};//FIG_C[0]=1; FIG_C[1]=11; FIG_C[2]=111; FIG_C[3]=01; FIG_C[4]=011
+
+#endif //  MOGGO_H
\ No newline at end of file