summaryrefslogtreecommitdiff
path: root/i/marvin/src/ai/ai.cc
blob: d9bfd0a19bdb68dcb688b3359437356e664fd59a (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
// AI.CC
// marvin - programme du robot 2006. {{{
//
// Copyright (C) 2006 Nicolas Haller
//
// 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.
//
// Contact :
//        Web: http://perso.efrei.fr/~haller/
//      Email: <nicolas@boiteameuh.org>
// }}}
#include "ai.hh"
#include "config/config.hh"
#include "timer/timer.hh"

/// Fonction callback useless
static void
callback (void)
{}

//constructeur
Ai::Ai (const Config & config)
    :es_(config), log_ ("Ai"),
     schedulableMotor_(callback, motor_.getFd()),
     schedulableEs_(callback, es_.getFd()),
     round_duration_ (config.get<int> ("ai.round_duration")),
     schedule_time_ (config.get<int> ("ai.schedule_time")),
     reference_sensor_mask_ (config.get<int> ("ai.ref_sensor_mask"))
{
    scheduler_.insert (schedulableMotor_);
    scheduler_.insert (schedulableEs_);
}

Ai::~Ai(void)
{
    // On r�initialise
    // Initialise les moteurs
    // XXX
    motor_.reset  ();
    // initialise la carte es
    es_.reset ();
    // On sync
    do
      {
	scheduler_.schedule (500, true);
      }
    while (!sync ());
}

// Initialisation du robal
void
Ai::init (void)
{
    // Initialisation de motor
    motor_.reset ();
    // Initialisation de la carte Es
    es_.reset ();
    // Paf on sync
    while (!update ());
}

/// Stop the mouvement of the motor
void
Ai::stop (void)
{
    motor_.stop ();
    // XXX In fact, we should wait a little here...
    while (!update ());
}

/// Synchronize data of under class
bool
Ai::sync (void)
{
    return es_.sync () && motor_.sync ();
}

/// Wait for something to happened
bool
Ai::update (void)
{
    /// Wait schedule_time 
    scheduler_.schedule (schedule_time_, true);
    bool retour = sync ();
    // On v�rifie que le match n'est pas fini
    if (Timer::getRoundTime () > round_duration_)
	throw std::runtime_error ("End of match ! You win !");
    return retour;
}

/// Function to wait a little but still syncing data
void
Ai::temporisation(int msec)
{
    // Get current time of process
    int time = Timer::getProgramTime();
    time += msec;
    // Update until we have spend enough time
    do
    {
        update();
    }
    while (time > Timer::getProgramTime());
}

/// Wait until jack is out (true) or in (false)
void
Ai::waitJack (bool out)
{
    do
      {
	update ();
      }
    while (es_.isJackOut () != out);
}

/// Init things for a match.
void
Ai::prepare (void)
{
    // XXX We should check if the jack is not already in
    // We first wait for the jack to be put inside
    waitJack (false);
    // We reference all the color
    referenceSensors ();
    // We first wait for the jack to be put inside
    waitJack (true);
    // Ok the match begin ! Go go go !
    Timer::startRound ();
}

/// Reference sensors
void
Ai::referenceSensors (void)
{
    // Reference sensors
    es_.refColor (reference_sensor_mask_);
    // Update data
    while (!update ());
    // Disable capturing all sensors
    es_.enableAllSensors (false);
    // Update data
    while (!update ());
}

/// programme d'homologation du robal
void Ai::progHomoloRobal(void)
{
    /// On init le robal
    prepare();
    /// PAF BEGIN!!! (lancer le timer)
    Timer::startRound();
    /// on avance un poil parce que le mur c'est mal
    motor_.move(400, 0);
    /// On tourne XXX
    motor_.rotate(-M_PI/4);
    /// On cherche au moins une balle (sinon c'est con)
    es_.barilletLancement();
    motor_.move(1000, 0); /// XXX Voir la distance
    /// XXX Avancer d'une certaine facon pour choper des balles
    /// XXX Ouais ba y'a encore ddes truc � faire l�
    /// On cherche un tru (repositionnement??)
    motor_.lockGoodHole();
    motor_.findHole();
    /// On trouve un trou, chouette
    /// Aspirer le trou
    es_.extraitBalle();
    /// mettre une baballe
    es_.dropWhiteBall();
    /// Tourner 3 fois en chantant du Mickael Jackson
    /// Again....
    /// Fin du match
}