Ben Rokia Bilel
/
TESTMOT
PID
main.cpp@0:203388cc79a8, 2020-02-13 (annotated)
- Committer:
- bilben99
- Date:
- Thu Feb 13 20:05:58 2020 +0000
- Revision:
- 0:203388cc79a8
PID;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bilben99 | 0:203388cc79a8 | 1 | #include "mbed.h" |
bilben99 | 0:203388cc79a8 | 2 | #include "bloc_io.h" |
bilben99 | 0:203388cc79a8 | 3 | #define PI 3.14159 |
bilben99 | 0:203388cc79a8 | 4 | #define R 0.22 //Roue de 22cm de rayon |
bilben99 | 0:203388cc79a8 | 5 | #define p 20 //Nombre de paires de pôles du mot |
bilben99 | 0:203388cc79a8 | 6 | int w=1; |
bilben99 | 0:203388cc79a8 | 7 | float I; |
bilben99 | 0:203388cc79a8 | 8 | float K=0.13; |
bilben99 | 0:203388cc79a8 | 9 | float G=0.9/K; |
bilben99 | 0:203388cc79a8 | 10 | float erreur; |
bilben99 | 0:203388cc79a8 | 11 | float commande; |
bilben99 | 0:203388cc79a8 | 12 | DigitalOut myled(LED1); |
bilben99 | 0:203388cc79a8 | 13 | //AnalogIn poignée (p17); |
bilben99 | 0:203388cc79a8 | 14 | Serial pc(USBTX, USBRX); |
bilben99 | 0:203388cc79a8 | 15 | //DigitalOut valid_pwm(p21); |
bilben99 | 0:203388cc79a8 | 16 | Bloc_IO MyPLD(p25, p26, p5, p6, p7, p8, p9, p10, p23, p24); |
bilben99 | 0:203388cc79a8 | 17 | InterruptIn valid(p22); |
bilben99 | 0:203388cc79a8 | 18 | Ticker calcul; |
bilben99 | 0:203388cc79a8 | 19 | Ticker Affiche; |
bilben99 | 0:203388cc79a8 | 20 | LocalFileSystem local("local"); |
bilben99 | 0:203388cc79a8 | 21 | FILE *fp = fopen("/local/mesures.txt", "w"); |
bilben99 | 0:203388cc79a8 | 22 | |
bilben99 | 0:203388cc79a8 | 23 | void CptFronts(void); |
bilben99 | 0:203388cc79a8 | 24 | void CalculF(void); //F pour la fonction |
bilben99 | 0:203388cc79a8 | 25 | //void Calibration(void); |
bilben99 | 0:203388cc79a8 | 26 | //void AfficheF(void); //F pour la fonction |
bilben99 | 0:203388cc79a8 | 27 | |
bilben99 | 0:203388cc79a8 | 28 | //int Pwmref = 0; |
bilben99 | 0:203388cc79a8 | 29 | int Val=10; |
bilben99 | 0:203388cc79a8 | 30 | int gflag=0; |
bilben99 | 0:203388cc79a8 | 31 | int Cpt = 0; |
bilben99 | 0:203388cc79a8 | 32 | int Freq=0; |
bilben99 | 0:203388cc79a8 | 33 | float Vitesse = 0; |
bilben99 | 0:203388cc79a8 | 34 | //float GazMin = 0, GazMax = 0; |
bilben99 | 0:203388cc79a8 | 35 | |
bilben99 | 0:203388cc79a8 | 36 | |
bilben99 | 0:203388cc79a8 | 37 | int main() |
bilben99 | 0:203388cc79a8 | 38 | { |
bilben99 | 0:203388cc79a8 | 39 | int Cpt50Ms=0; |
bilben99 | 0:203388cc79a8 | 40 | char cchoix = 'a'; |
bilben99 | 0:203388cc79a8 | 41 | valid.rise(&CptFronts);// Pour chaque front montant |
bilben99 | 0:203388cc79a8 | 42 | valid.fall(&CptFronts);// Pour chaque front descendant |
bilben99 | 0:203388cc79a8 | 43 | calcul.attach(&CalculF, 0.05); //appel calcul toutes les 0.1s |
bilben99 | 0:203388cc79a8 | 44 | |
bilben99 | 0:203388cc79a8 | 45 | while(true) { |
bilben99 | 0:203388cc79a8 | 46 | if(gflag==true) { |
bilben99 | 0:203388cc79a8 | 47 | fprintf(fp,"%g ; %d \n", Vitesse,Val ); |
bilben99 | 0:203388cc79a8 | 48 | |
bilben99 | 0:203388cc79a8 | 49 | gflag=false; |
bilben99 | 0:203388cc79a8 | 50 | |
bilben99 | 0:203388cc79a8 | 51 | |
bilben99 | 0:203388cc79a8 | 52 | Cpt50Ms++; |
bilben99 | 0:203388cc79a8 | 53 | switch(cchoix) { |
bilben99 | 0:203388cc79a8 | 54 | |
bilben99 | 0:203388cc79a8 | 55 | case 'a' : { |
bilben99 | 0:203388cc79a8 | 56 | |
bilben99 | 0:203388cc79a8 | 57 | if(Cpt50Ms>=80) { |
bilben99 | 0:203388cc79a8 | 58 | cchoix='b'; |
bilben99 | 0:203388cc79a8 | 59 | Cpt50Ms=0; |
bilben99 | 0:203388cc79a8 | 60 | } |
bilben99 | 0:203388cc79a8 | 61 | break; |
bilben99 | 0:203388cc79a8 | 62 | } |
bilben99 | 0:203388cc79a8 | 63 | case 'b' : { |
bilben99 | 0:203388cc79a8 | 64 | Val=13; |
bilben99 | 0:203388cc79a8 | 65 | |
bilben99 | 0:203388cc79a8 | 66 | if(Cpt50Ms>=80) { |
bilben99 | 0:203388cc79a8 | 67 | cchoix='c'; |
bilben99 | 0:203388cc79a8 | 68 | } |
bilben99 | 0:203388cc79a8 | 69 | break; |
bilben99 | 0:203388cc79a8 | 70 | } |
bilben99 | 0:203388cc79a8 | 71 | case 'c' : { |
bilben99 | 0:203388cc79a8 | 72 | fclose(fp); |
bilben99 | 0:203388cc79a8 | 73 | Val=10; |
bilben99 | 0:203388cc79a8 | 74 | |
bilben99 | 0:203388cc79a8 | 75 | } |
bilben99 | 0:203388cc79a8 | 76 | |
bilben99 | 0:203388cc79a8 | 77 | } |
bilben99 | 0:203388cc79a8 | 78 | } |
bilben99 | 0:203388cc79a8 | 79 | |
bilben99 | 0:203388cc79a8 | 80 | } |
bilben99 | 0:203388cc79a8 | 81 | } |
bilben99 | 0:203388cc79a8 | 82 | void CalculF() |
bilben99 | 0:203388cc79a8 | 83 | { |
bilben99 | 0:203388cc79a8 | 84 | |
bilben99 | 0:203388cc79a8 | 85 | Vitesse = Cpt*10*( (2*PI*R)*3.6)/(6*p) ; // compteur du nb de fronts mais il faut multiplié par 10 pour l'unité (100ms*10=1s), (1tours 3.6/6 pour les m/s) |
bilben99 | 0:203388cc79a8 | 86 | Freq=Cpt; |
bilben99 | 0:203388cc79a8 | 87 | erreur=Val-Vitesse; |
bilben99 | 0:203388cc79a8 | 88 | |
bilben99 | 0:203388cc79a8 | 89 | I=I+w*erreur*(0.05/0.03); |
bilben99 | 0:203388cc79a8 | 90 | |
bilben99 | 0:203388cc79a8 | 91 | |
bilben99 | 0:203388cc79a8 | 92 | commande=G*(erreur+I); |
bilben99 | 0:203388cc79a8 | 93 | if(commande>255) |
bilben99 | 0:203388cc79a8 | 94 | { |
bilben99 | 0:203388cc79a8 | 95 | w=0; |
bilben99 | 0:203388cc79a8 | 96 | } |
bilben99 | 0:203388cc79a8 | 97 | else |
bilben99 | 0:203388cc79a8 | 98 | { |
bilben99 | 0:203388cc79a8 | 99 | w=1; |
bilben99 | 0:203388cc79a8 | 100 | } |
bilben99 | 0:203388cc79a8 | 101 | if (commande>255) |
bilben99 | 0:203388cc79a8 | 102 | commande=255; |
bilben99 | 0:203388cc79a8 | 103 | if (commande<0) |
bilben99 | 0:203388cc79a8 | 104 | commande = 0; |
bilben99 | 0:203388cc79a8 | 105 | MyPLD.write(commande); |
bilben99 | 0:203388cc79a8 | 106 | Cpt = 0; //RAZ |
bilben99 | 0:203388cc79a8 | 107 | gflag=1; |
bilben99 | 0:203388cc79a8 | 108 | |
bilben99 | 0:203388cc79a8 | 109 | } |
bilben99 | 0:203388cc79a8 | 110 | void CptFronts(void) |
bilben99 | 0:203388cc79a8 | 111 | { |
bilben99 | 0:203388cc79a8 | 112 | Cpt++; |
bilben99 | 0:203388cc79a8 | 113 | } |
bilben99 | 0:203388cc79a8 | 114 | /*void AfficheF(void) |
bilben99 | 0:203388cc79a8 | 115 | { |
bilben99 | 0:203388cc79a8 | 116 | pc.printf("Vitesse : %f m/s \n\r", Vitesse); //Affiche de la vitesse en ms |
bilben99 | 0:203388cc79a8 | 117 | pc.printf("Frequ=cpt : %d \n\r", |
bilben99 | 0:203388cc79a8 | 118 | }*/ |