/
GestionReseau.cpp
332 lines (294 loc) · 12.5 KB
/
GestionReseau.cpp
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
/*
GestionReseau.cpp
*/
#include "GestionReseau.h"
uint16_t GestionReseau::signalValue[2] = {0};
void GestionReseau::setup(Node *node)
{
xTaskCreatePinnedToCore(signauxTask, "SignauxTask", 2 * 1024, (void *)node, 8, NULL, 0); // Création de la tâches pour les signaux
xTaskCreatePinnedToCore(loopTask, "LoopTask", 4 * 1024, (void *)node, 10, NULL, 0); // Création de la tâches pour le traitement
// tskIDLE_PRIORITY
}
void GestionReseau::signauxTask(void *p)
{
Node *node;
node = (Node *)p;
TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
uint16_t oldValue[2] = {0};
for (;;)
{
for (byte i = 0; i < 2; i++)
{
if (signalValue[i] > 0 && oldValue[i] != signalValue[i])
{
SignauxCmd::affiche(node->signal[i]->affiche(signalValue[i]));
oldValue[i] = signalValue[i];
#ifdef debug
debug.printf("[GestionReseau %d] Fonction signauxTask\n", __LINE__);
#endif
}
}
vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(2000));
}
}
void IRAM_ATTR GestionReseau::loopTask(void *pvParameters)
{
Node *node;
node = (Node *)pvParameters;
enum : bool
{
horaire,
antiHor
};
uint8_t index = 0;
bool sens0 = 0;
bool sens1 = 0;
bool access = 0;
bool busy = 0;
uint16_t oldLocAddress = 0;
uint8_t oldLocSpeed = 0;
uint8_t oldLocSens = 0;
uint8_t comptCmdLoco = 0;
#ifdef debug
char *cantonName;
cantonName = new char[4];
#endif
TickType_t xLastWakeTime;
xLastWakeTime = xTaskGetTickCount();
for (;;)
{
/*************************************************************************************
* Occupation du canton
* et etat des capteurs
************************************************************************************/
if (!node->busy())
{
node->sensor[horaire].state(LOW); // Desactivation des capteurs ponctuels si aucune loco reconnue
node->sensor[antiHor].state(LOW);
node->loco.speed(0);
node->loco.sens(0);
node->loco.address(0);
}
else
// Adapte la vitesse de la loco à la vitesse maxi du canton
if (node->maxSpeed() < node->loco.speed())
node->loco.speed(node->maxSpeed());
// Sens de roulage des locos,
if (node->sensor[antiHor].state() && !node->sensor[horaire].state())
node->loco.sens(2); // Anti Horaire
else if (node->sensor[horaire].state() && !node->sensor[antiHor].state())
node->loco.sens(1); // Horaire
/*************************************************************************************
* Information aupres des satellites environnants
************************************************************************************/
// En fonction des aiguilles qui appartiennent a ce canton et de leurs positions
// on en recherche quel est le SP1 et le SM1
auto rechercheSat = [node](bool satPos) -> byte
{
uint8_t idxA = 0; // Pour horaire
uint8_t idxS = 0;
if (satPos == 1) // Pour anti-horaire
{
idxA = 3;
idxS = 4;
}
uint8_t $idx = idxS;
if (node->aig[0 + idxA] != nullptr) // L'aiguille 0 à horaire (ou 3 à anti-horaire) existe
{
if (node->aig[0 + idxA]->estDroit()) // L'aiguille 0 à horaire (ou 3 à anti-horaire) est droite
{
$idx = 0 + idxS;
if (node->aig[1 + idxA] != nullptr) // L'aiguille 1 à horaire (ou 4 à anti-horaire) existe
{
if (!node->aig[1 + idxA]->estDroit()) // L'aiguille 1 à horaire (ou 4 à anti-horaire) est droite
$idx = 0 + idxS;
else
$idx = 1 + idxS; // L'aiguille 1 à horaire (ou 4 à anti-horaire) est déviée
}
}
else // L'aiguille 0 à horaire (ou 3 à anti-horaire) est déviée
{
$idx = 1 + idxS;
if (node->aig[2 + idxA] != nullptr) // L'aiguille 2 à horaire (ou 5 à anti-horaire) existe
{
if (node->aig[2 + idxA]->estDroit())
$idx = 2 + idxS; // L'aiguille 2 à horaire (ou 5 à anti-horaire) est droite
else
$idx = 3 + idxS; // L'aiguille 2 à horaire (ou 5 à anti-horaire) est déviée
}
}
}
return $idx;
};
node->SP1_idx(rechercheSat(horaire));
node->SM1_idx(rechercheSat(antiHor));
// debug.printf("[GestionReseau %d] node->SP1_idx = %d\n", __LINE__, node->SP1_idx());
// debug.printf("[GestionReseau %d] node->SM1_idx = %d\n", __LINE__, node->SM1_idx());
/*************************************************************************************
* Envoi sur le bus CAN des informations concernant ce satellite
************************************************************************************/
uint8_t nodeP_SM1_ID = 0;
bool nodeP_SM2_ACCES = false;
bool nodeP_SM2_BUSY = false;
if (node->nodeP[node->SP1_idx()] != nullptr)
{
nodeP_SM1_ID = node->nodeP[node->SP1_idx()]->ID();
nodeP_SM2_ACCES = node->SP2_acces();
nodeP_SM2_BUSY = node->SP2_busy();
}
uint8_t nodeP_SP1_ID = 0;
bool nodeP_SP2_ACCES = false;
bool nodeP_SP2_BUSY = false;
if (node->nodeP[node->SM1_idx()] != nullptr)
{
nodeP_SP1_ID = node->nodeP[node->SM1_idx()]->ID();
nodeP_SP2_ACCES = node->SM2_acces();
nodeP_SP2_BUSY = node->SM2_busy();
}
// debug.printf("[GestionReseau %d ] nodeP_SP1_ID : %d\n", __LINE__, nodeP_SM1_ID);
// debug.printf("[GestionReseau %d ] nodeP_SM1_ID : %d\n", __LINE__, nodeP_SP1_ID);
// debug.printf("[GestionReseau %d ] this node busy : %d\n", __LINE__, node->busy());
CanMsg::sendMsg(1, 0xE0, node->ID(), 0xE0, 0,
(node->loco.address() & 0xFF00) >> 8,
node->loco.address() & 0x00FF,
nodeP_SP1_ID,
nodeP_SM1_ID,
nodeP_SP2_ACCES,
nodeP_SP2_BUSY,
nodeP_SM2_ACCES,
nodeP_SM2_BUSY);
enum : uint8_t
{
Orange,
Rouge,
Vert,
Carre,
Ralentissement,
RRalentissement
};
/*************************************************************************************
*
************************************************************************************/
for (byte i = 0; i < 2; i++)
{
switch (i)
{
case 0:
index = node->SP1_idx();
sens0 = horaire;
sens1 = antiHor;
access = node->SP2_acces();
busy = node->SP2_busy();
#ifdef debug
strcpy(cantonName, "SP1");
#endif
break;
case 1:
index = node->SM1_idx();
sens0 = antiHor;
sens1 = horaire;
access = node->SM2_acces();
busy = node->SM2_busy();
#ifdef debug
strcpy(cantonName, "SM1");
#endif
break;
}
// debug.printf("[GestionReseau %d] index %d \n", __LINE__, index);
// debug.printf("[GestionReseau %d] node->sensor[sens0].state() %d \n", __LINE__, node->sensor[sens0].state());
// debug.printf("[GestionReseau %d] node->sensor[sens1].state() %d \n", __LINE__, node->sensor[sens1].state());
if (node->nodeP[index] != nullptr)
{
if (node->nodeP[index]->acces()) // Le canton SP1/SM1 est accessible
{
// debug.printf("[GestionReseau %d] Le canton %s est accessible\n", __LINE__, cantonName);
if (node->nodeP[index]->busy()) // Le canton SP1/SM1 est occupé
{
// debug.printf("[GestionReseau %d] Le canton %s est accessible mais occupe\n", __LINE__, cantonName);
signalValue[i] = Rouge;
// if (node->loco.sens() == 1)
//{
if (node->sensor[sens1].state())
node->loco.stop();
else if (node->sensor[sens0].state())
node->loco.speed(30);
//}
// if (node->loco.sens() == 2)
// {
// if (node->sensor[antiHor].state())
// node->loco.stop();
// else if (node->sensor[horaire].state())
// node->loco.ralentis(30);
// }
}
else // Le canton SP1/SM1 est accessible et libre
{
// debug.printf("[GestionReseau %d] Le canton %s est accessible et libre\n", __LINE__, cantonName);
signalValue[i] = Vert;
if (access) // Le canton SP2 est-il accessible ?
{
// debug.printf("[GestionReseau %d] SP2 accessible\n", __LINE__);
if (busy) // Le canton SP2 est-il occupé ?
{
// debug.printf("[GestionReseau %d] SP2 occupe\n", __LINE__);
signalValue[i] = Ralentissement;
}
else // Le canton SP2 n'est pas occupé
{
// debug.printf("[GestionReseau %d] SP2 libre\n", __LINE__);
signalValue[i] = Vert;
}
}
else // Le canton SP2 n'est pas accessible
{
// debug.printf("[GestionReseau %d] SP2 non accessible\n", __LINE__);
signalValue[i] = Ralentissement;
}
}
}
else // Le canton SP1/SM1 est n'est pas accessible
{
// debug.printf("[GestionReseau %d] Le canton %s n'est pas accessible\n", __LINE__, cantonName);
signalValue[i] = Carre;
// if (node->sensor[sens0].state())
// node->loco.stop();
// else
// node->loco.ralentis(30);
}
}
else // Le canton SP1/SM1 n'existe pas
{
// debug.printf("[GestionReseau %d] Le canton %s n'existe pas\n", __LINE__, cantonName);
signalValue[i] = Carre;
// if (node->sensor[sens0].state())
// node->loco.stop();
// else
// node->loco.ralentis(30);
}
}
// Envoi des commandes à la loco
if (node->loco.address() > 0)
{
// if (node->loco.address() != oldLocAddress || node->loco.speed() != oldLocSpeed || node->loco.sens() != oldLocSens)
if (node->loco.speed() != oldLocSpeed)
comptCmdLoco = 0;
if (comptCmdLoco < 5)
{
CanMsg::sendMsg(1, 0xF0, node->ID(), CENTRALE_DCC_ID, 0,
(node->loco.address() & 0xFF00) >> 8,
node->loco.address() & 0x00FF,
node->loco.speed(),
node->loco.sens()); // Message à la centrale DCC++
#ifdef debug
debug.printf("[GestionReseau %d] Loco %d vitesse %d\n", __LINE__, node->loco.address(), node->loco.speed());
#endif
oldLocAddress = node->loco.address();
oldLocSpeed = node->loco.speed();
oldLocSens = node->loco.sens();
comptCmdLoco++;
}
}
vTaskDelayUntil(&xLastWakeTime, pdMS_TO_TICKS(50));
}
}