-
Notifications
You must be signed in to change notification settings - Fork 1
/
boxstates.cpp
494 lines (427 loc) · 16.1 KB
/
boxstates.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#include "boxstates.h"
#include <QApplication>
#include <cmath>
#include "competition.h"
#include "mainwindow.h"
#include "infoscreen.h"
#include <QTimer>
#define VOLTAGE_WARNING_LEVEL 20 // %
#define VOLTAGE_WARNING_HYST 1 // %
#define ABSENT_WARNING_LEVEL 20000 // ms
#define LOW_VOLTAGE_TIMEOUT 10 // s
BoxStates::BoxStates(QObject *parent) :
QObject(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(refreshAbsentTimes()));
timer->start(111); // run update-timer
timeBaseState.absent=true;
triggerStationState[START].absent=true;
triggerStationState[GOAL].absent=true;
matrixDisplayState[1].absent=true;
matrixDisplayState[2].absent=true;
timeBaseState.lowVoltageSince=QTime();
timeBaseState.lowVoltage=false;
triggerStationState[START].lowVoltageSince=QTime();
triggerStationState[START].lowVoltage=false;
triggerStationState[GOAL].lowVoltageSince=QTime();
triggerStationState[GOAL].lowVoltage=false;
matrixDisplayState[1].lowVoltageSince=QTime();
matrixDisplayState[1].lowVoltage=false;
matrixDisplayState[2].lowVoltageSince=QTime();
matrixDisplayState[2].lowVoltage=false;
for(int i=1; i<=N_BOATBOXES; i++)
{
boatBoxState[i].lowVoltageSince=QTime();
boatBoxState[i].lowVoltage=false;
}
}
void BoxStates::setTimeBaseTime(QTime baseTime)
{
// this implements the feature, that old time-stamps to not appear in a new competition
// see also emitNewTimeStamp()
if(firstTimeBaseTimeReceived.isValid() == false){
firstTimeBaseTimeReceived = baseTime;
}
timeBaseState.baseTime = baseTime;
timeBaseState.txtBaseTime.setText(MainWindow::convertTimeToString(baseTime) + QString(" (%1ms)").arg(MainWindow::app()->localTimeOffset_ms));
if(timeBaseState.baseTime.isValid() && timeBaseState.absent)
{
timeBaseState.absent=false;
MainWindow::app()->infoscreen()->appendInfo(tr("Verbindung zur Zeitbasis hergestellt"));
}
}
// battery voltages
void BoxStates::setTimeBaseVoltage(double voltage)
{
timeBaseState.lastAliveTime=MainWindow::app()->getTimeBaseTime();
if(!timeBaseState.lowVoltageSince.isValid())
{
timeBaseState.lowVoltageSince=MainWindow::app()->getTimeBaseTime();
}
if(voltage!=timeBaseState.voltage)
{
timeBaseState.voltage=voltage;
timeBaseState.txtVoltage.setText(QString("%1%").arg(QString::number(100*voltageToBatteryState(voltage), 'f', 0)));
// check voltage
if(100*voltageToBatteryState(voltage)<VOLTAGE_WARNING_LEVEL-VOLTAGE_WARNING_HYST)
{
// voltage too low
if(timeBaseState.lowVoltageSince.secsTo(MainWindow::app()->getTimeBaseTime()) > LOW_VOLTAGE_TIMEOUT && !timeBaseState.lowVoltage)
{
timeBaseState.lowVoltage=true;
MainWindow::app()->infoscreen()->appendWarning(tr("Akkustand der Zeitbasis kritisch"));
}
}
if(100*voltageToBatteryState(voltage)>VOLTAGE_WARNING_LEVEL+VOLTAGE_WARNING_HYST)
{
// voltage OK
timeBaseState.lowVoltage=false;
timeBaseState.lowVoltageSince=MainWindow::app()->getTimeBaseTime();
}
}
}
void BoxStates::setTriggerStationVoltage(BoxStates::Station s, double voltage)
{
triggerStationState[s].lastAliveTime=MainWindow::app()->getTimeBaseTime();
if(!triggerStationState[s].lowVoltageSince.isValid())
{
triggerStationState[s].lowVoltageSince=MainWindow::app()->getTimeBaseTime();
}
if(voltage!=triggerStationState[s].voltage)
{
triggerStationState[s].voltage=voltage;
triggerStationState[s].txtVoltage.setText(QString("%1%").arg(QString::number(100*voltageToBatteryState(voltage), 'f', 0)));
// check voltage
if(100*voltageToBatteryState(voltage)<VOLTAGE_WARNING_LEVEL-VOLTAGE_WARNING_HYST)
{
// voltage too low
if(triggerStationState[s].lowVoltageSince.secsTo(MainWindow::app()->getTimeBaseTime()) > LOW_VOLTAGE_TIMEOUT && !triggerStationState[s].lowVoltage)
{
triggerStationState[s].lowVoltage=true;
if(s==START)
{
MainWindow::app()->infoscreen()->appendWarning(tr("Akkustand der Startbox kritisch"));
}
else
{
MainWindow::app()->infoscreen()->appendWarning(tr("Akkustand der Zielbox kritisch"));
}
}
}
if(100*voltageToBatteryState(voltage)>VOLTAGE_WARNING_LEVEL+VOLTAGE_WARNING_HYST)
{
// voltage OK
triggerStationState[s].lowVoltage=false;
triggerStationState[s].lowVoltageSince=MainWindow::app()->getTimeBaseTime();
}
}
}
void BoxStates::setMatrixDisplayVoltage(int display, double voltage)
{
matrixDisplayState[display].lastAliveTime=MainWindow::app()->getTimeBaseTime();
if(!matrixDisplayState[display].lowVoltageSince.isValid())
{
matrixDisplayState[display].lowVoltageSince=MainWindow::app()->getTimeBaseTime();
}
if(voltage!=matrixDisplayState[display].voltage)
{
matrixDisplayState[display].voltage=voltage;
matrixDisplayState[display].txtVoltage.setText(QString("%1%").arg(QString::number(100*voltageToBatteryStatePb(voltage), 'f', 0)));
//qDebug() << "Matrix Display Voltage: " << voltage;
// check voltage
if(100*voltageToBatteryStatePb(voltage)<VOLTAGE_WARNING_LEVEL-VOLTAGE_WARNING_HYST)
{
// voltage too low
if(matrixDisplayState[display].lowVoltageSince.secsTo(MainWindow::app()->getTimeBaseTime()) > LOW_VOLTAGE_TIMEOUT && !matrixDisplayState[display].lowVoltage)
{
matrixDisplayState[display].lowVoltage=true;
MainWindow::app()->infoscreen()->appendWarning(tr("Akkustand der Kiboko Matrix %1 kritisch").arg(display));
}
}
if(100*voltageToBatteryStatePb(voltage)>VOLTAGE_WARNING_LEVEL+VOLTAGE_WARNING_HYST)
{
// voltage OK
matrixDisplayState[display].lowVoltage=false;
matrixDisplayState[display].lowVoltageSince=MainWindow::app()->getTimeBaseTime();
}
}
}
void BoxStates::setBoatBoxVoltage(int ID, BoxStates::Station s, double voltage)
{
boatBoxState[ID].lastAliveTime[s]=MainWindow::app()->getTimeBaseTime();
if(!boatBoxState[ID].lowVoltageSince.isValid())
{
boatBoxState[ID].lowVoltageSince=MainWindow::app()->getTimeBaseTime();
}
if(voltage!=boatBoxState[ID].voltage)
{
boatBoxState[ID].voltage=voltage;
boatBoxState[ID].txtVoltage.setText(QString("%1%").arg(QString::number(100*voltageToBatteryState(voltage), 'f', 0)));
// check voltage
if(100*voltageToBatteryState(voltage)<VOLTAGE_WARNING_LEVEL-VOLTAGE_WARNING_HYST)
{
// voltage too low
if(boatBoxState[ID].lowVoltageSince.secsTo(MainWindow::app()->getTimeBaseTime()) > LOW_VOLTAGE_TIMEOUT && !boatBoxState[ID].lowVoltage)
{
boatBoxState[ID].lowVoltage=true;
MainWindow::app()->infoscreen()->appendWarning(tr("Akkustand von BoatBox %1 kritisch").arg(ID));
}
}
if(100*voltageToBatteryState(voltage)>VOLTAGE_WARNING_LEVEL+VOLTAGE_WARNING_HYST)
{
// voltage OK
boatBoxState[ID].lowVoltage=false;
boatBoxState[ID].lowVoltageSince=MainWindow::app()->getTimeBaseTime();
}
}
}
// rssi values
void BoxStates::setTriggerStationRssi(BoxStates::Station s, double rssi)
{
triggerStationState[s].lastAliveTime=MainWindow::app()->getTimeBaseTime();
if(rssi!=triggerStationState[s].rssi)
{
triggerStationState[s].rssi=rssi;
triggerStationState[s].txtSignalStrength.setText(QString("%1%").arg(QString::number(100*rssiToSignalStrength(rssi), 'f', 0)));
}
}
void BoxStates::setMatrixDisplayRssi(int display, double rssi)
{
matrixDisplayState[display].lastAliveTime=MainWindow::app()->getTimeBaseTime();
if(rssi!=matrixDisplayState[display].rssi)
{
matrixDisplayState[display].rssi=rssi;
matrixDisplayState[display].txtSignalStrength.setText(QString("%1%").arg(QString::number(100*rssiToSignalStrength(rssi), 'f', 0)));
}
}
void BoxStates::setBoatBoxRssi(int ID, BoxStates::Station s, double rssi)
{
boatBoxState[ID].lastAliveTime[s]=MainWindow::app()->getTimeBaseTime();
if(rssi!=boatBoxState[ID].rssi[s])
{
boatBoxState[ID].rssi[s]=rssi;
boatBoxState[ID].txtSignalStrength[s].setText(QString("%1%").arg(QString::number(100*rssiToSignalStrength(rssi), 'f', 0)));
}
}
// trigger times
void BoxStates::setTsTriggerTimeL(BoxStates::Station s, QTime time)
{
triggerStationState[s].lastAliveTime=MainWindow::app()->getTimeBaseTime();
if(time > MainWindow::app()->getTimeBaseTime().addSecs(10))
{
qDebug() << "received timestamp from future!";
return;
}
if(time!=triggerStationState[s].triggerTimeL) // block repeated time stamps received from the boatbox
{
triggerStationState[s].triggerTimeL=time;
// generate new timestamp
if(s==BoxStates::START)
{
emitNewTimeStamp(time, TimeStamp::LS);
}
else if(s==BoxStates::GOAL)
{
emitNewTimeStamp(time, TimeStamp::LGL);
}
}
}
void BoxStates::setTsTriggerTimeR(Station s, QTime time)
{
triggerStationState[s].lastAliveTime=MainWindow::app()->getTimeBaseTime();
if(time > MainWindow::app()->getTimeBaseTime().addSecs(10))
{
qDebug() << "received timestamp from future!";
return;
}
if(time!=triggerStationState[s].triggerTimeR)
{
triggerStationState[s].triggerTimeR=time;
// generate new timestamp
if(s==BoxStates::START)
{
emitNewTimeStamp(time, TimeStamp::LS);
}
else if(s==BoxStates::GOAL)
{
emitNewTimeStamp(time, TimeStamp::LGR);
}
}
}
void BoxStates::setBoatBoxTriggerTime(int ID, Station s, QTime time, TimeStamp::Source source)
{
boatBoxState[ID].lastAliveTime[s]=MainWindow::app()->getTimeBaseTime();
if(time > MainWindow::app()->getTimeBaseTime().addSecs(10))
{
qDebug() << "received timestamp from future!";
return;
}
// check, if this time-stamp wasn't received recently
if(!boatBoxState[ID].recentTriggerTimes.contains(time))
{
boatBoxState[ID].recentTriggerTimes.prepend(time);
if(boatBoxState[ID].recentTriggerTimes.size()>10){
boatBoxState[ID].recentTriggerTimes.removeLast();
}
boatBoxState[ID].triggerSource=source;
emitNewTimeStamp(time, source, ID);
}
}
// converts a voltage given in Volts into a battery state,
// which is in the range from 0 to 1. (for Li-Ion batterys)
double BoxStates::voltageToBatteryState(double voltage)
{
double best = 4.0; // V
double worst = 3.3; // V
return std::max(0.0,std::min(1.0, (voltage-worst)*1.0/(best-worst)));
}
// converts a voltage given in Volts into a battery state,
// which is in the range from 0 to 1. (for AGM Pb batterys)
double BoxStates::voltageToBatteryStatePb(double voltage)
{
double best = 12.4; // V
double worst = 10.7; // V
return std::max(0.0,std::min(1.0, (voltage-worst)*1.0/(best-worst)));
}
// converts a RSSI value given in dBm into a signal strength,
// which is in the range from 0 to 1.
double BoxStates::rssiToSignalStrength(double rssi)
{
double best = -20; // best signal at -20dBm
double worst = -90; // out of range at -90dBm
return std::max(0.0,std::min(1.0, (rssi-worst)*1.0/(best-worst)));
}
// slot for Competition::boatboxBindingChanged()
void BoxStates::updateBoatNames()
{
Competition* c = MainWindow::competition();
for(int boatboxID=1; boatboxID<N_BOATBOXES+1; boatboxID++)
{
QString name=c->getBoatName(c->getBoatIdOfBoatbox(boatboxID));
if(name.contains(QRegExp("[A-Z]")))
{
name.append(tr(" (Zille %1)").arg(c->getBoatIdOfBoatbox(boatboxID)));
}
boatBoxState[boatboxID].txtBoatName.setText(name);
}
}
// TODO: test this function!
// slot for 5Hz timer
void BoxStates::refreshAbsentTimes()
{
checkAbsentTimes();
QTime t;
// timebase
t = timeBaseState.lastAliveTime;
if(t.isValid()){
timeBaseState.txtAbsentSince.setText(getAbsentText(t));
t = timeBaseState.baseTime;
if(t.isValid()){
QTime now = MainWindow::app()->getTimeBaseTime();
int msecs = timeBaseState.lastAliveTime.msecsTo(now); // milliseconds
QTime extrapolatedBaseTime(timeBaseState.baseTime);
extrapolatedBaseTime = extrapolatedBaseTime.addMSecs(msecs);
timeBaseState.txtBaseTime.setText(MainWindow::convertTimeToString(extrapolatedBaseTime) + QString(" (%1ms)").arg(MainWindow::app()->localTimeOffset_ms));
}
}
// triggerstations
t = triggerStationState[START].lastAliveTime;
if(t.isValid()){
triggerStationState[START].txtAbsentSince.setText(getAbsentText(t));
}
t = triggerStationState[GOAL].lastAliveTime;
if(t.isValid()){
triggerStationState[GOAL].txtAbsentSince.setText(getAbsentText(t));
}
// matrix display
t = matrixDisplayState[1].lastAliveTime;
if(t.isValid()){
matrixDisplayState[1].txtAbsentSince.setText(getAbsentText(t));
}
t = matrixDisplayState[2].lastAliveTime;
if(t.isValid()){
matrixDisplayState[2].txtAbsentSince.setText(getAbsentText(t));
}
// boatboxes
for(int id=1; id<=N_BOATBOXES; id++){
t = boatBoxState[id].lastAliveTime[START];
if(t.isValid()){
boatBoxState[id].txtAbsentSince[START].setText(getAbsentText(t));
}
t = boatBoxState[id].lastAliveTime[GOAL];
if(t.isValid()){
boatBoxState[id].txtAbsentSince[GOAL].setText(getAbsentText(t));
}
}
}
// calculates the time between time and now, and returns a
// human readable string
// e.g. 3.4s, 57s, 2m34s, 5h5m
QString BoxStates::getAbsentText(QTime time)
{
QTime now = MainWindow::app()->getTimeBaseTime();
int msecs = time.msecsTo(now); // milliseconds
if(msecs < 10000){ // until 10s
return QString("%1.%2s").arg(QString::number(msecs/1000)).arg(QString::number((msecs%1000)/100)); // e.g. "3.4 s"
}else if(msecs < 60000){ // until 1 minute
return QString("%1s").arg(QString::number(msecs/1000)); // e.g. "37 s"
}else if(msecs < 36000000){
return QString("%1m%2s").arg(QString::number(msecs/60000)).arg(QString::number((msecs%60000)/1000)); // e.g. "5m34s"
}else{
return QString("%1h%2m").arg(QString::number(msecs/3600000)).arg(QString::number((msecs%3600000)/60000)); // e.g. "1h59m"
}
}
void BoxStates::checkAbsentTimes()
{
// check timebase absent time
if(timeBaseState.lastAliveTime.msecsTo(MainWindow::app()->getTimeBaseTime())>ABSENT_WARNING_LEVEL && !timeBaseState.absent)
{
timeBaseState.absent=true;
MainWindow::app()->infoscreen()->appendError(tr("Verbindung zur Zeitbasis abgebrochen"));
}
// check start absent time
if(triggerStationState[START].lastAliveTime.msecsTo(MainWindow::app()->getTimeBaseTime())>ABSENT_WARNING_LEVEL && !triggerStationState[START].absent)
{
triggerStationState[START].absent=true;
MainWindow::app()->infoscreen()->appendError(tr("Verbindung zur Startbox abgebrochen"));
}
if(triggerStationState[START].lastAliveTime.msecsTo(MainWindow::app()->getTimeBaseTime())<ABSENT_WARNING_LEVEL && triggerStationState[START].absent)
{
triggerStationState[START].absent=false;
MainWindow::app()->infoscreen()->appendInfo(tr("Verbindung zur Startbox hergestellt"));
}
// check goal absent time
if(triggerStationState[GOAL].lastAliveTime.msecsTo(MainWindow::app()->getTimeBaseTime())>ABSENT_WARNING_LEVEL && !triggerStationState[GOAL].absent)
{
triggerStationState[GOAL].absent=true;
MainWindow::app()->infoscreen()->appendError(tr("Verbindung zur Zielbox abgebrochen"));
}
if(triggerStationState[GOAL].lastAliveTime.msecsTo(MainWindow::app()->getTimeBaseTime())<ABSENT_WARNING_LEVEL && triggerStationState[GOAL].absent)
{
triggerStationState[GOAL].absent=false;
MainWindow::app()->infoscreen()->appendInfo(tr("Verbindung zur Zielbox hergestellt"));
}
// check matrix display absent time
for(int display=1; display<=2; display++)
{
if(matrixDisplayState[display].lastAliveTime.msecsTo(MainWindow::app()->getTimeBaseTime())>ABSENT_WARNING_LEVEL && !matrixDisplayState[display].absent)
{
matrixDisplayState[display].absent=true;
MainWindow::app()->infoscreen()->appendError(tr("Verbindung zur Kiboko Matrix %1 abgebrochen").arg(display));
}
if(matrixDisplayState[display].lastAliveTime.msecsTo(MainWindow::app()->getTimeBaseTime())<ABSENT_WARNING_LEVEL && matrixDisplayState[display].absent)
{
matrixDisplayState[display].absent=false;
MainWindow::app()->infoscreen()->appendInfo(tr("Verbindung zur Kiboko Matrix %1 hergestellt").arg(display));
}
}
}
void BoxStates::emitNewTimeStamp(QTime time, TimeStamp::Source source, int boatBoxID)
{
if(time > firstTimeBaseTimeReceived){
TimeStamp* ts=new TimeStamp(0, time, source);
ts->setBoatboxID(boatBoxID);
emit receivedNewTimeStamp(ts);
}
}