This repository has been archived by the owner on Jun 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrandRobot.cs
executable file
·338 lines (273 loc) · 10.6 KB
/
GrandRobot.cs
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
using Gadgeteer.Modules.GHIElectronics;
using System;
using System.Collections;
using System.Threading;
using GR.BR;
using GR.Membres;
using GR.Vision;
using GHI.Processor;
using GHI.Pins;
using System.IO.Ports;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
namespace GR
{
#region structures
struct EtatRobot
{
public Couleur couleurEquipe;
public int disposition;
public int coefPos;
public int coefAngle;
public bool stop;
public sens s;
}
#endregion
partial class GrandRobot
{
const int Y_TABLE = 3000;
const int X_TABLE = 2000;
public enum Axe { Null = 0, X, Y }
public GestionnaireStrategie Strategie;
EtatRobot m_etatRobot;
public IHM m_ihm;
public Jack JackDemarrage;
public CBaseRoulante BaseRoulante;
public ControleurAX12 controleurAX12;
public CPince pince;
public CFunnyBras funnyBras;
public CReservoir reservoir;
public CBras bras;
public GroupeInfrarouge IR;
// public CCapteurUltrason CapteurUltrason;
public positionBaseRoulante Position = new positionBaseRoulante();
public static bool obstacle = false;
public DateTime InstantDebut;
public int cylindresRecup = 0;
public int deltafusee = 10;
public GrandRobot(ConfigurationPorts ports, Couleur equipe, int disposition, IHM ihm)
{
m_ihm = ihm;
m_etatRobot = new EtatRobot();
m_etatRobot.couleurEquipe = equipe;
m_etatRobot.disposition = disposition;
Strategie = new GestionnaireStrategie();
JackDemarrage = new Jack(ports.IO, ports.Jack);
Debug.Print("Ceration de la plateforme "+ports.Plateforme);
BaseRoulante = new CBaseRoulante(ports.Plateforme);
Debug.Print("plateforme créée");
BaseRoulante.setCouleur(m_etatRobot.couleurEquipe);
BaseRoulante.getPosition(ref Position);
controleurAX12 = new ControleurAX12(ports.contAX12);
Debug.Print("Controleur actif");
pince = new CPince(controleurAX12, ports.pince);
//funnyBras = new CFunnyBras(controleurAX12, ports.funnyBras);
Debug.Print("funny bras actif");
bras = new CBras(controleurAX12, ports.bras);
reservoir = new CReservoir(m_etatRobot.couleurEquipe, controleurAX12, ports.reservoir);
Debug.Print("reservoir actif");
// à la date d'aujourd'hui, veille de la coupe, l'infrarouge avant gauche ne marche pas
IR = new GroupeInfrarouge(ports.IO, ports.InfrarougeAVD, ports.InfrarougeAVG, ports.InfrarougeARD, ports.InfrarougeARG);
Debug.Print("infrarouge actif");
// NB : il n'y a pas de capteur ultrason sur notre grand robot
}
void recalageX(int angle, int x, sens s, int speed, int temps)
{
BaseRoulante.recalagePosX(angle, x, speed, s, temps);
}
void recalageY(int angle, int y, sens s, int speed, int temps)
{
BaseRoulante.recalagePosY(angle, y, speed, s, temps);
}
/// <summary>
/// Attend le Jack
/// </summary>
public void AttendreJack()
{
while (!JackDemarrage.Etat) Thread.Sleep(1);
Debug.Print(JackDemarrage.Etat+" plop jzck");
m_ihm.retourPhase(Couleurs.vert);
}
/// <param name="tempsImparti">Temps en secondes au bout du quel l'arrêt du robot est forcé</param>
public void Demarrer(double tempsImparti)
{
Timer timeout;
DateTime fin = new DateTime();
/*
var thDecompte = new Thread(() =>
{
while (Strategie.ExecutionPossible() && DateTime.Now < fin)
{
// Tracage.Ecrire("Temps restant: " + (fin - DateTime.Now).ToString().Substring(3, 5) + ".");
Thread.Sleep(10000);
}
});
*/
InitialiserStrategie();
Thread thStrat = new Thread(() => EffectuerStrategie());
InstantDebut = DateTime.Now;
fin = InstantDebut.AddSeconds(tempsImparti);
// thDecompte.Start();
thStrat.Start();
// if (thStrat.IsAlive) m_ihm.retourPhase(Couleurs.bleu);
timeout = new Timer(state =>
{
m_ihm.retourPhase(Couleurs.orange);
// Tracage.Ecrire("Fin du temps imparti.");
/*if (thRanger.IsAlive)
{
Debug.Print("plop ran");
thRanger.Suspend();
}
if (thReservoir.IsAlive)
{
Debug.Print("plop res 1");
thReservoir.Suspend();
}
* */
// reservoir.arretUrgenceRoulette();
/*
Thread thFunny = new Thread(() =>
{
Thread.Sleep(3000);
funnyBras.lancer();
});
thFunny.Start();
*/
funnyBras = new CFunnyBras(controleurAX12, 7);
Thread.Sleep(2000);
funnyBras.lancer();
m_ihm.retourPhase(Couleurs.rouge);
BaseRoulante.stop();
m_ihm.retourPhase(Couleurs.vert);
pince.killPince();
bras.killBras();
reservoir.killReservoir();
if (thStrat.IsAlive)
{
Debug.Print("kill main thread");
thStrat.Abort();
}
m_ihm.retourPhase(Couleurs.bleu);
//reservoir.m_rouletteIntelligente.m_ax12Roulette.setMode(AX12Mode.joint);
//reservoir.m_rouletteIntelligente.m_ax12Roulette.
/* var thBase = new Thread(() =>
{
});
thBase.Start();
var thReservoir = new Thread(() =>
{
});
thReservoir.Start();
*/
}, null, (int)(tempsImparti * 1000), -1);
}
public void EffectuerStrategie()
{
// Tracage.Ecrire("Debut de l'execution de la strategie.");
// Debug.Print("Debut de l'execution de la strategie.");
// Debug.Print(Strategie.NombreAction+" nombre d'actions");
while (Strategie.ExecutionPossible())
{
// Debug.Print("execution possible");
// Tracage.Ecrire("Execution de l'action suivante.");
Strategie.ExecuterSuivante();
}
}
public Couleur robotGetCouleur()
{
return m_etatRobot.couleurEquipe;
}
public int robotGetDisposition()
{
return m_etatRobot.disposition;
}
etatBR robotGoToXY(ushort x, ushort y, sens s, bool boolDetection = false, int speed = 2)
{
etatBR retour;
if (boolDetection)
{
// on passe le sens "dir" au timer via la variable "state"
// analogue au timeout-callback pour les amoureux du js
//Timer t = new Timer(new TimerCallback(Detecter), s, 0, 1000);
obstacle = false; // paramètre pour savoir si il y'a bien un obstacle
var thDetection = new Thread(() =>
{
while (true)
{
Detecter(s);
//Thread.Sleep(20);
}
});
thDetection.Start();
retour = BaseRoulante.allerDect(x, y, s, speed);// x,y,s
thDetection.Suspend();
obstacle = false;
//t.Dispose();
}
else
{
retour = BaseRoulante.allerEn(x, y, s, speed);
}
Debug.Print("pos_visee " + x + " " + y);
return retour;
}
public void Detecter(object o)
{
sens dir = (sens)o;
// si on avance, les ultrasons sont utiles
if (dir == sens.avancer)
{
if ((!IR.AVG.Read() || !IR.AVD.Read()))// infrarouge OK.. et c'est une condition et && obstacleUS
{
obstacle = true;
}
else obstacle = false;
}
// si on recule, les ultrasons ne sont plus utiles
else
{
// on teste les capteurs IR arrières
if (!IR.ARG.Read() || !IR.ARD.Read())
{
obstacle = true;
}
else obstacle = false;
}
}
public etatBR robotRotate(int alpha)
{
etatBR retour;
Debug.Print("avant rotate " + alpha);
retour = BaseRoulante.Tourner(alpha);
Debug.Print("après rotate " + alpha);
return retour;
}
public void Recaler(Axe axe = Axe.Null)
{
/*
var posBR = new positionBaseRoulante();
etatBR retour;
BaseRoulante.getPosition(ref posBR);
switch (axe)
{
case Axe.X:
Tracage.Ecrire("Recalage sur l'axe X.");
retour = AllerEn(posBR.x > Table.Taille.X / 2 ? Table.Taille.X : 0, posBR.y, BR.sens.avancer);
break;
case Axe.Y:
Tracage.Ecrire("Recalage sur l'axe Y.");
retour = AllerEn(posBR.x, posBR.y > Table.Taille.Y / 2 ? Table.Taille.Y : 0, BR.sens.avancer);
break;
default:
var dx = posBR.x > Table.Taille.X / 2 ? Table.Taille.X - posBR.x : posBR.x;
var dy = posBR.y > Table.Taille.Y / 2 ? Table.Taille.Y - posBR.y : posBR.y;
if (dx < dy) goto case Axe.X;
else goto case Axe.Y;
}
Tracage.Ecrire(retour == etatBR.arrive ? "Succes" : "Echec");
*/}
}
}