summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/motor/asserv.cc
blob: adb3fb118934a81e994da4a2f70bf6b7acf31c94 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// asserv.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 "asserv.h"
#include "config/config.h"
#include "date/date.h"
#include "utils/hexa.h"

/// Constructeur.
Asserv::Asserv (AsservTracker &asservTracker)
    : ttyspeed_ (0), accel_ (-1), kp_ (-1), ki_ (-1), kd_ (-1),
      statMotor_ (false), counter_ (false),
      inBufSize_ (64), inBufPos_ (0), inBuf_ (0),
      firstCounter_ (true),
      countLeft_ (0), countRight_ (0),
      asservTracker_ (asservTracker),
      log_ ("asserv")
{
    // Lit la conf.
    Config rc ("rc/asserv");
    while (!rc.eof ())
      {
	if (!(
	      rc.get ("accel", accel_) ||
	      rc.get ("kp", kp_) ||
	      rc.get ("ki", ki_) ||
	      rc.get ("kd", kd_) ||
	      rc.get ("tty", ttyname_) ||
	      rc.get ("ttyspeed", ttyspeed_) ||
	      rc.get ("stats", statMotor_) ||
	      rc.get ("counter", counter_) ||
	      rc.get ("buffer", inBufSize_)
	     ))
	    rc.noId ();
      }
    // Ouvre le port s�rie.
    serial_.open (ttyname_.c_str ());
    // Alloue la m�moire du tampon d'entr�e.
    inBuf_ = new char[inBufSize_ + 1];
}

/// Destructeur.
Asserv::~Asserv (void)
{
    // Envois le signal de reset.
    send ('z');
    waitOk ();
    delete inBuf_;
}

/// Reset la carte et envois les param�tres.
void
Asserv::reset (void)
{
    // Reset les donn�es.
    countLeft_ = countRight_ = 0;
    inBufPos_ = 0;
    while (!sendQueue_.empty ()) sendQueue_.pop ();
    // Envois le signal de reset.
    send ('z');
    // Renvois les param�tres.
    if (accel_ != -1)
	setAccel (accel_);
    if (kp_ != -1)
	setKp (kp_);
    if (ki_ != -1)
	setKi (ki_);
    if (kd_ != -1)
	setKi (kd_);
    if (statMotor_)
	setStatMotor (statMotor_);
    if (counter_)
	setCounter (counter_);
}

/// Active l'asservissement.
void
Asserv::go (bool fl)
{
    send ('g', fl);
}

/// Stop !
void
Asserv::stop (void)
{
    send ('s');
}

/// R�glage de la vitesse.
void
Asserv::speed (int l, int r)
{
    send ('v', l, r);
}

/// Teste si l'�mission est termin�e.
bool
Asserv::ok (void)
{
    read ();
    return sendQueue_.empty ();
}

/// Attend que toute les �missions soit termin�es.
void
Asserv::waitOk (void)
{
    while (!ok ()) Date::wait (1);
}

/// Lit et traite les messages de la cartes.
void
Asserv::read (void)
{
    int c;
    while ((c = serial_.getchar ()) != -1)
      {
	if (c == '\n' || c == '\r')
	  {
	    if (inBufPos_)
		// Tampon plein.
		handleMessage ();
	  }
	else
	  {
	    if (c == '!' || inBufPos_ >= inBufSize_)
		inBufPos_ = 0;
	    inBuf_[inBufPos_++] = c;
	  }
      }
}

/// Change les param�tres de la carte.
void
Asserv::setAccel (int accel)
{
    send ('a', accel);
    accel_ = accel;
}

void
Asserv::setKp (int kp)
{
    send ('p', kp);
    kp_ = kp;
}

void
Asserv::setKi (int ki)
{
    send ('i', ki);
    ki_ = ki;
}

void
Asserv::setKd (int kd)
{
    send ('d', kd);
    kd_ = kd;
}

void
Asserv::setStatMotor (bool fl)
{
    send ('m', fl);
    statMotor_ = fl;
}

void
Asserv::setCounter (bool fl = true)
{
    firstCounter_ = true;
    send ('c', fl);
    counter_ = fl;
}

/// Envoie un message.
void
Asserv::send (char com)
{
    bool wasEmpty = ok ();
    std::string s;
    s += '!';
    s += com;
    s += '\r';
    sendQueue_.push (s);
    if (wasEmpty) sendLast ();
}

void
Asserv::send (char com, bool fl)
{
    bool wasEmpty = ok ();
    std::string s;
    s += '!';
    s += com;
    s += fl ? '1' : '0';
    s += '\r';
    sendQueue_.push (s);
    if (wasEmpty) sendLast ();
}

