Robot's source code

Dependencies:   mbed

Committer:
sype
Date:
Mon May 04 06:18:27 2015 +0000
Revision:
105:6da275b01395
Parent:
93:4d5664e9188a
Asserv B

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