-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsabre.cpp
392 lines (365 loc) · 12.3 KB
/
sabre.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
//Copyright (c) Piet Wauters 2022 <[email protected]>
#include "3WeaponSensor.h"
// Below are are all the settings needed per phase: IO direction (input/outpout), IO values (High or Low), analog channel, Threashold
// These are machine constants. It might be usefull to move this to flash and make it calibratable
MeasurementCtl SabreSets[] = {{IODirection_al_cr, IOValues_al_cr, bl_analog, 1750},
{IODirection_bl_cl, IOValues_bl_cl, cr_analog, 1750},
{IODirection_ar_br, IOValues_ar_br, cl_analog, 1184},
{IODirection_ar_cl, IOValues_ar_cl, br_analog, 1750},
{IODirection_br_cr, IOValues_br_cr, cl_analog, 1750},
{IODirection_br_bl, IOValues_br_bl, cr_analog, 1184},
{IODirection_al_cl, IOValues_al_cl, bl_analog, 1184},
{IODirection_ar_cr, IOValues_ar_cr, cl_analog, 1750},
{IODirection_al_bl, IOValues_al_bl, cr_analog, 1750}
};
void MultiWeaponSensor::DoSabre()
{
static bool AdAboveThreshold;
static bool TempOrangeL, TempOrangeR;
//*************************************************************************
//*************************************************************************
//** From this point you find the functions for sabre **
//*************************************************************************
//*************************************************************************
// PhaseCounter =START_OF_SABRE_TABLE;
Set = SabreSets;
//*************************************************************************
// Phas_S0: Test if wire is broken: a1-b1 -> b1 =?= low *
//*************************************************************************
if(FullScanCounter == 0)
{
AdAboveThreshold = Do_Common_Start();
if (!AdAboveThreshold)
{
Counter_b1--;
if (!Counter_b1)
{
WhiteL = true;
Counter_b1++;
}
NotConnectedLeft = true;
}
else
{
Counter_b1 = COUNT_B1_INIT_SABRE;
NotConnectedLeft = false;
WhiteL = false;
}
}
else
{
Skip_phase();
}
//*************************************************************************
// Phas_S1: Test if opponent contact: a1-b1-c2 -> c2 =?= high *
//*************************************************************************
AdAboveThreshold = Do_Common_Start();
if (WhiteL)
{
// A1-wire is broken
/* if(ADRESH < SABRE_A_BROKEN_THREASHOLD)
AdAboveThreshold = 0;
else
AdAboveThreshold = 1;*/
}
if (AdAboveThreshold)
{
if (!SignalRight)
{
if(Counter_c1)
Counter_c1--;
if (!Counter_c1)
{
MaybeSignalRight = true;
Const_COUNT_C1_INIT_SABRE = COUNT_C1_INIT_SABRE - 1;
if(Counter_c2)
Counter_c2--; //here I assume Counter_c2 is not yet 0, or if it is, it will not make a difference anymore
c1_reached1 = false;
}
else
{
if (Counter_c1 == 1)
{
c1_reached1 = true;
}
}
}
// This part is needed for automatic weapon detection. It is only relevant if "the point is pressed down", because
// to see if we have to switch from sabre to foil we check that there is contact between the point and the opponent's
// lame AND that the point is pressed down. (white light is on)
// We will check during the dummy phase if the highbyte of the long is also zero
if (LongCounter_b1)
LongCounter_b1--;
}
else
{
if (c1_reached1)
{
// this means we have blocked a hit, so we should block the otherone too
c1_reached1 = false;
WeHaveBlockedAhit = true;
Const_COUNT_C1_INIT_SABRE = COUNT_C1_INIT_SABRE + 1;
Counter_c2++;
}
Counter_c1 = Const_COUNT_C1_INIT_SABRE;
LongCounter_b1 = LONG_COUNT_B_INIT_SABRE;
}
//*************************************************************************
// Phas_S2: Test if Leak jacket - Weapon: b1-c1 ==> c1 ?= high *
//*************************************************************************
Skip_phase();
/*AdAboveThreshold = Do_Common_Start();
if (AdAboveThreshold)
{
TempOrangeR = true;
}
else
{
TempOrangeR = false;
}
*/
//*************************************************************************
// Phas_S3: Test if wire is broken: a2-b2 -> b2 =?= low *
//*************************************************************************
if(FullScanCounter == 1)
{
AdAboveThreshold = Do_Common_Start();
if (!AdAboveThreshold)
{
Counter_b2--;
if (!Counter_b2)
{
WhiteR = true;
Counter_b2++;
}
NotConnectedRight = true;
}
else
{
Counter_b2 = COUNT_B1_INIT_SABRE;
NotConnectedRight = false;
WhiteR = false;
}
}
else
{
Skip_phase();
}
//*************************************************************************
// Phas_S4: Test if opponent contact: a2-b2-c1 -> c1 =?= high *
//*************************************************************************
AdAboveThreshold = Do_Common_Start();
if (WhiteR)
{
// A2-wire is broken
/* if(ADRESH < SABRE_A_BROKEN_THREASHOLD)
AdAboveThreshold = 0;
else
AdAboveThreshold = 1;*/
}
if (AdAboveThreshold)
{
if (!SignalLeft)
{
if(Counter_c2)
Counter_c2--;
if (!Counter_c2)
{
MaybeSignalLeft = true;
Const_COUNT_C1_INIT_SABRE = COUNT_C1_INIT_SABRE - 1;
if(Counter_c1)
Counter_c1--; //here I assume Counter_b2 is not yet 0, or if it is, it will not make a difference anymore
c2_reached1 = false;
}
else
{
if (Counter_c2 == 1)
{
c2_reached1 = true;
}
}
}
// This part is needed for automatic weapon detection. It is only relevant if "the point is pressed down", because
// to see if we have to switch from sabre to foil we check that there is contact between the point and the opponent's
// lame AND that the point is pressed down. (white light is on)
// We will check during the dummy phase if the highbyte of the long is also zero
if (LongCounter_b2)
LongCounter_b2--;
}
else
{
if (c2_reached1)
{
// this means we have blocked a hit, so we should block the otherone too
c2_reached1 = false;
WeHaveBlockedAhit = true;
Const_COUNT_C1_INIT_SABRE = COUNT_C1_INIT_SABRE + 1;
Counter_c1++;
}
Counter_c2 = Const_COUNT_C1_INIT_SABRE;
LongCounter_b2 = LONG_COUNT_B_INIT_SABRE;
}
//*************************************************************************
// Phas_S5: Test if Leak jacket - Weapon: c2 =?= high*
//*************************************************************************
Skip_phase();
/*{
AdAboveThreshold = Do_Common_Start();
if (AdAboveThreshold)
{
TempOrangeL = true;
}
else
{
TempOrangeL = false;
}
//Set++;
}*/
//*************************************************************************
// Phas_S6: Test if Weapons in contact: b2 =?= High *
//*************************************************************************
WeaponContact = false;
AdAboveThreshold = Do_Common_Start();
if ((SignalLeft && SignalRight))
return;
if (AdAboveThreshold)
{
// There is weapon contact
WeaponContact = true;
BlockWhipover = true;
}
if (BlockWhipover)
{
// Maybe we need to block
BlockCounter++;
if (BlockCounter > BLOCKINGPERIOD1) //if less than 4.5 ms allow touch
{
if (BlockCounter < BLOCKINGPERIOD2)
{
// Between 4.5 and 10 ms block touch
MaybeSignalLeft = false;
MaybeSignalRight = false;
}
else
{
// after more than 10 ms allow touch
BlockCounter--; // needed to avoid overflow if weapon contact remains
if (!WeaponContact)
{
//if after 15 ms the weapons are not in contact any more -> forget about blocking
BlockCounter = 0;
BlockWhipover = false;
}
}
}
}
// No need for Blocking!!
if (MaybeSignalLeft)
{
StartLock(SABRE_LOCK_TIME);
Green = true;
Buzz = true;
SignalLeft = true;
}
if (MaybeSignalRight)
{
StartLock(SABRE_LOCK_TIME);
Red = true;
Buzz = true;
SignalRight = true;
}
/************************************************************************/
/* used for videoref to check if there is a parry */
/************************************************************************/
/* Since the Parry-signal will be transmitted by serial line to the videoref application */
/* We don't want to send too many signals going up and down. */
/* I have no clue yet of what a good parry debouncing time would be, but if we decide there was a parry,
/* it has to stay visible for at least a few video frames*/
if (!bParrySignal)
{
//No Parry is signalled yet, check if we need to light the Parry signal
if (Counter_parry)
{
if (!AdAboveThreshold)
{
Counter_parry = Const_FOIL_PARRY_ON_TIME;
}
else
Counter_parry--;
}
else
{
bParrySignal = true;
Counter_parry = Const_FOIL_PARRY_OFF_TIME;
}
}
else
{
// We have allready signalled a parry -> check if we should switch off the parry signal
if (Counter_parry)
{
if (AdAboveThreshold)
{
Counter_parry = Const_FOIL_PARRY_OFF_TIME;
}
else
Counter_parry--;
}
else
{
bParrySignal = false;
Counter_parry = Const_FOIL_PARRY_ON_TIME;
}
}
// This part is needed for automatic weapon detection. It is only relevant if "the point is pressed down", because
// to see if we have to switch from sabre to foil we check that there is contact between the point and the opponent's
// lame AND that the point is pressed down. (white light is on)
// We will check during the dummy phase if the highbyte of the long is also zero
//*************************************************************************
// Phas_S7: Test if contact jacket - point: a1-c1 ==> c1 ?= high *
//*************************************************************************
if(FullScanCounter == 2)
{
AdAboveThreshold = Do_Common_Start();
if (AdAboveThreshold)
{
TempOrangeR = true;
if (LongCounter_c1)
LongCounter_c1--;
}
else
{
TempOrangeR = false;
LongCounter_c1 = LONG_COUNT_C_INIT_SABRE;
}
}
else
{
Skip_phase();
}
//*************************************************************************
// Phas_S8: Test if contact jacket - point: a1-c1 ==> c1 ?= high *
//*************************************************************************
if(FullScanCounter == 3)
{
AdAboveThreshold = Do_Common_Start();
if (AdAboveThreshold)
{
TempOrangeL = true;
if (LongCounter_c2)
LongCounter_c2--;
}
else
{
TempOrangeL = false;
LongCounter_c2 = LONG_COUNT_C_INIT_SABRE;
}
}
else
{
Skip_phase();
}
OrangeR = TempOrangeL;
OrangeL = TempOrangeR;
return;
}