summaryrefslogtreecommitdiff
path: root/2005/i/robert/src/ai/ai.cc
blob: 8762475591f48ac5697b0b0e57ae721419b91fcb (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
// ai.cc
// robert - programme du robot 2005. {{{
//
// Copyright (C) 2005 Nicolas Haller
//
// Robot APB Team/Efrei 2005.
//        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 "ai.hh"
#include "config/config.hh"
#include "timer/timer.hh"
#include "ovision/see/ovision.hh"

static void
callback (void)
{
}

/// Constructeur
Ai::Ai(const Config & config)
    :motor_(config), es_(config), 
     schedulableMotor_ (callback, motor_.getFd ()),
     schedulableEs_ (callback, es_.getFd ()),
     roundDuration_(config.get<int>("ai.roundDuration")),
     vitesseAsc_(config.get<int>("ai.vitesseAsc"))
{
    scheduler_.insert (schedulableMotor_);
    scheduler_.insert (schedulableEs_);
}

/// Initialise le robot
void Ai::init(void)
{
    // Initialise les moteurs
    motor_.init();
    // initialise la carte es
    es_.init();
    // on init la vision
    oVision_.init(motor_.colorState() ? Group::redSkittle : 
	    			   Group::greenSkittle); /// XXX
}

/// stop le robot
void Ai::stop(void)
{
    // Stop les moteurs
    motor_.stop();
}

/// Lance le robot
void Ai::run(void)
{
    // Attend l'entr�e du jack "pret � initialiser"
    waitJack(false);
    // Attend la premi�re sortie du jack(init)
    waitJack(true);
    //Initialise le robot
    init();
    // On recule dans le mur
    recale ();
    // On avance vers la position de d�part en y
    basic (155); // 215 - 60 = 155
    // On rotate de 90 �
    rotation (0);
    // On se recale vers x
    recale();
    // Attend le jack "pret � partir"
    waitJack(false);
    // Attend la seconde sortie du jack(Daniel)
    waitJack(true);
    // on lance le temps
    Timer::startRound();
    // On temporise pour laisser Taz sortir
    temporisation(500);
    // On avance pour pouvoir aller vers la boule parall�lement au socle
    basic(540); // 600 - 60
    // Aller devant la boule
    goTo(1200, -975, M_PI);
    // Taper la boule
    basic(-220);
    // se placer devant nos quilles pr�s du pont
    basic(220);
    // On va deri�re les quilles
    goTo(motor_.getX(),motor_.getY() - 150 ,M_PI); /// Mauvaise valeur, changer valeur
    // On set la vitesse � 2
    int MaxLSpeedConf = motor_.getMaxLSpeed();
    motor_.setMaxSpeed(2, -1);
    // On colle les quilles
    basic (-250); // 260 - 10 de marge 
    // On retaure la vitesse
    motor_.setMaxSpeed(MaxLSpeedConf, -1);
    // On temporise....
    temporisation(200);  // XXX Mauvaise valeur, changer valeur
}

/// Attend.
void Ai::wait (int t)
{
    motor_.wait(t);
    throw std::runtime_error("nan faut pas utiliser");
}

/// Fonction de temporisation
void Ai::temporisation(int t)
{
    int time = Timer::getRoundTime();
    time += t;
    while (time > Timer::getRoundTime())
    {
	update();
    }
}

// Attend le jack entr� (false) ou sorti (true).
void Ai::waitJack (bool out)
{
    while (motor_.jackState() != out)
	update();
}

/// Rejoint un point. (argument en mm)
void Ai::goTo (double x, double y ,double a)
{
    motor_.goTo(x,y,a);
    do
    {
	update();
    }while(!motor_.idle());
}

/// Recale contre une bordure.
// XXX Voir ca plus pr�cisemment
void Ai::recale (void)
{
    motor_.recalage();
    do
    {
	update();
    }
    while(!motor_.idle());
    double angle = motor_.getA();
    if(angle > M_PI / 4 && angle < (3 * M_PI) / 4) // Si on point vers la droite
    {
	motor_.setPosition(motor_.getX(), -60, -M_PI / 2);
    }
    else if (angle < M_PI / 4 || angle > (7 * M_PI) / 4) // Si on pointe vers 0
    {
	motor_.setPosition(60, motor_.getY(), 0);
    }
    else // On pointe vers y
    {
	motor_.setPosition(motor_.getX(), -2040, 0);
    }
}

/// Mouvement basic.
void Ai::basic (double d)
{
    motor_.linearMove(d);
    do
    {
	update(); // XXX Et si on se prend un mur?????
    }
    while (!motor_.idle());
}

/// Rotation (argument en radian)
void Ai::rotation (double a)
{
    motor_.rotation(a);
    do
    {
	update();
    }while (!motor_.idle()); // XXX G�rer le cas d'une rotation trop petite
}

/// Monte(vrai) ou descend(faux) l'ascenceur
void Ai::ascenceur (bool monte)
{
    if (monte)
	es_.monterAsc();
    else
	es_.descendreAsc();
    do
    {
	update();
    }
    while(!es_.ascIsIdle());
}

/// D�sactive les ventouse
void Ai::ventouses (void) /// XXX Temps � r�gler dans la config
{
    es_.ventouses();
    update();
}

bool Ai::update (void)
{
   scheduler_.schedule();
   motor_.sync(); 
   es_.sync();
   // Gestion des cas critiques
   /// XXX
   
   return true;
}