summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/motor/tracker.cc
blob: 4bd0e7b59fd84d530a3ee66f2463b59d60dde3f0 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
230
231
232
233
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
278
279
280
281
282
// tracker.cc
// nono - programme du robot 2004. {{{
//
// Copyright (C) 2004 Nicolas Schodet
//
// Robot APB Team/Efrei 2004.
//        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 "tracker.h"
#include "config/config.h"
#include "utils/mathutil.h"

/// Constructeur.
Tracker::Tracker (void)
    : posX_ (0.0), posY_ (0.0), angle_ (0.0),
      f_ (10.0), zero_ (0), log_ ("tracker")
{
    // Lit la conf.
    Config rc ("rc/tracker");
    while (!rc.eof ())
      {
	if (!(
	      rc.get ("startx", posX_) ||
	      rc.get ("starty", posY_) ||
	      rc.get ("startangle", angle_) ||
	      rc.get ("footing", f_)
	     ))
	    rc.noId ();
      }
}

/// Destructeur.
Tracker::~Tracker (void)
{
}

/// Set the position.
void
Tracker::setPos (double x, double y, double angle)
{
    posX_ = x;
    posY_ = y;
    angle_ = angle;
}

/// Get the position.
void
Tracker::getPos (double &x, double &y, double &angle) const
{
    x = posX_;
    y = posY_;
    angle = angle_;
}

/// R�cup�re l'angle entre l'angle courant et a normalis� entre -pi et pi.
double
Tracker::getAngleDiff (double a) const
{
    return angleNorm (angle_ - a);
}

/// R�cup�re la distance � parcourir avec chaque roue pour parcourir un angle.
double
Tracker::getAngleDistance (double angleDiff) const
{
    return 0.5 * f_ * fabs (angleDiff);
}

/// R�cup�re les coordonn�es d'un point � moins de dist (mm), dans la
/// direction de (dx, dy).
void
Tracker::getPoint (double dx, double dy, double &x, double &y, double dist)
    const
{
    // Calcule le delta.
    double dtx = dx - posX_;
    double dty = dy - posY_;
    // Test si trops loin.
    double sqd = dtx * dtx + dty * dty;
    if (sqd > dist * dist)
      {
	x = posX_ + dtx * dist / sqrt (sqd);
	y = posY_ + dty * dist / sqrt (sqd);
      }
    else
      {
	x = dx;
	y = dy;
      }
}

/// Calcule l'erreur lin�aire et angulaire pour atteindre un point.
/// (x, y) : point � atteindre (mm).
/// el, ea : erreur lin�aire et angulaire (-1..+1).
/// a : angle.
void
Tracker::computeError (double x, double y, double &el, double &ea, double &a)
    const
{
    // Calcule la diff�rence avec la cible voulue.
    double eX = x - posX_;
    double eY = y - posY_;
    // Calcule la distance.
    double d = sqrt (eX * eX + eY * eY);
    // Ram�ne sur -1..+1
    eX /= d;
    eY /= d;
    // Calcule sin et cos.
    double sa = sin (angle_);
    double ca = cos (angle_);
    // Calcule l'erreur lin�aire (projection sur la direction courante).
    el = eX * ca + eY * sa;
    // Calcule l'erreur angulaire (projection sur la direction
    // perpendiculaire).
    ea = eX * -sa + eY * ca;
    // Calcule l'angle.
    a = ea > 0.0 ? acos (el) : -acos (el);
}

/// Calcule l'erreur angulaire pour atteindre un angle, retourne faux si
/// atteind.
/// a : angle � atteindre (rad).
/// ea : erreur angulaire (-1..+1).
/// eps : angle en dessous duquel on consid�re qu'on est arriv� (rad).
bool
Tracker::computeAngleError (double a, double &ea, double eps) const
{
    ea = getAngleDiff (a);
    if (-eps < ea && ea < eps)
      {
	ea = 0.0;
	return false;
      }
    ea *= 1.0 / M_PI;
    return true;
}

