Newer
Older
#include "commandAllerA.h"
#include "odometrie.h"
#include <math.h>
#include "strategieV2.h"
#ifndef abs
#define abs(x) fabs(x)
#endif
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;
}
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;
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
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))
}
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 vitesseLineaireMax, 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 angleVitesseMax = M_PI/10.0f;
float angleVitesseMax = 0.5f*vitAngMax*vitAngMax/accAngMax;
//float distanceVitesseMax = 350.0f;
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);
// reste sur place tant que le robot n'a pas le bon angle
float angleMaxPourAvancer = M_PI/10.0f;//25.0f;
float angleMaxPourContinuer = M_PI/90.f;;
if (abs(diffAng) < angleMaxPourAvancer)
{
bonAngle = true;
}
else
{
linSpeed *= 0.95f;
return;
}
}
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
float distanceBut = delta.getNorme();
bool distanceOk = /*(distanceBut > derniereDistance) || */(distanceBut < 30.0f);
// vitesse angulaire
if (distanceOk)
{
vitAngMax = VITESSE_ANGULAIRE_SLOW_MAX;
//linSpeed = 0.;
}
if (abs(diffAng) > angleVitesseMax)
{
bool hasToDecelerate = (!distanceOk) && (fabs(diffAng) < (angSpeed * angSpeed / accAngMax - accAngMax*2.));
if (diffAng > 0)
{
if (!hasToDecelerate)
angSpeed += accAngMax;
else if (angSpeed > accAngMax)
angSpeed -= accAngMax;
if (angSpeed > vitAngMax)
angSpeed = vitAngMax;
}
else
{
if (!hasToDecelerate)
angSpeed -= accAngMax;
else if (angSpeed < -accAngMax)
angSpeed += accAngMax;
if (angSpeed < -vitAngMax)
angSpeed = -vitAngMax;
}
}
else
{
angSpeed = diffAng*vitAngMax/angleVitesseMax;
}
_angleVise = but.getX();
_angleLimit = but.getY();
if (distanceOk/* && (abs(diffAng) < angleMaxPourContinuer)*/)
if (abs(diffAng) > angleMaxPourAvancer)
{
linSpeed *= 0.97f;
}
else
linSpeed += accLinMax;
if (linSpeed > vitLinMax)
linSpeed = vitLinMax;
else if (linSpeed < -vitLinMax)
linSpeed = -vitLinMax;
if (m_reculer)
{
float linSpeedVisee = -sqrt(vFin2+2.0f*distanceBut*decLinMax);
if (linSpeed - accLinMax < linSpeedVisee)
linSpeed = linSpeedVisee;
else
linSpeed -= accLinMax;
}
{
float linSpeedVisee = sqrt(vFin2+2.0f*distanceBut*decLinMax);
if (linSpeed + accLinMax > linSpeedVisee)
linSpeed = linSpeedVisee;
else
linSpeed += accLinMax;
}
/*float linSpeedVisee;
if (m_reculer)
linSpeedVisee = -sqrt(vFin2+2.0f*distanceBut*decLinMax);
else
linSpeedVisee = sqrt(vFin2+2.0f*distanceBut*decLinMax);
if (m_reculer)
linSpeed -= accLinMax;
else
linSpeed += accLinMax;
if (abs(linSpeed) > abs(linSpeedVisee))
linSpeed = linSpeedVisee;*/
}
void CommandAllerA::resetSpeeds()
{
linSpeed = Odometrie::odometrie->getVitesseLineaire();
angSpeed = Odometrie::odometrie->getVitesseAngulaire();
}
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
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
{
angSpeed = -sqrt(-2.0f*diff*accAngMax);*/
if (diff >= 0)
{
float angSpeedVisee = sqrt(2.0f*diff*accAngMax);
if (angSpeed + accAngMax > angSpeedVisee)
angSpeed = angSpeedVisee;
else
angSpeed += accAngMax;
}
else
{
float angSpeedVisee = -sqrt(-2.0f*diff*accAngMax);
if (angSpeed - accAngMax < angSpeedVisee)
angSpeed = angSpeedVisee;
else
angSpeed -= accAngMax;
}
}
}
Vitesse CommandTournerVers::getLinearSpeed()
{
return 0.0f;
}
Angle CommandTournerVers::getAngularSpeed()
{
return angSpeed;
}
bool CommandTournerVers::fini() const
{
return m_fini;
}
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
////////////////////////////////
// CommandTournerVersAngle //
////////////////////////////////
CommandTournerVersAngle::CommandTournerVersAngle(float angleVise)
: Command()
{
but = angleVise;
angSpeed = 0;
m_fini = false;
signeAngle = SGN_UNDEF;
}
void CommandTournerVersAngle::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();
float angleVise = but;
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
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 CommandTournerVersAngle::getLinearSpeed()
{
return 0.0f;
}
Angle CommandTournerVersAngle::getAngularSpeed()
{
return angSpeed;
}
bool CommandTournerVersAngle::fini() const
{
return m_fini;
}
////////////////////////////////
// CommandVirage //
////////////////////////////////
// rayon > 0
// angle > 0 : vers la gauche, angle < 0 : vers la droite
CommandVirage::CommandVirage(float rayon, float angle, float vitesseLineaireMax, 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;
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
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;
}
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
////////////////////////////////
// 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;
}