summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/asserv/asserv.hh
blob: 0df1b214ca0a1a581371e65bec5fc71b6c49e836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
105
106
107
108
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
#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:
		/// 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
	double kp_; 
	double ki_;
	double kd_;
	int speedIntMax_;
	int maxSpeed_; //unit� AVR
	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);
	/// 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 = 0, double ySpeed = 0);
	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 (double kp);
	void setKi (double ki);
	void setKd (double kd);
	void setSpeedIntMax (int maxInt);
	void setAccel (int accel);
	double getAccel (void);
	void setMaxSpeed(int maxSpeed);
	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