summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/motor/motor.cc
blob: ddd00ce4d0793cdd665b1870a237c3d219f2ffac (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
// motor.cc
// nono - programme du robot 2004. {{{
//
// Copyright (C) 2003 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 "motor.h"
#include "config/config.h"

#include <math.h>

// Pointeur vers l'instance unique.
Motor *Motor::instance_ = 0;

/// Constructeur.
Motor::Motor (void)
    : asserv_ (*this), log_ ("motor"),
      s_ (1.0), vs_ (1.0), a_ (1.0),
      brakeDistance_ (0.0),
      movement_ (0), maxSpeed_ (0x40), minSpeed_ (0x04)
{
    // M�morise le pointeur d'instance.
    instance_ = this;
    // Lit la conf.
    Config rc ("rc/motor");
    while (!rc.eof ())
      {
	if (!(
	      rc.get ("scale", s_) ||
	      rc.get ("maxspeed", maxSpeed_) ||
	      rc.get ("minspeed", minSpeed_)
	     ))
	    rc.noId ();
      }
    // Initialise l'�chelle.
    vs_ = s_ * asserv_.getSpeedFactor ();
    a_ = (s_ > 0 ? s_ : -s_) * asserv_.getAccelFactor ();
    brakeDistance_ = 0.5 * (maxSpeed_ * maxSpeed_ * vs_ * vs_) / a_;
    log_ (Log::debug) << "scale " << vs_ << ' ' << a_ << ' ' << brakeDistance_
	<< std::endl;
    // C'est partit !
    asserv_.reset ();
}

/// Destructeur.
Motor::~Motor (void)
{
    instance_ = 0;
    delete movement_;
}

/// Appel�e lors d'une mise � jour des compteurs.
/// l, r : mise � jour moteur gauche et droit (unit).
/// zero : vrai si le mouvement est � consid�rer comme nul.
void
Motor::updateCounter (int l, int r, bool zero)
{
    double dL = l * s_;
    double dR = r * s_;
    tracker_.update (dL, dR, zero);
    if (movement_)
	if (!movement_->control ())
	  {
	    delete movement_;
	    movement_ = 0;
	    asserv_.stop ();
	  }
}

/// Choisi le mouvement en cours.
void
Motor::setMovement (Movement *m)
{
    delete movement_;
    movement_ = m;
    m->init (tracker_, asserv_, *this);
}

/// Teste si les mouvements sont termin�s.
bool
Motor::end (void)
{
    ok ();
    return !movement_;
}

/// Attend la fin de tout les mouvements.
void
Motor::waitEnd (void)
{
    do {
	waitOk ();
      } while (!end ());
}

/// Teste si les moteurs sont arr�t�s.
bool
Motor::stopped (void)
{
    ok ();
    return tracker_.stopped ();
}

/// Attend que les moteurs soient arr�t�s.
void
Motor::waitStopped (void)
{
    do {
	waitOk ();
      } while (!stopped ());
}

/// Param�tre la vitesse lin�aire (-1..+1) et angulaire (-1..+1). Limite
/// la vitesse pour pouvoir freiner au bout de dist (mm).
void
Motor::speed (double l, double a, double dist)
{
    speedLimit (l - a,  l + a, computeSpeed (dist));
}

/// Param�tre la vitesse des moteurs (-1..+1).
void
Motor::speed (double l, double r)
{
    speedLimit (l, r, maxSpeed_);
}

/// Param�tre la vitesse des moteurs (-1..+1) avec une vitesse limite
/// (unit).
void
Motor::speedLimit (double l, double r, double vLimit)
{
    // Il faut un minimum tout de m�me. La politique, c'est on avance.
    if (vLimit < minSpeed_)
	vLimit = minSpeed_;
    // Convertie le (-1..+1) en (unit).
    int vL = (int) ((vs_ > 0 ? l : -l) * maxSpeed_ * 0.5);
    int vR = (int) ((vs_ > 0 ? r : -r) * maxSpeed_ * 0.5);
    // Teste le d�passement de vitesse minimale.
    while ((l != 0.0 || r != 0.0) && vL > -minSpeed_ && vL < minSpeed_ && vR >
	   -minSpeed_ && vR < minSpeed_)
      {
	l *= 1.5;
	r *= 1.5;
	vL = (int) ((vs_ > 0 ? l : -l) * maxSpeed_ * 0.5);
	vR = (int) ((vs_ > 0 ? r : -r) * maxSpeed_ * 0.5);
      }
    // Teste le d�passement de vitesse maximal.
    if (vL > vLimit)
      {
	vL = static_cast<int> (vLimit);
	vR = (int) (r / l * vL);
      }
    else if (vL < -vLimit)
      {
	vL = static_cast<int> (-vLimit);
	vR = (int) (r / l * vL);
      }
    if (vR > vLimit)
      {
	vR = static_cast<int> (vLimit);
	vL = (int) (l / r * vR);
      }
    else if (vR < -vLimit)
      {
	vR = static_cast<int> (-vLimit);
	vL = (int) (l / r * vR);
      }
    // Envois la commande.
    log_ (Log::debug) << "speed " << l << ' ' << r << ' ' << vL << ' '
	<< vR << ' ' << vLimit << std::endl;;
    asserv_.speed (vL, vR);
}

/// Calcule la vitesse (unit) en ligne droit afin de pouvoir freiner au bout
/// d'une distance (mm).
double
Motor::computeSpeed (double dist) const
{
    if (dist <= 0.0)
	return 0.0;
    if (dist > brakeDistance_)
	return maxSpeed_;
    else
	return sqrt (2.0 * dist * a_) / (vs_ > 0 ? vs_ : -vs_);
}

/// Va vers un point, en formant un arc de cercle, renvois faux si atteind.
/// \deprecated Utiliser goTo().
bool
Motor::goToArc (double x, double y, double eps/*5.0*/)
{
    double l, r;
    if (tracker_.computeArcs (x, y, l, r, eps))
      {
	log_ (Log::debug) << "goto arc " << l << ' ' << r;
	if (l > 0.0)
	    l = sqrt (2.0 * a_ * l);
	else
	    l = -sqrt (2.0 * a_ * -l);
	if (r > 0.0)
	    r = sqrt (2.0 * a_ * r);
	else
	    r = -sqrt (2.0 * a_ * -r);
	log_ << ' ' << l << ' ' << r << std::endl;
	speed (l, r);
	return true;
      }
    else
      {
	asserv_.stop ();
	return false;
      }
}