falta verificar cambio de joystick a accel

Dependencies:   mbed MPU6050

Revision:
4:3426b2472f99
Parent:
3:81dc55b0a8d7
Child:
5:f8ca9f670abe
diff -r 81dc55b0a8d7 -r 3426b2472f99 recoge_manzanas.cpp
--- a/recoge_manzanas.cpp	Sun Nov 18 20:49:38 2018 +0000
+++ b/recoge_manzanas.cpp	Mon Nov 19 06:25:35 2018 +0000
@@ -15,6 +15,9 @@
 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.
 Ticker timer;
+DigitalIn pulsador(PH_1);
+AnalogIn in1(PC_2);
+AnalogIn in2(PC_3);
 
 #define  VEL 200                     //Velocidad de actualizacion de dato en el controlador.
 #define  MIN 1
@@ -27,15 +30,45 @@
 
 int columna=1,manzanas=0;
 int fila=1, fil=1, colum=1;
+bool _boton=0;
 void blink();
+void boton();
 
 //--------------------------------------------------
 //-------------------TICKER------------------------
 //--------------------------------------------------  
 void attime() {
     blink();
+    //boton();
     }; 
-    
+
+//--------------------------------------------------
+//-------------------BOTON------------------------
+//--------------------------------------------------  
+void boton()
+{
+  int x=0;
+  while(1) 
+  {
+    if (pulsador == 1) 
+          x=0;
+    else
+        {
+            command.printf("Button pressed %d\n", x);
+            x++;
+            if(x>20)
+                {
+                    for(int i=0; i<5;i++)
+                    {
+                    //led = !led; // Toggle the LED state
+                    _boton=!_boton;
+                    wait(0.2);
+                    }
+                x=0;
+                }
+        }
+  }
+}  
 //--------------------------------------------------
 //-------------------SPI------------------------
 //--------------------------------------------------   
@@ -56,7 +89,7 @@
 void test()                 //test
 {
     sendSPI(0x09,0);        //no decodificacion
-    sendSPI(0x0A,0x00);     //intensidad
+    sendSPI(0x0A,0x9);     //intensidad
     sendSPI(0x0B,0x07);     //usa 7 leds                     
     sendSPI(0x0C,1);        //no apaga
     sendSPI(0x0F,0);        //operacion normal     
@@ -217,28 +250,21 @@
 //-------------------------------------------------- 
 int velocidad(float _acc[3])
 {
-    uint8_t vel;
+    int vel,x=3,y=5; //
     int acc[3];
-        acc[0]=_acc[0];
-        acc[1]=_acc[1];
+        acc[0]=(int)_acc[0];
+        acc[1]=(int)_acc[1];
        if((acc[0]>GIRO) || (acc[0]<-GIRO) || (acc[1]>GIRO) || (acc[1]<-GIRO))
         {
-                if(acc[0]>GIRO)
-                    vel=acc[0];
-                else if(acc[0]<-GIRO)
-                    vel=-acc[0];
-                else if(acc[1]>GIRO)
-                    vel=acc[1];
-                else if(acc[1]<-GIRO)
-                    vel=-acc[1];
-                command.printf("---Velocidad if %d \n", vel);     
-         wait(3/vel);           
+                if((acc[0]>(GIRO+x)) || (acc[0]<-(GIRO+x)) || (acc[1]>(GIRO+x)) || (acc[1]<-(GIRO+x)))
+                    {
+                        vel=300; 
+                        if((acc[0]>(GIRO+y)) || (acc[0]<-(GIRO+y)) || (acc[1]>(GIRO+y)) || (acc[1]<-(GIRO+y)))
+                            vel=100;
+                    }
+                else
+                    vel=500;   
         }
-    int v=1/vel;
-    
-    command.printf("Velocidad %d \n", v);    
-    command.printf("x %d \n", acc[1]);
-    command.printf("y %d \n", acc[0]);
     return vel;
 }
     
@@ -248,12 +274,30 @@
 void buscador()
 {
     uint8_t correr;
+    int y=0,x=0,acc[3],v;
     //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]);
-        velocidad(acc);
-        //command.printf("Velocidad %d \n", vel);
+    float _acc[3];     
+    //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]);
+       if(_boton==0) 
+        {
+            x=((int)(in1.read()*-650))+13;
+            y=((int)(in2.read()*-650))+13;
+            //x=-x;
+            //y=-y;
+            acc[0]=x;
+            acc[1]=y;
+            command.printf("Joystick: \t X= %d, \t Y= %d, \n", x,y); 
+        }
+        else
+        {
+            Wire.getAccelero(_acc);
+            acc[0]=(int)_acc[0];
+            acc[1]=(int)_acc[1];
+            command.printf("Accelerometer: \t X= %d, \t Y= %d, \n", acc[0],acc[1]);     
+        }
+        
+        command.printf("Velocidad %d \n", v);
         if(acc[0]>GIRO)         //eje x derecha
         {
             if(acc[1]>G_DIAG)     //eje y arriba
@@ -278,7 +322,9 @@
                     correr=28;
                 else
                 correr=0; //
-        revisar_mov(correr);
+        revisar_mov(correr);        
+        v=velocidad(_acc);
+        wait_ms(v);
         //wait (DEL);
     //command.printf("\n correr:\n %d",correr);    
 }
@@ -316,6 +362,10 @@
     return x;
     }
 
+
+//--------------------------------------------------
+//-------------------MAIN------------------------
+//--------------------------------------------------   
 int main ()
 {
     int guarda_linea,x=0;