-
Notifications
You must be signed in to change notification settings - Fork 2
/
model4841-production-lorawan-cMeasurementLoop.cpp
762 lines (636 loc) · 20.8 KB
/
model4841-production-lorawan-cMeasurementLoop.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
Module: model4841-production-lorawan-cMeasurementLoop.cpp
Function:
Sensor sketch measuring and transmitting air-quality info.
Copyright:
See accompanying LICENSE file for copyright and license information.
Author:
Terry Moore, MCCI Corporation July 2019
*/
#include "model4841-production-lorawan-cMeasurementLoop.h"
#ifndef ARDUINO_MCCI_CATENA_4630
# error "This sketch targets the MCCI Catena 4630"
#endif
extern SPIClass gSPI2;
extern bool gfFlash;
/****************************************************************************\
|
| An object to represent the uplink activity
|
\****************************************************************************/
void cMeasurementLoop::begin()
{
// register for polling.
if (! this->m_registered)
{
this->m_registered = true;
gCatena.registerObject(this);
this->m_Pms7003.setCallback(measurementAvailable, this);
this->m_UplinkTimer.begin(this->m_txCycleSec * 1000);
}
if (! this->m_running)
{
this->m_exit = false;
this->m_fsm.init(*this, &cMeasurementLoop::fsmDispatch);
}
}
void cMeasurementLoop::end()
{
if (this->m_running)
{
this->m_exit = true;
this->m_fsm.eval();
}
}
void cMeasurementLoop::requestActive(bool fEnable)
{
if (fEnable)
this->m_rqActive = true;
else
this->m_rqInactive = true;
this->m_fsm.eval();
}
cMeasurementLoop::State cMeasurementLoop::fsmDispatch(
cMeasurementLoop::State currentState,
bool fEntry
)
{
State newState = State::stNoChange;
auto const pHal = this->getHal();
if (fEntry && pHal->isEnabled(this->m_Pms7003.DebugFlags::kTrace))
{
this->getHal()->printf("cMeasurementLoop::fsmDispatch: enter %s\n",
this->getStateName(currentState)
);
}
switch (currentState)
{
case State::stInitial:
newState = State::stInactive;
break;
case State::stInactive:
if (fEntry)
{
this->m_Pms7003.requestOff();
}
if (this->m_rqActive)
{
this->m_rqActive = this->m_rqInactive = false;
this->m_active = true;
this->m_UplinkTimer.retrigger();
newState = State::stWakePms;
}
break;
case State::stSleeping:
if (fEntry)
{
this->m_Pms7003.requestOff();
gLed.Set(McciCatena::LedPattern::Sleeping);
}
if (this->m_rqInactive)
{
this->m_rqActive = this->m_rqInactive = false;
this->m_active = false;
newState = State::stInactive;
}
else if (this->m_UplinkTimer.isready())
newState = State::stWakePms;
else if (this->m_UplinkTimer.getRemaining() > 1500)
this->sleep();
break;
case State::stWakePms:
if (fEntry)
{
this->m_Pms7003.eventWake();
this->setTimer(2 * 60 * 1000);
this->resetMeasurement();
}
if (this->timedOut())
newState = State::stSleepPms;
else if (this->measurementAwake())
{
this->clearTimer();
newState = State::stMeasurePms;
}
break;
case State::stMeasurePms:
if (fEntry)
{
this->setTimer(this->kNumMeasurements * 2 * 1000);
}
if (this->timedOut())
{
newState = State::stSleepPms;
}
else if (this->measurementComplete())
{
this->clearTimer();
this->m_measurement_valid = true;
newState = State::stSleepPms;
}
break;
case State::stSleepPms:
if (fEntry)
{
this->m_Pms7003.requestOff();
this->setTimer(10);
}
if (this->timedOut())
newState = State::stTransmit;
break;
case State::stTransmit:
if (fEntry)
{
TxBuffer_t b;
this->fillTxBuffer(b);
this->startTransmission(b);
}
if (this->txComplete())
{
newState = State::stSleeping;
// calculate the new sleep interval.
this->updateTxCycleTime();
}
break;
case State::stFinal:
break;
default:
break;
}
return newState;
}
/****************************************************************************\
|
| Got a measurement
|
\****************************************************************************/
void cMeasurementLoop::measurementAvailable(
void *pUserData,
const McciCatenaPMS7003::cPMS7003::Measurements<std::uint16_t> *pData,
bool fWarmedUp
)
{
cMeasurementLoop * const pThis = (cMeasurementLoop *)pUserData;
// gCatena.SafePrintf(
// "CF1 pm 1.0=%-5u 2.5=%-5u 10=%-5u ",
// pData->cf1.m1p0, pData->cf1.m2p5, pData->cf1.m10
// );
pThis->processMeasurement(pData, fWarmedUp);
}
/****************************************************************************\
|
| Put a measurement into the list
|
\****************************************************************************/
void cMeasurementLoop::processMeasurement(
const McciCatenaPMS7003::cPMS7003::Measurements<std::uint16_t> *pData,
bool fWarmedUp
)
{
gCatena.SafePrintf(
"ATM pm 1.0=%-5u 2.5=%-5u 10=%-5u ",
pData->atm.m1p0, pData->atm.m2p5, pData->atm.m10
);
gCatena.SafePrintf(
"Dust .3=%-5u .5=%-5u 1.0=%-5u 2.5=%-5u 5=%-5u 10=%-5u%s\n",
pData->dust.m0p3, pData->dust.m0p5, pData->dust.m1p0,
pData->dust.m2p5, pData->dust.m5, pData->dust.m10,
fWarmedUp ? "" : " (warmup)"
);
bool fEvent = false;
if (! this->m_measurement_received)
{
fEvent = true;
this->m_measurement_received = true;
}
if (fWarmedUp)
{
if (this->m_iMeasurement == 0)
{
this->m_fWarmedUp = true;
fEvent = true;
}
const unsigned i = this->m_iMeasurement;
if (i < kNumMeasurements)
{
this->m_Pm.m1p0[i] = pData->atm.m1p0;
this->m_Pm.m2p5[i] = pData->atm.m2p5;
this->m_Pm.m10[i] = pData->atm.m10;
this->m_Dust.m0p3[i] = pData->dust.m0p3;
this->m_Dust.m0p5[i] = pData->dust.m0p5;
this->m_Dust.m1p0[i] = pData->dust.m1p0;
this->m_Dust.m2p5[i] = pData->dust.m2p5;
this->m_Dust.m5[i] = pData->dust.m5;
this->m_Dust.m10[i] = pData->dust.m10;
this->m_iMeasurement = i + 1;
if (i + 1 == kNumMeasurements)
fEvent = true;
}
}
if (fEvent)
this->m_fsm.eval();
}
/****************************************************************************\
|
| Prepare a buffer to be transmitted.
|
\****************************************************************************/
void cMeasurementLoop::fillTxBuffer(cMeasurementLoop::TxBuffer_t& b)
{
auto const savedLed = gLed.Set(McciCatena::LedPattern::Measuring);
b.begin();
Flags flag;
flag = Flags(0);
// insert format byte
b.put(kMessageFormat);
// insert a byte that will become flags later.
std::uint8_t * const pFlag = b.getp();
b.put(std::uint8_t(flag));
// send Vbat
float Vbat = gCatena.ReadVbat();
gCatena.SafePrintf("Vbat: %d mV\n", (int) (Vbat * 1000.0f));
b.putV(Vbat);
flag |= Flags::Vbat;
// send Vdd if we can measure it.
// vBus is sent as 5000 * v
float Vbus = gCatena.ReadVbus();
gCatena.SafePrintf("Vbus: %d mV\n", (int) (Vbus * 1000.0f));
this->setVbus(Vbus);
b.putV(Vbus);
flag |= Flags::Vbus;
// send boot count
uint32_t bootCount;
if (gCatena.getBootCount(bootCount))
{
b.putBootCountLsb(bootCount);
flag |= Flags::Boot;
}
if (this->m_fTempRh)
{
using cTempRh = decltype(this->m_TempRh);
McciCatenaSht3x::cSHT3x::Measurements m;
if (! this->m_TempRh.getTemperatureHumidity(m));
// temperature is 2 bytes from -0x80.00 to +0x7F.FF degrees C
// pressure is 2 bytes, hPa * 10.
// humidity is two bytes, where 0 == 0/65535 and 0xFFFFF == 65535/65535 = 100%.
gCatena.SafePrintf(
"SHT3x: T: %d RH: %d\n",
(int) m.Temperature,
(int) m.Humidity
);
b.putT(m.Temperature);
// no method for 2-byte RH, directly encode it.
b.put2uf((m.Humidity / 100.0f) * 65535.0f);
flag |= Flags::TH;
}
// sort and process
if (this->m_measurement_valid)
{
McciCatenaPMS7003::cPMS7003::Measurements<float> results;
if (this->postProcess(results))
{
flag |= Flags::PM | Flags::Dust;
b.put2uf(this->particle2uf(results.atm.m1p0));
b.put2uf(this->particle2uf(results.atm.m2p5));
b.put2uf(this->particle2uf(results.atm.m10));
b.put2uf(this->particle2uf(results.dust.m0p3));
b.put2uf(this->particle2uf(results.dust.m0p5));
b.put2uf(this->particle2uf(results.dust.m1p0));
b.put2uf(this->particle2uf(results.dust.m2p5));
b.put2uf(this->particle2uf(results.dust.m5));
b.put2uf(this->particle2uf(results.dust.m10));
}
}
if (this->m_fSgpc3)
{
// we'll start by triggering a measurement of the VOC sensor;
// it's important to do this first to make sure sleep timing is
// correct. If the command succeeds, the local variables will
// be set to the values we just read; if it fails, they'll be -1
if (this->m_Sgpc3Sensor.measureIAQ() != 0)
{
gCatena.SafePrintf("Error while measuring IAQ: %s\n",
this->m_Sgpc3Sensor.getError());
}
else
{
auto const tvoc = this->m_Sgpc3Sensor.getTVOC();
b.put2(uint32_t(tvoc));
flag |= Flags::TVOC;
// finally, let's print those to the serial console
gCatena.SafePrintf("TVOC: %d ppb\n", tvoc);
// and then, we'll use remainingWaitTimeMS() to ensure the correct
// Measurement rate
this->m_Sgpc3Sensor.delayMS(this->m_Sgpc3Sensor.remainingWaitTimeMS());
}
// get the baseline value that shuold be stored in non volatile memory
if (this->m_Sgpc3Sensor.getBaseline() != 0)
{
gCatena.SafePrintf("Error while getting Baseline: %s\n",
this->m_Sgpc3Sensor.getError());
}
else
{
gCatena.SafePrintf("Baseline value: %d\n",
this->m_Sgpc3Sensor.getBaselineValue());
}
}
*pFlag = std::uint8_t(flag);
gLed.Set(savedLed);
}
/****************************************************************************\
|
| Reduce a single data set
|
\****************************************************************************/
extern "C" {
static int compare16(const void *pLeft, const void *pRight);
}
static int compare16(const void *pLeft, const void *pRight)
{
auto p = (const std::uint16_t *)pLeft;
auto q = (const std::uint16_t *)pRight;
return (int)*p - (int)*q;
}
void cMeasurementLoop::processOneMeasurement(
float &r,
std::uint16_t *pv
)
{
// set pointers to q1 and q3 cells by counting in symmetrically from the ends of the vector.
// For example if kNumMeasurements is 10, then we call the q1 value pv[2], and the
// q3 value is pv[7]; values [0],[1] are below q1, and [8],[9] are above q3.
const std::uint16_t * const pq1 = pv + kNumMeasurements / 4;
const std::uint16_t * const pq3 = pv + kNumMeasurements - (kNumMeasurements / 4) - 1;
// sort pv in place.
qsort(pv, kNumMeasurements, sizeof(pv[0]), compare16);
// calculate IQR = q3 - q1
std::int32_t iqr = *pq3 - *pq1;
// calculate 1.5 IRQ. It's positive, so >> is well defined.
std::int32_t iqr15 = (3 * iqr) >> 1;
// calcluate the low and high limits.
std::int32_t lowlim = pq1[0] - iqr15;
std::int32_t highlim = pq3[0] + iqr15;
// define left and right pointers for summation.
const std::uint16_t *p1;
const std::uint16_t *p2;
// scan from left to find first value to accumulate.
for (p1 = pv; p1 < pq1 && *p1 < lowlim; ++p1)
;
// scan from right to find last value to accumulate.
for (p2 = pv + kNumMeasurements - 1; pq3 < p2 && *p2 > highlim; --p2)
;
// sum the values.
std::uint32_t sum = 0;
for (auto p = p1; p <= p2; ++p)
sum += *p;
// divide by n * 65536.0
float const div = (p2 - p1 + 1) * 65535.0f;
// store into r.
r = sum / div;
}
/****************************************************************************\
|
| Reduce all the data
|
\****************************************************************************/
bool cMeasurementLoop::postProcess(
McciCatenaPMS7003::cPMS7003::Measurements<float> &results
)
{
std::uint16_t m[kNumMeasurements];
processOneMeasurement(results.atm.m1p0, this->m_Pm.m1p0);
processOneMeasurement(results.atm.m2p5, this->m_Pm.m2p5);
processOneMeasurement(results.atm.m10, this->m_Pm.m10);
processOneMeasurement(results.dust.m0p3, this->m_Dust.m0p3);
processOneMeasurement(results.dust.m0p5, this->m_Dust.m0p5);
processOneMeasurement(results.dust.m1p0, this->m_Dust.m1p0);
processOneMeasurement(results.dust.m2p5, this->m_Dust.m2p5);
processOneMeasurement(results.dust.m5, this->m_Dust.m5);
processOneMeasurement(results.dust.m10, this->m_Dust.m10);
return true;
}
/****************************************************************************\
|
| Start uplink of data
|
\****************************************************************************/
void cMeasurementLoop::startTransmission(
cMeasurementLoop::TxBuffer_t &b
)
{
auto const savedLed = gLed.Set(McciCatena::LedPattern::Sending);
// by using a lambda, we can access the private contents
auto sendBufferDoneCb =
[](void *pClientData, bool fSuccess)
{
auto const pThis = (cMeasurementLoop *)pClientData;
pThis->m_txpending = false;
pThis->m_txcomplete = true;
pThis->m_txerr = ! fSuccess;
pThis->m_fsm.eval();
};
bool fConfirmed = false;
if (gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fConfirmedUplink))
{
gCatena.SafePrintf("requesting confirmed tx\n");
fConfirmed = true;
}
this->m_txpending = true;
this->m_txcomplete = this->m_txerr = false;
if (! gLoRaWAN.SendBuffer(b.getbase(), b.getn(), sendBufferDoneCb, (void *)this, fConfirmed, kUplinkPort))
{
// uplink wasn't launched.
this->m_txcomplete = true;
this->m_txerr = true;
this->m_fsm.eval();
}
}
void cMeasurementLoop::sendBufferDone(bool fSuccess)
{
this->m_txpending = false;
this->m_txcomplete = true;
this->m_txerr = ! fSuccess;
this->m_fsm.eval();
}
/****************************************************************************\
|
| The Polling function --
|
\****************************************************************************/
void cMeasurementLoop::poll()
{
bool fEvent;
// no need to evaluate unless something happens.
fEvent = false;
// if we're not active, and no request, nothing to do.
if (! this->m_active)
{
if (! this->m_rqActive)
return;
// we're asked to go active. We'll want to eval.
fEvent = true;
}
if (this->m_fTimerActive)
{
if ((millis() - this->m_timer_start) >= this->m_timer_delay)
{
this->m_fTimerActive = false;
this->m_fTimerEvent = true;
fEvent = true;
}
}
// check the transmit time.
if (this->m_UplinkTimer.peekTicks() != 0)
{
fEvent = true;
}
if (fEvent)
this->m_fsm.eval();
}
/****************************************************************************\
|
| Update the TxCycle count.
|
\****************************************************************************/
void cMeasurementLoop::updateTxCycleTime()
{
auto txCycleCount = this->m_txCycleCount;
// update the sleep parameters
if (txCycleCount > 1)
{
// values greater than one are decremented and ultimately reset to default.
this->m_txCycleCount = txCycleCount - 1;
}
else if (txCycleCount == 1)
{
// it's now one (otherwise we couldn't be here.)
gCatena.SafePrintf("resetting tx cycle to default: %u\n", this->m_txCycleSec_Permanent);
this->setTxCycleTime(this->m_txCycleSec_Permanent, 0);
}
else
{
// it's zero. Leave it alone.
}
}
/****************************************************************************\
|
| Handle sleep between measurements
|
\****************************************************************************/
void cMeasurementLoop::sleep()
{
const bool fDeepSleep = checkDeepSleep();
if (! this->m_fPrintedSleeping)
this->doSleepAlert(fDeepSleep);
if (fDeepSleep)
this->doDeepSleep();
}
bool cMeasurementLoop::checkDeepSleep()
{
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
bool fDeepSleep;
std::uint32_t const sleepInterval = this->m_UplinkTimer.getRemaining() / 1000;
if (sleepInterval < 2)
fDeepSleep = false;
else if (fDeepSleepTest)
{
fDeepSleep = true;
}
#ifdef USBCON
else if (Serial.dtr())
{
fDeepSleep = false;
}
#endif
else if (gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDisableDeepSleep))
{
fDeepSleep = false;
}
else if ((gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fUnattended)) != 0)
{
fDeepSleep = true;
}
else
{
fDeepSleep = false;
}
return fDeepSleep;
}
void cMeasurementLoop::doSleepAlert(bool fDeepSleep)
{
this->m_fPrintedSleeping = true;
if (fDeepSleep)
{
bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
const uint32_t deepSleepDelay = fDeepSleepTest ? 10 : 30;
gCatena.SafePrintf("using deep sleep in %u secs"
#ifdef USBCON
" (USB will disconnect while asleep)"
#endif
": ",
deepSleepDelay
);
// sleep and print
gLed.Set(McciCatena::LedPattern::TwoShort);
for (auto n = deepSleepDelay; n > 0; --n)
{
uint32_t tNow = millis();
while (uint32_t(millis() - tNow) < 1000)
{
gCatena.poll();
yield();
}
gCatena.SafePrintf(".");
}
gCatena.SafePrintf("\nStarting deep sleep.\n");
uint32_t tNow = millis();
while (uint32_t(millis() - tNow) < 100)
{
gCatena.poll();
yield();
}
}
else
gCatena.SafePrintf("using light sleep\n");
}
void cMeasurementLoop::doDeepSleep()
{
// bool const fDeepSleepTest = gCatena.GetOperatingFlags() &
// static_cast<uint32_t>(gCatena.OPERATING_FLAGS::fDeepSleepTest);
std::uint32_t const sleepInterval = this->m_UplinkTimer.getRemaining() / 1000;
if (sleepInterval == 0)
return;
/* ok... now it's time for a deep sleep */
gLed.Set(McciCatena::LedPattern::Off);
this->deepSleepPrepare();
/* sleep */
gCatena.Sleep(sleepInterval);
/* recover from sleep */
this->deepSleepRecovery();
/* and now... we're awake again. trigger another measurement */
this->m_fsm.eval();
}
void cMeasurementLoop::deepSleepPrepare(void)
{
this->m_Pms7003.end();
Serial.end();
Wire.end();
SPI.end();
if (gfFlash)
gSPI2.end();
}
void cMeasurementLoop::deepSleepRecovery(void)
{
Serial.begin();
Wire.begin();
SPI.begin();
if (gfFlash)
gSPI2.begin();
this->m_Pms7003.begin();
}