summaryrefslogtreecommitdiff
path: root/i/marvin/src/es/es.cc
blob: b397cef91b6d05b3e2d4b8e8c88ab187ea3b99d4 (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
// es.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 "es/es.hh"
#include "config/config.hh"

#include <cstring>
#include <iostream>

/// Constructeur
Es::Es (const Config & config)
    : proto_ (*this), log_ ("Es"), lcd_key_pressed_ (-1),
    front_sensor_ (false)
{
    // R�cup�ration des valeurs de configuration dans le fichier
    loadConfig (config);
    proto_.open (tty_);

    sensorsRvb_.resize (9);
    for (int compt = 0; compt < 9; compt++)
	sensorsRvb_[0].resize (4);
    sniffRvb_.resize (9);
    sharps_.resize (3);
}

void
Es::reset (void)
{
    // On reset l'AVR
    proto_.send ('z');
    // Send configuration TODO
    // We want to reference some sensors
    // First, enable all sensors
    enableAllSensors (true);
    /// XXX Is it to often ?
//     setAckStat (1);
    // XXX We should wait !
    // Reference sensors 1-7
    setRefColor (127, 0);
    // Disable useless sensors
    enableAllSensors (false);
}

bool
Es::wait (int timeout /*-1*/)
{
    return proto_.wait (timeout);
}

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

void
Es::loadConfig (const Config & config)
{
    tty_ = config.get<std::string>("es.tty");
}

// Envoie de la config des sensors RVB
void Es::setRVBSensorsConfig(int false_ic, int max_ov)
{
    proto_.send ('p',"bb", false_ic, max_ov);
}

// Envoie de la config des sniff RVB
void Es::setRVBSniffConfig (int ref_ratio, int green_limit, int clear_limit)
{
    proto_.send ('x', "bww", ref_ratio, green_limit, clear_limit);
}

// R�gle les stats des sensors RVB
void Es::setRVBSensorsStat(int mask_captor, int freq)
{
    proto_.send ('S', "wb", mask_captor, freq);
}

// r�gle la couleur actuelle comme r�f�rente
void Es::setRefColor(int mask_captor, int mode = 0)
{
    proto_.send ('r', "wb", mask_captor, mode);
}

/// R�gle les stats d'affichage de la couleur
void
Es::setRVBSniffStat (int mask_captor, int freq)
{
    proto_.send ('A', "wb", mask_captor, freq);
}

/// Configure statistic for the sensor of the ball
void
Es::setRVBBallStat (int sensor_num, int freq)
{
    proto_.send ('B', "bb", sensor_num, freq);
}

// Discute avec les servo...
void Es::setServoPos (int servo_mask, int servoPos)
{
    proto_.send ('m', "bb", servo_mask, servoPos);
}

/// Set update frequency of sharps
void
Es::setSharpUpdate (int sharp_mask, int freq)
{
    proto_.send ('h', "bb", sharp_mask, freq);
}

/// Set statistics frequency of sharps
void
Es::setSharpStat (int sharp_mask, int freq)
{
    proto_.send ('H', "bb", sharp_mask, freq);
}

/// Set configuration of threshold sharps
void
Es::setSharpThreshold (int sharp_num, int threshold_high,
			int threshold_low)
{
    proto_.send ('o', "bww", sharp_num, threshold_high, threshold_low);
}

/// Print something on the LCD (max 32 char)
void
Es::lcdPrint (const std::string &message)
{
    // XXX Yerk !
    std::memset (lcd_mess_char_, '\0', 32);
    int size = message.size ();
    std::memcpy (lcd_mess_char_, message.data (), size > 32 ? 32 : size);
    proto_.send ('L', lcd_mess_char_, 32);
}

/// Get the current pressed keys of the LCD
void
Es::lcdGetKey (int freq)
{
    proto_.send ('l', "b", freq);
}

// R�gle la vitesse des turbines
void Es::setTurbineSpeed(int turbNb, int speed)
{
    proto_.send ('v', "bw", turbNb, speed);
}

// R�gle le sens de rotation du barillet
void Es::setTheMeaningOfRotationOfBarillet(int answer)
{
    proto_.send ('w', "b", answer);
}

// Init n�2 of barillet
void Es::init2Barillet(void)
{
    proto_.send ('g');
}

// D�pose une balle du barillet
void Es::deposeBalle(void)
{
    proto_.send ('d');
}

/// Extrait une balle
void Es::extraitBalle(void)
{
    proto_.send ('e');
}

/// Rotation du barillet
void Es::rotationBarillet(int posFinal)
{
    proto_.send ('t', "b", posFinal);
}

/// Put barillet in sleep mode
void
Es::barilletSleep (void)
{
    proto_.send ('s');
}

/// Enable all the sensors or just the 4 and 1 near the ground
void
Es::enableAllSensors (bool enable)
{
    if (enable)
	proto_.send ('u', "b", 1);
    else
	proto_.send ('u', "b", 0);
}

/// Shut up !
void
Es::shutUp (void)
{
    proto_.send ('f', "b", 0);
}

/// Set frequency of ack
void
Es::setAckStat (int freq)
{
    proto_.send ('F', "b", freq);
}

/// Stat for the main ()
void
Es::setMainStat (int freq)
{
    proto_.send ('Z', "b", freq);
}

void Es::receive(char command, const Proto::Frame & frame)
{
    int errCode, red, blue, clear, green, compt, value;

    switch (command)
      {
      case 'F':
	if (proto_.decode (frame, "b", errCode))
	  {
	    switch (errCode)
	      {
	      case 1:
		front_sensor_ = true;
		break;
	      }
	    shutUp ();
	  }
	break;
      case 'Z':
	// TODO
	break;
	case 'S':
	if (proto_.decode (frame, "wwwww", compt, red, blue, clear, green))
	  {
	    sensorsRvb_[compt][0] = red;
	    sensorsRvb_[compt][1] = blue;
	    sensorsRvb_[compt][2] = clear;
	    sensorsRvb_[compt][3] = green;
	  }
	break;
      case 'A':
	if (proto_.decode (frame, "bb", compt, value))
	  {
	    sniffRvb_[compt] = value;
	  }
	break;
      case 'B':
	if (proto_.decode (frame, "bb", compt, value))
	  {
	    sniffRvb_[compt] = value;
	  }
	break;
      case 'H':
	if (proto_.decode (frame, "ww", compt, value))
	  {
	    std::cout << "[" << compt << "]" << " = " << value << std::endl;
	    sharps_[compt] = value;
	  }
	break;
      }
}

bool Es::sync (void)
{
    return proto_.sync ();
}