void
Asserv::send (char com, int a1)
{
    bool wasEmpty = ok ();
    std::string s;
    s += '!';
    s += com;
    s += digit2hex ((a1 >> 4) & 0x0f);
    s += digit2hex (a1 & 0x0f);
    s += '\r';
    sendQueue_.push (s);
    if (wasEmpty) sendLast ();
}

void
Asserv::send (char com, int a1, int a2)
{
    bool wasEmpty = ok ();
    std::string s;
    s += '!';
    s += com;
    s += digit2hex ((a1 >> 4) & 0x0f);
    s += digit2hex (a1 & 0x0f);
    s += digit2hex ((a2 >> 4) & 0x0f);
    s += digit2hex (a2 & 0x0f);
    s += '\r';
    sendQueue_.push (s);
    if (wasEmpty) sendLast ();
}

/// Renvois le dernier message.
void
Asserv::sendLast (void)
{
    if (sendQueue_.empty ()) return;
    std::string &s = sendQueue_.front ();
    log_ (Log::debug) << "send " << s << std::endl;
    serial_.write (s.data (), s.size ());
}

/// Traite un message.
void
Asserv::handleMessage (void)
{
    inBuf_[inBufPos_] = 0;
    log_ (Log::debug) << "recv " << inBuf_ << std::endl;
    if (inBufPos_ > 1 && inBuf_[0] == '!')
      {
	switch (inBuf_[1])
	  {
	  case 'o':
	  case 'z':
	    // Ok.
	    if (!sendQueue_.empty ())
		sendQueue_.pop ();
	    sendLast ();
	    break;
	  case 'e':
	    // Erreur, renvois la derni�re commande.
	    sendLast ();
	    break;
	  case 'm':
	    // Recois des nouvelles stats.
	    handleStatMotor ();
	    break;
	  case 'c':
	    // Recois une nouvelle valeur pour les compteurs.
	    handleCounter ();
	    break;
	  }
      }
    // Efface le tampon.
    inBufPos_ = 0;
}

/// Traite un message de stats.
void
Asserv::handleStatMotor (void)
{
    if (inBufPos_ != 2 + 1 + 2 + 1 + 4 + 1 + 4)
      {
	// Mauvaise transmission.
	log_ (Log::warning) << "stat motor error" << std::endl;
	return;
      }
    char side = inBuf_[2];
    int vacc = hexSignedChar2int (inBuf_ + 2 + 1);
    int e = hexSignedShort2int (inBuf_ + 2 + 1 + 2 + 1);
    int pwm = hexSignedShort2int (inBuf_ + 2 + 1 + 2 + 1 + 4 + 1);
    log_ (Log::verydebug) << "stat motor " << side << ' ' << vacc << ' ' << e
	<< ' ' << pwm << std::endl;
}

/// Traite un message du compteur.
void
Asserv::handleCounter (void)
{
    if (inBufPos_ != 2 + 4 + 1 + 4)
      {
	// Mauvaise transmission.
	log_ (Log::warning) << "counter error" << std::endl;
	return;
      }
    int l = hexSignedShort2int (inBuf_ + 2);
    int r = hexSignedShort2int (inBuf_ + 2 + 4 + 1);
    if (firstCounter_)
      {
	// Premi�re valeur ignor�e.
	countLeft_ = l;
	countRight_ = r;
	firstCounter_ = false;
      }
    // Attention � l'overflow.
    log_ (Log::verydebug) << "counter before " << countLeft_ << ' ' <<
	countRight_ << ' ' << l << ' ' << r << std::endl;
    if (l > 0x4000 && countLeft_ < -0x4000)
	countLeft_ += 0x10000;
    if (l < -0x4000 && countLeft_ > 0x4000)
	countLeft_ -= 0x10000;
    if (r > 0x4000 && countRight_ < -0x4000)
	countRight_ += 0x10000;
    if (r < -0x4000 && countRight_ > 0x4000)
	countRight_ -= 0x10000;
    log_ (Log::verydebug) << "counter after " << countLeft_ << ' ' <<
	countRight_ << ' ' << l << ' ' << r << std::endl;
    // Met � jour le tracker.
    asservTracker_.updateCounter (l - countLeft_, r - countRight_);
    log_ (Log::verydebug) << "tracker update " << l - countLeft_ << ' ' << r -
	countRight_ << std::endl;
    // Retiens les anciennes valeurs.
    countLeft_ = l;
    countRight_ = r;
}