/// Calcule la longueur de l'arc gauche et droite pour atteindre un point.
/// EPS pr�cise la distance minimale. Renvois faux si atteind.
/// \deprecated Pas vraiment l'algo qui faut.
bool
Tracker::computeArcs (double x, double y, double &l, double &r, double eps)
    const
{
    // Vecteur vers le point � atteindre.
    double dX = x - posX_, dY = y - posY_;
    // Calcule la distance au point (x, y).
    double d = sqrt (dX * dX + dY * dY);
    if (d < eps)
      {
	l = r = 0.0;
	log_ (Log::verydebug) << "compute arc eps" << std::endl;
	return false;
      }
    // Calcule l'angle entre la direction courante et la direction du point �
    // atteindre, �vite d'utiliser un atan.
    //  Calcule le vecteur perpendiculaire au vecteur normal selon la
    //  direction courante.
    double vX = -sin (angle_);
    double vY = cos (angle_);
    //  Calcul de l'angle devant/deriere.
    if (dX * vY + dY * -vX < 0.0)
      {
	l = r = 0;
	log_ (Log::verydebug) << "compute arc back" << std::endl;
	return false;
      }
    //  Calcul de l'angle par produit scalaire.
    double s = dX * vX + dY * vY;
    double dA = asin (s / d);	// sin (a) = cos (a + pi/2)
    log_ (Log::verydebug) << "compute arc dA " << dA << " d " << d <<
	std::endl;
    // Si l'angle est trops petit, on va tout droit, sinon /0.
    if (dA > -0.0001 && dA < 0.0001)
      {
	l = r = d;
	log_ (Log::verydebug) << "compute arc small angle" << std::endl;
      }
    // Si l'angle est trops grand, on ne fait pas le tour du monde.
    else if (dA > M_PI_2 || dA < -M_PI_2)
      {
	l = r = 0;
	log_ (Log::verydebug) << "compute arc big angle" << std::endl;
	return false;
      }
    else
      {
	// Calcule le rayon de courbure � suivre.
	double ro = 0.5 * d / sin (dA);	// sin (dA) = cos (pi/2 - dA)
	// Rayon plus petit que l'empatement/2, stop.
	if (ro < 0.25 * f_ && ro > 0.25 * f_)
	  {
	    l = r = 0.0;
	    log_ (Log::verydebug) << "compute arc small ro " << ro <<
		std::endl;
	    return false;
	  }
	// L'angle du d�placement est le m�me que dA.
	l = (ro - 0.5 * f_) * dA;
	r = (ro + 0.5 * f_) * dA;
	log_ (Log::verydebug) << "compute arc ro " << ro << std::endl;
      }
    return true;
}

/// Met � jour la position.
void
Tracker::update (double dL, double dR, bool zero)
{
    // Compte les zeros.
    if (zero)
      {
	zero_++;
      }
    else if (dL == 0.0 && dR == 0.0)
      {
	zero_++;
	return;
      }
    else
      {
	zero_ = 0;
      }
    // Calcule l'angle et l'avancement moyen.
    // Avec a petit (c'est le cas, car f_ >> abs (dR - dL)), a ~= atan (a).
    double dA = (dR - dL) / f_;
    double dS = 0.5 * (dL + dR);
    log_ (Log::verydebug) << "update dL " << dL << " dR " << dR << " dA " <<
	dA << " dS " << dS << std::endl;
    // Si l'angle est petit, �vite une division par presque 0.
    if (dA < 0.0001 && dA > -0.0001)
      {
	// Consid�re que l'on avance en ligne droite selon la moiti� de
	// l'angle.
	double a = angle_ + 0.5 * dA;
	posX_ += dS * cos (a);
	posY_ += dS * sin (a);
	angle_ += dA;
	log_ (Log::verydebug) << "small dX " << dS * cos (a) << " dY "
	    << dS * sin (a) << std::endl;
      }
    else
      {
	// dS : arc de cercle parcouru (gr�ce � Thales).
	// R : rayon, dS = dA * R, R = dS / dA
	// On a plus qu'� calculer les dX, dY avec cos et sin.
	double oldA = angle_;
	angle_ += dA;
	posX_ += (sin (angle_) - sin (oldA)) * dS / dA;
	posY_ += (cos (oldA) - cos (angle_)) * dS / dA;
	log_ (Log::verydebug) << "big dX " <<
	    -(sin (oldA) - sin (angle_)) * dS / dA <<
	    " dY " << -(cos (oldA) - cos (angle_)) * dS / dA <<
	    " R " << dS / dA << std::endl;
      }
    log_ (Log::info) << "update pos " << *this << std::endl;
}

/// Affiche la position.
std::ostream &
operator<< (std::ostream &os, const Tracker &t)
{
    os << t.getX () << ' ' << t.getY () << ' ' << t.getAngle ();
    return os;
}