-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathbld_elevator_count.rs
More file actions
461 lines (394 loc) · 14.8 KB
/
bld_elevator_count.rs
File metadata and controls
461 lines (394 loc) · 14.8 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
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Elevator occupancy counting — ADR-041 Category 3: Smart Building.
//!
//! Counts occupants in an elevator cabin (1-12 persons) using confined-space
//! multipath analysis:
//! - Amplitude variance scales with body count in a small reflective space
//! - Phase diversity increases with more scatterers
//! - Sudden multipath geometry changes indicate door open/close events
//!
//! Host API used: `csi_get_amplitude()`, `csi_get_variance()`,
//! `csi_get_phase()`, `csi_get_motion_energy()`,
//! `csi_get_n_persons()`
use libm::fabsf;
#[cfg(not(feature = "std"))]
use libm::sqrtf;
#[cfg(feature = "std")]
fn sqrtf(x: f32) -> f32 { x.sqrt() }
/// Maximum subcarriers to process.
const MAX_SC: usize = 32;
/// Maximum occupants the elevator model supports.
const MAX_OCCUPANTS: usize = 12;
/// Overload threshold (default).
const DEFAULT_OVERLOAD: u8 = 10;
/// Baseline calibration frames.
const BASELINE_FRAMES: u32 = 200;
/// EMA smoothing for amplitude statistics.
const ALPHA: f32 = 0.15;
/// Variance ratio threshold for door open/close detection.
const DOOR_VARIANCE_RATIO: f32 = 4.0;
/// Debounce frames for door events.
const DOOR_DEBOUNCE: u8 = 3;
/// Cooldown frames after door event.
const DOOR_COOLDOWN: u16 = 40;
/// Event emission interval.
const EMIT_INTERVAL: u32 = 10;
// ── Event IDs (330-333: Elevator) ───────────────────────────────────────────
pub const EVENT_ELEVATOR_COUNT: i32 = 330;
pub const EVENT_DOOR_OPEN: i32 = 331;
pub const EVENT_DOOR_CLOSE: i32 = 332;
pub const EVENT_OVERLOAD_WARNING: i32 = 333;
/// Door state.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DoorState {
Closed,
Open,
}
/// Elevator occupancy counter.
pub struct ElevatorCounter {
/// Baseline amplitude per subcarrier (empty cabin).
baseline_amp: [f32; MAX_SC],
/// Baseline variance per subcarrier.
baseline_var: [f32; MAX_SC],
/// Previous frame amplitude for delta detection.
prev_amp: [f32; MAX_SC],
/// Smoothed overall variance.
smoothed_var: f32,
/// Smoothed amplitude spread.
smoothed_spread: f32,
/// Calibration accumulators.
calib_amp_sum: [f32; MAX_SC],
calib_amp_sq_sum: [f32; MAX_SC],
calib_count: u32,
calibrated: bool,
/// Estimated occupant count.
count: u8,
/// Overload threshold.
overload_thresh: u8,
/// Door state.
door: DoorState,
/// Door event debounce counter.
door_debounce: u8,
/// Door event pending type (true = open, false = close).
door_pending_open: bool,
/// Door cooldown counter.
door_cooldown: u16,
/// Frame counter.
frame_count: u32,
}
impl ElevatorCounter {
pub const fn new() -> Self {
Self {
baseline_amp: [0.0; MAX_SC],
baseline_var: [0.0; MAX_SC],
prev_amp: [0.0; MAX_SC],
smoothed_var: 0.0,
smoothed_spread: 0.0,
calib_amp_sum: [0.0; MAX_SC],
calib_amp_sq_sum: [0.0; MAX_SC],
calib_count: 0,
calibrated: false,
count: 0,
overload_thresh: DEFAULT_OVERLOAD,
door: DoorState::Closed,
door_debounce: 0,
door_pending_open: false,
door_cooldown: 0,
frame_count: 0,
}
}
/// Process one frame.
///
/// `amplitudes`: per-subcarrier amplitude array.
/// `phases`: per-subcarrier phase array.
/// `motion_energy`: overall motion energy from host.
/// `host_n_persons`: person count hint from host (0 if unavailable).
///
/// Returns events as `(event_type, value)` pairs.
pub fn process_frame(
&mut self,
amplitudes: &[f32],
phases: &[f32],
motion_energy: f32,
host_n_persons: i32,
) -> &[(i32, f32)] {
let n_sc = amplitudes.len().min(phases.len()).min(MAX_SC);
if n_sc < 2 {
return &[];
}
self.frame_count += 1;
if self.door_cooldown > 0 {
self.door_cooldown -= 1;
}
// ── Calibration phase ───────────────────────────────────────────
if !self.calibrated {
for i in 0..n_sc {
self.calib_amp_sum[i] += amplitudes[i];
self.calib_amp_sq_sum[i] += amplitudes[i] * amplitudes[i];
}
self.calib_count += 1;
if self.calib_count >= BASELINE_FRAMES {
let n = self.calib_count as f32;
for i in 0..n_sc {
self.baseline_amp[i] = self.calib_amp_sum[i] / n;
let mean_sq = self.calib_amp_sq_sum[i] / n;
let mean = self.baseline_amp[i];
self.baseline_var[i] = mean_sq - mean * mean;
if self.baseline_var[i] < 0.001 {
self.baseline_var[i] = 0.001;
}
self.prev_amp[i] = amplitudes[i];
}
self.calibrated = true;
}
return &[];
}
// ── Compute multipath statistics ────────────────────────────────
// 1. Overall amplitude variance deviation from baseline.
let mut var_sum = 0.0f32;
let mut spread_sum = 0.0f32;
let mut delta_sum = 0.0f32;
for i in 0..n_sc {
let dev = amplitudes[i] - self.baseline_amp[i];
var_sum += dev * dev;
// Amplitude spread: max-min range.
spread_sum += fabsf(amplitudes[i] - self.baseline_amp[i]);
// Frame-to-frame delta for door detection.
delta_sum += fabsf(amplitudes[i] - self.prev_amp[i]);
self.prev_amp[i] = amplitudes[i];
}
let n_f = n_sc as f32;
let frame_var = var_sum / n_f;
let frame_spread = spread_sum / n_f;
let frame_delta = delta_sum / n_f;
// EMA smooth.
self.smoothed_var = ALPHA * frame_var + (1.0 - ALPHA) * self.smoothed_var;
self.smoothed_spread = ALPHA * frame_spread + (1.0 - ALPHA) * self.smoothed_spread;
// ── Door detection ──────────────────────────────────────────────
// A door open/close causes a sudden change in multipath geometry.
let baseline_avg_var = {
let mut s = 0.0f32;
for i in 0..n_sc {
s += self.baseline_var[i];
}
s / n_f
};
let door_threshold = sqrtf(baseline_avg_var) * DOOR_VARIANCE_RATIO;
let is_door_event = frame_delta > door_threshold;
if is_door_event && self.door_cooldown == 0 {
let pending_open = self.door == DoorState::Closed;
if self.door_pending_open == pending_open {
self.door_debounce = self.door_debounce.saturating_add(1);
} else {
self.door_pending_open = pending_open;
self.door_debounce = 1;
}
} else {
self.door_debounce = 0;
}
let mut door_event: Option<i32> = None;
if self.door_debounce >= DOOR_DEBOUNCE && self.door_cooldown == 0 {
if self.door_pending_open {
self.door = DoorState::Open;
door_event = Some(EVENT_DOOR_OPEN);
} else {
self.door = DoorState::Closed;
door_event = Some(EVENT_DOOR_CLOSE);
}
self.door_cooldown = DOOR_COOLDOWN;
self.door_debounce = 0;
}
// ── Occupant count estimation ───────────────────────────────────
// In a confined elevator cabin, multipath variance scales roughly
// linearly with body count. We use a simple calibrated mapping.
//
// Fuse: host hint (if available) + own variance-based estimate.
let var_ratio = if baseline_avg_var > 0.001 {
self.smoothed_var / baseline_avg_var
} else {
self.smoothed_var * 100.0
};
// Empirical mapping: each person adds roughly 1.0 to var_ratio.
let var_estimate = (var_ratio * 1.2) as u8;
// Motion-energy based bonus: more people = more ambient motion.
let motion_bonus = if motion_energy > 0.5 { 1u8 } else { 0u8 };
let own_estimate = var_estimate.saturating_add(motion_bonus);
let clamped_estimate = if own_estimate > MAX_OCCUPANTS as u8 {
MAX_OCCUPANTS as u8
} else {
own_estimate
};
// Fuse with host hint if available.
if host_n_persons > 0 {
let host_val = host_n_persons as u8;
// Weighted average: 60% host, 40% own.
let fused = ((host_val as u16 * 6 + clamped_estimate as u16 * 4) / 10) as u8;
self.count = if fused > MAX_OCCUPANTS as u8 {
MAX_OCCUPANTS as u8
} else {
fused
};
} else {
self.count = clamped_estimate;
}
// ── Build events ────────────────────────────────────────────────
static mut EVENTS: [(i32, f32); 4] = [(0, 0.0); 4];
let mut n_events = 0usize;
// Door events (immediate).
if let Some(evt) = door_event {
if n_events < 4 {
unsafe {
EVENTS[n_events] = (evt, self.count as f32);
}
n_events += 1;
}
}
// Periodic count and overload.
if self.frame_count % EMIT_INTERVAL == 0 {
if n_events < 4 {
unsafe {
EVENTS[n_events] = (EVENT_ELEVATOR_COUNT, self.count as f32);
}
n_events += 1;
}
// Overload warning.
if self.count >= self.overload_thresh && n_events < 4 {
unsafe {
EVENTS[n_events] = (EVENT_OVERLOAD_WARNING, self.count as f32);
}
n_events += 1;
}
}
unsafe { &EVENTS[..n_events] }
}
/// Get current occupant count estimate.
pub fn occupant_count(&self) -> u8 {
self.count
}
/// Get current door state.
pub fn door_state(&self) -> DoorState {
self.door
}
/// Set overload threshold.
pub fn set_overload_threshold(&mut self, thresh: u8) {
self.overload_thresh = thresh;
}
/// Check if calibration is complete.
pub fn is_calibrated(&self) -> bool {
self.calibrated
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_elevator_init() {
let ec = ElevatorCounter::new();
assert!(!ec.is_calibrated());
assert_eq!(ec.occupant_count(), 0);
assert_eq!(ec.door_state(), DoorState::Closed);
}
#[test]
fn test_calibration() {
let mut ec = ElevatorCounter::new();
let amps = [1.0f32; 16];
let phases = [0.0f32; 16];
for _ in 0..BASELINE_FRAMES {
let events = ec.process_frame(&s, &phases, 0.0, 0);
assert!(events.is_empty());
}
assert!(ec.is_calibrated());
}
#[test]
fn test_occupancy_increases_with_variance() {
let mut ec = ElevatorCounter::new();
let baseline_amps = [1.0f32; 16];
let phases = [0.0f32; 16];
// Calibrate with empty cabin.
for _ in 0..BASELINE_FRAMES {
ec.process_frame(&baseline_amps, &phases, 0.0, 0);
}
// Introduce variance (people in cabin).
let mut occupied_amps = [1.0f32; 16];
for i in 0..16 {
occupied_amps[i] = 1.0 + ((i % 3) as f32) * 2.0;
}
for _ in 0..50 {
ec.process_frame(&occupied_amps, &phases, 0.2, 0);
}
assert!(ec.occupant_count() >= 1, "should detect at least 1 occupant");
}
#[test]
fn test_host_hint_fusion() {
let mut ec = ElevatorCounter::new();
let amps = [1.0f32; 16];
let phases = [0.0f32; 16];
// Calibrate.
for _ in 0..BASELINE_FRAMES {
ec.process_frame(&s, &phases, 0.0, 0);
}
// Feed with host hint of 5 persons.
for _ in 0..30 {
ec.process_frame(&s, &phases, 0.1, 5);
}
// Count should be influenced by host hint.
assert!(ec.occupant_count() >= 2, "host hint should influence count");
}
#[test]
fn test_overload_event() {
let mut ec = ElevatorCounter::new();
ec.set_overload_threshold(3);
let amps = [1.0f32; 16];
let phases = [0.0f32; 16];
// Calibrate.
for _ in 0..BASELINE_FRAMES {
ec.process_frame(&s, &phases, 0.0, 0);
}
// Feed high count via host hint.
let mut found_overload = false;
for _ in 0..100 {
let events = ec.process_frame(&s, &phases, 0.5, 8);
for &(et, _) in events {
if et == EVENT_OVERLOAD_WARNING {
found_overload = true;
}
}
}
assert!(found_overload, "should emit OVERLOAD_WARNING when count >= threshold");
}
#[test]
fn test_door_detection() {
let mut ec = ElevatorCounter::new();
let steady_amps = [1.0f32; 16];
let phases = [0.0f32; 16];
// Calibrate.
for _ in 0..BASELINE_FRAMES {
ec.process_frame(&steady_amps, &phases, 0.0, 0);
}
// Feed steady frames to initialize prev_amp.
for _ in 0..10 {
ec.process_frame(&steady_amps, &phases, 0.0, 0);
}
// Sudden large amplitude changes (simulates door opening).
// Alternate between two very different amplitude patterns so that
// frame-to-frame delta stays high across the debounce window.
let door_amps_a = [8.0f32; 16];
let door_amps_b = [1.0f32; 16];
let mut found_door_event = false;
for frame in 0..20 {
let amps = if frame % 2 == 0 { &door_amps_a } else { &door_amps_b };
let events = ec.process_frame(amps, &phases, 0.3, 0);
for &(et, _) in events {
if et == EVENT_DOOR_OPEN || et == EVENT_DOOR_CLOSE {
found_door_event = true;
}
}
}
assert!(found_door_event, "should detect door event from sudden amplitude change");
}
#[test]
fn test_short_input() {
let mut ec = ElevatorCounter::new();
let events = ec.process_frame(&[1.0], &[0.0], 0.0, 0);
assert!(events.is_empty());
}
}