Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Thu Apr 30 16:16:29 2015 +0000
Revision:
100:a827a645d6c2
Parent:
94:5c37bcf73d14
Parent:
93:4d5664e9188a
Child:
101:78dc61bd330d
Merge 2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jagang 62:454cd844fe1e 1 #include "planB.h"
Jagang 71:95d76c181b22 2 #include "defines.h"
Jagang 71:95d76c181b22 3
Near32 94:5c37bcf73d14 4
Jagang 71:95d76c181b22 5 extern Serial logger;
Jagang 71:95d76c181b22 6
Jagang 71:95d76c181b22 7 aserv_planB::aserv_planB(Odometry2 &odometry,Motor &motorL,Motor &motorR) : m_odometry(odometry), m_motorL(motorL), m_motorR(motorR)
Jagang 62:454cd844fe1e 8 {
Near32 94:5c37bcf73d14 9 consigne_g = 0.0;
Near32 94:5c37bcf73d14 10 consigne_d = 0.0;
Near32 94:5c37bcf73d14 11 vitesse_g = 0;
Near32 94:5c37bcf73d14 12 vitesse_d = 0;
Near32 94:5c37bcf73d14 13 erreur_g = 0;
Near32 94:5c37bcf73d14 14 erreur_d = 0;
Near32 94:5c37bcf73d14 15 cmd_g = 0;
Near32 94:5c37bcf73d14 16 cmd_d = 0;
Near32 94:5c37bcf73d14 17 somme_erreur = 0;
Near32 94:5c37bcf73d14 18 somme_erreur_d = 0;
Near32 94:5c37bcf73d14 19 delta_erreur = 0;
sype 79:d97090bb6470 20 erreur_precedente = 0;
Near32 94:5c37bcf73d14 21 Kp = 0.30;
Near32 94:5c37bcf73d14 22 Ki = 0.01;
Near32 94:5c37bcf73d14 23 Kd = 0.5;
Near32 94:5c37bcf73d14 24 //etat_angle = 0;
sype 79:d97090bb6470 25 cmd = 0;
sype 79:d97090bb6470 26 N = 0;
Near32 94:5c37bcf73d14 27 moyenne = 0;
Near32 94:5c37bcf73d14 28 limite = 0;
sype 79:d97090bb6470 29 done = false;
sype 79:d97090bb6470 30 state = 0; // Etat ou l'on ne fait rien
Jagang 71:95d76c181b22 31 }
Jagang 71:95d76c181b22 32
Jagang 71:95d76c181b22 33 void aserv_planB::setGoal(float x, float y, float theta)
Jagang 71:95d76c181b22 34 {
Jagang 71:95d76c181b22 35 m_goalX = x;
Jagang 71:95d76c181b22 36 m_goalY = y;
Jagang 71:95d76c181b22 37 m_goalTheta = theta;
Near32 94:5c37bcf73d14 38
Near32 94:5c37bcf73d14 39 state = 1; // Etat de rotation 1
sype 64:6489bcfc1173 40 }
sype 64:6489bcfc1173 41
Near32 94:5c37bcf73d14 42 void aserv_planB::control_speed()
sype 64:6489bcfc1173 43 {
Jagang 72:b2a128486332 44 vitesse_d = m_odometry.getVitRight();
Jagang 72:b2a128486332 45 vitesse_g = m_odometry.getVitLeft();
sype 79:d97090bb6470 46
sype 64:6489bcfc1173 47 erreur_g = consigne_g - vitesse_g;
sype 64:6489bcfc1173 48 cmd_g = erreur_g*Kp;
sype 64:6489bcfc1173 49 erreur_d = consigne_d - vitesse_d;
sype 64:6489bcfc1173 50 cmd_d = erreur_d*Kp;
sype 79:d97090bb6470 51
sype 64:6489bcfc1173 52 m_motorL.setSpeed(cmd_g);
sype 64:6489bcfc1173 53 m_motorR.setSpeed(cmd_d);
Near32 94:5c37bcf73d14 54 }
Jagang 62:454cd844fe1e 55
Jagang 62:454cd844fe1e 56 void aserv_planB::update(float dt)
Jagang 62:454cd844fe1e 57 {
Near32 94:5c37bcf73d14 58 if(state == 1)
Near32 94:5c37bcf73d14 59 {
Near32 94:5c37bcf73d14 60 float thetaGoal = atan2(m_goalY-m_odometry.getY(),m_goalX-m_odometry.getX());
Near32 94:5c37bcf73d14 61
sype 85:8e95432d99d3 62 float erreur_theta = thetaGoal-m_odometry.getTheta();
sype 84:24d727006218 63
Jagang 93:4d5664e9188a 64 if(erreur_theta <= PI) erreur_theta += 2.0f*PI;
Jagang 93:4d5664e9188a 65 if(erreur_theta >= PI) erreur_theta -= 2.0f*PI;
sype 84:24d727006218 66
Near32 94:5c37bcf73d14 67 /*if(erreur_theta < 0) etat_angle = 1;
Near32 94:5c37bcf73d14 68 else if(erreur_theta > 0) etat_angle = 2;
Near32 94:5c37bcf73d14 69 else etat_angle = 0;*/
Near32 94:5c37bcf73d14 70 /*limite = (0.5-Kp*erreur_theta)/Ki;
Near32 94:5c37bcf73d14 71 if(somme_erreur >= limite) somme_erreur = limite;
Near32 94:5c37bcf73d14 72 if(somme_erreur <= -limite) somme_erreur = -limite;*/
Jagang 72:b2a128486332 73
Near32 94:5c37bcf73d14 74 cmd = erreur_theta*Kp + somme_erreur*Ki - delta_erreur*Kd;
Near32 94:5c37bcf73d14 75 somme_erreur += erreur_theta;
Near32 94:5c37bcf73d14 76 delta_erreur = erreur_theta - erreur_precedente;
sype 79:d97090bb6470 77 erreur_precedente = erreur_theta;
Jagang 72:b2a128486332 78
sype 79:d97090bb6470 79 m_motorL.setSpeed(-cmd);
sype 79:d97090bb6470 80 m_motorR.setSpeed(cmd);
sype 79:d97090bb6470 81
Near32 94:5c37bcf73d14 82 //! Pas bon coeff, mais c'est l'idée
Near32 94:5c37bcf73d14 83 consigne_g = 0;//-erreur_theta*0.0001;
Near32 94:5c37bcf73d14 84 consigne_d = 0;//erreur_theta*0.0001;
Near32 94:5c37bcf73d14 85
Near32 94:5c37bcf73d14 86 /*if(erreur_theta <= abs(0.7))
Near32 94:5c37bcf73d14 87 {
Near32 94:5c37bcf73d14 88 done = true;
Near32 94:5c37bcf73d14 89 logger.printf("Posey\r\n");
sype 85:8e95432d99d3 90 state = 2;
Near32 94:5c37bcf73d14 91 }*/
sype 79:d97090bb6470 92 }
Near32 94:5c37bcf73d14 93 /*switch(etat_angle)
sype 79:d97090bb6470 94 {
Near32 94:5c37bcf73d14 95 case 0:
Near32 94:5c37bcf73d14 96
Near32 94:5c37bcf73d14 97 break;
Near32 94:5c37bcf73d14 98
Near32 94:5c37bcf73d14 99 case 1:
Near32 94:5c37bcf73d14 100
Near32 94:5c37bcf73d14 101 break;*/
sype 70:56086a37f31f 102 }