Robot's source code

Dependencies:   mbed

main.cpp

Committer:
Near32
Date:
2015-03-20
Revision:
45:b7617d7cedce
Parent:
44:d5f95af61243
Child:
47:4909e97570f6

File content as of revision 45:b7617d7cedce:

#include "mbed.h"

#include "defines.h"

#include "QEI.h"
#include "Map.h"
#include "AX12.h"


#include "Odometry.h"
#include "Asserv.h"
#include "Motor.h"
/*----------------------------------------------------------------------------------------------*/
/*Serial*/    
Serial logger(OUT_TX, OUT_RX); // tx, rx
//Serial logger(USBTX,USBRX);
/*----------------------------------------------------------------------------------------------*/

/* --- Initialisation de la liste des obstable --- */
int Obstacle::lastId = 0;

int main()
{
    logger.printf("Initialisation...\r\n");
    //mbed
    /*
    PwmOut pw1(p22);
    DigitalOut dir1(p21);
    PwmOut pw2(p24);
    DigitalOut dir2(p23);
    */
    
    //mbuino
    /*
    PwmOut pw1(P0_17);
    DigitalOut dir1(P0_18);
    PwmOut pw2(P0_23);
    DigitalOut dir2(P0_19);
    */
    /*
    //nucleo
    PwmOut pw1(PB_8);
    DigitalOut dir1(D12);
    PwmOut pw2(PB_9);
    DigitalOut dir2(D13);
    */
    //nucleo
    
    PwmOut pw1(PWM_MOT1);
    DigitalOut dir1(DIR_MOT1);
    PwmOut pw2(PWM_MOT2);
    DigitalOut dir2(DIR_MOT2);
    
        
    Motor motorR(PWM_MOT1,DIR_MOT1);
    Motor motorL(PWM_MOT2,DIR_MOT2);    
    logger.printf("mise a jour des pwm.\r\n");
    Timer t;
    
    AX12 test(PA_9,NC,0x05,250000);
    AnalogIn ain0(PA_0);
    AnalogIn ain1(PA_1);
    AnalogIn ain2(PA_4);
    AnalogIn ain3(PB_0);
    AnalogIn ain4(PC_1);
    
    /*----------------------------------------------------------------------------------------------*/
    /*Odometry*/
    QEI qei_left(PB_3,PA_10,NC,1024*4,QEI::X4_ENCODING);
    QEI qei_right(PB_4,PB_5,NC,1024*4,QEI::X4_ENCODING);
    Odometry odometry(&qei_left,&qei_right,63/2.,63/2.,280);
    DigitalOut led(LED1);
    
    test.SetMode(0); // Position
    
    /*char dataOff[] = {0};
    char dataOn[] = {1};
    while(1)
    {
        logger.printf("0: %f\r\n",ain0.read()*3.3);
        logger.printf("1: %f\r\n",ain1.read()*3.3);
        logger.printf("2: %f\r\n",ain2.read()*3.3);
        logger.printf("3: %f\r\n",ain3.read()*3.3);
        logger.printf("4: %f\r\n\r\n",ain4.read()*3.3);
        led = !led;
        wait(1);
        motorR.setSpeed(1.0);
        wait(1);
        motorR.setSpeed(0.0);
    }*/
    bool testOdo = false;
    
    
    Asserv<float> instanceAsserv(&odometry);
    
    /*----------------------------------------------------------------------------------------------*/
    instanceAsserv.setGoal( (float)150,(float)0, (float)0);        
    
    while(1)
    {       
        //Asservissement :
        
        t.start();
        
        instanceAsserv.update((float)t.read());        
        Mat<float> X( instanceAsserv.getX() );        
        float phi_r = instanceAsserv.getPhiR();
        float phi_l = instanceAsserv.getPhiL();
        float phi_max = instanceAsserv.getPhiMax();        
        //logger.printf(" x : %f ;\t y : %f ;\t theta : %f ;\t VD = %f ;\t W = %f \n\r",X.get(1,1),X.get(2,1),X.get(3,1), X.get(4,1), X.get(5,1) );
        //logger.printf(" x : %f ;\t y : %f ;\t theta : %f ",X.get(1,1),X.get(2,1),X.get(3,1) );
        
        //logger.printf("PhiR = %f ; \t PhiL = %f ; \n\r",phi_r,phi_l);//X.get(4,1),X.get(5,1));
        
        //transpose(X).afficherMblue();
        
        
        //pcs.printf("%f : %f : %f\n", (double)odometry.getX()*100,(double)odometry.getY()*100,(double)odometry.getTheta()*180/3.14);                                            
        //blue.printf("State : \n\r");
        //(instanceAsserv.getX()).afficherMblue();
        //blue.printf("Odometry : \n\r");
        //z.afficherMblue();  
        
        motorR.setSpeed(phi_r/phi_max);
        motorL.setSpeed(phi_l/phi_max);     
        
        //Timer Handling :
        t.stop();        
        //logger.printf("%f s ecoulee.\n\r", t.read());
        t.reset();
        t.start();                
    }
        
    
    while(1)
    {
        //setPWM(&pw1,0.0);
        //setPWM(&pw2,0.0);
        pw1.write(0.0);
        pw2.write(0.0);
        led = !led;
        wait(1);
    }
}