Voili voilou

Dependencies:   RoboClaw StepperMotor mbed

Fork of Robot2016_2-0 by ARES

Committer:
IceTeam
Date:
Wed Apr 20 13:13:37 2016 +0000
Revision:
46:8eae88c45a78
Parent:
39:309f38d1e49c
Mise en static;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sype 0:ad9600df4a70 1 #include "Odometry.h"
sype 0:ad9600df4a70 2
IceTeam 46:8eae88c45a78 3 // GotoThet : settheta enleve.
sype 0:ad9600df4a70 4 // M1 = Moteur droit, M2 = Moteur gauche
sype 0:ad9600df4a70 5
sype 10:ae3178aa94e9 6 Odometry::Odometry(double diameter_right, double diameter_left, double v, uint16_t quadrature, RoboClaw &rc) : roboclaw(rc)
sype 10:ae3178aa94e9 7 {
sype 0:ad9600df4a70 8 m_v = v;
sype 10:ae3178aa94e9 9 m_distPerTick_left = diameter_left*PI/quadrature;
sype 10:ae3178aa94e9 10 m_distPerTick_right = diameter_right*PI/quadrature;
sype 4:3e6e78d6d3d9 11
sype 3:62e9d715de65 12 roboclaw.ForwardM1(ADR, 0);
sype 3:62e9d715de65 13 roboclaw.ForwardM2(ADR, 0);
sype 10:ae3178aa94e9 14 roboclaw.ResetEnc(ADR);
sype 4:3e6e78d6d3d9 15 // Erreur autorisée sur le déplacement en angle
sype 3:62e9d715de65 16 erreur_ang = 0.01;
sype 4:3e6e78d6d3d9 17
sype 0:ad9600df4a70 18 m_pulses_right = 0;
sype 0:ad9600df4a70 19 m_pulses_left = 0;
sype 10:ae3178aa94e9 20 pos_prog = 0;
sype 2:abdf8c6823a1 21 wait_ms(100);
sype 0:ad9600df4a70 22 }
sype 0:ad9600df4a70 23
sype 10:ae3178aa94e9 24 void Odometry::setPos(double x, double y, double theta)
sype 10:ae3178aa94e9 25 {
sype 0:ad9600df4a70 26 this->x = x;
sype 0:ad9600df4a70 27 this->y = y;
sype 0:ad9600df4a70 28 this->theta = theta;
sype 0:ad9600df4a70 29 }
sype 14:d5e21f71c2a9 30 void Odometry::getEnc()
sype 14:d5e21f71c2a9 31 {
sype 38:da3a2c781672 32 logger.printf("EncM1 : %d\tEncM2 : %d\n\r", roboclaw.ReadEncM1(ADR), roboclaw.ReadEncM2(ADR));
sype 14:d5e21f71c2a9 33 }
sype 0:ad9600df4a70 34
sype 10:ae3178aa94e9 35 void Odometry::setX(double x)
sype 10:ae3178aa94e9 36 {
sype 0:ad9600df4a70 37 this->x = x;
sype 0:ad9600df4a70 38 }
sype 0:ad9600df4a70 39
sype 10:ae3178aa94e9 40 void Odometry::setY(double y)
sype 10:ae3178aa94e9 41 {
sype 0:ad9600df4a70 42 this->y = y;
sype 0:ad9600df4a70 43 }
sype 0:ad9600df4a70 44
sype 10:ae3178aa94e9 45 void Odometry::setTheta(double theta)
sype 10:ae3178aa94e9 46 {
sype 0:ad9600df4a70 47 this->theta = theta;
sype 0:ad9600df4a70 48 }
sype 0:ad9600df4a70 49
sype 10:ae3178aa94e9 50 void Odometry::update_odo(void)
sype 10:ae3178aa94e9 51 {
Near32 32:068bd2b2e1f3 52 int32_t roboclawENCM1 = roboclaw.ReadEncM1(ADR);
Near32 32:068bd2b2e1f3 53 int32_t roboclawENCM2 = roboclaw.ReadEncM2(ADR);
Near32 32:068bd2b2e1f3 54 int32_t delta_right = roboclawENCM1 - m_pulses_right;
Near32 32:068bd2b2e1f3 55 m_pulses_right = roboclawENCM1;
Near32 32:068bd2b2e1f3 56 int32_t delta_left = roboclawENCM2 - m_pulses_left;
Near32 32:068bd2b2e1f3 57 m_pulses_left = roboclawENCM2;
sype 14:d5e21f71c2a9 58
sype 14:d5e21f71c2a9 59 double deltaS = (m_distPerTick_left*delta_left + m_distPerTick_right*delta_right)*C / 2.0f;
sype 14:d5e21f71c2a9 60 double deltaTheta = (m_distPerTick_left*delta_left - m_distPerTick_right*delta_right)*C / m_v;
sype 4:3e6e78d6d3d9 61
sype 14:d5e21f71c2a9 62 /*double R = deltaS/deltaTheta;
sype 10:ae3178aa94e9 63
sype 10:ae3178aa94e9 64 double xO = x - R*sin(theta);
sype 10:ae3178aa94e9 65 double yO = y + R*cos(theta);
sype 4:3e6e78d6d3d9 66
sype 0:ad9600df4a70 67 theta += deltaTheta;
sype 4:3e6e78d6d3d9 68
sype 10:ae3178aa94e9 69 if(deltaTheta == 0) {
sype 10:ae3178aa94e9 70 x = x + deltaS*cos(theta);
sype 10:ae3178aa94e9 71 y = y + deltaS*sin(theta);
sype 10:ae3178aa94e9 72 }
sype 10:ae3178aa94e9 73 else {
sype 10:ae3178aa94e9 74 x = xO + R*sin(theta);
sype 10:ae3178aa94e9 75 y = yO - R*cos(theta);
sype 14:d5e21f71c2a9 76 }*/
IceTeam 36:2d7357a385bc 77
IceTeam 36:2d7357a385bc 78
IceTeam 36:2d7357a385bc 79
IceTeam 36:2d7357a385bc 80 double dx = deltaS*cos(theta+deltaTheta/2);
IceTeam 36:2d7357a385bc 81 double dy = deltaS*sin(theta+deltaTheta/2);
IceTeam 36:2d7357a385bc 82 x += dx;
IceTeam 36:2d7357a385bc 83 y += dy;
IceTeam 36:2d7357a385bc 84
Near32 34:7f8c29ddee61 85 theta += deltaTheta;
Near32 34:7f8c29ddee61 86 while(theta > PI) theta -= 2*PI;
Near32 34:7f8c29ddee61 87 while(theta <= -PI) theta += 2*PI;
Near32 34:7f8c29ddee61 88
sype 0:ad9600df4a70 89 }
sype 0:ad9600df4a70 90
sype 14:d5e21f71c2a9 91 void Odometry::GotoXY(double x_goal, double y_goal)
sype 14:d5e21f71c2a9 92 {
sype 14:d5e21f71c2a9 93 double theta_ = atan2(y_goal-y, x_goal-x);
sype 14:d5e21f71c2a9 94 double dist_ = sqrt(carre(x_goal-x)+carre(y_goal-y));
sype 38:da3a2c781672 95 logger.printf("Dist : %3.2f\tTheta : %3.2f\n\r", dist_, theta_*180/PI);
sype 14:d5e21f71c2a9 96 GotoThet(theta_);
sype 14:d5e21f71c2a9 97 GotoDist(dist_);
sype 14:d5e21f71c2a9 98 }
sype 14:d5e21f71c2a9 99
sype 10:ae3178aa94e9 100 void Odometry::GotoXYT(double x_goal, double y_goal, double theta_goal)
sype 10:ae3178aa94e9 101 {
sype 0:ad9600df4a70 102 double theta_ = atan2(y_goal-y, x_goal-x);
sype 3:62e9d715de65 103 double dist_ = sqrt(carre(x_goal-x)+carre(y_goal-y));
sype 38:da3a2c781672 104 logger.printf("Dist : %3.2f\tTheta : %3.2f\n\r", dist_, theta_*180/PI);
sype 2:abdf8c6823a1 105 GotoThet(theta_);
sype 3:62e9d715de65 106 GotoDist(dist_);
sype 14:d5e21f71c2a9 107 GotoThet(theta_goal);
sype 0:ad9600df4a70 108 }
sype 2:abdf8c6823a1 109
sype 10:ae3178aa94e9 110 void Odometry::GotoThet(double theta_)
sype 10:ae3178aa94e9 111 {
IceTeam 46:8eae88c45a78 112 S::led = 0;
sype 10:ae3178aa94e9 113 //pos_prog++;
sype 38:da3a2c781672 114 //logger.printf("Theta : %3.2f\n\r", theta_*180/PI);
sype 10:ae3178aa94e9 115 //arrived = false;
sype 10:ae3178aa94e9 116
sype 10:ae3178aa94e9 117 int32_t distance_ticks_left;
sype 10:ae3178aa94e9 118 int32_t distance_ticks_right;
sype 10:ae3178aa94e9 119
sype 10:ae3178aa94e9 120 int32_t pos_initiale_right = m_pulses_right, pos_initiale_left = m_pulses_left;
sype 4:3e6e78d6d3d9 121
sype 4:3e6e78d6d3d9 122 // Le calcul d'erreur est bon (testé), tu peux le vérifier par dessin
sype 3:62e9d715de65 123 double erreur_theta = theta_ - getTheta();
sype 4:3e6e78d6d3d9 124
sype 3:62e9d715de65 125 while(erreur_theta >= PI) erreur_theta -= 2*PI;
sype 14:d5e21f71c2a9 126 while(erreur_theta < -PI) erreur_theta += 2*PI;
sype 14:d5e21f71c2a9 127
sype 38:da3a2c781672 128 logger.printf("ET : %3.2f\n\r", erreur_theta*180/PI);
sype 14:d5e21f71c2a9 129
sype 14:d5e21f71c2a9 130 if(erreur_theta < 0) {
sype 14:d5e21f71c2a9 131 distance_ticks_left = (int32_t) pos_initiale_left + (erreur_theta*m_v/2)/m_distPerTick_left;
sype 14:d5e21f71c2a9 132 distance_ticks_right = (int32_t) pos_initiale_right - (erreur_theta*m_v/2)/m_distPerTick_right;
sype 4:3e6e78d6d3d9 133 } else {
sype 14:d5e21f71c2a9 134 distance_ticks_left = (int32_t) pos_initiale_left + (erreur_theta*m_v/2)/m_distPerTick_left;
sype 14:d5e21f71c2a9 135 distance_ticks_right = (int32_t) pos_initiale_right - (erreur_theta*m_v/2)/m_distPerTick_right;
sype 2:abdf8c6823a1 136 }
sype 10:ae3178aa94e9 137
sype 38:da3a2c781672 138 //logger.printf("TV %3.2f\tTh %3.2f\tET %3.2f\n\r",theta_*180/PI,getTheta()*180/PI,erreur_theta*180/PI);
sype 38:da3a2c781672 139 //logger.printf("X : %3.2f\tY : %3.2f\tTheta : %3.2f\n\r", getX(), getY(), getTheta()*180/PI);
sype 38:da3a2c781672 140 //logger.printf("M1 %6d\tM2 %6d\n\r",distance_ticks_right, distance_ticks_left);
sype 10:ae3178aa94e9 141
sype 10:ae3178aa94e9 142 roboclaw.SpeedAccelDeccelPositionM1M2(ADR, accel_angle, vitesse_angle, deccel_angle, distance_ticks_right, accel_angle, vitesse_angle, deccel_angle, distance_ticks_left, 1);
sype 10:ae3178aa94e9 143
sype 38:da3a2c781672 144 //logger.printf("IniR:%6d\tDistR:%6d\tIniL:%6d\tDistL:%6d\n\r", pos_initiale_right, distance_ticks_right, pos_initiale_left, distance_ticks_left);
sype 10:ae3178aa94e9 145
IceTeam 46:8eae88c45a78 146 while((m_pulses_right != distance_ticks_right)&&(m_pulses_left != distance_ticks_left)) logger.printf("[Theta] %6d\t%6d\t%6d\t%6d\t%6d\n\r",m_pulses_right - pos_initiale_right, distance_ticks_right, m_pulses_left - pos_initiale_left, distance_ticks_left);
IceTeam 31:8bcc3a0bfa8a 147 wait(0.4);
IceTeam 46:8eae88c45a78 148 //setTheta(theta_);
IceTeam 46:8eae88c45a78 149 S::led = 1;
sype 10:ae3178aa94e9 150 //arrived = true;
IceTeam 46:8eae88c45a78 151 logger.printf("[End GotoThet] X : %3.2f\tY : %3.2f\tTheta : %3.2f\n\r", getX(), getY(), getTheta()*180/PI);
sype 2:abdf8c6823a1 152 }
sype 2:abdf8c6823a1 153
sype 10:ae3178aa94e9 154 void Odometry::GotoDist(double distance)
sype 10:ae3178aa94e9 155 {
IceTeam 46:8eae88c45a78 156 S::led = 0;
sype 10:ae3178aa94e9 157 //pos_prog++;
sype 38:da3a2c781672 158 //logger.printf("Dist : %3.2f\n\r", distance);
sype 10:ae3178aa94e9 159 //arrived = false;
sype 10:ae3178aa94e9 160
sype 10:ae3178aa94e9 161 int32_t pos_initiale_right = m_pulses_right, pos_initiale_left = m_pulses_left;
sype 10:ae3178aa94e9 162
sype 10:ae3178aa94e9 163 int32_t distance_ticks_right = (int32_t) distance/m_distPerTick_right + pos_initiale_right;
sype 10:ae3178aa94e9 164 int32_t distance_ticks_left = (int32_t) distance/m_distPerTick_left + pos_initiale_left;
sype 10:ae3178aa94e9 165
sype 10:ae3178aa94e9 166 roboclaw.SpeedAccelDeccelPositionM1M2(ADR, accel_dista, vitesse_dista, deccel_dista, distance_ticks_right, accel_dista, vitesse_dista, deccel_dista, distance_ticks_left, 1);
sype 10:ae3178aa94e9 167
sype 38:da3a2c781672 168 //logger.printf("IniR:%6d\tDistR:%6d\tIniL:%6d\tDistL:%6d\n\r", pos_initiale_right, distance_ticks_right, pos_initiale_left, distance_ticks_left);
sype 14:d5e21f71c2a9 169
IceTeam 46:8eae88c45a78 170 while((m_pulses_right != distance_ticks_right)&&(m_pulses_left != distance_ticks_left)); logger.printf("[Dist] PR:%6d\tIR:%6d\tDR:%6d\tPL:%6d\tIL:%6d\tDL:%6d\n\r",m_pulses_right, pos_initiale_right, distance_ticks_right, m_pulses_left, pos_initiale_left, distance_ticks_left);
IceTeam 31:8bcc3a0bfa8a 171 wait(0.4);
IceTeam 46:8eae88c45a78 172 S::led = 1;
sype 38:da3a2c781672 173 //logger.printf("arrivey %d\n\r",pos_prog);
IceTeam 46:8eae88c45a78 174 logger.printf("[End GotoDist] X : %3.2f\tY : %3.2f\tTheta : %3.2f\n\r", getX(), getY(), getTheta()*180/PI);
sype 2:abdf8c6823a1 175 }
IceTeam 35:4e3d9ab1b94b 176
IceTeam 35:4e3d9ab1b94b 177 void Odometry::TestEntraxe(int i) {
IceTeam 35:4e3d9ab1b94b 178 int32_t distance_ticks_left;
IceTeam 35:4e3d9ab1b94b 179 int32_t distance_ticks_right;
IceTeam 35:4e3d9ab1b94b 180
IceTeam 35:4e3d9ab1b94b 181 int32_t pos_initiale_right = m_pulses_right, pos_initiale_left = m_pulses_left;
IceTeam 35:4e3d9ab1b94b 182
IceTeam 35:4e3d9ab1b94b 183 double erreur_theta = 2*PI*i - getTheta();
IceTeam 35:4e3d9ab1b94b 184 if(erreur_theta < 0) {
IceTeam 35:4e3d9ab1b94b 185 distance_ticks_left = (int32_t) pos_initiale_left + (erreur_theta*m_v/2)/m_distPerTick_left;
IceTeam 35:4e3d9ab1b94b 186 distance_ticks_right = (int32_t) pos_initiale_right - (erreur_theta*m_v/2)/m_distPerTick_right;
IceTeam 35:4e3d9ab1b94b 187 } else {
IceTeam 35:4e3d9ab1b94b 188 distance_ticks_left = (int32_t) pos_initiale_left + (erreur_theta*m_v/2)/m_distPerTick_left;
IceTeam 35:4e3d9ab1b94b 189 distance_ticks_right = (int32_t) pos_initiale_right - (erreur_theta*m_v/2)/m_distPerTick_right;
IceTeam 35:4e3d9ab1b94b 190 }
IceTeam 35:4e3d9ab1b94b 191
IceTeam 35:4e3d9ab1b94b 192 roboclaw.SpeedAccelDeccelPositionM1M2(ADR, accel_angle, vitesse_angle, deccel_angle, distance_ticks_right, accel_angle, vitesse_angle, deccel_angle, distance_ticks_left, 1);
IceTeam 35:4e3d9ab1b94b 193
IceTeam 35:4e3d9ab1b94b 194 while((m_pulses_right != distance_ticks_right)&&(m_pulses_left != distance_ticks_left));
IceTeam 35:4e3d9ab1b94b 195 wait(0.4);
IceTeam 35:4e3d9ab1b94b 196 }
IceTeam 39:309f38d1e49c 197
IceTeam 39:309f38d1e49c 198 void Odometry::Forward(float i) {
IceTeam 39:309f38d1e49c 199 int32_t pos_initiale_right = m_pulses_right, pos_initiale_left = m_pulses_left;
IceTeam 39:309f38d1e49c 200
IceTeam 39:309f38d1e49c 201 int32_t distance_ticks_right = (int32_t) i/m_distPerTick_right + pos_initiale_right;
IceTeam 39:309f38d1e49c 202 int32_t distance_ticks_left = (int32_t) i/m_distPerTick_left + pos_initiale_left;
IceTeam 39:309f38d1e49c 203
IceTeam 39:309f38d1e49c 204 roboclaw.SpeedAccelDeccelPositionM1M2(ADR, accel_dista, vitesse_dista, deccel_dista, distance_ticks_right, accel_dista, vitesse_dista, deccel_dista, distance_ticks_left, 1);
IceTeam 39:309f38d1e49c 205
IceTeam 39:309f38d1e49c 206 //logger.printf("IniR:%6d\tDistR:%6d\tIniL:%6d\tDistL:%6d\n\r", pos_initiale_right, distance_ticks_right, pos_initiale_left, distance_ticks_left);
IceTeam 39:309f38d1e49c 207
IceTeam 46:8eae88c45a78 208 while((m_pulses_right != distance_ticks_right)&&(m_pulses_left != distance_ticks_left)) logger.printf("PR:%6d\tIR:%6d\tDR:%6d\tPL:%6d\tIL:%6d\tDL:%6d\n\r",m_pulses_right, pos_initiale_right, distance_ticks_right, m_pulses_left, pos_initiale_left, distance_ticks_left);
IceTeam 39:309f38d1e49c 209 wait(0.4);
IceTeam 46:8eae88c45a78 210 S::led = 1;
IceTeam 39:309f38d1e49c 211 }