-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoilet_monitor_device.nut
350 lines (308 loc) · 9.79 KB
/
toilet_monitor_device.nut
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
DEBUG <- false;
/*
debug printing function to be used only during live debugging
there should be no calls to this left in code, this is a live
debugging tool only
*/
function debugprint(string) {
if (!server.isconnected()) server.connect();
while (!server.isconnected());
server.log("DEBUG: " + string);
}
/*
range_sensor implements the interface to the HC-SR04 ultrasonic range sensor
*/
class range_sensor {
range_in = 0.0;
trig_ = null;
echo_ = null;
constructor(trig_pin, echo_pin) {
trig_ = trig_pin;
echo_ = echo_pin;
}
function range() {
// get the start time
local start_ms = hardware.millis();
// send trigger pulse
trig_.write(0);
trig_.write(1);
trig_.write(0);
// Wait for the echo pulse to start
while(echo_.read() == 0){
if ((hardware.millis() - start_ms) > 500) {
// TODO handle millis() rollover intelligently
if (DEBUG) server.log("timeout on pulse start");
start_ms = hardware.millis();
break;
}
}
local echobegin_us = hardware.micros();
// Wait for the echo pulse to end
while(echo_.read() == 1){
if ((hardware.millis() - start_ms) > 500){
// TODO handle invalid ranges intelligently
if (DEBUG) server.log("timeout on pulse end");
break;
}
}
local echolen_us = hardware.micros() - echobegin_us;
range_in = (echolen_us / 148.0);
return range_in;
}
};
/*
detector is a state machine used to detect the presence of a person in front of the
toilet monitor.
*/
enum STATE {
nobody = "nobody",
pee = "pee",
poop = "poop",
error = "error"
init = "init"
}
class detector {
min_range = 3.0;
max_range_poo = 30.0;
max_range_pee = 40.0;
max_range = 2000.0;
count_thresh = 4; // number of samples that must fall in a range bin
state = STATE.init;
sensor_ = 1234;
pee_count_ = 0;
poo_count_ = 0;
prev_state_ = STATE.init;
/*
requires a range sensor to be passed in
*/
constructor(rs){
sensor_ = rs;
}
/*
checks the current range, sees if a person is present, and then checks if the
person present status has changed since last called.
returns true if the status has changed
*/
function changed() {
state = detect_person(sensor_.range());
if (prev_state_ != state) {
prev_state_ = state;
return true;
}
return false;
}
/*
updates the state and returns it to the caller
*/
function detect_person(range_in) {
if ((range_in < min_range) || (range_in > max_range)) {
// range is invalid
if (DEBUG) server.log("error: unexpected range value: " + range_in);
state = STATE.error;
pee_count_ = 0;
poo_count_ = 0;
}
else if (range_in < max_range_poo) {
// range is valid and within poop range
poo_count_++;
if (poo_count_ > count_thresh) {
state = STATE.poop;
poo_count_ = count_thresh;
pee_count_ = 0;
}
}
else if (range_in < max_range_pee) {
// range is valid and in pee range
pee_count_++;
if (pee_count_ > count_thresh) {
state = STATE.pee;
pee_count_ = count_thresh;
poo_count_ = 0;
}
}
else {
poo_count_--;
pee_count_--;
if ((poo_count_ < 0) || (pee_count_ < 0)) {
poo_count_ = 0;
pee_count_ = 0;
state = STATE.nobody;
}
}
return state;
}
};
/*
batt_monitor implements the interface to the MAX17043 SoC monitor.
*/
class batt_monitor {
static I2C_ADDR = 0x6C;
// register definitions:
static REG_VCELL = "\x02";
static REG_SOC = "\x04";
static REG_MODE = "\x06";
static REG_VERSION = "\x08";
static REG_CONFIG = "\x0C";
static REG_COMMAND = "\xFE";
i2c_ = null;
prev_soc = 0.0;
constructor(i2c_instance) {
i2c_ = i2c_instance;
this.reset();
this.write_config();
}
function reset() {
if (DEBUG) server.log("reset SoC monitor");
i2c_.write(I2C_ADDR, REG_COMMAND + "\x54\x00");
}
/*
sets the config register of the soc monitor
*/
function write_config() {
const DFT_RCOMP = "\x97"; // factory recommended compensation values
const DFT_ALERT = "\x00"; // disable alert functionality and set threshold to 0%
if (DEBUG) server.log("configured SOC monitor");
i2c_.write(I2C_ADDR, REG_CONFIG + DFT_RCOMP + DFT_ALERT);
}
/*
returns cell state of charge in percent
*/
function charge_percent() {
local soc = i2c_.read(I2C_ADDR, REG_SOC, 2);
if (soc == null) {
if (DEBUG) server.log("error: could not read SoC charge percentage");
return;
}
soc = soc[0] + (soc[1] * (1.0/256.0));
if (DEBUG) server.log("cell soc%: " + soc);
return soc;
}
/*
returns cell voltage in volts.
note that cell voltage is only valid 500ms after startup.
*/
function voltage() {
const MV_PER_COUNT = 1.25;
local voltage = i2c_.read(I2C_ADDR, REG_VCELL, 2);
if (voltage ==null) {
if (DEBUG) server.log("error: could not read SoC monitor voltage");
return;
}
voltage = to_int_(voltage) >> 4; // account for unused LSBs
voltage = (voltage * MV_PER_COUNT) / 1000.0; // convert to volts
if (DEBUG) server.log("cell voltage: " + voltage);
return voltage
}
/*
returns true if the SoC has changed appreciably since this function last returned true
this allows the system to only take action on large soc changes
*/
function changed() {
const HYSTERESIS_PERCENT = 0.5; // only report a change if SoC has changed by more than this from last update
local soc = charge_percent();
if (soc != null) {
if (math.abs(soc - prev_soc) > HYSTERESIS_PERCENT) {
prev_soc = soc;
return true;
}
}
return false; // note that we also get here if the SoC reading is invalid
}
/*
reads the silicon version of the SoC monitor
this is mostly useful as a debug tool to make sure the IC is connected and working
*/
function version(){
if (DEBUG) server.log("read SoC monitor version: ");
local version = i2c_.read(I2C_ADDR, "\x08", 2);
if (version == null) {
if (DEBUG) server.log("error: could not read SoC monitor version");
return
}
// convert the bytes returned to a single integer value
version = to_int_(version);
if (DEBUG) server.log(version);
return version;
}
/*
converts a string of bytes to a single integer
used for deserialization of I2C returns
*/
function to_int_(str_val){
local out_val = 0;
foreach (i, byte in str_val) {
out_val += byte<<( 8 * (str_val.len() - i - 1) );
}
return out_val
}
};
/*
performs setup of all hardware pins and system states
*/
function setup() {
server.setsendtimeoutpolicy(SUSPEND_ON_ERROR, WAIT_TIL_SENT, 60.0);
// only connect to the server by default in debug mode, to reduce power draw
if (DEBUG) server.connect();
trig <- hardware.pin9;
trig.configure(DIGITAL_OUT, 0);
echo <- hardware.pinD;
echo.configure(DIGITAL_IN);
// SoC monitor I2C bus
i2c_pullup <- hardware.pinE;
i2c_pullup.configure(DIGITAL_OUT, 1);
i2c <- hardware.i2c12;
i2c.configure(CLOCK_SPEED_400_KHZ);
}
function send_signature() {
if (!server.isconnected()) server.connect();
while (!server.isconnected());
server.log(format( "********************************\n" +
"TOILET MONITOR ONLINE\n" +
"MAC: %s \nSSID: %s \nRSSI: %d \n" +
"********************************\n",
imp.getmacaddress(), imp.getssid(), imp.getrssi() ));
if (!DEBUG) {
server.log("disconnecting")
server.flush(30.0);
server.disconnect();
}
}
// ------------------------------------------
// MAIN PROGRAM ENTRY POINT
// ------------------------------------------
// initialize hardware:
setup();
// instantiate objects:
local rs = range_sensor(trig, echo);
local d = detector(rs.weakref());
local soc = batt_monitor(i2c);
// let server know that we're online
send_signature();
function mainloop() {
local cell_soc = 0.0;
local cell_volts = 0.0;
// detect state transitions
if(d.changed() || soc.changed() || DEBUG) {
// connect to server (if we're not already)
if (!server.isconnected()) server.connect();
while (!server.isconnected());
// update state and range
server.log("range: " + rs.range_in + "in state: " + d.state)
agent.send("state", d.state);
agent.send("range", rs.range_in);
// update soc
cell_soc = soc.charge_percent();
cell_volts = soc.voltage();
server.log("batt: " + cell_volts + "V " + cell_soc + "%");
agent.send("battery_status", [cell_soc, cell_volts]);
if (!DEBUG) {
server.log("disconnecting")
server.flush(30.0);
server.disconnect();
}
}
// sleep till next loop iteration
imp.wakeup(0.5, mainloop);
}
mainloop();
server.onunexpecteddisconnect(function(unused){server.connect()});