Prog Thib Ju

Dependencies:   mbed

main.cpp

Committer:
MaxLaMenace
Date:
2019-04-15
Revision:
1:7d94c1b86ad6
Parent:
0:5535594ef331
Child:
2:8817ad0d0a78

File content as of revision 1:7d94c1b86ad6:

#include "mbed.h"
 
// Definition des variables : moteur
PwmOut moteur1(D10); 
PwmOut moteur2(D9);
DigitalOut Trig(D12);
InterruptIn reception(D8);

Serial pc(USBTX, USBRX);

Timer t2;
double  distance ;
double rc;

// Definition des variables : codeur
InterruptIn ComptIncr(PA_8);

Timer t1;

float tps1;
float tps2;
float k;
int a;
float b;

// Definition des fonctions : moteur

void front_montant()
{
    t2.start();
}

void front_descendant()
{
    t2.stop();
    double distance = 330*t2.read()/2*1.06;
    pc.printf("Distance = %lf \r\n", distance);
    t2.reset();
    if (distance<=0.10){ rc = 0;}
    if (distance<=0.20 && distance>0.10){ rc = 0.5;}
    if (distance >=0.20) { rc = 1; }
    moteur2.write(rc);
    
    pc.printf("RC = %lf \r\n", rc);
} 

// Definition des fonctions : codeur

void vitesse();

void vitesse(){
    if (a == 0){
        tps1 = t1.read();
        k = 0;
        }    
        a++;
    if (a == 81) {
        tps2 = t1.read();
        k = tps2 - tps1;
      // printf("Intervalle de temps =%f\n",k);
        a = 0;
        //printf("Vitesse = %f tours/s\n", 10/t.read());
    }  
}    

// Debut du main
int main() 
{
    
    
    // Initialisation Moteur
    t2.reset();
    reception.rise(&front_montant);
    reception.fall(&front_descendant);
    moteur1.period_ms(10);
    moteur2.period_ms(10);
    moteur1.write(0);
    moteur2.write(0);
    pc.baud(9600);
    
    // Initialisation Codeur
    a = 0;
    k = 0;
    b = 0;
    t1.start();

    ComptIncr.rise(&vitesse);
    
    // Debut du while
    while(1)
    {
        // Partie moteur
        Trig = 1;
        wait(0.001);
        Trig =0 ; 
        wait(0.001);

    
        // Partie codeur
        b = t1.read();
        //printf("Compteur = %d\n", a);
        //printf("Temps 1 = %f\n", tps1);
        //printf("Temps 2 = %f\n", tps2);
        //printf("Test temps 1 = %f\n", b);
    }
}