Test

Dependencies:   mbed DRV8825

captUS.cpp

Committer:
g0dd4
Date:
2020-10-28
Revision:
26:4670968fbc63
Parent:
25:216e62f05f82

File content as of revision 26:4670968fbc63:

/* #include */
#include "pins.h"

/* #define */
#define DISTLIM 800
#define OFFSET 200
#define MAXMOY 1

/* Variables globales */
Timer TimUS;
Ticker TickUS_actu;

//const float DISTLIM = 600;

unsigned int us_high[6]= {0};
unsigned int us_low[6]= {0};
bool usVerifOrder[2][6] = {0};


unsigned int distt[6];
bool rebooted = 0;
bool wtt = 0;

int indix = 0;
unsigned int total[6] = {0};
unsigned int ttt[6] = {0};

void calculTotal()
{
    if(indix < MAXMOY) {
        for(int j=0; j<6 ; j++) {
            total[j]+=distt[j];
        }
        indix++;
    } else {
        for(int j=0; j<6 ; j++) {
            ttt[j]=total[j]/MAXMOY;
            total[j]=0;
        }

        /*
        bt.printf("ttt[1] = %d\n\r", ttt[0]);
        bt.printf("ttt[2] = %d\n\r", ttt[1]);
        bt.printf("ttt[3] = %d\n\r", ttt[2]);
        bt.printf("ttt[4] = %d\n\r", ttt[3]);
        bt.printf("ttt[5] = %d\n\r", ttt[4]);
        bt.printf("ttt[6] = %d\n\r", ttt[5]);
        bt.printf("\n\r");
        */

        indix=0;
    }
}

/*
void captUS_init()
{
    ::distance = new double(6); //équivalent au malloc()
    tps.reset();
    tps.start();
}
*/

void captUS_trig()
{
    convertToDistance();

    calculTotal();

    if((objRecule[indice]==0) && (fnc == 2)) {
        //if ((distt[5] >= DISTLIM) && (distt[0] >= DISTLIM)  && (distt[1] >= DISTLIM)) {
        //if (((distt[5] >= DISTLIM) && (distt[0] >= DISTLIM)  && (distt[1] >= DISTLIM)) /*|| ((distt[5] < 0) && (distt[0] < 0)  && (distt[1] < 0))*/) {
        if ((ttt[5] >= DISTLIM-OFFSET) && (ttt[0] >= DISTLIM)  && (ttt[1] >= DISTLIM-OFFSET)) {
            wtt = 1;
        }

        else {
            mot_dis();
            rebooted = 1;
            wtt=0;
        }
    }

    else if((objRecule[indice]==1) && (fnc == 2)) {
        //if ((distt[2] >= DISTLIM) && (distt[3] >= DISTLIM) && (distt[4] >= DISTLIM)) {
        //if (((distt[2] >= DISTLIM) && (distt[3] >= DISTLIM)  && (distt[4] >= DISTLIM)) /*|| ((distt[2] < 0) && (distt[3] < 0)  && (distt[4] < 0))*/) {
        if ((ttt[2] >= DISTLIM-OFFSET) && (ttt[3] >= DISTLIM)  && (ttt[4] >= DISTLIM-OFFSET)) {
            wtt=1;
        }

        else {
            mot_dis();
            rebooted = 1;
            wtt=0;
        }
    }

    /*
    pc.printf("US1 = %5.0lf uS\n\r", us_out[0]);
    pc.printf("US2 = %5.0lf uS\n\r", us_out[1]);
    //pc.printf("US3 = %5.0lf uS\n\r", us_out[2]);
    //pc.printf("US4 = %5.0lf uS\n\r", us_out[3]);
    //pc.printf("US5 = %5.0lf uS\n\r", us_out[4]);
    pc.printf("US6 = %5.0lf uS\n\r", us_out[5]);
    pc.printf("\n\r");
    */

    /*
    pc.printf("Dist1 = %5.0lf mm\n\r", ::distance[0]);
    pc.printf("Dist2 = %5.0lf mm\n\r", ::distance[1]);
    pc.printf("Dist3 = %5.0lf mm\n\r", ::distance[2]);
    pc.printf("Dist4 = %5.0lf mm\n\r", ::distance[3]);
    pc.printf("Dist5 = %5.0lf mm\n\r", ::distance[4]);
    pc.printf("Dist6 = %5.0lf mm\n\r", ::distance[5]);
    pc.printf("\n\r");
    */
    
    /*
    pc.printf("ttt[1] = %d\n\r", ttt[0]);
    pc.printf("ttt[2] = %d\n\r", ttt[1]);
    pc.printf("ttt[3] = %d\n\r", ttt[2]);
    pc.printf("ttt[4] = %d\n\r", ttt[3]);
    pc.printf("ttt[5] = %d\n\r", ttt[4]);
    pc.printf("ttt[6] = %d\n\r", ttt[5]);
    pc.printf("\n\r");
    */

    //calculTotal();

    TimUS.reset();
    trigger=1;
    wait(0.00002);
    trigger=0;
}

