-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnergy.cpp
More file actions
79 lines (64 loc) · 2.05 KB
/
Energy.cpp
File metadata and controls
79 lines (64 loc) · 2.05 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
// Copyright © 2018 Stanislav Hnatiuk. All rights reserved.
#include "Energy.h"
Energy::Energy() : BaseSensor() {
count = N_PHASES;
data = new float[count];
// xratio = new float[count];
filters = new Filter[count];
addr = new char* [count];
for (uint8_t i = 0; i < count; ++i) {
addr[i] = new char[4];
}
// Задание адресов и корректирующих коэффициентов.
addr[0] = "5001";
filters[0].ratio = 1.0533624791050533624791050533625;
#if N_PHASES > 1
addr[1] = "5002";
filters[1].ratio = 1.0533624791050533624791050533625;
#if N_PHASES > 2
addr[2] = "5003";
filters[2].ratio = 1.0533624791050533624791050533625;
#endif
#endif
}
//! Считать значение потребления энергии и переключить на следующий датчик.
void Energy::read() {
// Возврат индекса к началу, при достижении последнего датчика.
if (index == count) {
index = 0;
}
// Измерение амплитуд напряжения.
uint16_t current;
uint16_t min_ = 1024, max_ = 0;
for (uint8_t i = 0; i < 200; ++i) {
current = analogRead(index + pinShift);
if (current > max_) {
max_ = current;
}
if (current < min_) {
min_ = current;
}
}
// Вычисление значения потребляемой энергии.
data[index] = filters[index].filtering(max_ - min_) * calc;
#if SERIAL_ENABLED
Serial.println(data[index]);
#endif
// Переключение на следующий датчик.
++index;
}
//! Фильтровать и сглаживать скачки с задержкой в одну итерацию.
float Filter::filtering(const uint16_t next) {
if (curr == prev) {
if (next < 3 && next == curr)
curr = 0;
else
// Пороговый фильтр.
curr = 0.7 * curr + 0.3 * next;
} else {
// Фильтр сглаживания.
prev = curr;
curr = 0.4 * curr + 0.6 * next;
}
return curr * ratio;
}