Newer
Older
#include "commandAllerA.h"
#include "odometrie.h"
#include <math.h>
float diffAngle(float a, float b)
{
float t = a-b;
while (t > M_PI)
{
t -= 2*M_PI;
}
while (t < -M_PI)
{
t += 2*M_PI;
}
return t;
}
enum Signe
{
SGN_NEG = -1,
SGN_UNDEF = 0,
SGN_POS = 1
};
////////////////////////////////
// CommandAllerEnArcA //
////////////////////////////////
CommandAllerEnArcA::CommandAllerEnArcA(Position p, Position c, float v, bool reculer)
: Command()
{
but = p;
centre = c;
vMax = v;
m_reculer = reculer;
linSpeed = Odometrie::odometrie->getVitesseLineaire();
angSpeed = Odometrie::odometrie->getVitesseAngulaire();
bonAngle = false;
m_fini = false;
Position pos = Odometrie::odometrie->getPos().getPosition();
float pmcx = pos.x-centre.x;
float pmcy = pos.y-centre.y;
float bmcx = but.x-centre.x;
float bmcy = but.y-centre.y;
if (pmcx*bmcy-pmcy*bmcx > 0.0f)
cote = SGN_POS;
else
cote = SGN_NEG;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
}
void CommandAllerEnArcA::update()
{
float accAngMax = ACCELERATION_ANGULAIRE_MAX;
float vitAngMax = VITESSE_ANGULAIRE_MAX;
float accLinMax = ACCELERATION_LINEAIRE_MAX;
float decLinMax = DECELERATION_LINEAIRE_MAX;
float vitLinMax = vMax;//VITESSE_LINEAIRE_MAX;
float angle = Odometrie::odometrie->getPos().getAngle();
Position pos = Odometrie::odometrie->getPos().getPosition();
float rayon = (centre-but).getNorme();
/*
float vdx = pos.y-but.y;
float vdy = but.x-pos.x;
float pmcx = pos.x-centre.x;
float pmcy = pos.y-centre.y;
float vdnorme = rayon/sqrt(vdx*vdx+vdy*vdy);
if (pmcy*vdx+pmcx*vdy < 0) // determinant pour connaitre le sens
vdnorme = -vdnorme;
Position pInter(centre.x + vdnorme*vdx, centre.y + vdnorme*vdy);
*/
float pmcx = pos.x-centre.x;
float pmcy = pos.y-centre.y;
float bmcx = but.x-centre.x;
float bmcy = but.y-centre.y;
float sintheta = (pmcx*bmcy-pmcy*bmcx)/(rayon*sqrt(pmcx*pmcx+pmcy*pmcy));
float theta = asin(sintheta);
float aVise = atan2(pmcy,pmcx)+0.85f*theta;
Position pVise(centre.x+rayon*cos(aVise), centre.y+rayon*sin(aVise));
float vmpx = pVise.x-pos.x;
float vmpy = pVise.y-pos.y;
float d2 = sqrt(vmpx*vmpx+vmpy*vmpy);
float an = atan2(vmpy,vmpx);
float rVise = 0.5f*d2/sin(an-angle);
float linSpeedVise = vitLinMax;
float angSpeedVise = vitLinMax/rVise;
if (angSpeedVise > vitAngMax)
{
linSpeedVise *= vitAngMax/angSpeedVise;
angSpeedVise = vitAngMax;
}
// test si la commande a fini
if ((cote == SGN_POS && sintheta < 0.0f) || (cote == SGN_NEG && sintheta > 0.0f) || (abs(sintheta) < M_PI/180.0f))
m_fini = true;
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
float distance = theta*rVise; // (Odometrie::odometrie->getPos().getPosition() - but).getNorme();
float distanceVitesseMax = 0.5f*vitLinMax*vitLinMax/decLinMax;
if (distance < distanceVitesseMax)
{
linSpeedVise = sqrt(2*distance*decLinMax);
angSpeedVise = linSpeedVise/rVise;
}
if (linSpeed > linSpeedVise)
{
if (linSpeed-linSpeedVise < decLinMax)
linSpeed = linSpeedVise;
else
linSpeed -= decLinMax;
}
else
{
if (linSpeedVise-linSpeed < accLinMax)
linSpeed = linSpeedVise;
else
linSpeed += accLinMax;
}
if (angSpeed > angSpeedVise)
{
if (angSpeed-angSpeedVise < accAngMax)
angSpeed = angSpeedVise;
else
angSpeed -= accAngMax;
}
else
{
if (angSpeedVise-angSpeed < accAngMax)
angSpeed = angSpeedVise;
else
angSpeed += accAngMax;
}
// pour garder la trajectoire de cercle
// std::cout << linSpeed << std::endl;
if (abs(angSpeed) > abs(linSpeed/rVise))
angSpeed = linSpeed/rVise;
else if (abs(linSpeed) > abs(rVise*angSpeed))
linSpeed = rVise*angSpeed;
}
Vitesse CommandAllerEnArcA::getLinearSpeed()
{
return linSpeed;
}
Angle CommandAllerEnArcA::getAngularSpeed()
{
return angSpeed;
}
// est ce que la commande a fini ?
bool CommandAllerEnArcA::fini() const
{
return m_fini;
}
////////////////////////////////
// CommandAllerA //
////////////////////////////////
CommandAllerA::CommandAllerA(Position p, bool reculer, float vitesseFin)
m_reculer = reculer;
linSpeed = Odometrie::odometrie->getVitesseLineaire();
angSpeed = Odometrie::odometrie->getVitesseAngulaire();
bonAngle = false;
}
void CommandAllerA::update()
{
float accAngMax = ACCELERATION_ANGULAIRE_MAX;
float vitAngMax = VITESSE_ANGULAIRE_MAX;
float accLinMax = ACCELERATION_LINEAIRE_MAX;
float decLinMax = DECELERATION_LINEAIRE_MAX;
float vitLinMax = VITESSE_LINEAIRE_MAX;
//float angleVitesseMax = M_PI/10.0f;
float angleVitesseMax = 0.5f*vitAngMax*vitAngMax/accAngMax;
//float distanceVitesseMax = 350.0f;
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
float angle = Odometrie::odometrie->getPos().getAngle();
Position pos = Odometrie::odometrie->getPos().getPosition();
Position delta = but-pos;
float angleVise = atan2(delta.getY(),delta.getX());
if (m_reculer)
angleVise += M_PI;
float diffAng = diffAngle(angleVise,angle);
// vitesse angulaire
if (abs(diffAng) > angleVitesseMax)
{
if (diffAng > 0)
angSpeed += accAngMax;
else
angSpeed -= accAngMax;
if (angSpeed > vitAngMax)
angSpeed = vitAngMax;
else if (angSpeed < -vitAngMax)
angSpeed = -vitAngMax;
}
else
{
angSpeed = diffAng*vitAngMax/angleVitesseMax;
}
// reste sur place tant que le robot n'a pas le bon angle
if (!bonAngle)
{
if (abs(diffAng) < angleMaxPourAvancer)
{
bonAngle = true;
}
else
{
linSpeed *= 0.95f;
return;
}
}
// vitesse linéaire
float distanceBut = delta.getNorme();
if (abs(diffAng) > angleMaxPourAvancer)
{
linSpeed *= 0.97f;
}
else if (distanceBut > distanceVitesseMax)
{
if (m_reculer)
linSpeed -= accLinMax;
else
linSpeed += accLinMax;
if (linSpeed > vitLinMax)
linSpeed = vitLinMax;
else if (linSpeed < -vitLinMax)
linSpeed = -vitLinMax;
}
else
{
if (m_reculer)
}
}
Vitesse CommandAllerA::getLinearSpeed()
{
return linSpeed;
}
Angle CommandAllerA::getAngularSpeed()
{
return angSpeed;
}
bool CommandAllerA::fini() const
{
return m_fini;
}
////////////////////////////////
// CommandTournerVers //
////////////////////////////////
CommandTournerVers::CommandTournerVers(Position p)
: Command()
{
but = p;
angSpeed = 0;
}
void CommandTournerVers::update()
{
float accAngMax = ACCELERATION_ANGULAIRE_MAX;
float vitAngMax = VITESSE_ANGULAIRE_MAX;
// float angleVitesseMax = M_PI/6.0f;
float angleVitesseMax = 0.5f*vitAngMax*vitAngMax/accAngMax;
float angle = Odometrie::odometrie->getPos().getAngle();
Position pos = Odometrie::odometrie->getPos().getPosition();
Position delta = but-pos;
float angleVise = atan2(delta.getY(),delta.getX());
float diff = diffAngle(angleVise,angle);
// gestion de si la commande a fini
if (abs(diff) < M_PI/90.0f)// || (signeAngle == SGN_NEG && diff > 0.0f) || (signeAngle == SGN_POS && diff < 0.0f))
{
m_fini = true;
}
else if (signeAngle == SGN_UNDEF && abs(diff) < 1.0f)
{
if (diff > 0.0f)
signeAngle = SGN_POS;
else
signeAngle = SGN_NEG;
}
// calcul de la vitesse angulaire
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
if (abs(diff) > angleVitesseMax)
{
if (diff > 0)
angSpeed += accAngMax;
else if (diff < 0)
angSpeed -= accAngMax;
if (angSpeed > vitAngMax)
angSpeed = vitAngMax;
else if (angSpeed < -vitAngMax)
angSpeed = -vitAngMax;
}
else
{
if (diff >= 0)
angSpeed = sqrt(2.0f*diff*accAngMax);
else
angSpeed = -sqrt(-2.0f*diff*accAngMax);
}
}
Vitesse CommandTournerVers::getLinearSpeed()
{
return 0.0f;
}
Angle CommandTournerVers::getAngularSpeed()
{
return angSpeed;
}
bool CommandTournerVers::fini() const
{
return m_fini;
}
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
////////////////////////////////
// CommandVirage //
////////////////////////////////
// rayon > 0
// angle > 0 : vers la gauche, angle < 0 : vers la droite
CommandVirage::CommandVirage(float rayon, float angle, float vitesseFin)
{
if (angle > 0.0f)
rayonCourbure = rayon;
else
rayonCourbure = -rayon;
linSpeed = Odometrie::odometrie->getVitesseLineaire();
angSpeed = Odometrie::odometrie->getVitesseAngulaire();
angleVise = angle + Odometrie::odometrie->getPos().getAngle();
vFin2 = vitesseFin*vitesseFin;
m_fini = false;
}
void CommandVirage::update()
{
float accLinMax = ACCELERATION_LINEAIRE_MAX;
float decLinMax = DECELERATION_LINEAIRE_MAX;
float vitLinMax = VITESSE_LINEAIRE_MAX;
float distanceVitesseMax = 0.5f*(vitLinMax*vitLinMax-vFin2)/decLinMax;
float angleRestant = diffAngle(angleVise, Odometrie::odometrie->getPos().getAngle());
float distanceRestante = abs(rayonCourbure*angleRestant);
// gestion de si la commande a fini
// si l'angle restant est bon ou si on a dépassé l'angle visé
if (abs(angleRestant) < M_PI/90.0f || ((angleRestant > 0.0f) != (rayonCourbure > 0.0f)))
{
m_fini = true;
}
// phase de vitesse max
if (distanceRestante > distanceVitesseMax)
{
linSpeed += accLinMax;
if (linSpeed > vitLinMax)
linSpeed = vitLinMax;
}
// phase de décéleration
else
{
linSpeed = sqrt(vFin2+2.0f*distanceRestante*decLinMax);
}
// calcul de la vitesse angulaire
angSpeed = linSpeed/rayonCourbure;
}
Vitesse CommandVirage::getLinearSpeed()
{
return linSpeed;
}
Angle CommandVirage::getAngularSpeed()
{
return angSpeed;
}
// est ce que la commande a fini ?
bool CommandVirage::fini() const
{
return m_fini;
}
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
////////////////////////////////
// CommandAttendre //
////////////////////////////////
CommandAttendre::CommandAttendre(int nbUpdates)
: Command(), compte(nbUpdates)
{
}
void CommandAttendre::update()
{
compte--;
}
Vitesse CommandAttendre::getLinearSpeed()
{
return 0.0f;
}
Angle CommandAttendre::getAngularSpeed()
{
return 0.0f;
}
bool CommandAttendre::fini() const
{
return (compte <= 0);
}
////////////////////////////////
// CommandTestAvancer //
////////////////////////////////
CommandTestAvancer::CommandTestAvancer()
: Command()
{
}
void CommandTestAvancer::update()
{
}
Vitesse CommandTestAvancer::getLinearSpeed()
{
return VITESSE_LINEAIRE_MAX;
}
Angle CommandTestAvancer::getAngularSpeed()
{
return 0.0f;
}
////////////////////////////////
// CommandTestTournerGauche //
////////////////////////////////
CommandTestTournerGauche::CommandTestTournerGauche()
: Command()
{
}
void CommandTestTournerGauche::update()
{
}
Vitesse CommandTestTournerGauche::getLinearSpeed()
{
return 0.0f;
}
Angle CommandTestTournerGauche::getAngularSpeed()
{
return VITESSE_ANGULAIRE_MAX;
}