-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVibrationSensor.h
163 lines (139 loc) · 4.14 KB
/
VibrationSensor.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
#include "esphome.h"
#include "Adafruit_LIS3DH.h"
#include "Adafruit_Sensor.h"
#define TIME_WINDOW 3000 // 3 secs
#define TIME_UNTIL_ON 30000 // 30 secs
#define TIME_UNTIL_DONE 300000 // 5 mins
#define STARTUP_SAMPLES 30 // number of readings to average on startup to determine a baseline
#define ACTIVE_READINGS 2 // number of active readings to require within TIME_WINDOW before becoming active
enum ApplianceState {
IDLE,
MAYBE_ON,
ON,
MAYBE_DONE,
DONE,
STARTUP
};
class VibrationSensor : public PollingComponent, public Sensor {
private:
uint8_t startupIndex = 0;
sensors_vec_t *startupValues;
const char *name;
uint8_t address;
bool error;
float initialX, initialY, initialZ, threshold;
uint8_t activeReadings;
long lastMaybeActive, lastActive, lastIdle;
ApplianceState state;
// Average the first STARTUP_SAMPLES readings from the sensor to determine the baseline
bool buildStartSamples(sensors_event_t *sEvent) {
startupValues[startupIndex] = sEvent->acceleration;
startupIndex++;
bool startupComplete = false;
if (startupIndex >= STARTUP_SAMPLES) {
state = IDLE;
for (uint8_t k = 0; k < STARTUP_SAMPLES; k++) {
initialX += startupValues[k].x;
initialY += startupValues[k].y;
initialZ += startupValues[k].z;
}
delete[] startupValues;
initialX /= STARTUP_SAMPLES;
initialY /= STARTUP_SAMPLES;
initialZ /= STARTUP_SAMPLES;
// make sure we start IDLE
lastActive = millis() - (TIME_WINDOW + (TIME_WINDOW/2));
startupComplete = true;
}
publish_state(state);
return startupComplete;
}
public:
Adafruit_LIS3DH lis;
VibrationSensor(const char *n, uint8_t addr, float thresh) : PollingComponent(200) {
name = n;
address = addr;
threshold = thresh;
state = STARTUP;
startupValues = new sensors_vec_t[STARTUP_SAMPLES];
}
float get_setup_priority() const override { return esphome::setup_priority::BUS; }
void setup() override {
if (!lis.begin(address)) {
error = true;
ESP_LOGE(name, "failed starting LIS3DH 0x%x", address);
return;
}
lis.setRange(LIS3DH_RANGE_2_G);
sensors_event_t sEvent;
if (!lis.getEvent(&sEvent)) {
error = true;
ESP_LOGE(name, "failed reading initial state of 0x%x", address);
return;
}
buildStartSamples(&sEvent);
}
void update() override {
if (error) {
return;
}
sensors_event_t sEvent;
if (!lis.getEvent(&sEvent)) {
ESP_LOGE(name, "failed reading state of 0x%x", address);
return;
}
if (state == STARTUP && !buildStartSamples(&sEvent)) {
return;
}
float force = fabs(sEvent.acceleration.x - initialX) +
fabs(sEvent.acceleration.y - initialY) +
fabs(sEvent.acceleration.z - initialZ);
ESP_LOGD(name, "0x%x force: %f", address, force);
long now = millis();
if (force > threshold) {
lastMaybeActive = now;
if (activeReadings >= ACTIVE_READINGS) {
lastActive = now;
}
else {
activeReadings++;
}
}
else if (now - lastMaybeActive >= TIME_WINDOW) {
activeReadings = 0;
}
bool recentlyActive = (now - lastActive) < TIME_WINDOW;
switch (state) {
case IDLE:
if (recentlyActive) {
state = MAYBE_ON;
} else {
lastIdle = now;
}
break;
case MAYBE_ON:
if (recentlyActive && now > (lastIdle + TIME_UNTIL_ON)) {
state = ON;
} else if (!recentlyActive) {
state = IDLE;
}
break;
case ON:
if (!recentlyActive) {
state = MAYBE_DONE;
}
break;
case MAYBE_DONE:
if (recentlyActive) {
state = ON;
} else if (now > (lastActive + TIME_UNTIL_DONE)) {
state = DONE;
}
break;
case DONE:
state = IDLE;
break;
}
publish_state(state);
}
};