Robot's source code

Dependencies:   mbed

Committer:
Jagang
Date:
Thu Apr 30 16:23:15 2015 +0000
Revision:
101:78dc61bd330d
Parent:
100:a827a645d6c2
.

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
Jagang 71:95d76c181b22 4 extern Serial logger;
Jagang 71:95d76c181b22 5
Jagang 71:95d76c181b22 6 aserv_planB::aserv_planB(Odometry2 &odometry,Motor &motorL,Motor &motorR) : m_odometry(odometry), m_motorL(motorL), m_motorR(motorR)
Jagang 62:454cd844fe1e 7 {
sype 79:d97090bb6470 8 erreur_precedente = 0;
Jagang 101:78dc61bd330d 9 Kp_angle = 1.2; //Fixed à 0.436 pour 180 deg
Jagang 101:78dc61bd330d 10 Kd_angle = 4.455;
sype 79:d97090bb6470 11 cmd = 0;
Jagang 101:78dc61bd330d 12 cmd_g = 0, cmd_d = 0;
sype 79:d97090bb6470 13 N = 0;
sype 79:d97090bb6470 14 done = false;
sype 79:d97090bb6470 15 state = 0; // Etat ou l'on ne fait rien
Jagang 101:78dc61bd330d 16 distance_g = 0;
Jagang 101:78dc61bd330d 17 distance_d = 0;
Jagang 101:78dc61bd330d 18 Kp_distance = 0.0075;
Jagang 101:78dc61bd330d 19 Ki_distance = 0;
Jagang 101:78dc61bd330d 20 Kd_distance = 0;
Jagang 71:95d76c181b22 21 }
Jagang 71:95d76c181b22 22
Jagang 71:95d76c181b22 23 void aserv_planB::setGoal(float x, float y, float theta)
Jagang 71:95d76c181b22 24 {
Jagang 71:95d76c181b22 25 m_goalX = x;
Jagang 71:95d76c181b22 26 m_goalY = y;
Jagang 71:95d76c181b22 27 m_goalTheta = theta;
Jagang 101:78dc61bd330d 28 thetaGoal = atan2(m_goalY-m_odometry.getY(),m_goalX-m_odometry.getX());
Jagang 101:78dc61bd330d 29 distance_g = sqrt(carre(m_goalY-m_odometry.getY())+carre(m_goalX-m_odometry.getX()));
Jagang 101:78dc61bd330d 30 distance_d = distance_g;
Jagang 101:78dc61bd330d 31 state = 2; // Etat de rotation 1
sype 64:6489bcfc1173 32 }
sype 64:6489bcfc1173 33
Jagang 101:78dc61bd330d 34 /*void aserv_planB::control_speed()
sype 64:6489bcfc1173 35 {
Jagang 72:b2a128486332 36 vitesse_d = m_odometry.getVitRight();
Jagang 72:b2a128486332 37 vitesse_g = m_odometry.getVitLeft();
sype 79:d97090bb6470 38
sype 64:6489bcfc1173 39 erreur_g = consigne_g - vitesse_g;
sype 64:6489bcfc1173 40 cmd_g = erreur_g*Kp;
sype 64:6489bcfc1173 41 erreur_d = consigne_d - vitesse_d;
sype 64:6489bcfc1173 42 cmd_d = erreur_d*Kp;
sype 79:d97090bb6470 43
sype 64:6489bcfc1173 44 m_motorL.setSpeed(cmd_g);
sype 64:6489bcfc1173 45 m_motorR.setSpeed(cmd_d);
Jagang 101:78dc61bd330d 46 }*/
Jagang 62:454cd844fe1e 47
Jagang 62:454cd844fe1e 48 void aserv_planB::update(float dt)
Jagang 62:454cd844fe1e 49 {
Jagang 101:78dc61bd330d 50 // Etat 1 : Angle theta pour viser dans la direction du point M(x,y)
Jagang 101:78dc61bd330d 51 if(state == 1 && N < 100)
Jagang 101:78dc61bd330d 52 {
sype 85:8e95432d99d3 53 float erreur_theta = thetaGoal-m_odometry.getTheta();
sype 84:24d727006218 54
Jagang 101:78dc61bd330d 55 if(erreur_theta <= PI) erreur_theta += 2.0*PI;
Jagang 101:78dc61bd330d 56 if(erreur_theta >= PI) erreur_theta -= 2.0*PI;
sype 84:24d727006218 57
Jagang 101:78dc61bd330d 58 //logger.printf("%.2f\r\n", erreur_theta*180/PI);
Jagang 72:b2a128486332 59
Jagang 101:78dc61bd330d 60 cmd = erreur_theta*Kp_angle + (erreur_theta-erreur_precedente)*Kd_angle;
sype 79:d97090bb6470 61 erreur_precedente = erreur_theta;
Jagang 72:b2a128486332 62
sype 79:d97090bb6470 63 m_motorL.setSpeed(-cmd);
sype 79:d97090bb6470 64 m_motorR.setSpeed(cmd);
sype 79:d97090bb6470 65
Jagang 101:78dc61bd330d 66 N++;
Jagang 101:78dc61bd330d 67 if(N==100) // && (abs(erreur_theta)<=2.0)
Jagang 101:78dc61bd330d 68 {
sype 85:8e95432d99d3 69 state = 2;
Jagang 101:78dc61bd330d 70 logger.printf("%.2f %.2f\r\n", erreur_theta*180/PI, thetaGoal*180/PI);
Jagang 101:78dc61bd330d 71 m_odometry.setDistLeft(0);
Jagang 101:78dc61bd330d 72 m_odometry.setDistRight(0);
Jagang 101:78dc61bd330d 73 memo_g = m_odometry.getDistLeft();
Jagang 101:78dc61bd330d 74 memo_d = m_odometry.getDistRight();
Jagang 101:78dc61bd330d 75 }
sype 79:d97090bb6470 76 }
Jagang 101:78dc61bd330d 77
Jagang 101:78dc61bd330d 78 // Etat 2 : Parcours du robot jusqu'au point M(x,y)
Jagang 101:78dc61bd330d 79 if(state == 2)
sype 79:d97090bb6470 80 {
Jagang 101:78dc61bd330d 81 float erreur_distance_g = distance_g-(m_odometry.getDistLeft()-memo_g); //- distance parcourue par la roue gauche depuis l'état 2
Jagang 101:78dc61bd330d 82 float erreur_distance_d = distance_d-(m_odometry.getDistRight()-memo_d);
Jagang 101:78dc61bd330d 83 cmd_g = erreur_distance_g*Kp_distance;
Jagang 101:78dc61bd330d 84 cmd_d = erreur_distance_d*Kp_distance;
Jagang 101:78dc61bd330d 85
Jagang 101:78dc61bd330d 86 m_motorL.setSpeed(0);
Jagang 101:78dc61bd330d 87 m_motorR.setSpeed(0);
Jagang 101:78dc61bd330d 88
Jagang 101:78dc61bd330d 89 logger.printf("%.2f %.2f\r\n", m_odometry.getDistLeft(), m_odometry.getDistRight());
Jagang 101:78dc61bd330d 90
Jagang 101:78dc61bd330d 91 //N++;
Jagang 101:78dc61bd330d 92 if(N==100) state = 2;
Jagang 101:78dc61bd330d 93 }
Jagang 101:78dc61bd330d 94
Jagang 101:78dc61bd330d 95 // Etat 3 : Placement au bon angle Phi souhaité au point M(x,y)
Jagang 101:78dc61bd330d 96 if(state == 3)
Jagang 101:78dc61bd330d 97 {
Jagang 101:78dc61bd330d 98 m_motorL.setSpeed(0);
Jagang 101:78dc61bd330d 99 m_motorR.setSpeed(0);
Jagang 101:78dc61bd330d 100 }
sype 70:56086a37f31f 101 }