TER Atienza Pongnot 2019 / Mbed 2 deprecated Carte_Moteur_test_asservissement_1M

Dependencies:   mbed 7366_lib TLE5206_lib

Committer:
gpongnot
Date:
Tue Mar 12 11:11:47 2019 +0000
Revision:
6:85ade96c99b1
Parent:
3:737ac9c24ca5
Child:
7:09004b460bd1
Test PI

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