programme course avec menu amélioré

Dependencies:   MMA8451Q mbed

Fork of Programme_course_2017_virage_ameliore by Freescale_Cachan

Mlib.cpp

Committer:
leandre
Date:
2017-03-17
Revision:
2:a37de5eb8bf3
Parent:
0:3ec7fc598e48
Child:
3:c0aae5aa230b

File content as of revision 2:a37de5eb8bf3:

#include "Mlib.h"

/***********AUTRES_FCT***************/

char middle_3(char *tab)
{
    tri_a_bulle(tab, 3);
    return tab[1];
}

int middle_3U(int *tab)
{
    tri_a_bulleU(tab, 3);
    return tab[1];
}

float middle_3F(float *tab)
{
    
    return tri_a_bulleF(tab, 3);
}


void tri_a_bulle(char* t, int const size) 
{
    int en_desordre = 1;
    int i,j;

    for (i = 0; (i < size) && en_desordre; ++i)
    {
        en_desordre = 0;
        for (j = 1; j < (size - i); ++j)
        {
            if (t[j-1] > t[j])
            {
                int temp = t[j-1];
                t[j-1] = t[j];
                t[j] = temp;
                en_desordre = 1;
            }
        }
    }
}
void tri_a_bulleU(int* t, int const size) 
{
    int en_desordre = 1;
    int i,j;

    for (i = 0; (i < size) && en_desordre; ++i)
    {
        en_desordre = 0;
        for (j = 1; j < (size - i); ++j)
        {
            if (t[j-1] > t[j])
            {
                int temp = t[j-1];
                t[j-1] = t[j];
                t[j] = temp;
                en_desordre = 1;
            }
        }
    }
}
float tri_a_bulleF(float* t2, int const size) 
{
    float t[3];
    t[0] = t2[0];
    t[1] = t2[1];
    t[2] = t2[2];
    
        
    int en_desordre = 1;
    int i,j;

    for (i = 0; (i < size) && en_desordre; ++i)
    {
        en_desordre = 0;
        for (j = 1; j < (size - i); ++j)
        {
            if (t[j-1] > t[j])
            {
                float temp = t[j-1];
                t[j-1] = t[j];
                t[j] = temp;
                en_desordre = 1;
            }
        }
    }
    return t[1];
}

unsigned char Moyenne(unsigned char *tab)
{
    int i = 0;
    int somme = 0;
    for(i = 0; i < 128;i++)
    {
        somme+=tab[i];
    }
    return somme/128.0;
}
char Moyenne2(char *tab, int nb)
{
    int i = 0;
    int somme = 0;
    for(i = 0; i < nb;i++)
    {
        somme+=tab[i];
    }
    return float(somme/nb);
}

void menu()
{
    static int p = 0;
    p++;
        wait(0.1);
        if(BP2)
        {
            seuil_der++;
            if(seuil_der == 2) seuil_der = -1;
            led1 = 0;
            wait(0.5);
        }
        if(seuil_der == 0)
        {
            led1 = p%2;
        }
        else if(seuil_der == 1)
        {
            led1 = 1;
        }
        else
        {
            led1 = 0;
        }
        if(S1 == 1)
        {
            led2 = 1;
        }
        else led2 = 0;
        if(S2 == 1)
        {
            led3 = 1;
        }
        else led3= 0;
}

void fin_course()
{
    static int arr = 0;
    static char moy_inter;
    
    /*if((somme_derivee >= (Moyenne2(last_five, 5) + (SEUIL_NB_DERRIVEE+seuil_der))) || arr != 0)
    {
        if(t_debut.read_ms() > 500 && S1 == 1 && arr == 0)
        {
           arr = t_debut.read_ms(); 
        }
        else if(arr != 0 && t_debut.read_ms() > arr+100)
        {
            VIT = 0;
            pc.printf("arret\ndelai:%d ms\n",(t_debut.read_ms()-arr));
        }
        
        
    }
    
    for(int j = 4; j > 0; j--)
    {
        last_five[j] = last_five[j-1];
    }*/
     
    if (t_debut.read_ms()>500){
        moy_inter=Moyenne2(Interval, TAILLE_TAB_INT);
        //pc.printf("%d\n",moy_inter);
        
        if((mem_a>6)&&(mem_b>6)){  // si la voiture n'est pas trop sur l'exterieur de la piste
            if(S1 && (moy_inter<70) && (moy_inter>40)){  //si l'espace entre les deux lignes est plus petit que la normale (arret)
             
                //for(int i=0;i<5;i++)  pc.printf("%d ",Interval[i]);
                //pc.printf("\n");
                VIT=0;
                pc.printf("\narret ");
                pc.printf("\n%d ",moy_inter);
        
                }   
            }
    }
        
    for(int j = TAILLE_TAB_INT-1; j > 0; j--)
    {
        Interval[j] = Interval[j-1];
    }   
}





//void F_BalanceBlancs()
//{
//    unsigned char moy = 0;
//    moy = Moyenne(cam_data);
//    if(moy > LUMI_MOYENNE && tempsInte >= MIN_INTE) tempsInte-=INCREM_BALANCE;
//    else if(tempsInte <= MAX_INTE) tempsInte+=INCREM_BALANCE;
//}