-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSensor.h
More file actions
303 lines (204 loc) · 8.16 KB
/
Sensor.h
File metadata and controls
303 lines (204 loc) · 8.16 KB
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
#ifndef Sensor_h
#define Sensor_h
#include "config.h"
#include "utilities.h"
#include "Settings.h"
#define DEFAULT_SCALE 1.00
#define DEFAULT_OFFSET 0.00
#define NOT_DEFINED -1
const uint8_t SENSOR_NUM_SAMPLES = 20;
const uint8_t SENSOR_SAMPLING_PERIOD = 1;
const uint8_t SENSOR_SAMPLING_PHASE = 0;
const uint8_t SENSOR_NUM_PERIODS = 3;
const int SENSOR_MEDIAN_READING = 512;
const float SENSOR_GRAD_PER_SEC = (float) 360 / TIMER_ONE_SEC;
enum SensorParam {
SENSOR_PARAM_SCALE,
SENSOR_PARAM_OFFSET,
SENSOR_NUMPARAMS
};
enum SensorPrintParam {
SENSOR_PRINT_PARAM,
SENSOR_PRINT_DUMP
};
/**
* @brief Sensor class implements a running average of a series of samples from an analog input.
*
*/
class Sensor {
public:
Sensor(int pin, float offset = DEFAULT_OFFSET,
float scale = DEFAULT_SCALE,
uint8_t num_samples = SENSOR_NUM_SAMPLES,
uint8_t sampling_period = SENSOR_SAMPLING_PERIOD,
uint8_t sampling_phase = SENSOR_SAMPLING_PHASE);
// Takes measurement and accumulate the value. Multiple measurements will be averaged when reading is called.
void sample();
// Initialization
void init();
// triggered when the sensor is init
virtual void on_init(){ _active = true; _ready = false; _calibrate = false; };
virtual void reset();
float reading(){ return _avg_reading; };
// triggered on the readings counter overflow
virtual void on_counter_overflow(){;};
// accumulate reading in the _reading sum
virtual void increment_sum(int reading){;};
// Compute the reading of the sensor, converted to measurement units (offset/scale applied).
virtual void compute_reading(){;};
// rounded reading
long readingR() { return round(reading()); };
// Returns true if necessary number of samples has been taken already
bool ready() { return _ready; };
void clear_ready() { _ready = false; };
virtual void dump() {;};
// print sensor parameters
virtual void print();
virtual void calibrate() { _calibrate = !_calibrate; };
void set_output(Print* stream) { _stream = stream; };
virtual void setParam(float value, SensorParam p) { _param[p] = value; compute_reading();};
float getParam(SensorParam p) { return _param[p]; };
void suspend() { _active = false; };
void resume() { _active = true; };
protected:
virtual float transpose_reading(float value) { return value * _param[SENSOR_PARAM_SCALE] + _param[SENSOR_PARAM_OFFSET]; };
// ADC input pin number
int _pin;
// sensor is ready for the reading computation
bool _ready;
// counter of readings
uint8_t _counter;
// qty of readings
uint8_t _num_samples;
// transpose factors (used for calculation of the sensor reading)
float _param[SENSOR_NUMPARAMS];
// number of ticks between the samples
uint8_t _sampling_period;
// offset in ticks for the first reading
uint8_t _sampling_phase;
// counter of the sample() calls
uint8_t _sample_counter;
// calculated sensor reading
volatile float _avg_reading;
// accumulated value for readings used for the sensor reading calculation
long _reading_sum;
// keeps the last ADC reading
int _last_reading;
// if true the sample() call is void
bool _active;
// if true the sensor is in calibrate mode
bool _calibrate;
Print* _stream;
};
class SimpleSensor : public Sensor {
public:
SimpleSensor(int pin, float offset = DEFAULT_OFFSET,
float scale = DEFAULT_SCALE,
uint8_t num_samples = SENSOR_NUM_SAMPLES,
uint8_t sampling_period = SENSOR_SAMPLING_PERIOD,
uint8_t sampling_phase = SENSOR_SAMPLING_PHASE);
void on_init() override;
void reset() override;
void increment_sum(int reading) override;
void compute_reading() override;
void on_counter_overflow() override { _ready = true;};
void dump() override;
private:
// pointer to the readings storage
int *_readings;
};
/**
* @brief RMS sensor class is for measuring the effective amplitude and the period of the periodic signal.
* Amplitude is measured using the True RMS method.
*
*/
class RMSSensor : public Sensor {
public:
RMSSensor(int pin, float offset = DEFAULT_OFFSET,
float scale = DEFAULT_SCALE,
uint8_t num_samples = SENSOR_NUM_SAMPLES,
uint8_t sampling_period = SENSOR_SAMPLING_PERIOD,
uint8_t sampling_phase = SENSOR_SAMPLING_PHASE,
uint16_t num_periods = SENSOR_NUM_PERIODS);
void increment_sum(int reading) override;
void compute_reading() override;
void reset() override;
void on_init() override;
void on_counter_overflow() override;
void dump() override;
void print() override;
// returns avg number of ticks corresponding to the period of the signal
float get_period() { return _avg_period; };
// returns the frequency of the signal in Hz
float get_frequency() { return _avg_frequency; };
int get_median_error() { return _median_error ; };
bool bad_sine() { bool lbs = _last_bad_sine; _last_bad_sine = _bad_sine; return _bad_sine && lbs; };
protected:
float transpose_reading(float value) override { return value * _param[SENSOR_PARAM_SCALE]; };
void setParam(float value, SensorParam p) override;
private:
// median reading
int _median;
// variables needed for calibration
int _running_median_error;
int _median_error;
// average period computed
volatile float _avg_period;
// average frequency computed
volatile float _avg_frequency;
// if true bad sine detected
volatile bool _bad_sine;
volatile bool _last_bad_sine;
// last observed amplitude of the signal in ADC units
int _last_amplitude;
float _expected_delta;
int _running_max_delta;
float _max_delta_deviation;
// counter of ticks elapsed since the start of the period
int _period_tick_counter;
// qty of detected periods
int _period_counter;
// pointer at the last detected period
int _period_index;
// max number of periods to analyze
uint16_t _num_periods;
// accumulated periods in ticks.
int _period_sum;
// keeps the counter value at the start of the period
int _period_start;
// accumulated sum of squares within the period
long _running_sum;
long *_sq_deltas;
int *_periods;
};
/**
* @brief SensorManager class orchestrates sensor sampling and parameter handling
*
*/
class SensorManager {
public:
SensorManager( Settings * settings , Print * stream = nullptr) {
_stream = stream;
_active = true;
_settings = settings;
};
void register_sensor(Sensor* sensor);
// Take a sample for all the registered sensors
void sample();
Sensor* get(uint8_t ptr) { return _sensors[ptr]; };
void print(uint8_t ptr, SensorPrintParam mode = SENSOR_PRINT_PARAM );
uint8_t get_num_sensors() { return _num_sensors; };
// save sensor params to EEPROM
void saveParams();
// load sensor params from EEPROM. If sensor params were not saved before, they are initialized in EEPROM
void loadParams();
void suspend() { _active = false; };
void resume() { _active = true; };
private:
Print * _stream;
Settings * _settings;
Sensor* _sensors[MAX_NUM_SENSORS];
uint8_t _num_sensors = 0;
bool _active;
};
#endif