summaryrefslogtreecommitdiff
path: root/2004/i/nono/src/qdai/qdai.cc
blob: 684b67e5be7802aad9e28999213475549f59d8d2 (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
// qdai.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 "qdai.h"
#include "motor/movement_types.h"
#include "motor/movement_reposition.h"
#include "date/date.h"

#include <iostream>
#include <cmath>

/// Constructeur.
QdAi::QdAi (void)
    : ana_ (gspp_),
      ga_ (motor_.getAsserv ()),
      gc_ (ga_, gspp_, ga_.getNbIo ()),
      gp_ (gc_),
      v4l_ ("/dev/video", ImageLoader::yuv),
      lastCapture_ (0),
      front_ (135), bigFront_ (182), width_ (260), rear_ (135)
{
}

/// Initialise le robot.
void
QdAi::init (void)
{
    /// Calibre la camera.
    //v4l_.calibrate ();
    /// Asservi les moteurs.
    motor_.go ();
}

/// Arr�te le robot.
void
QdAi::stop (void)
{
    /// Attend l'arret complet de l'appareil.
    motor_.waitStopped ();
}

/// Lance le robot.
int
QdAi::run (void)
{
    int state = 0;
    int oldstate = -1;
    bool stop = false;
    int gpioJack = gp_.getByName ("jack");
    int gpioRoul = gp_.getByName ("roul");
    int gpioRoulS = gp_.getByName ("rouls");
    int gpioContactL = gp_.getByName ("contactl");
    int gpioContactR = gp_.getByName ("contactr");
    int anaSharpL = ana_.getByName ("ana1");
    int anaSharpR = ana_.getByName ("ana0");
    while (!stop && Date::getInstance ().round () < roundDuration_)
      {
	// Mises � jour.
	update ();
	if (oldstate != state)
	  {
	    std::cout << "qdai: " << state << std::endl;
	    oldstate = state;
	  }
	// Automate.
	switch (state)
	  {
	  case 0:
	    // Initialise le matos.
	    gspp_.moveTo (1, 5);
	    std::cout << '\b' << std::endl;
	    if (gp_.get (gpioJack))
		state = 1;
	    break;
	  case 1:
	    // D�but de match.
	    Date::getInstance ().startRound ();
	    gp_.set (gpioRoul, 1);
	    state = 1000;
	    break;
// Parcours balle gratuite.
	  case 1000:
	    // Direction premi�re balle.
	      {
		Goto *g = new GotoSimple (900, 1500);
		Movement *m = new MovementGoto (g);
		motor_.addMovement (m);
		if (!updateToEnd ()) break;
	      }
	    state = 1001;
	    break;
	  case 1001:
	    // Direction en-but.
	      {
		Goto *g = new GotoSimple (1200, 1800);
		Movement *m = new MovementGoto (g);
		motor_.addMovement (m);
		if (!updateToEnd ()) break;
	      }
	      {
		Goto *g = new GotoSimple (2400, 1800);
		Movement *m = new MovementGoto (g);
		motor_.addMovement (m);
		if (!updateToEnd ()) break;
	      }
	    state = 1002;
	    break;
	  case 1002:
	    // Levage benne.
	    gspp_.moveTo (1, 57);
	    // Raprochement en-but.
	      {
		Movement *m = new MovementReposition (0.4, gp_, gpioContactL,
						      gpioContactR, ana_,
						      anaSharpL, anaSharpR);
		motor_.addMovement (m);
		if (!updateToEnd ()) break;
	      }
	    // Recalage du tracker.
	    motor_.getTracker ().adjustX (9 * 300 - front_);
	    motor_.getTracker ().adjustAngle (0);
	    state = 1003;
	    break;
	  case 1003:
	    // D�charge.
	    gp_.reset (gpioRoul);
	    if (!updateWait (200)) break;
	    gp_.set (gpioRoulS, 1);
	    if (!updateWait (200)) break;
	    gp_.set (gpioRoul, 1);
	    if (!updateWait (3000)) break;
	    state = 1004;
	    break;
	  case 1005:
	    // Recule et retourne.
	      {
		Movement *m = new MovementBasic (-1.0, 100);
		motor_.addMovement (m);
		if (!updateToEnd ()) break;
	      }
	      {
		Movement *m = new MovementRotation (-2/3 * M_PI);
		motor_.addMovement (m);
		if (!updateToEnd ()) break;
	      }
	    stop = true;
	    break;
// Parcours d�couverte.
	  case 2010:
	    // Recherche de balle.
	      {
		Movement *m = new MovementRoundCruise (0.1);
		motor_.addMovement (m);
	      }
	    // Attendre de trouver une balle.
	    state = 2020;
	    break;
	  case 2020:
	    // Trackage de balle.
	      {
	      }
	    state = 2021;
	    break;
	  case 2021:
	    // Gobage de la balle.
	      {
		Movement *m = new MovementBasic (0.7, 300);
		motor_.addMovement (m);
		updateToEnd ();
	      }
	    state = 2030;
	    break;
	  case 2030:
	    // Goto zone d'essai.
	      {
		// Goto pr�s de la zone d'essais.
		  {
		    Goto *g = new GotoSimple (8 * 300, 450);
		    Movement *m = new MovementGoto (g);
		    motor_.addMovement (m);
		    updateToEnd ();
		  }
		// Rotation vers la zone d'essais.
		  {
		    Movement *m = new MovementRotation (0.0);
		    motor_.addMovement (m);
		    updateToEnd ();
		  }
		// Goto la zone d'essais.
		  {
		    Goto *g = new GotoSimple (8 * 300 + 15, 450);
		    Movement *m = new MovementGoto (g);
		    motor_.addMovement (m);
		    updateToEnd ();
		  }
	      }
	    state = 2040;
	    break;
	  case 2040:
	    // Essai.
	      {
		stop = true;
		// Lever la benne.
		// Sortir les balles.
		// Baisser la benne.
	      }
	    break;
	  default:
	    std::cout << "qdai: unknown state" << std::endl;
	  }
      }
    return 0;
}

/// Mises � jour p�riodiques.
void
QdAi::update (void)
{
    // Mise � jour moteur.
    motor_.waitOk ();
    // Mise � jour capteurs.
    gspp_.update ();
    // Mise � jour camera.
    if (lastCapture_ + captureInterval_ < Date::getInstance ().start ())
      {
      }
}

/// Mises � jour jusqu'� fin du mouvement en cours. Renvois faux � la fin
/// du match.
bool
QdAi::updateToEnd (void)
{
    while (!motor_.end ())
      {
	if (Date::getInstance ().round () >= roundDuration_)
	    return false;
	update ();
	Date::wait (50);
      }
    return true;
}

/// Mises � jour jusqu'� timeout. Renvois faux � la fin du match.
bool
QdAi::updateWait (int t)
{
    int start;
    start = Date::getInstance ().start ();
    while (Date::getInstance ().start () < start + t)
      {
	if (Date::getInstance ().round () >= roundDuration_)
	    return false;
	update ();
	Date::wait (50);
      }
    return true;
}

int
main (int argc, char **argv)
{
    QdAi qdai_;
    qdai_.init ();
    qdai_.run ();
    qdai_.stop ();
}