-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Expand file tree
/
Copy pathind_clean_room.rs
More file actions
359 lines (313 loc) · 11 KB
/
ind_clean_room.rs
File metadata and controls
359 lines (313 loc) · 11 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
//! Clean room monitoring — ADR-041 Category 5 Industrial module.
//!
//! Personnel count and movement tracking for cleanroom contamination control
//! per ISO 14644 standards.
//!
//! Features:
//! - Real-time occupancy count tracking
//! - Configurable maximum occupancy enforcement (default 4)
//! - Turbulent motion detection (rapid movement that disturbs laminar airflow)
//! - Periodic compliance reports
//!
//! Budget: L (<2 ms per frame). Event IDs 520-523.
/// Default maximum allowed occupancy.
const DEFAULT_MAX_OCCUPANCY: u8 = 4;
/// Motion energy threshold for turbulent movement.
/// Normal cleanroom movement is slow and deliberate.
const TURBULENT_MOTION_THRESH: f32 = 0.6;
/// Debounce frames for occupancy violation.
const VIOLATION_DEBOUNCE: u8 = 10;
/// Debounce frames for turbulent motion.
const TURBULENT_DEBOUNCE: u8 = 3;
/// Compliance report interval (frames, ~30 seconds at 20 Hz).
const COMPLIANCE_REPORT_INTERVAL: u32 = 600;
/// Cooldown after occupancy violation alert (frames).
const VIOLATION_COOLDOWN: u16 = 200;
/// Cooldown after turbulent motion alert (frames).
const TURBULENT_COOLDOWN: u16 = 100;
/// Event IDs (520-series: Industrial/Clean Room).
pub const EVENT_OCCUPANCY_COUNT: i32 = 520;
pub const EVENT_OCCUPANCY_VIOLATION: i32 = 521;
pub const EVENT_TURBULENT_MOTION: i32 = 522;
pub const EVENT_COMPLIANCE_REPORT: i32 = 523;
/// Clean room monitor.
pub struct CleanRoomMonitor {
/// Maximum allowed occupancy.
max_occupancy: u8,
/// Current smoothed person count.
current_count: u8,
/// Previous reported count (for change detection).
prev_count: u8,
/// Occupancy violation debounce counter.
violation_debounce: u8,
/// Turbulent motion debounce counter.
turbulent_debounce: u8,
/// Violation cooldown.
violation_cooldown: u16,
/// Turbulent cooldown.
turbulent_cooldown: u16,
/// Frame counter.
frame_count: u32,
/// Frames in compliance (occupancy <= max).
compliant_frames: u32,
/// Total frames while room is occupied.
occupied_frames: u32,
/// Total violation events.
total_violations: u32,
/// Total turbulent events.
total_turbulent: u32,
}
impl CleanRoomMonitor {
pub const fn new() -> Self {
Self {
max_occupancy: DEFAULT_MAX_OCCUPANCY,
current_count: 0,
prev_count: 0,
violation_debounce: 0,
turbulent_debounce: 0,
violation_cooldown: 0,
turbulent_cooldown: 0,
frame_count: 0,
compliant_frames: 0,
occupied_frames: 0,
total_violations: 0,
total_turbulent: 0,
}
}
/// Create with custom maximum occupancy.
pub const fn with_max_occupancy(max: u8) -> Self {
Self {
max_occupancy: max,
current_count: 0,
prev_count: 0,
violation_debounce: 0,
turbulent_debounce: 0,
violation_cooldown: 0,
turbulent_cooldown: 0,
frame_count: 0,
compliant_frames: 0,
occupied_frames: 0,
total_violations: 0,
total_turbulent: 0,
}
}
/// Process one frame.
///
/// # Arguments
/// - `n_persons`: host-reported person count
/// - `presence`: host-reported presence flag (0/1)
/// - `motion_energy`: host-reported motion energy
///
/// Returns events as `(event_id, value)` pairs.
pub fn process_frame(
&mut self,
n_persons: i32,
presence: i32,
motion_energy: f32,
) -> &[(i32, f32)] {
self.frame_count += 1;
if self.violation_cooldown > 0 {
self.violation_cooldown -= 1;
}
if self.turbulent_cooldown > 0 {
self.turbulent_cooldown -= 1;
}
// Clamp person count to reasonable range.
let count = if n_persons < 0 {
0u8
} else if n_persons > 255 {
255u8
} else {
n_persons as u8
};
self.prev_count = self.current_count;
self.current_count = count;
// Track compliance.
if count > 0 {
self.occupied_frames += 1;
if count <= self.max_occupancy {
self.compliant_frames += 1;
}
}
static mut EVENTS: [(i32, f32); 4] = [(0, 0.0); 4];
let mut n_events = 0usize;
// --- Step 1: Emit count changes ---
if count != self.prev_count && n_events < 4 {
unsafe { EVENTS[n_events] = (EVENT_OCCUPANCY_COUNT, count as f32); }
n_events += 1;
}
// --- Step 2: Occupancy violation ---
if count > self.max_occupancy {
self.violation_debounce = self.violation_debounce.saturating_add(1);
if self.violation_debounce >= VIOLATION_DEBOUNCE
&& self.violation_cooldown == 0
&& n_events < 4
{
self.total_violations += 1;
self.violation_cooldown = VIOLATION_COOLDOWN;
// Value encodes: count * 10 + max_allowed.
let val = count as f32;
unsafe { EVENTS[n_events] = (EVENT_OCCUPANCY_VIOLATION, val); }
n_events += 1;
}
} else {
self.violation_debounce = 0;
}
// --- Step 3: Turbulent motion detection ---
if motion_energy > TURBULENT_MOTION_THRESH && presence > 0 {
self.turbulent_debounce = self.turbulent_debounce.saturating_add(1);
if self.turbulent_debounce >= TURBULENT_DEBOUNCE
&& self.turbulent_cooldown == 0
&& n_events < 4
{
self.total_turbulent += 1;
self.turbulent_cooldown = TURBULENT_COOLDOWN;
unsafe { EVENTS[n_events] = (EVENT_TURBULENT_MOTION, motion_energy); }
n_events += 1;
}
} else {
self.turbulent_debounce = 0;
}
// --- Step 4: Periodic compliance report ---
if self.frame_count % COMPLIANCE_REPORT_INTERVAL == 0 && n_events < 4 {
let compliance_pct = if self.occupied_frames > 0 {
(self.compliant_frames as f32 / self.occupied_frames as f32) * 100.0
} else {
100.0
};
unsafe { EVENTS[n_events] = (EVENT_COMPLIANCE_REPORT, compliance_pct); }
n_events += 1;
}
unsafe { &EVENTS[..n_events] }
}
/// Current occupancy count.
pub fn current_count(&self) -> u8 {
self.current_count
}
/// Maximum allowed occupancy.
pub fn max_occupancy(&self) -> u8 {
self.max_occupancy
}
/// Whether currently in violation.
pub fn is_in_violation(&self) -> bool {
self.current_count > self.max_occupancy
}
/// Compliance percentage (0-100).
pub fn compliance_percent(&self) -> f32 {
if self.occupied_frames == 0 {
return 100.0;
}
(self.compliant_frames as f32 / self.occupied_frames as f32) * 100.0
}
/// Total number of violation events.
pub fn total_violations(&self) -> u32 {
self.total_violations
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_init_state() {
let mon = CleanRoomMonitor::new();
assert_eq!(mon.current_count(), 0);
assert_eq!(mon.max_occupancy(), DEFAULT_MAX_OCCUPANCY);
assert!(!mon.is_in_violation());
assert!((mon.compliance_percent() - 100.0).abs() < 0.01);
}
#[test]
fn test_custom_max_occupancy() {
let mon = CleanRoomMonitor::with_max_occupancy(2);
assert_eq!(mon.max_occupancy(), 2);
}
#[test]
fn test_occupancy_count_change() {
let mut mon = CleanRoomMonitor::new();
// First frame with 2 persons.
let events = mon.process_frame(2, 1, 0.1);
let mut count_event = false;
for &(et, val) in events {
if et == EVENT_OCCUPANCY_COUNT {
count_event = true;
assert!((val - 2.0).abs() < 0.01);
}
}
assert!(count_event, "should emit count change event");
assert_eq!(mon.current_count(), 2);
}
#[test]
fn test_occupancy_violation() {
let mut mon = CleanRoomMonitor::with_max_occupancy(3);
let mut violation_detected = false;
// Feed frames with 5 persons (over limit of 3).
for _ in 0..20 {
let events = mon.process_frame(5, 1, 0.1);
for &(et, _) in events {
if et == EVENT_OCCUPANCY_VIOLATION {
violation_detected = true;
}
}
}
assert!(violation_detected, "violation should be detected when over max");
assert!(mon.is_in_violation());
assert!(mon.total_violations() >= 1);
}
#[test]
fn test_no_violation_under_limit() {
let mut mon = CleanRoomMonitor::with_max_occupancy(4);
for _ in 0..50 {
let events = mon.process_frame(3, 1, 0.1);
for &(et, _) in events {
assert!(et != EVENT_OCCUPANCY_VIOLATION, "no violation when under limit");
}
}
assert!(!mon.is_in_violation());
}
#[test]
fn test_turbulent_motion() {
let mut mon = CleanRoomMonitor::new();
let mut turbulent_detected = false;
// Feed frames with high motion energy.
for _ in 0..10 {
let events = mon.process_frame(2, 1, 0.8);
for &(et, val) in events {
if et == EVENT_TURBULENT_MOTION {
turbulent_detected = true;
assert!(val > TURBULENT_MOTION_THRESH);
}
}
}
assert!(turbulent_detected, "turbulent motion should be detected");
}
#[test]
fn test_compliance_report() {
let mut mon = CleanRoomMonitor::with_max_occupancy(4);
let mut compliance_reported = false;
// Run for COMPLIANCE_REPORT_INTERVAL frames.
for _ in 0..COMPLIANCE_REPORT_INTERVAL + 1 {
let events = mon.process_frame(3, 1, 0.1);
for &(et, val) in events {
if et == EVENT_COMPLIANCE_REPORT {
compliance_reported = true;
assert!((val - 100.0).abs() < 0.01, "should be 100% compliant");
}
}
}
assert!(compliance_reported, "compliance report should be emitted periodically");
}
#[test]
fn test_compliance_degrades_with_violations() {
let mut mon = CleanRoomMonitor::with_max_occupancy(2);
// 50 frames compliant.
for _ in 0..50 {
mon.process_frame(1, 1, 0.1);
}
// 50 frames in violation.
for _ in 0..50 {
mon.process_frame(5, 1, 0.1);
}
let pct = mon.compliance_percent();
assert!(pct < 100.0 && pct > 0.0, "compliance should be partial, got {}%", pct);
assert!((pct - 50.0).abs() < 1.0, "expect ~50% compliance, got {}%", pct);
}
}