programme ines avce xbee

Dependencies:   MMA8451Q mbed xbee_lib

Fork of Programme_course by Freescale_Cachan

Mlib.cpp

Committer:
inesmas3
Date:
2017-02-08
Revision:
1:4a9196bcf97a
Parent:
0:3ec7fc598e48
Child:
2:ddf9879f0e25

File content as of revision 1:4a9196bcf97a:

#include "Mlib.h"

void Gestion_bosse()
{
    if(BOSSE_ACTIVE)//Si on active la détection de la bosse
    {
        led5=0;
        led6=0;
        led7=0;
    
        pc.printf("bosse: %d , X: %1.2f , ajustement : %hi\n ",bosse,middle_3F(last_three),ajust_vit_bosse);
        
        
        last_three[2] = last_three[1]; // les 3 dernieres valeurs de X sont stockées daans un tableau
        last_three[1] = last_three[0];
        last_three[0] = acc.getAccX();
        
        if(bosse == 1)
        {
            ajust_vit_bosse = 60; 
            led6=1;
            led4=1;
        }
        else if(bosse == 3)
        ajust_vit_bosse = -80;
        
        else 
        {
            ajust_vit_bosse = 0;
            led7=1;
            led1=1;
        }
        
        if((abs(middle_3F(last_three)) > 0.13 ) && (bosse == 0)) // si montée détectée
        {
            bosse = 1;
            //tep = (int)t_debut.read_ms();
        }
        else if(bosse == 1 && (middle_3F(last_three) > -0.1)) 
        {
            bosse = 2;
        }
        else if(bosse == 2 && (middle_3F(last_three) > 0.19)) 
        {
            bosse = 3;
            
        }
        else if(bosse==3 && (middle_3F(last_three) > 0.19))
        {
                 ajust_vit_bosse = -50;
                 led5=1;
                 led2=1;
                 pc.printf("bosse: %d , X: %1.2f , ajustement : %hi\n",bosse,middle_3F(last_three),ajust_vit_bosse);
         }
                 
        else if (bosse==3 && (middle_3F(last_three) < 0.19)) 
        {
            bosse=0;
            ajust_vit_bosse=0;
            }
    }
}

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

char middle_3(char *tab) // prend la valeur du milieu quand les valeurs sont rangées
{
    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) // permet de trier les valeurs du tableau dans l'ordre croissant
{
    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) // bouton B
        {
            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;
    if(somme_derivee >= (Moyenne2(last_five, 5) + (SEUIL_NB_DERRIVEE+5*seuil_der)) || arr != 0)
    {
        if(t_debut.read() > 3 && S1 == 1 && arr == 0)
        {
           arr = t_debut.read_ms();
        }
        else if(arr != 0 && t_debut.read_ms() > arr+100)
        {
            VIT = 0;
        }
    }
    for(int j = 4; j > 0; j--)
    {
        last_five[j] = last_five[j-1];
//                pc.printf("%d\n", last_five[j]);
    }
//            pc.printf("%d     |     %d\n", (somme_derivee), (Moyenne2(last_five, 5)));
    last_five[0] = somme_derivee;
}





//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;
//}