TER Atienza Pongnot 2019 / Mbed 2 deprecated Carte_Moteur_test_asservissement_1M

Dependencies:   mbed 7366_lib TLE5206_lib

Committer:
natienza
Date:
Wed Mar 13 10:29:04 2019 +0000
Revision:
8:91eb7435c3e0
Parent:
7:09004b460bd1
Child:
9:eb3f9744ae5d
correcteur PI implemeter avec equ de recurrence

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gpongnot 0:509b29d50fcb 1 //Includes
gpongnot 0:509b29d50fcb 2 #include "mbed.h"
gpongnot 0:509b29d50fcb 3 #include "7366_lib.h"
gpongnot 0:509b29d50fcb 4 #include "TLE5206_lib.h"
gpongnot 0:509b29d50fcb 5
gpongnot 3:737ac9c24ca5 6 // Caractéristiques de structure
gpongnot 4:80f612396136 7 #define RAYON_ROUE 0.025 // [m]
gpongnot 4:80f612396136 8 #define DIST1 0.15 // [m]
gpongnot 4:80f612396136 9 #define DIST2 0.15 // [m]
gpongnot 4:80f612396136 10 #define DIST3 0.0075 // [m]
gpongnot 3:737ac9c24ca5 11 #define PI 3.14159265359
gpongnot 4:80f612396136 12 #define RESOLUTION_ENCO 14336 // = 14*1024 = rapport_reduction * nbr de top par tour
gpongnot 4:80f612396136 13 #define ETALONNAGE_LACET 0.88
gpongnot 4:80f612396136 14 #define ETALONNAGE_XY 1.065
gpongnot 0:509b29d50fcb 15 // Liaison SPI avec les compteurs
gpongnot 0:509b29d50fcb 16 #define SPI_SCLK PA_5 //A4
gpongnot 0:509b29d50fcb 17 #define SPI_MISO PA_6 //A5
gpongnot 0:509b29d50fcb 18 #define SPI_MOSI PA_7 //A6
gpongnot 0:509b29d50fcb 19 #define SPI_CS3 PA_0//A0
gpongnot 0:509b29d50fcb 20 #define SPI_CS2 PA_1
gpongnot 0:509b29d50fcb 21 #define SPI_CS1 PA_3
gpongnot 0:509b29d50fcb 22
gpongnot 0:509b29d50fcb 23 // Vers le pont en H
gpongnot 0:509b29d50fcb 24 #define H1_IN1 PA_9 //D1 - PWM1/2
gpongnot 0:509b29d50fcb 25 #define H1_IN2 PA_10 //D0 - PWM1/3
gpongnot 0:509b29d50fcb 26 #define H2_IN1 PA_8 //D9 - PWM1/1
gpongnot 0:509b29d50fcb 27 #define H2_IN2 PB_7 //D4 - PWM17/1
gpongnot 0:509b29d50fcb 28 #define H3_IN1 PB_6 //D5 - PWM16/1
gpongnot 0:509b29d50fcb 29 #define H3_IN2 PA_4 //A3 - PWM3/2
gpongnot 0:509b29d50fcb 30
gpongnot 0:509b29d50fcb 31 #define DECOUP_HACH 50 //us - 20 000 kHz pour les oreilles
natienza 2:486bb9b6bd78 32 #define PERIODE_AFF 500 //ms
natienza 2:486bb9b6bd78 33 #define PERIODE_ASSERV 50 //ms
natienza 8:91eb7435c3e0 34 #define Te 50 //ms /!\ Te doit être égale à la période d'asservissement
gpongnot 0:509b29d50fcb 35
gpongnot 3:737ac9c24ca5 36 // Constantes Asservissement
gpongnot 5:efd14a490f49 37 #define GAIN_POS 1
gpongnot 5:efd14a490f49 38 #define GAIN_ANG 0
natienza 8:91eb7435c3e0 39 #define Ti 200 //ms
gpongnot 6:85ade96c99b1 40 #define GAIN_POS_INT 0.1
gpongnot 6:85ade96c99b1 41 #define GAIN_ANG_INT 0.1
gpongnot 6:85ade96c99b1 42 #define ERREUR_POS 0.5
gpongnot 6:85ade96c99b1 43 #define ERREUR_ANG 0.5
gpongnot 3:737ac9c24ca5 44
natienza 2:486bb9b6bd78 45 Serial pc(USBTX,USBRX);
natienza 2:486bb9b6bd78 46 Timer timer;
gpongnot 0:509b29d50fcb 47 DigitalOut myled(LED3);
gpongnot 0:509b29d50fcb 48
gpongnot 0:509b29d50fcb 49 SPI_7366 compt1(SPI_MOSI, SPI_MISO, SPI_SCLK,SPI_CS1);
gpongnot 0:509b29d50fcb 50 SPI_7366 compt2(SPI_MOSI, SPI_MISO, SPI_SCLK,SPI_CS2);
gpongnot 0:509b29d50fcb 51 SPI_7366 compt3(SPI_MOSI, SPI_MISO, SPI_SCLK,SPI_CS3);
gpongnot 0:509b29d50fcb 52
gpongnot 0:509b29d50fcb 53 TLE5206 moteur1(H1_IN1,H1_IN2);
gpongnot 0:509b29d50fcb 54 TLE5206 moteur2(H2_IN1,H2_IN2);
gpongnot 0:509b29d50fcb 55 TLE5206 moteur3(H3_IN1,H3_IN2);
gpongnot 0:509b29d50fcb 56
gpongnot 3:737ac9c24ca5 57 struct Vect3{
gpongnot 3:737ac9c24ca5 58 double x;
gpongnot 3:737ac9c24ca5 59 double y;
gpongnot 3:737ac9c24ca5 60 double z;
gpongnot 3:737ac9c24ca5 61 };
gpongnot 3:737ac9c24ca5 62
gpongnot 3:737ac9c24ca5 63
gpongnot 3:737ac9c24ca5 64 struct Vect2{
gpongnot 3:737ac9c24ca5 65 float x;
gpongnot 3:737ac9c24ca5 66 float y;
gpongnot 3:737ac9c24ca5 67 };
gpongnot 3:737ac9c24ca5 68
gpongnot 3:737ac9c24ca5 69 Vect3 initVect3(){
gpongnot 3:737ac9c24ca5 70 Vect3 result;
gpongnot 3:737ac9c24ca5 71 result.x = 0;
gpongnot 3:737ac9c24ca5 72 result.y = 0;
gpongnot 3:737ac9c24ca5 73 result.z = 0;
gpongnot 3:737ac9c24ca5 74 return result;
gpongnot 3:737ac9c24ca5 75 }
gpongnot 3:737ac9c24ca5 76
gpongnot 3:737ac9c24ca5 77 Vect3 calculatePosition(){
gpongnot 3:737ac9c24ca5 78 static Vect3 theta = initVect3();
gpongnot 3:737ac9c24ca5 79 static Vect3 position = initVect3();
gpongnot 3:737ac9c24ca5 80 Vect3 dTheta;
gpongnot 3:737ac9c24ca5 81 double dPsi = 0;
gpongnot 3:737ac9c24ca5 82
gpongnot 3:737ac9c24ca5 83 dTheta.x = 2*PI/RESOLUTION_ENCO*compt2.read_value() - theta.x;
gpongnot 3:737ac9c24ca5 84 dTheta.y = 2*PI/RESOLUTION_ENCO*compt1.read_value() - theta.y;
gpongnot 3:737ac9c24ca5 85 dTheta.z = 2*PI/RESOLUTION_ENCO*compt3.read_value() - theta.z;
gpongnot 3:737ac9c24ca5 86 theta.x += dTheta.x;
gpongnot 3:737ac9c24ca5 87 theta.y += dTheta.y;
gpongnot 3:737ac9c24ca5 88 theta.z += dTheta.z;
gpongnot 3:737ac9c24ca5 89
gpongnot 4:80f612396136 90 dPsi = ETALONNAGE_LACET*RAYON_ROUE * ( dTheta.x + dTheta.y + dTheta.z)/(DIST1 + DIST2 + DIST3);
natienza 2:486bb9b6bd78 91
gpongnot 3:737ac9c24ca5 92 position.z += dPsi;// Psi actuel
gpongnot 3:737ac9c24ca5 93
gpongnot 4:80f612396136 94 position.x += ETALONNAGE_XY*((-RAYON_ROUE * dTheta.x + DIST1 * dPsi) * cos(position.z) + (-RAYON_ROUE * dTheta.y + DIST1 * dPsi) * cos(position.z + 2*PI/3) + (-RAYON_ROUE * dTheta.z + DIST1 * dPsi)*cos(position.z + 4*PI/3))*2/3;
gpongnot 4:80f612396136 95 position.y +=-ETALONNAGE_XY*((-RAYON_ROUE * dTheta.x + DIST1 * dPsi) * sin(position.z) + (-RAYON_ROUE * dTheta.y + DIST1 * dPsi) * sin(position.z + 2*PI/3) + (-RAYON_ROUE * dTheta.z + DIST1 * dPsi)*sin(position.z + 4*PI/3))*2/3;
gpongnot 3:737ac9c24ca5 96
gpongnot 3:737ac9c24ca5 97 return position;
gpongnot 3:737ac9c24ca5 98 }
gpongnot 3:737ac9c24ca5 99
gpongnot 3:737ac9c24ca5 100 Vect3 calcErreur(Vect3 consigne, Vect3 position){
gpongnot 3:737ac9c24ca5 101 Vect3 erreur;
gpongnot 3:737ac9c24ca5 102 erreur.x = position.x - consigne.x;
gpongnot 3:737ac9c24ca5 103 erreur.x = position.y - consigne.y;
gpongnot 3:737ac9c24ca5 104 erreur.x = position.z - consigne.z;
gpongnot 3:737ac9c24ca5 105 return erreur;
gpongnot 3:737ac9c24ca5 106 }
gpongnot 3:737ac9c24ca5 107
gpongnot 3:737ac9c24ca5 108 Vect3 calcCommandeXYZ(Vect3 erreur){
gpongnot 6:85ade96c99b1 109 static Vect3 commande;
natienza 8:91eb7435c3e0 110 static Vect3 oldErreur;
gpongnot 6:85ade96c99b1 111 if (erreur.x > ERREUR_POS){
gpongnot 6:85ade96c99b1 112 commande.x = GAIN_POS*erreur.x;
gpongnot 6:85ade96c99b1 113 } else {
natienza 8:91eb7435c3e0 114 commande.x = commande.x + GAIN_POS*(Ti+Te)/Te*erreur.x - GAIN_POS*oldErreur.x;
natienza 8:91eb7435c3e0 115 //commande.x = GAIN_POS*(erreur.x + GAIN_POS_INT*commande.x);
gpongnot 6:85ade96c99b1 116 }
gpongnot 6:85ade96c99b1 117 if (erreur.y > ERREUR_POS){
gpongnot 6:85ade96c99b1 118 commande.y = GAIN_POS*erreur.y;
gpongnot 6:85ade96c99b1 119 } else {
natienza 8:91eb7435c3e0 120 commande.y = commande.y + GAIN_POS*(Ti+Te)/Te*erreur.y - GAIN_POS*oldErreur.y;
natienza 8:91eb7435c3e0 121 //commande.y = GAIN_POS*(erreur.y + GAIN_POS_INT*commande.y);
gpongnot 6:85ade96c99b1 122 }
gpongnot 6:85ade96c99b1 123 if (erreur.z > ERREUR_ANG){
gpongnot 6:85ade96c99b1 124 commande.z = GAIN_ANG*erreur.z;
gpongnot 6:85ade96c99b1 125 } else {
natienza 8:91eb7435c3e0 126 commande.z = commande.z + GAIN_ANG*(Ti+Te)/Te*erreur.z - GAIN_ANG*oldErreur.z;
natienza 8:91eb7435c3e0 127 //commande.z = GAIN_ANG*(erreur.z + GAIN_ANG_INT*commande.z);
gpongnot 6:85ade96c99b1 128 }
natienza 8:91eb7435c3e0 129 oldErreur = erreur;
gpongnot 3:737ac9c24ca5 130 return commande;
gpongnot 3:737ac9c24ca5 131 }
gpongnot 3:737ac9c24ca5 132
gpongnot 3:737ac9c24ca5 133 Vect3 calcCommande123(Vect3 commandeXYZ, Vect3 position){
gpongnot 3:737ac9c24ca5 134 Vect3 commande123;
gpongnot 3:737ac9c24ca5 135 commande123.x = -commandeXYZ.x*cos(position.z+0*PI/3)+commandeXYZ.y*sin(position.z+0*PI/3)+commandeXYZ.z;
gpongnot 3:737ac9c24ca5 136 commande123.y = -commandeXYZ.x*cos(position.z+2*PI/3)+commandeXYZ.y*sin(position.z+2*PI/3)+commandeXYZ.z;
gpongnot 3:737ac9c24ca5 137 commande123.z = -commandeXYZ.x*cos(position.z+4*PI/3)+commandeXYZ.y*sin(position.z+4*PI/3)+commandeXYZ.z;
gpongnot 3:737ac9c24ca5 138 return commande123;
gpongnot 3:737ac9c24ca5 139 }
gpongnot 3:737ac9c24ca5 140
gpongnot 3:737ac9c24ca5 141 void moveBot(Vect3 commande123){
gpongnot 5:efd14a490f49 142 moteur1.write(commande123.y);
gpongnot 5:efd14a490f49 143 moteur2.write(commande123.x);
gpongnot 3:737ac9c24ca5 144 moteur3.write(commande123.z);
gpongnot 3:737ac9c24ca5 145 }
gpongnot 3:737ac9c24ca5 146
gpongnot 3:737ac9c24ca5 147 int main(){
gpongnot 3:737ac9c24ca5 148
natienza 2:486bb9b6bd78 149 //setup
gpongnot 0:509b29d50fcb 150 compt1.setup();
gpongnot 0:509b29d50fcb 151 compt2.setup();
gpongnot 0:509b29d50fcb 152 compt3.setup();
gpongnot 0:509b29d50fcb 153
gpongnot 0:509b29d50fcb 154 moteur1.setup(DECOUP_HACH);
gpongnot 0:509b29d50fcb 155 moteur2.setup(DECOUP_HACH);
gpongnot 0:509b29d50fcb 156 moteur3.setup(DECOUP_HACH);
gpongnot 0:509b29d50fcb 157
natienza 2:486bb9b6bd78 158 timer.start();
gpongnot 5:efd14a490f49 159 pc.printf("SETUP effectue\n\r");
natienza 2:486bb9b6bd78 160
natienza 2:486bb9b6bd78 161 //variables
gpongnot 3:737ac9c24ca5 162 Vect3 position = initVect3();
gpongnot 3:737ac9c24ca5 163 Vect3 erreur = initVect3();
gpongnot 3:737ac9c24ca5 164 Vect3 commandeXYZ = initVect3();
gpongnot 3:737ac9c24ca5 165 Vect3 commande123 = initVect3();
gpongnot 3:737ac9c24ca5 166 Vect3 consigne = initVect3();
gpongnot 5:efd14a490f49 167 consigne.x = 0.30;
gpongnot 5:efd14a490f49 168 consigne.y = 0.30;
gpongnot 5:efd14a490f49 169 consigne.z = PI/6;
gpongnot 7:09004b460bd1 170
natienza 2:486bb9b6bd78 171 uint32_t seuilAffichage = PERIODE_AFF;
natienza 2:486bb9b6bd78 172 uint32_t seuilAsserv = PERIODE_ASSERV;
natienza 2:486bb9b6bd78 173
gpongnot 0:509b29d50fcb 174 // Loop
gpongnot 0:509b29d50fcb 175 while(1) {
natienza 2:486bb9b6bd78 176
natienza 2:486bb9b6bd78 177 if (timer.read_ms() > seuilAffichage){
natienza 2:486bb9b6bd78 178 seuilAffichage += PERIODE_AFF;
gpongnot 5:efd14a490f49 179 //pc.printf("lacet : %f\n\rpositionX : %f\n\rpositionY: %f\n\n\r",360/(2*PI)*position.z, position.x, position.y);
gpongnot 5:efd14a490f49 180 pc.printf("erreur lacet : %f\n\rerreurX : %f\n\rereeurY: %f\n\n\r",erreur.z, erreur.x, erreur.y);
gpongnot 5:efd14a490f49 181 pc.printf("commande lacet : %f\n\rcommandeX : %f\n\rcommandeY: %f\n\n\r",commande123.z, commande123.x, commande123.y);
gpongnot 3:737ac9c24ca5 182 //pc.printf("compt3 : %f\n\rcompt1 : %f\n\rcompt2: %f\n\n\r",2*PI/RESOLUTION_ENCO*compt3.read_value(),2*PI/RESOLUTION_ENCO*compt1.read_value(), 2*PI/RESOLUTION_ENCO*compt2.read_value());
gpongnot 3:737ac9c24ca5 183 //pc.printf("compt3 : %f\n\rcompt1 : %f\n\rcompt2: %f\n\n\r",2*PI/RESOLUTION_ENCO*compt3.read_value(),2*PI/RESOLUTION_ENCO*compt1.read_value(), 2*PI/RESOLUTION_ENCO*compt2.read_value());
gpongnot 5:efd14a490f49 184
natienza 2:486bb9b6bd78 185 myled = !myled;
natienza 2:486bb9b6bd78 186 }
natienza 2:486bb9b6bd78 187 if (timer.read_ms() > seuilAsserv){
natienza 2:486bb9b6bd78 188 seuilAsserv += PERIODE_ASSERV;
gpongnot 3:737ac9c24ca5 189 position = calculatePosition();
gpongnot 3:737ac9c24ca5 190 erreur = calcErreur(consigne, position);
gpongnot 3:737ac9c24ca5 191 commandeXYZ = calcCommandeXYZ(erreur);
gpongnot 3:737ac9c24ca5 192 commande123 = calcCommande123(commandeXYZ, position);
gpongnot 3:737ac9c24ca5 193 moveBot(commande123);
gpongnot 7:09004b460bd1 194 }
gpongnot 0:509b29d50fcb 195 }
gpongnot 0:509b29d50fcb 196 }