-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfrequency_counter.h
354 lines (320 loc) · 11.3 KB
/
frequency_counter.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
// frequency_counter.h
#pragma once
#include "scheduler.h"
#include "home_assistant.h"
namespace ustd {
#ifdef __ESP32__
#define G_INT_ATTR IRAM_ATTR
#else
#ifdef __ESP__
#define G_INT_ATTR ICACHE_RAM_ATTR
#else
#define G_INT_ATTR
#endif
#endif
#define USTD_MAX_PIRQS (10)
volatile unsigned long pirqcounter[USTD_MAX_PIRQS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
volatile unsigned long plastIrqTimer[USTD_MAX_PIRQS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
volatile unsigned long pbeginIrqTimer[USTD_MAX_PIRQS] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
void G_INT_ATTR ustd_pirq_master(uint8_t irqno) {
unsigned long curr = micros();
if (pbeginIrqTimer[irqno] == 0)
pbeginIrqTimer[irqno] = curr;
else
++pirqcounter[irqno];
plastIrqTimer[irqno] = curr;
}
void G_INT_ATTR ustd_pirq0() {
ustd_pirq_master(0);
}
void G_INT_ATTR ustd_pirq1() {
ustd_pirq_master(1);
}
void G_INT_ATTR ustd_pirq2() {
ustd_pirq_master(2);
}
void G_INT_ATTR ustd_pirq3() {
ustd_pirq_master(3);
}
void G_INT_ATTR ustd_pirq4() {
ustd_pirq_master(4);
}
void G_INT_ATTR ustd_pirq5() {
ustd_pirq_master(5);
}
void G_INT_ATTR ustd_pirq6() {
ustd_pirq_master(6);
}
void G_INT_ATTR ustd_pirq7() {
ustd_pirq_master(7);
}
void G_INT_ATTR ustd_pirq8() {
ustd_pirq_master(8);
}
void G_INT_ATTR ustd_pirq9() {
ustd_pirq_master(9);
}
void (*ustd_pirq_table[USTD_MAX_PIRQS])() = {ustd_pirq0, ustd_pirq1, ustd_pirq2, ustd_pirq3,
ustd_pirq4, ustd_pirq5, ustd_pirq6, ustd_pirq7,
ustd_pirq8, ustd_pirq9};
unsigned long getResetpIrqCount(uint8_t irqno) {
unsigned long count = (unsigned long)-1;
noInterrupts();
if (irqno < USTD_MAX_PIRQS) {
count = pirqcounter[irqno];
pirqcounter[irqno] = 0;
}
interrupts();
return count;
}
double frequencyMultiplicator = 1000000.0;
double getResetpIrqFrequency(uint8_t irqno, unsigned long minDtUs = 50) {
double frequency = 0.0;
noInterrupts();
if (irqno < USTD_MAX_PIRQS) {
unsigned long count = pirqcounter[irqno];
unsigned long dt = timeDiff(pbeginIrqTimer[irqno], plastIrqTimer[irqno]);
if (dt > minDtUs) { // Ignore small Irq flukes
frequency = (count * frequencyMultiplicator) / dt; // = count/2.0*1000.0000 uS / dt;
// no. of waves (count/2) / dt.
}
pbeginIrqTimer[irqno] = 0;
pirqcounter[irqno] = 0;
plastIrqTimer[irqno] = 0;
}
interrupts();
return frequency;
}
class FrequencyCounter {
/*! Interrupt driven frequency counter.
On an ESP32, measurement of frequencies at a GPIO are possible in ranges of 0-250kHz.
There are six measurement modes:
LOWFREQUENCY_FAST (good for measurements <50Hz, not much filtering)
LOWFREQUENCY_MEDIUM (good for sub-Hertz enabled measurements, e.g. Geiger-counters)
LOWFREQUENCY_LONGTERM (good for <50Hz, little deviation, high precision)
The HIGHFREQUENCY modes automatically reset the filter, if the input signal drops to
0Hz.
HIGHFREQUENCY_FAST (no filtering)
HIGHFREQUENCY_MEDIUM (some filtering)
HIGHFREQUENCY_LONGTERM (string filtering, good for precision measurement of signals with little
deviation)
Precision <80kHz is better than 0.0005%. Interrupt load > 80kHz impacts the
performance of the ESP32 severely and error goes up to 2-5%.
*/
public:
enum InterruptMode { IM_RISING, IM_FALLING, IM_CHANGE };
enum MeasureMode {
LOWFREQUENCY_FAST,
LOWFREQUENCY_MEDIUM,
LOWFREQUENCY_LONGTERM,
HIGHFREQUENCY_FAST,
HIGHFREQUENCY_MEDIUM,
HIGHFREQUENCY_LONGTERM
};
String FREQUENCY_COUNTER_VERSION = "0.1.0";
Scheduler *pSched;
int tID;
String name;
uint8_t pin_input;
uint8_t irqno_input;
int8_t interruptIndex_input;
MeasureMode measureMode;
InterruptMode irqMode;
bool detectZeroChange = false;
bool irqsAttached = false;
ustd::sensorprocessor frequency = ustd::sensorprocessor(4, 600, 0.01);
double inputFrequencyVal = 0.0;
double frequencyRenormalisation = 1.0;
uint8_t ipin = 255;
#ifdef __ESP__
HomeAssistant *pHA;
#endif
FrequencyCounter(String name, uint8_t pin_input, int8_t interruptIndex_input,
MeasureMode measureMode = HIGHFREQUENCY_MEDIUM,
InterruptMode irqMode = InterruptMode::IM_FALLING)
: name(name), pin_input(pin_input), interruptIndex_input(interruptIndex_input),
measureMode(measureMode), irqMode(irqMode) {
setMeasureMode(measureMode, true);
}
~FrequencyCounter() {
if (irqsAttached) {
detachInterrupt(irqno_input);
}
}
void setMeasureMode(MeasureMode mode, bool silent = false) {
switch (mode) {
case LOWFREQUENCY_FAST:
detectZeroChange = false;
measureMode = LOWFREQUENCY_FAST;
frequency.smoothInterval = 4;
frequency.pollTimeSec = 15;
frequency.eps = 0.01;
frequency.reset();
break;
case LOWFREQUENCY_MEDIUM:
detectZeroChange = false;
measureMode = LOWFREQUENCY_MEDIUM;
frequency.smoothInterval = 12;
frequency.pollTimeSec = 120;
frequency.eps = 0.01;
frequency.reset();
break;
case LOWFREQUENCY_LONGTERM:
detectZeroChange = false;
measureMode = LOWFREQUENCY_LONGTERM;
frequency.smoothInterval = 60;
frequency.pollTimeSec = 600;
frequency.eps = 0.001;
frequency.reset();
break;
case HIGHFREQUENCY_FAST:
detectZeroChange = true;
measureMode = HIGHFREQUENCY_FAST;
frequency.smoothInterval = 1;
frequency.pollTimeSec = 15;
frequency.eps = 0.1;
frequency.reset();
break;
case HIGHFREQUENCY_MEDIUM:
detectZeroChange = true;
measureMode = HIGHFREQUENCY_MEDIUM;
frequency.smoothInterval = 10;
frequency.pollTimeSec = 120;
frequency.eps = 0.01;
frequency.reset();
break;
default:
case HIGHFREQUENCY_LONGTERM:
detectZeroChange = true;
measureMode = HIGHFREQUENCY_LONGTERM;
frequency.smoothInterval = 60;
frequency.pollTimeSec = 600;
frequency.eps = 0.001;
frequency.reset();
break;
}
if (!silent)
publishMeasureMode();
}
bool begin(Scheduler *_pSched) {
pSched = _pSched;
pinMode(pin_input, INPUT_PULLUP);
if (interruptIndex_input >= 0 && interruptIndex_input < USTD_MAX_PIRQS) {
irqno_input = digitalPinToInterrupt(pin_input);
switch (irqMode) {
case IM_FALLING:
attachInterrupt(irqno_input, ustd_pirq_table[interruptIndex_input], FALLING);
frequencyMultiplicator = 1000000.0;
break;
case IM_RISING:
attachInterrupt(irqno_input, ustd_pirq_table[interruptIndex_input], RISING);
frequencyMultiplicator = 1000000.0;
break;
case IM_CHANGE:
attachInterrupt(irqno_input, ustd_pirq_table[interruptIndex_input], CHANGE);
frequencyMultiplicator = 500000.0;
break;
}
irqsAttached = true;
} else {
return false;
}
auto ft = [=]() { this->loop(); };
tID = pSched->add(ft, name, 2000000); // uS schedule
auto fnall = [=](String topic, String msg, String originator) {
this->subsMsg(topic, msg, originator);
};
pSched->subscribe(tID, name + "/frequency/#", fnall);
return true;
}
#ifdef __ESP__
void registerHomeAssistant(String homeAssistantFriendlyName, String projectName = "",
String icon = "mdi:gauge",
String homeAssistantDiscoveryPrefix = "homeassistant") {
pHA = new HomeAssistant(name, tID, homeAssistantFriendlyName, projectName,
FREQUENCY_COUNTER_VERSION, homeAssistantDiscoveryPrefix);
pHA->addSensor("frequency", "Frequency", "Hz", "None", icon);
pHA->begin(pSched);
publish();
}
#endif
void publishMeasureMode() {
switch (measureMode) {
case LOWFREQUENCY_FAST:
pSched->publish(name + "/sensor/mode", "LOWFREQUENCY_FAST");
break;
case LOWFREQUENCY_MEDIUM:
pSched->publish(name + "/sensor/mode", "LOWFREQUENCY_MEDIUM");
break;
case LOWFREQUENCY_LONGTERM:
pSched->publish(name + "/sensor/mode", "LOWFREQUENCY_LONGTERM");
break;
case HIGHFREQUENCY_FAST:
pSched->publish(name + "/sensor/mode", "HIGHFREQUENCY_FAST");
break;
case HIGHFREQUENCY_MEDIUM:
pSched->publish(name + "/sensor/mode", "HIGHFREQUENCY_MEDIUM");
break;
case HIGHFREQUENCY_LONGTERM:
pSched->publish(name + "/sensor/mode", "HIGHFREQUENCY_LONGTERM");
break;
}
}
void publish_frequency() {
char buf[32];
sprintf(buf, "%10.3f", inputFrequencyVal);
char *p1 = buf;
while (*p1 == ' ')
++p1;
pSched->publish(name + "/sensor/frequency", p1);
}
void publish() {
publish_frequency();
}
void loop() {
double freq = getResetpIrqFrequency(interruptIndex_input, 0) * frequencyRenormalisation;
if (detectZeroChange) {
if ((frequency.lastVal == 0.0 && freq > 0.0) ||
(frequency.lastVal > 0.0 && freq == 0.0))
frequency.reset();
}
if (freq >= 0.0 && freq < 1000000.0) {
if (frequency.filter(&freq)) {
inputFrequencyVal = freq;
publish_frequency();
}
}
}
void subsMsg(String topic, String msg, String originator) {
if (topic == name + "/sensor/state/get") {
publish();
}
if (topic == name + "/sensor/frequency/get") {
publish_frequency();
}
if (topic == name + "/sensor/mode/set") {
if (msg == "LOWFREQUENCY_FAST" || msg == "0") {
setMeasureMode(MeasureMode::LOWFREQUENCY_FAST);
}
if (msg == "LOWFREQUENCY_MEDIUM" || msg == "1") {
setMeasureMode(MeasureMode::LOWFREQUENCY_MEDIUM);
}
if (msg == "LOWFREQUENCY_LONGTERM" || msg == "2") {
setMeasureMode(MeasureMode::LOWFREQUENCY_LONGTERM);
}
if (msg == "HIGHFREQUENCY_FAST" || msg == "3") {
setMeasureMode(MeasureMode::HIGHFREQUENCY_FAST);
}
if (msg == "HIGHFREQUENCY_MEDIUM" || msg == "4") {
setMeasureMode(MeasureMode::HIGHFREQUENCY_MEDIUM);
}
if (msg == "HIGHFREQUENCY_LONGTERM" || msg == "5") {
setMeasureMode(MeasureMode::HIGHFREQUENCY_LONGTERM);
}
}
if (topic == name + "/sensor/mode/get") {
publishMeasureMode();
}
};
}; // FrequencyCounter
} // namespace ustd