void echoRise1()
{
    if(usVerifOrder[1][0] == false) {
        us_high[0]=TimUS.read_us();
        usVerifOrder[0][0] = true; 
    }
}

void echoFall1()
{
    if(usVerifOrder[0][0] == true) {
        us_low[0]=TimUS.read_us();
        usVerifOrder[1][0] = true;
    }
}

void echoRise2()
{
    if(usVerifOrder[1][1] == false) {
        us_high[1]=TimUS.read_us();
        usVerifOrder[0][1] = true;
    }
}

void echoFall2()
{
    if(usVerifOrder[0][1] == true) {
        us_low[1]=TimUS.read_us();
        usVerifOrder[1][1] = true;
    }
}

void echoRise3()
{
    if(usVerifOrder[1][2] == false) {
        us_high[2]=TimUS.read_us();
        usVerifOrder[0][2] = true;
    }
}

void echoFall3()
{
    if(usVerifOrder[0][2] == true) {
        us_low[2]=TimUS.read_us();
        usVerifOrder[1][2] = true;
    }
}

void echoRise4()
{
    if(usVerifOrder[1][3] == false) {
        us_high[3]=TimUS.read_us();
        usVerifOrder[0][3] = true;
    }
}

void echoFall4()
{
    if(usVerifOrder[0][3] == true) {
        us_low[3]=TimUS.read_us();
        usVerifOrder[1][3] = true;
    }
}

void echoRise5()
{    
    if(usVerifOrder[1][4] == false) {
        us_high[4]=TimUS.read_us();
        usVerifOrder[0][4] = true;
    }
}

void echoFall5()
{
    if(usVerifOrder[0][4] == true) {
        us_low[4]=TimUS.read_us();
        usVerifOrder[1][4] = true;
    }
}

void echoRise6()
{
    if(usVerifOrder[1][5] == false) {
        us_high[5]=TimUS.read_us();
        usVerifOrder[0][5] = true;
    }
}

void echoFall6()
{
    if(usVerifOrder[0][5] == true) {
        us_low[5]=TimUS.read_us();
        usVerifOrder[1][5] = true;
    }
}


void convertToDistance()
{
    /**************************************
     * Nous convertisons grâce au valeur  *
     * qui sont retournées par echoRiseX  *
     * et echoFallx                       *
     **************************************/

    for(int i = 0; i<6; i++) {
        if(usVerifOrder[0][i] == true && usVerifOrder[1][i] == true)
            distt[i] = (int) (((us_low[i] - us_high[i])*340)/1000); //conversion en distance(mm)
        else
            distt[i] = LARGEUR_TAB;
    }

    /****************************************
     * nous retournons l'adresse du tableau *
     ****************************************/
    //return ::distance;
}