summaryrefslogtreecommitdiff
path: root/i/marvin/src/asserv/asserv.cc
blob: 48c2ad51cacdfc80f6ffa4ce4860a6c8ab24ab0f (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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// asserv.cc
// marvin - programme du robot 2006. {{{
//
// Copyright (C) 2006 Nicolas Schodet
//
// Robot APB Team/Efrei 2006.
//        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"

/// Constructor.
Asserv::Asserv (Asserv::Receiver &receiver)
    : proto_ (*this), receiver_ (receiver)
{
    // Get Config
    Config &config = Config::getInstance ();
    intervalCounterStat_ = config.get<int> ("asserv.counter_stat", 0);
    intervalPos_ = config.get<int> ("asserv.pos_report");
    intervalSpeedStat_ = config.get<int> ("asserv.speed_stat", 0);
    intervalPosStat_ = config.get<int> ("asserv.pos_stat", 0);
    intervalPwmStat_ = config.get<int> ("asserv.pwm_stat", 0);
    intervalTimerStat_ = config.get<int> ("asserv.timer_stat", 0);
    intervalInPort_ = config.get<int> ("asserv.in_port_report");
    footing_ = config.get<int> ("asserv.footing");
    tAccel_ = config.get<int> ("asserv.t_accel");
    aAccel_ = config.get<int> ("asserv.a_accel");
    tMaxSpeed_ = config.get<int> ("asserv.t_max_speed");
    aMaxSpeed_ = config.get<int> ("asserv.a_max_speed");
    tMaxSpeedSlow_ = config.get<int> ("asserv.t_max_speed_slow");
    aMaxSpeedSlow_ = config.get<int> ("asserv.a_max_speed_slow");
    tkp_ = config.get<int> ("asserv.tkp");
    tki_ = config.get<int> ("asserv.tki");
    tkd_ = config.get<int> ("asserv.tkd");
    akp_ = config.get<int> ("asserv.akp");
    aki_ = config.get<int> ("asserv.aki");
    akd_ = config.get<int> ("asserv.akd");
    esat_ = config.get<int> ("asserv.esat");
    isat_ = config.get<int> ("asserv.isat");
    lInvertPwm_ = config.get<bool> ("asserv.l_invert_pwm");
    rInvertPwm_ = config.get<bool> ("asserv.r_invert_pwm");
    stepPerMm_ = config.get<double> ("asserv.step_per_mm");
    tty_ = config.get<std::string> ("asserv.tty");
    proto_.open (tty_);
    reset ();
}

/// 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/*-1*/)
{
    return proto_.wait (timeout);
}

/// R�cup�re le File Descriptor
int
Asserv::getFd (void) const
{
    return proto_.getFd ();
}

/// Reset the board and send parameters.
void
Asserv::reset (void)
{
    proto_.send ('z');
    setIntervalCounterStat (intervalCounterStat_);
    setIntervalPos (intervalPos_);
    setIntervalSpeedStat (intervalSpeedStat_);
    setIntervalPosStat (intervalPosStat_);
    setIntervalPwmStat (intervalPwmStat_);
    setIntervalTimerStat (intervalTimerStat_);
    setIntervalInPort (intervalInPort_);
    setFooting (footing_);
    setAccel (tAccel_, aAccel_);
    setMaxSpeed (tMaxSpeed_, aMaxSpeed_, tMaxSpeedSlow_, aMaxSpeedSlow_);
    setCoef (tkp_, tki_, tkd_, akp_, akd_, akd_, esat_, isat_);
    setPwmDir (lInvertPwm_, rInvertPwm_);
}

/// Set PWM.
void
Asserv::pwm (int l, int r)
{
    proto_.send ('w', "ww", l, r);
}

/// Position consign offset.
void
Asserv::offset (double t, double a)
{
    proto_.send ('c', "ww", mmToStep (t), mmToStep (a));
}

/// Set speed.
void
Asserv::speed (int t, int a)
{
    proto_.send ('s', "bb", t, a);
}

/// Speed controlled position consign offset.
void
Asserv::speedTo (double t, double a, int seq)
{
    proto_.send ('s', "ddb", mmToStep (t), mmToStep (a), seq);
}

/// Speed controlled angle consign offset.
void
Asserv::speedAngle (double a, int seq)
{
    proto_.send ('s', "ddb", 0, radToStep (a), seq);
}

/// Find a hole.
void
Asserv::findHole (int seq)
{
    proto_.send ('h', "b", seq);
}

/// Acknoledge.
void
Asserv::ack (int seq)
{
    proto_.send ('a', "b", seq);
}

/// Change counter stat interval.
void
Asserv::setIntervalCounterStat (int i)
{
    intervalCounterStat_ = i;
    proto_.send ('C', "b", i);
}

/// Change position report interval.
void
Asserv::setIntervalPos (int i)
{
    intervalPos_ = i;
    proto_.send ('X', "b", i);
}

/// Change speed stat interval.
void
Asserv::setIntervalSpeedStat (int i)
{
    intervalSpeedStat_ = i;
    proto_.send ('S', "b", i);
}

/// Change position control stat interval.
void
Asserv::setIntervalPosStat (int i)
{
    intervalPosStat_ = i;
    proto_.send ('P', "b", i);
}

/// Change pwm stat interval.
void
Asserv::setIntervalPwmStat (int i)
{
    intervalPwmStat_ = i;
    proto_.send ('W', "b", i);
}

/// Change timer stat interval.
void
Asserv::setIntervalTimerStat (int i)
{
    intervalTimerStat_ = i;
    proto_.send ('T', "b", i);
}

/// Change input port report interval.
void
Asserv::setIntervalInPort (int i)
{
    intervalInPort_ = i;
    proto_.send ('I', "b", i);
}

/// Set current position.
void
Asserv::setPos (double x, double y, double a)
{
    setXPos (x);
    setYPos (y);
    setAngle (a);
}

/// Set current x position.
void
Asserv::setXPos (double x)
{
    proto_.send ('p', "bd", 'X', mmToStep (x));
}

/// Set current y position.
void
Asserv::setYPos (double y)
{
    proto_.send ('p', "bd", 'Y', mmToStep (y));
}

/// Set current angle.
void
Asserv::setAngle (double a)
{
    proto_.send ('p', "bd", 'A', radToAvr (a));
}

/// Set footing.
void
Asserv::setFooting (int f)
{
    footing_ = f;
    proto_.send ('p', "bw", 'f', f);
}

/// Set acceleration.
void
Asserv::setAccel (int t, int a)
{
    tAccel_ = t;
    aAccel_ = a;
    proto_.send ('p', "bww", 'a', t, a);
}

/// Set maximum speed for automatic movements.
void
Asserv::setMaxSpeed (int t, int a, int ts, int as)
{
    tMaxSpeed_ = t;
    aMaxSpeed_ = a;
    tMaxSpeedSlow_ = ts;
    aMaxSpeedSlow_ = as;
    proto_.send ('p', "bbbbb", 's', t, a, ts, as);
}

/// Set motor control coeficients.
void
Asserv::setCoef (int tkp, int tki, int tkd, int akp, int aki, int akd,
		 int esat, int isat)
{
    tkp_ = tkp;
    tki_ = tki;
    tkd_ = tkd;
    akp_ = akp;
    aki_ = aki;
    akd_ = akd;
    esat_ = esat;
    isat_ = isat;
    proto_.send ('p', "bww", 'p', tkp, akp);
    proto_.send ('p', "bww", 'i', tki, aki);
    proto_.send ('p', "bww", 'd', tkd, akd);
    proto_.send ('p', "bw", 'E', esat);
    proto_.send ('p', "bw", 'I', isat);
}

/// Set PWM direction.
void
Asserv::setPwmDir (bool invertL, bool invertR)
{
    lInvertPwm_ = invertL;
    rInvertPwm_ = invertR;
    proto_.send ('p', "bbb", 'w', invertL ? 1 : 0, invertR ? 1 : 0);
}

/// Store to eeprom.
void
Asserv::storeParams (void)
{
    proto_.send ('p', "bb", 'E', 1);
}

/// Clear eeprom.
void
Asserv::clearParams (void)
{
    proto_.send ('p', "bb", 'E', 0);
}

/// Impl�mentation du proto::Receiver.
void
Asserv::receive (char command, const Proto::Frame &frame)
{
    int seq, l, r, x, y, a, t, te, ti, ae, ai, port;
    switch (command)
      {
      case 'A':
	if (proto_.decode (frame, "b", seq))
	    receiver_.receiveAck (seq);
	break;
      case 'C':
	if (proto_.decode (frame, "ww", l, r))
	    receiver_.receiveCounterStat (l, r);
	break;
      case 'X':
	if (proto_.decode (frame, "DDD", x, y, a))
	    receiver_.receivePos (stepToMm (x / 256), stepToMm (y / 256), avrToRad (a));
	break;
      case 'S':
	if (proto_.decode (frame, "BB", t, a))
	    receiver_.receiveSpeedStat (t, a);
	break;
      case 'P':
	if (proto_.decode (frame, "WWWW", te, ti, ae, ai))
	    receiver_.receivePosStat (te, ti, ae, ai);
	break;
      case 'W':
	if (proto_.decode (frame, "WW", l, r))
	    receiver_.receivePwmStat (l, r);
	break;
      case 'T':
	// TODO (si on a le temps).
	break;
      case 'I':
	if (proto_.decode (frame, "w", port))
	    receiver_.receiveInPort (port);
	break;
      }
}

/// Convert mm to steps.
int
Asserv::mmToStep (double mm) const
{
    return static_cast<int> (mm * stepPerMm_);
}

/// Convert steps to mm.
double
Asserv::stepToMm (int step) const
{
    return static_cast<double> (step) / stepPerMm_;
}

/// Convert rad to avr angles.
int
Asserv::radToAvr (double a) const
{
    return static_cast<int> (a * M_1_PI * 0.5 * (1 << 24)) & 0xffffff;
}

/// Convert avr angles to rad.
double
Asserv::avrToRad (int a) const
{
    return static_cast<double> (a) * 2 * M_PI / (1 << 24);
}

/// Convert rad to steps.
int
Asserv::radToStep (double a) const
{
    return static_cast<int> (static_cast<double> (footing_) * a * 0.5);
}