Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 7366_lib TLE5206_lib
main.cpp@5:efd14a490f49, 2019-02-21 (annotated)
- Committer:
- gpongnot
- Date:
- Thu Feb 21 14:23:12 2019 +0000
- Revision:
- 5:efd14a490f49
- Parent:
- 4:80f612396136
- Child:
- 7:09004b460bd1
essai correcteur P juste sur position avec 2 moteurs ok!!; rajouter le 3e moteur et implementer un PI (comment on regle les parametres -->simu?)
Who changed what in which revision?
User | Revision | Line number | New 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 |
gpongnot | 0:509b29d50fcb | 34 | |
gpongnot | 3:737ac9c24ca5 | 35 | // Constantes Asservissement |
gpongnot | 5:efd14a490f49 | 36 | #define GAIN_POS 1 |
gpongnot | 5:efd14a490f49 | 37 | #define GAIN_ANG 0 |
gpongnot | 3:737ac9c24ca5 | 38 | |
natienza | 2:486bb9b6bd78 | 39 | Serial pc(USBTX,USBRX); |
natienza | 2:486bb9b6bd78 | 40 | Timer timer; |
gpongnot | 0:509b29d50fcb | 41 | DigitalOut myled(LED3); |
gpongnot | 0:509b29d50fcb | 42 | |
gpongnot | 0:509b29d50fcb | 43 | SPI_7366 compt1(SPI_MOSI, SPI_MISO, SPI_SCLK,SPI_CS1); |
gpongnot | 0:509b29d50fcb | 44 | SPI_7366 compt2(SPI_MOSI, SPI_MISO, SPI_SCLK,SPI_CS2); |
gpongnot | 0:509b29d50fcb | 45 | SPI_7366 compt3(SPI_MOSI, SPI_MISO, SPI_SCLK,SPI_CS3); |
gpongnot | 0:509b29d50fcb | 46 | |
gpongnot | 0:509b29d50fcb | 47 | TLE5206 moteur1(H1_IN1,H1_IN2); |
gpongnot | 0:509b29d50fcb | 48 | TLE5206 moteur2(H2_IN1,H2_IN2); |
gpongnot | 0:509b29d50fcb | 49 | TLE5206 moteur3(H3_IN1,H3_IN2); |
gpongnot | 0:509b29d50fcb | 50 | |
gpongnot | 3:737ac9c24ca5 | 51 | struct Vect3{ |
gpongnot | 3:737ac9c24ca5 | 52 | double x; |
gpongnot | 3:737ac9c24ca5 | 53 | double y; |
gpongnot | 3:737ac9c24ca5 | 54 | double z; |
gpongnot | 3:737ac9c24ca5 | 55 | }; |
gpongnot | 3:737ac9c24ca5 | 56 | |
gpongnot | 3:737ac9c24ca5 | 57 | |
gpongnot | 3:737ac9c24ca5 | 58 | struct Vect2{ |
gpongnot | 3:737ac9c24ca5 | 59 | float x; |
gpongnot | 3:737ac9c24ca5 | 60 | float y; |
gpongnot | 3:737ac9c24ca5 | 61 | }; |
gpongnot | 3:737ac9c24ca5 | 62 | |
gpongnot | 3:737ac9c24ca5 | 63 | Vect3 initVect3(){ |
gpongnot | 3:737ac9c24ca5 | 64 | Vect3 result; |
gpongnot | 3:737ac9c24ca5 | 65 | result.x = 0; |
gpongnot | 3:737ac9c24ca5 | 66 | result.y = 0; |
gpongnot | 3:737ac9c24ca5 | 67 | result.z = 0; |
gpongnot | 3:737ac9c24ca5 | 68 | return result; |
gpongnot | 3:737ac9c24ca5 | 69 | } |
gpongnot | 3:737ac9c24ca5 | 70 | |
gpongnot | 3:737ac9c24ca5 | 71 | Vect3 calculatePosition(){ |
gpongnot | 3:737ac9c24ca5 | 72 | |
gpongnot | 3:737ac9c24ca5 | 73 | static Vect3 theta = initVect3(); |
gpongnot | 3:737ac9c24ca5 | 74 | static Vect3 position = initVect3(); |
gpongnot | 3:737ac9c24ca5 | 75 | Vect3 dTheta; |
gpongnot | 3:737ac9c24ca5 | 76 | double dPsi = 0; |
gpongnot | 3:737ac9c24ca5 | 77 | |
gpongnot | 3:737ac9c24ca5 | 78 | dTheta.x = 2*PI/RESOLUTION_ENCO*compt2.read_value() - theta.x; |
gpongnot | 3:737ac9c24ca5 | 79 | dTheta.y = 2*PI/RESOLUTION_ENCO*compt1.read_value() - theta.y; |
gpongnot | 3:737ac9c24ca5 | 80 | dTheta.z = 2*PI/RESOLUTION_ENCO*compt3.read_value() - theta.z; |
gpongnot | 3:737ac9c24ca5 | 81 | theta.x += dTheta.x; |
gpongnot | 3:737ac9c24ca5 | 82 | theta.y += dTheta.y; |
gpongnot | 3:737ac9c24ca5 | 83 | theta.z += dTheta.z; |
gpongnot | 3:737ac9c24ca5 | 84 | |
gpongnot | 4:80f612396136 | 85 | dPsi = ETALONNAGE_LACET*RAYON_ROUE * ( dTheta.x + dTheta.y + dTheta.z)/(DIST1 + DIST2 + DIST3); |
natienza | 2:486bb9b6bd78 | 86 | |
gpongnot | 3:737ac9c24ca5 | 87 | position.z += dPsi;// Psi actuel |
gpongnot | 3:737ac9c24ca5 | 88 | |
gpongnot | 4:80f612396136 | 89 | 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 | 90 | 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 | 91 | |
gpongnot | 3:737ac9c24ca5 | 92 | return position; |
gpongnot | 3:737ac9c24ca5 | 93 | } |
gpongnot | 3:737ac9c24ca5 | 94 | |
gpongnot | 3:737ac9c24ca5 | 95 | Vect3 calcErreur(Vect3 consigne, Vect3 position){ |
gpongnot | 3:737ac9c24ca5 | 96 | Vect3 erreur; |
gpongnot | 3:737ac9c24ca5 | 97 | erreur.x = position.x - consigne.x; |
gpongnot | 3:737ac9c24ca5 | 98 | erreur.x = position.y - consigne.y; |
gpongnot | 3:737ac9c24ca5 | 99 | erreur.x = position.z - consigne.z; |
gpongnot | 3:737ac9c24ca5 | 100 | return erreur; |
gpongnot | 3:737ac9c24ca5 | 101 | } |
gpongnot | 3:737ac9c24ca5 | 102 | |
gpongnot | 3:737ac9c24ca5 | 103 | Vect3 calcCommandeXYZ(Vect3 erreur){ |
gpongnot | 3:737ac9c24ca5 | 104 | Vect3 commande; |
gpongnot | 3:737ac9c24ca5 | 105 | commande.x = GAIN_POS*erreur.x; |
gpongnot | 3:737ac9c24ca5 | 106 | commande.y = GAIN_POS*erreur.y; |
gpongnot | 3:737ac9c24ca5 | 107 | commande.z = GAIN_ANG*erreur.z; |
gpongnot | 3:737ac9c24ca5 | 108 | return commande; |
gpongnot | 3:737ac9c24ca5 | 109 | } |
gpongnot | 3:737ac9c24ca5 | 110 | |
gpongnot | 3:737ac9c24ca5 | 111 | Vect3 calcCommande123(Vect3 commandeXYZ, Vect3 position){ |
gpongnot | 3:737ac9c24ca5 | 112 | Vect3 commande123; |
gpongnot | 3:737ac9c24ca5 | 113 | commande123.x = -commandeXYZ.x*cos(position.z+0*PI/3)+commandeXYZ.y*sin(position.z+0*PI/3)+commandeXYZ.z; |
gpongnot | 3:737ac9c24ca5 | 114 | commande123.y = -commandeXYZ.x*cos(position.z+2*PI/3)+commandeXYZ.y*sin(position.z+2*PI/3)+commandeXYZ.z; |
gpongnot | 5:efd14a490f49 | 115 | //commande123.z = -commandeXYZ.x*cos(position.z+4*PI/3)+commandeXYZ.y*sin(position.z+4*PI/3)+commandeXYZ.z; |
gpongnot | 5:efd14a490f49 | 116 | commande123.z = 0; |
gpongnot | 3:737ac9c24ca5 | 117 | return commande123; |
gpongnot | 3:737ac9c24ca5 | 118 | } |
gpongnot | 3:737ac9c24ca5 | 119 | |
gpongnot | 3:737ac9c24ca5 | 120 | void moveBot(Vect3 commande123){ |
gpongnot | 5:efd14a490f49 | 121 | moteur1.write(commande123.y); |
gpongnot | 5:efd14a490f49 | 122 | moteur2.write(commande123.x); |
gpongnot | 3:737ac9c24ca5 | 123 | moteur3.write(commande123.z); |
gpongnot | 3:737ac9c24ca5 | 124 | } |
gpongnot | 3:737ac9c24ca5 | 125 | |
gpongnot | 3:737ac9c24ca5 | 126 | int main(){ |
gpongnot | 3:737ac9c24ca5 | 127 | |
natienza | 2:486bb9b6bd78 | 128 | //setup |
gpongnot | 0:509b29d50fcb | 129 | compt1.setup(); |
gpongnot | 0:509b29d50fcb | 130 | compt2.setup(); |
gpongnot | 0:509b29d50fcb | 131 | compt3.setup(); |
gpongnot | 0:509b29d50fcb | 132 | |
gpongnot | 0:509b29d50fcb | 133 | moteur1.setup(DECOUP_HACH); |
gpongnot | 0:509b29d50fcb | 134 | moteur2.setup(DECOUP_HACH); |
gpongnot | 0:509b29d50fcb | 135 | moteur3.setup(DECOUP_HACH); |
gpongnot | 0:509b29d50fcb | 136 | |
gpongnot | 0:509b29d50fcb | 137 | moteur1.write(-0.4); |
gpongnot | 0:509b29d50fcb | 138 | moteur2.write(-0.7); |
gpongnot | 0:509b29d50fcb | 139 | moteur3.write(0.7); |
gpongnot | 0:509b29d50fcb | 140 | |
natienza | 2:486bb9b6bd78 | 141 | timer.start(); |
gpongnot | 5:efd14a490f49 | 142 | pc.printf("SETUP effectue\n\r"); |
natienza | 2:486bb9b6bd78 | 143 | |
natienza | 2:486bb9b6bd78 | 144 | //variables |
gpongnot | 3:737ac9c24ca5 | 145 | Vect3 position = initVect3(); |
gpongnot | 3:737ac9c24ca5 | 146 | Vect3 erreur = initVect3(); |
gpongnot | 3:737ac9c24ca5 | 147 | Vect3 commandeXYZ = initVect3(); |
gpongnot | 3:737ac9c24ca5 | 148 | Vect3 commande123 = initVect3(); |
gpongnot | 3:737ac9c24ca5 | 149 | Vect3 consigne = initVect3(); |
gpongnot | 5:efd14a490f49 | 150 | consigne.x = 0.30; |
gpongnot | 5:efd14a490f49 | 151 | consigne.y = 0.30; |
gpongnot | 5:efd14a490f49 | 152 | consigne.z = PI/6; |
natienza | 2:486bb9b6bd78 | 153 | uint32_t seuilAffichage = PERIODE_AFF; |
natienza | 2:486bb9b6bd78 | 154 | uint32_t seuilAsserv = PERIODE_ASSERV; |
natienza | 2:486bb9b6bd78 | 155 | |
gpongnot | 0:509b29d50fcb | 156 | // Loop |
gpongnot | 0:509b29d50fcb | 157 | while(1) { |
natienza | 2:486bb9b6bd78 | 158 | |
natienza | 2:486bb9b6bd78 | 159 | if (timer.read_ms() > seuilAffichage){ |
natienza | 2:486bb9b6bd78 | 160 | seuilAffichage += PERIODE_AFF; |
gpongnot | 5:efd14a490f49 | 161 | //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 | 162 | pc.printf("erreur lacet : %f\n\rerreurX : %f\n\rereeurY: %f\n\n\r",erreur.z, erreur.x, erreur.y); |
gpongnot | 5:efd14a490f49 | 163 | pc.printf("commande lacet : %f\n\rcommandeX : %f\n\rcommandeY: %f\n\n\r",commande123.z, commande123.x, commande123.y); |
gpongnot | 3:737ac9c24ca5 | 164 | //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 | 165 | //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 | 166 | |
natienza | 2:486bb9b6bd78 | 167 | myled = !myled; |
natienza | 2:486bb9b6bd78 | 168 | } |
natienza | 2:486bb9b6bd78 | 169 | if (timer.read_ms() > seuilAsserv){ |
natienza | 2:486bb9b6bd78 | 170 | seuilAsserv += PERIODE_ASSERV; |
gpongnot | 3:737ac9c24ca5 | 171 | position = calculatePosition(); |
gpongnot | 3:737ac9c24ca5 | 172 | erreur = calcErreur(consigne, position); |
gpongnot | 3:737ac9c24ca5 | 173 | commandeXYZ = calcCommandeXYZ(erreur); |
gpongnot | 3:737ac9c24ca5 | 174 | commande123 = calcCommande123(commandeXYZ, position); |
gpongnot | 3:737ac9c24ca5 | 175 | moveBot(commande123); |
gpongnot | 3:737ac9c24ca5 | 176 | } |
gpongnot | 3:737ac9c24ca5 | 177 | |
gpongnot | 0:509b29d50fcb | 178 | } |
gpongnot | 0:509b29d50fcb | 179 | } |