summaryrefslogtreecommitdiff
path: root/i/marvin/src/asserv
diff options
context:
space:
mode:
authorhaller2005-11-17 11:10:17 +0000
committerhaller2005-11-17 11:10:17 +0000
commitda4a77ff094a0dc74414ec1de2cac08e8b7b60f4 (patch)
tree402c39246c164af44648fd98d2ea85500d58df58 /i/marvin/src/asserv
parent164d8870bc058450e6967a594dd9033d088316af (diff)
Importation brut du code récupérable de robert
Diffstat (limited to 'i/marvin/src/asserv')
-rw-r--r--i/marvin/src/asserv/Makefile.defs7
-rw-r--r--i/marvin/src/asserv/asserv.cc496
-rw-r--r--i/marvin/src/asserv/asserv.hh160
-rw-r--r--i/marvin/src/asserv/test_asserv.cc348
4 files changed, 1011 insertions, 0 deletions
diff --git a/i/marvin/src/asserv/Makefile.defs b/i/marvin/src/asserv/Makefile.defs
new file mode 100644
index 0000000..80bf8ee
--- /dev/null
+++ b/i/marvin/src/asserv/Makefile.defs
@@ -0,0 +1,7 @@
+PROGRAMS += test_asserv
+
+asserv_OBJECTS = asserv.o
+
+test_asserv_OBJECTS = test_asserv.o $(asserv_OBJECTS) $(config_OBJECTS) $(timer_OBJECTS) $(proto_OBJECTS) $(log_OBJECTS) $(serial_OBJECTS) $(utils_OBJECTS)
+
+test_asserv: $(test_asserv_OBJECTS)
diff --git a/i/marvin/src/asserv/asserv.cc b/i/marvin/src/asserv/asserv.cc
new file mode 100644
index 0000000..082f39b
--- /dev/null
+++ b/i/marvin/src/asserv/asserv.cc
@@ -0,0 +1,496 @@
+// asserv.cc
+// robert - programme du robot 2005 {{{
+//
+// Copyright (C) 2005 Nicolas Haller
+//
+// Robot APB Team/Efrei 2005.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+
+#include "asserv.hh"
+
+#include<cmath>
+
+/// Constructeur
+Asserv::Asserv (const Config & config, Receiver & receiver)
+ :proto_(*this), receiver_(receiver), log_("asserv")
+{
+ // Rechargement des paramètres
+ loadConfig(config);
+ //Ouverture du port série
+ proto_.open(ttyName_);
+}
+
+/// Reset de l'AVR
+void Asserv::reset(void)
+{
+ // Reset AVR
+ proto_.send('z');
+ // Envoie des données de conf sur l'AVR
+ setFooting(footing_);
+ setEpsilon(epsilon_);
+ setKp(kp_);
+ setKi(ki_);
+ setKd(kd_);
+ setESat(eSat_);
+ setSpeedIntMax(speedIntMax_);
+ setDSample(dSample_);
+ setAccel(accel_);
+ setMaxSpeed(maxSLin_, maxSRot_);
+ setUseTazFSM(useTazFSM_);
+ setInvPwm(leftInvPwm_, rightInvPwm_);
+ statCounter(pCounter_);
+ statPosition(pPosition_);
+ statMotor(pMotor_);
+ statPwm(pPwm_);
+ statTiming(pTiming_);
+ statInPort(pStatInPort_);
+ statSharp(pStatInPort_);
+}
+
+/// Essaie de purger la liste d'émission et indique si elle est vide.
+bool Asserv::sync(void)
+{
+ return proto_.sync();
+}
+
+/// Attend que toute les émissions soit terminées
+bool Asserv::wait(int timeout)
+{
+ return proto_.wait(timeout);
+}
+
+/// Récupère le File Descriptor
+int Asserv::getFd(void)
+{
+ return proto_.getFd();
+}
+
+/// Commandes d'asservissement
+void Asserv::linearMove(double distance)
+{
+ // Conversion mm->PasW
+ int distPas = mm2pas(distance);
+ //log_ ("linearMove") << "avConv" << distance << "apConv" << distPas;
+ // On envoie la commande à l'AVR
+ proto_.send('l', "w", distPas);
+}
+
+void Asserv::angularMove(double angle)
+{
+ // Conversion radian->256ème
+ int a = radTo256(angle);
+ //log_ ("angularMove") << "avConv" << angle << "apConv" << a;
+ // Envopie vers avr
+ proto_.send('a',"b", a);
+}
+
+void Asserv::goToPosition(double xPos, double yPos)
+{
+ // Convertion mm->PasD
+ int x = mm2pas(xPos, true);
+ int y = mm2pas(yPos, true);
+ //log_ ("goToPosition") << "avConvX" << xPos << "apConvX" << x;
+ //log_ ("goToPosition") << "avConvY" << yPos << "apConvY" << y;
+ // Envoie vers l'AVR
+ proto_.send('g', "dd", x, y);
+}
+
+// XXX Vérifier la conversion ca me parais un peu petit
+void Asserv::fuckTheWall(double speed)
+{
+ // Conversion mm/s->Pas/Period
+ int v = mms2ppp(speed);
+ //log_ ("fuckTheWall") << "avConv" << speed << "apConv" << v;
+ // Envoie vers l'AVR
+ fuckTheWall(v);
+}
+
+void Asserv::fuckTheWall(int speed)
+{
+ //log_ ("fuckTheWall") << "avConv";
+ // Envoie vers l'AVR
+ proto_.send('f',"b", speed);
+}
+
+void Asserv::setSpeed(double xSpeed, double ySpeed)
+{
+ // Conversion mm/s->Pas/Period
+ int vx = mms2ppp(xSpeed);
+ int vy = mms2ppp(ySpeed);
+ //log_ ("setSpeed") << "avConvX" << xSpeed << "apConvX" << vx;
+ //log_ ("setSpeed") << "avConvY" << ySpeed << "apConvY" << vy;
+ // Envoie vers l'AVR
+ setSpeed(vx,vy);
+}
+
+void Asserv::setSpeed(int xSpeed, int ySpeed)
+{
+ //log_ ("setSpeed") << "avConvX" << xSpeed ;
+ //log_ ("setSpeed") << "avConvY" << ySpeed ;
+ // Envoie vers l'AVR
+ proto_.send('s',"bb", xSpeed, ySpeed);
+}
+
+void Asserv::setPwm(int xPwm, int yPwm)
+{
+ //log_ ("setPwm") << "avConvX" << xPwm;
+ //log_ ("setPwm") << "avConvY" << yPwm;
+ // Envoie sur l'AVR
+ proto_.send('w',"ww" ,xPwm, yPwm);
+}
+
+/// Informe l'AVR d'arréter de dire "j'ai fini"
+void Asserv::finishAck(void)
+{
+ proto_.send('F', "b", 2);
+}
+
+/// XXX Valeurs bizarres
+/// Statistiques
+void Asserv::statCounter(int period)
+{
+
+ pCounter_ = period;
+ // Convertion s->Period
+ //int p = s2period(period);
+ //log_ ("statCounter") << "avConv" << period << "apConv" << p;
+ // Envoie sur l'AVR
+ proto_.send('C', "b", period);
+}
+
+void Asserv::statPosition(int period)
+{
+ pPosition_ = period;
+ // Convertion s->Period
+ //int p = s2period(period);
+ // Envoie sur l'AVR
+ proto_.send('X', "b", period);
+}
+
+void Asserv::statMotor(int period)
+{
+ pMotor_ = period;
+ // Convertion s->Period
+ //int p = s2period(period);
+ // Envoie sur l'AVR
+ proto_.send('S', "b", period);
+}
+
+void Asserv::statPwm(int period)
+{
+ pPwm_ = period;
+ // Convertion s->Period
+ // int p = s2period(period);
+ // Envoie sur l'AVR
+ proto_.send('W', "b", period);
+}
+
+void Asserv::statTiming(int period)
+{
+ pTiming_ = period;
+ // Convertion s->Period
+ //int p = s2period(period);
+ // Envoie sur l'AVR
+ proto_.send('T', "b", period);
+}
+
+void Asserv::statInPort(int period)
+{
+ pStatInPort_ = period;
+ // Convertion s->Period
+ //int p = s2period(period);
+ // Envoie sur l'AVR
+ proto_.send('P', "b", period);
+}
+
+void Asserv::statSharp(int period)
+{
+ pSharp_ = period;
+ // Conversion s->period
+ //int p = s2period(period);
+ // Envoie sur l'AVR
+ proto_.send('H', "b", period);
+}
+
+/// Change les paramètres
+void Asserv::setXPos(double xPos)
+{
+ // Conversion mm->pas
+ int p = mm2pas(xPos, true);
+ // Envoie à l'AVR
+ proto_.send('p',"bd", 'x', p);
+}
+
+void Asserv::setYPos(double yPos)
+{
+ // Conversion mm->pas
+ int p = mm2pas(yPos, true);
+ // Envoie à l'AVR
+ proto_.send('p',"bd", 'y', p);
+}
+
+void Asserv::setAngle(double angle)
+{
+ // Envoie à l'AVR
+ proto_.send('p',"bd", 'a', static_cast<int>(angle / (2 * M_PI) * (1 << 24)));
+}
+
+void Asserv::setFooting(int16_t footing)
+{
+ footing_ = footing;
+ // Envoie à l'AVR
+ proto_.send('p',"bw", 'f', footing);
+}
+
+void Asserv::setEpsilon(double epsilon)
+{
+ epsilon_ = epsilon;
+ // Envoie à l'AVR
+ proto_.send('p',"bd", 'e', static_cast<int>(epsilon * 256));
+}
+
+void Asserv::setKp(int Kp)
+{
+ kp_ = Kp;
+ // Envoie à l'AVR
+ proto_.send('p',"bw", 'p', Kp);
+}
+
+void Asserv::setKi(int Ki)
+{
+ ki_ = Ki;
+ // Envoie à l'AVR
+ proto_.send('p',"bw", 'i', Ki);
+}
+
+void Asserv::setKd(int Kd)
+{
+ kd_ = Kd;
+ // Envoie à l'AVR
+ proto_.send('p',"bw", 'd', Kd);
+}
+
+void Asserv::setESat(int eSat)
+{
+ eSat_ = eSat;
+ // Envoie à l'AVR
+ proto_.send('p', "bw", 'E', eSat);
+}
+
+void Asserv::setSpeedIntMax(int maxInt)
+{
+ speedIntMax_ = maxInt;
+ proto_.send('p', "bW", 'I', maxInt);
+}
+
+void Asserv::setDSample (int dSample)
+{
+ dSample_ = dSample;
+ proto_.send('p', "bb", 's', dSample);
+}
+
+void Asserv::setAccel(int accel)
+{
+ accel_ = accel;
+ //log_ ("setAccel") << "avConv" << accel;
+ // envoie vers l'AVR
+ proto_.send('p',"bb", 'a', accel_);
+}
+
+/// Renvoie l'accélération en unité international (mm/s²)
+double Asserv::getAccel(void)
+{
+ return mmPpas_ / (sPperiod_ * sPperiod_) / accel_; // XXX Vérifier ca
+}
+
+/// Règle les vitesses maximum(lineaire et rotation)
+void Asserv::setMaxSpeed(int maxSLin, int maxSRot)
+{
+ maxSLin_ = (maxSLin > 0 ? maxSLin : maxSLin_);
+ maxSRot_ = (maxSRot > 0 ? maxSRot : maxSRot_);
+ // Envoie à l'AVR
+ proto_.send('p',"bbb", 'm', maxSLin_, maxSRot_);
+}
+
+int Asserv::getMaxLSpeed(void)
+{
+ return maxSLin_;
+}
+
+void Asserv::setUseTazFSM(bool use)
+{
+ useTazFSM_ = use;
+ // Envoie la commande à l'AVR
+ proto_.send('t', "b", use ? 1 : 0);
+}
+
+void Asserv::setInvPwm(bool leftInvPwm, bool rightInvPwm)
+{
+ leftInvPwm_ = leftInvPwm;
+ rightInvPwm_ = rightInvPwm;
+ // Envoie la commande à l'AVR
+ proto_.send('p', "bbb", 'w', leftInvPwm , rightInvPwm);
+}
+
+/// implémentation du proto::Receiver
+void Asserv::receive(char command, const Proto::Frame &frame)
+{
+ switch(command)
+ {
+ case 'C': //Compteurs des moteurs (uint16)
+ int lMotor;
+ int rMotor;
+ proto_.decode(frame, "ww", lMotor, rMotor);
+ receiver_.receiveCounter(lMotor, rMotor);
+ break;
+ case 'X':
+ int xPos;
+ proto_.decode(frame, "D", xPos);
+ // Convertion Pas->mm
+ receiver_.receivePosX(xPos * mmPpas_ / 256);
+ break;
+ case 'Y':
+ int yPos;
+ proto_.decode(frame, "D", yPos);
+ // Convertion Pas->mm
+ receiver_.receivePosY(yPos * mmPpas_ / 256);
+ break;
+ case 'A':
+ {
+ int aPos;
+ proto_.decode(frame, "D", aPos);
+ double aPosD = (aPos * (2 * M_PI) / (1 << 24));
+ receiver_.receivePosA(aPosD);
+ break;
+ }
+ case 'S':
+ int oldLSpeed, LSpeed, oldRSpeed, RSpeed;
+ proto_.decode(frame, "WWWW", oldLSpeed, LSpeed, oldRSpeed, RSpeed);
+ receiver_.receiveSpeedStat(oldLSpeed, LSpeed, oldRSpeed, RSpeed);
+ break;
+ case 'W':
+ int leftPwm, rightPwm;
+ proto_.decode(frame, "WW", leftPwm, rightPwm);
+ receiver_.receivePwm(leftPwm, rightPwm);
+ break;
+ case 'T':
+ {
+ int motorTimer4 = static_cast<int>(frame.args[1]);
+ int motorTimer3 = static_cast<int>(frame.args[2]);
+ int motorTimer2 = static_cast<int>(frame.args[3]);
+ int motorTimer1 = static_cast<int>(frame.args[4]);
+ int motorTimer0 = static_cast<int>(frame.args[5]);
+ receiver_.receiveTiming(motorTimer4, motorTimer3, motorTimer2,
+ motorTimer1, motorTimer0);
+ break;
+ }
+ case 'P':
+ {
+ int statePIn;
+ proto_.decode(frame, "b", statePIn);
+ receiver_.receiveInPort(statePIn);
+ break;
+ }
+ case 'H':
+ {
+ int stateSharp0, stateSharp1, stateSharp2;
+ proto_.decode(frame, "www", stateSharp0, stateSharp1, stateSharp2);
+ receiver_.receiveSharp(stateSharp0, stateSharp1, stateSharp2);
+ break;
+ }
+ case 't':
+ {
+ int state, subState;
+ proto_.decode(frame, "bb", state, subState);
+ receiver_.receiveTazState(state, subState);
+ }
+ case 'F':
+ receiver_.done();
+ break;
+ }
+}
+
+/// Charge les données de la classe config
+void Asserv::loadConfig(const Config & config)
+{
+ ttyName_ = config.get<std::string>("asserv.tty");
+ footing_ = config.get<int>("asserv.footing");
+ epsilon_ = config.get<double>("asserv.epsilon");
+ accel_ = config.get<int>("asserv.accel");
+ kp_ = config.get<int>("asserv.kp");
+ ki_ = config.get<int>("asserv.ki");
+ kd_ = config.get<int>("asserv.kd");
+ eSat_ = config.get<int>("asserv.eSat");
+ speedIntMax_ = config.get<int>("asserv.speedIntMax");
+ dSample_ = config.get<int>("asserv.dSample");
+ maxSLin_ = config.get<int>("asserv.maxSLin");
+ maxSRot_ = config.get<int>("asserv.maxSRot");
+ useTazFSM_ = config.get<bool>("asserv.useTazFSM");
+ mmPpas_ = config.get<double>("asserv.mmPpas");
+ sPperiod_ = config.get<double>("asserv.sPperiod");
+ pwmMax_ = config.get<int>("asserv.pwmMax");
+ leftInvPwm_ = config.get<bool>("asserv.leftInvPwm");
+ rightInvPwm_ = config.get<bool>("asserv.rightInvPwm");
+ pCounter_ = 0;
+ pPosition_ = 0;
+ pMotor_ = 0;
+ pPwm_ = 0;
+ pTiming_ = 0;
+ pStatInPort_ = 32;
+ pSharp_ = 0;
+}
+
+int Asserv::mm2pas(double dist, bool format24eme)
+{
+ if(format24eme)
+ return static_cast<int>((dist/mmPpas_)*256);
+ else
+ return static_cast<int>(dist/mmPpas_);
+}
+
+int Asserv::radTo256(double angle, bool format24eme)
+{
+ if(angle < 0)
+ angle += 2 * M_PI;
+ if(format24eme)
+ return static_cast<int>((angle / (2 * M_PI)) * 256 * (1 << 24));
+ else
+ return static_cast<int>((angle / (2 * M_PI))* 256);
+}
+
+int Asserv::mms2ppp(double vitesse)
+{
+ return static_cast<int>(vitesse * sPperiod_ / mmPpas_);
+}
+
+int Asserv::rCycl2Pwm(double rCycl)
+{
+ return static_cast<int>(rCycl*pwmMax_);
+}
+
+int Asserv::s2period(double period)
+{
+ return static_cast<int>(period/sPperiod_);
+}
+
+//int Asserv::mmps2ppperiod(double accel)
+//{
+// return static_cast<int>(1/(accel*(sPperiod_*sPperiod_/mmPpas_)));
+//}
diff --git a/i/marvin/src/asserv/asserv.hh b/i/marvin/src/asserv/asserv.hh
new file mode 100644
index 0000000..4064537
--- /dev/null
+++ b/i/marvin/src/asserv/asserv.hh
@@ -0,0 +1,160 @@
+#ifndef asserv_hh
+#define asserv_hh
+// asserv.hh
+// robert - programme du robot 2005 {{{
+//
+// Copyright (C) 2005 Nicolas Haller
+//
+// Robot APB Team/Efrei 2005.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+
+#include "utils/non_copyable.hh"
+#include "config/config.hh"
+#include "proto/proto.hh"
+#include "log/log.hh"
+
+/// Classe de dialogue avec la carte d'asservissement.
+class Asserv : public NonCopyable, public Proto::Receiver
+{
+ public:
+ /// Les clients de Asserv doivent dériver de Receiver.
+ class Receiver
+ {
+ public:
+ virtual ~Receiver(void){};
+ /// Recoit un packet.
+ virtual void receiveCounter (double lMotor, double rMotor) = 0;
+ virtual void receivePosX (double xPos) = 0;
+ virtual void receivePosY (double yPos) = 0;
+ virtual void receivePosA (double aPos) = 0;
+ virtual void receiveSpeedStat (int leftError, int leftInt, int rightError, int rightInt) = 0;
+ virtual void receivePwm (double leftPwm, double rightPwm) = 0;
+ virtual void receiveTiming (int motorTimer4,
+ int motorTimer3, int motorTimer2,
+ int motorTimer1,
+ int motorTimer0 ) = 0;
+ virtual void receiveInPort (int port) = 0;
+ virtual void receiveSharp (int sharp1, int sharp2, int sharp3) = 0;
+ virtual void receiveTazState(int state, int subState){}; // XXX Vérifier les formats
+ virtual void done (void) = 0;
+ };
+
+ private:
+
+ // Communication avec l'AVR
+ Proto proto_;
+ std::string ttyName_;
+ Receiver & receiver_;
+ // Paramètre de l'avr
+ int footing_; //truc
+ double epsilon_; //mm
+ int accel_; //unité AVR
+ int kp_;
+ int ki_;
+ int kd_;
+ int eSat_;
+ int speedIntMax_;
+ int dSample_;
+ int maxSLin_;//unité AVR
+ int maxSRot_;
+ bool leftInvPwm_, rightInvPwm_;
+ bool useTazFSM_;
+ // Stat
+ int pCounter_;
+ int pPosition_;
+ int pMotor_;
+ int pPwm_;
+ int pTiming_;
+ int pStatInPort_;
+ int pSharp_;
+
+ // Unités
+ double mmPpas_;
+ double sPperiod_;
+ int pwmMax_;
+
+ // Système de log.
+ Log log_;
+ public:
+ /// Constructeur.
+ Asserv (const Config & config, Asserv::Receiver & receiver);
+ /// Reset la carte et envois les paramètres.
+ void reset (void);
+ /// Essaie de purger la liste d'émission et indique si elle est vide.
+ bool sync (void);
+ /// Attend que toute les émissions soit terminées.
+ bool wait (int timeout = -1);
+ /// Récupère le File Descriptor
+ int getFd(void);
+ /// Commandes asservissement
+ void linearMove(double distance);
+ void angularMove(double angle);
+ void goToPosition(double xPos, double yPos);
+ void fuckTheWall(double speed);
+ void fuckTheWall(int speed);
+ void setSpeed(double xSpeed, double ySpeed);
+ void setSpeed(int xSpeed = 0, int ySpeed = 0);
+ void setPwm(int leftPwm = 0, int rightPwm = 0);
+ void finishAck(void); //met le paramètre F à 0
+ /// Statistiques
+ void statCounter(int period = 0);
+ void statPosition(int period = 0);
+ void statMotor(int period = 0);
+ void statPwm(int period = 0);
+ void statTiming(int period = 0);
+ void statInPort(int period = 0);
+ void statSharp(int period = 0);
+ //@{
+ /// Change les paramètres de la carte.
+ void setXPos(double pos);
+ void setYPos(double pos);
+ void setAngle(double angle);
+ void setFooting(int16_t dist);
+ void setEpsilon(double e);
+ void setKp (int kp);
+ void setKi (int ki);
+ void setKd (int kd);
+ void setESat (int eSat);
+ void setSpeedIntMax (int maxInt);
+ void setDSample(int dSample);
+ void setAccel (int accel);
+ double getAccel (void);
+ void setMaxSpeed(int maxSLin, int maxSRot);
+ int getMaxLSpeed(void);
+ void setUseTazFSM(bool use);
+ void setInvPwm(bool leftInvPwm, bool rightInvPwm);
+ //@}
+
+ /// implémentation du proto::Receiver
+ void receive (char command, const Proto::Frame &frame);
+
+ private:
+ /// Charge les données de la classe config
+ void loadConfig(const Config & config);
+ /// Fonctions de conversion
+ int mm2pas(double dist, bool format24eme = false); //Format 24/8 ou 8
+ int radTo256(double angle, bool format24eme = false); //Format 8 ou 8/24
+ int mms2ppp(double vitesse); // Format 8
+ int rCycl2Pwm(double rCycl); // Format 16
+ int s2period(double period); // Format 8
+ //int mmps2ppperiod(double accel); //Format 8 (1 vitesse par x period)
+};
+
+#endif // asserv_hh
diff --git a/i/marvin/src/asserv/test_asserv.cc b/i/marvin/src/asserv/test_asserv.cc
new file mode 100644
index 0000000..228748c
--- /dev/null
+++ b/i/marvin/src/asserv/test_asserv.cc
@@ -0,0 +1,348 @@
+// test_asserv.cc
+// robert - programme du robot 2005 {{{
+//
+// Copyright (C) 2005 Nicolas Haller
+//
+// Robot APB Team/Efrei 2005.
+// Web: http://assos.efrei.fr/robot/
+// Email: robot AT efrei DOT fr
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+//
+// }}}
+
+#include "asserv.hh"
+#include "config/config.hh"
+#include "timer/timer.hh"
+
+#include<stdexcept>
+#include<cstdlib>
+#include<cmath>
+
+/// Classe de test pour asserv.
+class TestAsserv : public Asserv::Receiver
+{
+ void receiveCounter (double lMotor, double rMotor)
+ {
+ std::cout << "C:" << "lMotor:" << lMotor << " rMotor:" << rMotor << std::endl;
+ }
+
+ void receivePosX (double xPos)
+ {
+ std::cout << "X:" << "XPos:" << xPos << std::endl;
+ }
+
+ void receivePosY (double yPos)
+ {
+ std::cout << "Y:" << "yPos:" << yPos << std::endl;
+ }
+
+ void receivePosA (double aPos)
+ {
+ std::cout << "A:" << " aPosR: " << aPos << " aPosD: " << aPos / (2 * M_PI) * 360 << std::endl;
+ }
+
+ void receiveSpeedStat (int leftError, int leftInt, int rightError, int rightInt)
+ {
+ std::cout << "S:" << " leftError: " << leftError << "leftInt: " <<
+ leftInt << " rightError: " << rightError <<
+ " rightInt: " << rightInt << std::endl;
+ }
+
+ void receivePwm (double leftPwm, double rightPwm)
+ {
+ std::cout << "W:" << "leftPwm" << leftPwm << " rightPwm:" << rightPwm << std::endl;
+ }
+
+ void receiveTiming (int motorTimer4, int motorTimer3,
+ int motorTimer2, int motorTimer1,
+ int motorTimer0 )
+ {
+ std::cout << "T:" << " motorTimer4: " << motorTimer4 << " motorTimer3: "
+ << motorTimer3 << " motorTimer2: " << motorTimer2
+ << " motorTimer1: " << motorTimer1 << " motorTimer0: "
+ << motorTimer0 << std::endl;
+ }
+
+ void receiveInPort (int port)
+ {
+ std::cout << "P:" << "state:" << port << std::endl;
+ }
+
+ void receiveSharp (int sharp0, int sharp1, int sharp2)
+ {
+ std::cout << "t:" << "sharp0:" << sharp0 << " sharp1:" << sharp1
+ << " sharp2:" << sharp2 << std::endl;
+ }
+
+ void done (void)
+ {
+ std::cout << "F:" << "J'ai fini!!" << std::endl;
+ }
+
+};
+
+/// Affiche un memo de suntaxe.
+ void
+syntax (void)
+{
+ std::cout << "test_asserv - test la classe de protocol asserv.\n"
+ "Syntaxe : test_asserv <...>\n"
+ " s <cmd> <args...> envois une commande\n"
+ " w <ms> attend pendant un nombre de millisecondes\n"
+ " ? affiche cet écran d'aide\n"
+ << std::endl;
+}
+
+ int
+main (int argc, char **argv)
+{
+ try
+ {
+ int i;
+ if (argc < 2)
+ {
+ syntax ();
+ return 1;
+ }
+ Config config(argc, argv);
+ TestAsserv testAsserv;
+ Asserv asserv(config, testAsserv);
+ i = 1;
+ while (i < argc)
+ {
+ switch (argv[i][0])
+ {
+ case 's':
+ {
+ switch(argv[++i][0])
+ {
+ case 'z':
+ asserv.reset();
+ break;
+ case 'l':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.linearMove(strtod(argv[i], 0));
+ break;
+ case 'a':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.angularMove(strtod(argv[i], 0));
+ break;
+ case 'g':
+ if(++i + 1 > argc)
+ throw std::runtime_error("syntax error");
+ asserv.goToPosition(strtod(argv[i], 0),
+ strtod(argv[i + 1], 0));
+ i++;
+ break;
+ case 'f':
+ if(++i + 1 > argc)
+ throw std::runtime_error("syntax error");
+ switch(argv[i][0])
+ {
+ case 'd':
+ asserv.fuckTheWall(strtod(argv[i + 1], 0));
+ break;
+ case 'i':
+ asserv.fuckTheWall(atoi(argv[i + 1]));
+ break;
+ default:
+ throw std::runtime_error("syntax error");
+ break;
+ }
+ i++;
+ break;
+ case 's':
+ if(++i + 2 > argc)
+ throw std::runtime_error("syntax error");
+ switch(argv[i][0])
+ {
+ case 'd':
+ i++;
+ asserv.setSpeed(strtod(argv[i], 0), strtod(argv[i + 1], 0));
+ break;
+ case 'i':
+ i++;
+ asserv.setSpeed(atoi(argv[i]), atoi(argv[i + 1]));
+ break;
+ default:
+ throw std::runtime_error("syntax error");
+ break;
+ }
+ i++;
+ break;
+ case 'w':
+ if(++i + 1 > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setPwm(strtol(argv[i], 0, 10), strtol(argv[i + 1], 0, 10));
+ i++;
+ break;
+ case 'F':
+ asserv.finishAck();
+ break;
+ case 'C':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.statCounter(strtol(argv[i], 0, 10));
+ break;
+ case 'X':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.statPosition(strtol(argv[i], 0, 10));
+ break;
+ case 'S':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.statMotor(strtol(argv[i], 0, 10));
+ break;
+ case 'W':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.statPwm(strtol(argv[i], 0, 10));
+ break;
+ case 'T':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.statTiming(strtol(argv[i], 0, 10));
+ break;
+ case 'P':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.statInPort(strtol(argv[i], 0, 10));
+ break;
+
+ case 'H':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.statSharp(atoi(argv[i]));
+ break;
+
+ case 'p':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ switch(argv[i][0])
+ {
+ case 'x':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setXPos(strtod(argv[i], 0));
+ break;
+ case 'y':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setYPos(strtod(argv[i], 0));
+ break;
+ case 'a':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setAngle(strtod(argv[i], 0));
+ break;
+ case 'f':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setFooting(strtol(argv[i], 0, 10));
+ break;
+ case 'e':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setEpsilon(strtod(argv[i], 0));
+ break;
+ case 'p':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setKp(strtol(argv[i], 0, 10));
+ break;
+ case 'i':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setKi(strtol(argv[i], 0, 10));
+ break;
+ case 'd':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setKd(strtol(argv[i], 0, 10));
+ break;
+ case 'E':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setESat(strtol(argv[i], 0, 10));
+ break;
+ case 'I':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setSpeedIntMax(strtol(argv[i], 0, 10));
+ break;
+ case 's':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setDSample(strtol(argv[i], 0, 10));
+ break;
+ case 'A':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setAccel(strtol(argv[i], 0, 10));
+ break;
+ case 'm':
+ if(++i > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setMaxSpeed(strtol(argv[i], 0, 10),
+ strtol(argv[i + 1], 0, 10));
+ i++;
+ break;
+ case 'W':
+ if (++i + 1 > argc)
+ throw std::runtime_error("syntax error");
+ asserv.setInvPwm(atoi(argv[i]), atoi(argv[i + 1]));
+ i++;
+ break;
+ }
+ }
+ asserv.sync();
+ break;
+ }
+ case 'w':
+ {
+ int stop, t;
+ if (i + 1 >= argc)
+ throw std::runtime_error ("syntax error");
+ stop = atoi (argv[++i]) + Timer::getProgramTime ();
+ t = Timer::getProgramTime ();
+ while (t < stop)
+ {
+ asserv.wait (stop - t);
+ t = Timer::getProgramTime ();
+ asserv.sync();
+ }
+ break;
+ }
+ case '?':
+ syntax ();
+ return 0;
+ default:
+ throw std::runtime_error ("syntax error");
+ }
+ i++;
+ }
+ }
+ catch (const std::exception &e)
+ {
+ std::cerr << e.what () << std::endl;
+ syntax ();
+ return 1;
+ }
+ return 0;
+}