-
Notifications
You must be signed in to change notification settings - Fork 0
/
Catena4610_cMeasurementLoop.h
476 lines (404 loc) · 14 KB
/
Catena4610_cMeasurementLoop.h
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
/*
Module: Catena4610_cMeasurementLoop.h
Function:
cMeasurementLoop definitions.
Copyright:
See accompanying LICENSE file for copyright and license information.
Author:
Dhinesh Kumar Pitchai, MCCI Corporation May 2021
*/
#ifndef _Catena4610_cMeasurementLoop_h_
# define _Catena4610_cMeasurementLoop_h_
#pragma once
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_BME280.h>
#include <Catena_Download.h>
#include <Catena_FSM.h>
#include <Catena_Led.h>
#include <Catena_Log.h>
#include <Catena_Mx25v8035f.h>
#include <Catena_PollableInterface.h>
#include <Catena_Si1133.h>
#include <Catena_Timer.h>
#include <Catena_TxBuffer.h>
#include <Catena.h>
#include <mcciadk_baselib.h>
#include <stdlib.h>
#include <Catena_Date.h>
#include <cstdint>
#include <cstring>
extern McciCatena::Catena gCatena;
extern McciCatena::Catena::LoRaWAN gLoRaWAN;
extern McciCatena::StatusLed gLed;
#define SUCCESS 0
#define BUFF_OVERFLOW 1
#define RUNT_PACKET 2
#define BAD_CRC 3
#define INVALID_MSG_ID 4
static constexpr unsigned kT35 = 5;
static const char* sessionType[] = {
"Custom_Application",
"ClassicFED3",
"ClosedEconomy_PR1",
"Dispenser",
"Extinction",
"FixedRatio1",
"FR_Customizable",
"FreeFeeding",
"MenuExample",
"Optogenetic_Self_Stim",
"Pavlovian",
"ProbReversalTask",
"ProgressiveRatio",
"RandomRatio"
};
static const char* eventActive[] = {
"Unknown",
"Left",
"LeftShort",
"LeftWithPellet",
"LeftinTimeout",
"LeftDuringDispense",
"Right",
"RightShort",
"RightWithPellet",
"RightinTimeout",
"RightDuringDispense",
"Pellet"
};
namespace McciCatena4610 {
/****************************************************************************\
|
| An object to represent the uplink activity
|
\****************************************************************************/
class cMeasurementBase
{
};
class cMeasurementFormat : public cMeasurementBase
{
public:
static constexpr uint8_t kMessageFormat = 0x24;
enum class Flags : uint8_t
{
Vbat = 1 << 0, // vBat
Vcc = 1 << 1, // system voltage
Vbus = 1 << 2, // Vbus input
Boot = 1 << 3, // boot count
TPH = 1 << 4, // temperature, pressure, humidity
Light = 1 << 5, // light (IR, white, UV)
FED3 = 1 << 6, // pellet feeder 3 data
};
// buffer size for uplink data
static constexpr size_t kTxBufferSize = 54;
// the structure of a measurement
struct Measurement
{
//----------------
// the subtypes:
//----------------
// environmental measurements
struct Env
{
// temperature (in degrees C)
float Temperature;
// pressure (in millibars/hPa)
float Pressure;
// humidity (in % RH)
float Humidity;
};
// ambient light measurements
struct Light
{
// "white" light, in w/m^2
float White;
};
// fed3 data
struct FED3
{
uint8_t DataBytes[10][44];
};
//---------------------------
// the actual members as POD
//---------------------------
// flags of entries that are valid.
Flags flags;
// measured battery voltage, in volts
float Vbat;
// measured system Vdd voltage, in volts
float Vsystem;
// measured USB bus voltage, in volts.
float Vbus;
// boot count
uint32_t BootCount;
// environmental data
Env env;
// ambient light
Light light;
// pellet feeder data
FED3 fed3;
};
};
class cMeasurementLoop : public McciCatena::cPollableObject
{
public:
// some parameters
static constexpr std::uint8_t kUplinkPort = 3;
static constexpr bool kEnableDeepSleep = false;
using MeasurementFormat = cMeasurementFormat;
using Measurement = MeasurementFormat::Measurement;
using Flags = MeasurementFormat::Flags;
static constexpr std::uint8_t kMessageFormat = MeasurementFormat::kMessageFormat;
void deepSleepPrepare();
void deepSleepRecovery();
uint8_t getRxBuffer(uint8_t &errcode);
uint16_t getMessageWord(unsigned offset);
uint16_t calcCRC(uint8_t u8length);
uint8_t validateAnswer();
enum OPERATING_FLAGS : uint32_t
{
fUnattended = 1 << 0,
fManufacturingTest = 1 << 1,
fConfirmedUplink = 1 << 16,
fDisableDeepSleep = 1 << 17,
fQuickLightSleep = 1 << 18,
fDeepSleepTest = 1 << 19,
};
enum DebugFlags : std::uint32_t
{
kError = 1 << 0,
kWarning = 1 << 1,
kTrace = 1 << 2,
kInfo = 1 << 3,
};
// constructor
cMeasurementLoop(
)
: m_txCycleSec_Permanent(3 * 60) // default uplink interval
, m_txCycleSec(30) // initial uplink interval
, m_txCycleCount(10) // initial count of fast uplinks
, m_DebugFlags(DebugFlags(kError | kTrace))
{};
// neither copyable nor movable
cMeasurementLoop(const cMeasurementLoop&) = delete;
cMeasurementLoop& operator=(const cMeasurementLoop&) = delete;
cMeasurementLoop(const cMeasurementLoop&&) = delete;
cMeasurementLoop& operator=(const cMeasurementLoop&&) = delete;
enum class SerialMessageOffset : uint8_t
{
ID,
ADDR_HI,
ADDR_LO,
BYTEC,
};
/**
* @enum MESSAGE
* @brief
* Indices to datagram frame positions
*/
enum MESSAGE
{
ID = int(SerialMessageOffset::ID), //!< Index of ID field
ADD_HI = int(SerialMessageOffset::ADDR_HI), //!< Index of Address high byte
ADD_LO = int(SerialMessageOffset::ADDR_LO), //!< Index of Address low byte
BYTE_CNT = int(SerialMessageOffset::BYTEC), //!< Index of byte counter
};
enum class State : std::uint8_t
{
stNoChange = 0, // this name must be present: indicates "no change of state"
stInitial, // this name must be present: it's the starting state.
stInactive, // parked; not doing anything.
stSleeping, // active; sleeping between measurements
stWarmup, // transition from inactive to measure, get some data.
stMeasure, // take measurents
stTransmit, // transmit data
stFinal, // this name must be present, it's the terminal state.
};
static constexpr const char *getStateName(State s)
{
switch (s)
{
case State::stNoChange: return "stNoChange";
case State::stInitial: return "stInitial";
case State::stInactive: return "stInactive";
case State::stSleeping: return "stSleeping";
case State::stWarmup: return "stWarmup";
case State::stMeasure: return "stMeasure";
case State::stTransmit: return "stTransmit";
case State::stFinal: return "stFinal";
default: return "<<unknown>>";
}
}
// concrete type for uplink data buffer
using TxBuffer_t = McciCatena::AbstractTxBuffer_t<MeasurementFormat::kTxBufferSize>;
uint8_t lastRec;
uint16_t u16timeOut;
uint32_t u32time, u32timeOut;
uint8_t u8BufferSize;
uint16_t u16InCnt, u16errCnt;
uint8_t errCode;
uint8_t num_bytes;
uint8_t au8Buffer[44];
// initialize measurement FSM.
void begin();
void end();
void setTxCycleTime(
std::uint32_t txCycleSec,
std::uint32_t txCycleCount
)
{
this->m_txCycleSec = txCycleSec;
this->m_txCycleCount = txCycleCount;
this->m_UplinkTimer.setInterval(txCycleSec * 1000);
if (this->m_UplinkTimer.peekTicks() != 0)
this->m_fsm.eval();
}
std::uint32_t getTxCycleTime()
{
return this->m_txCycleSec;
}
virtual void poll() override;
void setBme280(bool fEnable)
{
this->m_fBme280 = fEnable;
}
void setVbus(float Vbus)
{
// set threshold value as 4.0V as there is reverse voltage
// in vbus(~3.5V) while powered from battery in 4610.
this->m_fUsbPower = (Vbus > 4.0f) ? true : false;
}
// request that the measurement loop be active/inactive
void requestActive(bool fEnable);
// return true if a given debug mask is enabled.
bool isTraceEnabled(DebugFlags mask) const
{
return this->m_DebugFlags & mask;
}
// register an additional SPI for sleep/resume
// can be called before begin().
void registerSecondSpi(SPIClass *pSpi)
{
this->m_pSPI2 = pSpi;
}
private:
// sleep handling
void sleep();
bool checkDeepSleep();
void doSleepAlert(bool fDeepSleep);
void doDeepSleep();
// read data
void updateSynchronousMeasurements();
void updateLightMeasurements();
void resetMeasurements();
void updatePelletFeederData();
// telemetry handling.
void fillTxBuffer(TxBuffer_t &b, Measurement const & mData);
void startTransmission(TxBuffer_t &b);
void sendBufferDone(bool fSuccess);
bool txComplete()
{
return this->m_txcomplete;
}
static std::uint16_t activity2uf(float v)
{
return McciCatena::TxBuffer_t::f2uflt16(v);
}
void updateTxCycleTime();
// timeout handling
// set the timer
void setTimer(std::uint32_t ms);
// clear the timer
void clearTimer();
// test (and clear) the timed-out flag.
bool timedOut();
// instance data
private:
McciCatena::cFSM<cMeasurementLoop, State> m_fsm;
// evaluate the control FSM.
State fsmDispatch(State currentState, bool fEntry);
Adafruit_BME280 m_BME280;
McciCatena::Catena_Si1133 m_si1133;
// second SPI class
SPIClass *m_pSPI2;
// debug flags
DebugFlags m_DebugFlags;
// true if object is registered for polling.
bool m_registered : 1;
// true if object is running.
bool m_running : 1;
// true to request exit
bool m_exit : 1;
// true if in active uplink mode, false otehrwise.
bool m_active : 1;
// set true to request transition to active uplink mode; cleared by FSM
bool m_rqActive : 1;
// set true to request transition to inactive uplink mode; cleared by FSM
bool m_rqInactive : 1;
// set true if event timer times out
bool m_fTimerEvent : 1;
// set true while evenet timer is active.
bool m_fTimerActive : 1;
// set true if USB power is present.
bool m_fUsbPower : 1;
// set true if BME280 is present
bool m_fBme280 : 1;
// set true if SI1133 is present
bool m_fSi1133: 1;
// set true while a transmit is pending.
bool m_txpending : 1;
// set true when a transmit completes.
bool m_txcomplete : 1;
// set true when a transmit complete with an error.
bool m_txerr : 1;
// set true when we've printed how we plan to sleep
bool m_fPrintedSleeping : 1;
// set true when SPI2 is active
bool m_fSpi2Active: 1;
// set true if FED3 event is left poke
bool m_fLeftPoke : 1;
// set true if FED3 event is right poke
bool m_fRightPoke : 1;
// set true if FED3 event is pellet
bool m_fPelletPoke : 1;
// set true if if FED3 right poke is changed
bool m_fRightCount : 1;
// number of events happened
std::uint8_t m_eventCount;
// previous event happened
std::uint8_t m_prevEvent;
// uplink time control
McciCatena::cTimer m_UplinkTimer;
std::uint32_t m_txCycleSec;
std::uint32_t m_txCycleCount;
std::uint32_t m_txCycleSec_Permanent;
// simple timer for timing-out sensors.
std::uint32_t m_timer_start;
std::uint32_t m_timer_delay;
// the current measurement
Measurement m_data;
// the data to write to the file
Measurement m_FileData;
TxBuffer_t m_FileTxBuffer;
// index of buffer to be written to Tx buffer
std::uint8_t m_BufferIndex;
};
//
// operator overloads for ORing structured flags
//
static constexpr cMeasurementLoop::Flags operator| (const cMeasurementLoop::Flags lhs, const cMeasurementLoop::Flags rhs)
{
return cMeasurementLoop::Flags(uint8_t(lhs) | uint8_t(rhs));
};
static constexpr cMeasurementLoop::Flags operator& (const cMeasurementLoop::Flags lhs, const cMeasurementLoop::Flags rhs)
{
return cMeasurementLoop::Flags(uint8_t(lhs) & uint8_t(rhs));
};
static cMeasurementLoop::Flags operator|= (cMeasurementLoop::Flags &lhs, const cMeasurementLoop::Flags &rhs)
{
lhs = lhs | rhs;
return lhs;
};
} // namespace McciCatena4610
#endif /* _Catena4610_cMeasurementLoop_h_ */