Delete iot_simulator.py
Browse files- iot_simulator.py +0 -64
iot_simulator.py
DELETED
|
@@ -1,64 +0,0 @@
|
|
| 1 |
-
import random
|
| 2 |
-
from typing import Dict, Optional
|
| 3 |
-
|
| 4 |
-
class IoTSimulator:
|
| 5 |
-
"""
|
| 6 |
-
Simulates a robotic arm with four sensors:
|
| 7 |
-
- temperature
|
| 8 |
-
- vibration
|
| 9 |
-
- motor current
|
| 10 |
-
- position error
|
| 11 |
-
Supports fault injection for testing anomaly detection.
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
def __init__(self, seed: int = 42):
|
| 15 |
-
"""
|
| 16 |
-
Initialize the simulator with baseline values and fault mode.
|
| 17 |
-
Args:
|
| 18 |
-
seed: Random seed for reproducibility.
|
| 19 |
-
"""
|
| 20 |
-
random.seed(seed)
|
| 21 |
-
self.step = 0
|
| 22 |
-
self.base_temp = 35.0
|
| 23 |
-
self.base_vibration = 0.1
|
| 24 |
-
self.base_current = 2.0
|
| 25 |
-
self.base_position_error = 0.01
|
| 26 |
-
self.fault_mode: Optional[str] = None # 'overheat', 'vibration', 'stall', 'drift'
|
| 27 |
-
|
| 28 |
-
def set_fault(self, fault_type: Optional[str]):
|
| 29 |
-
"""
|
| 30 |
-
Set the fault mode.
|
| 31 |
-
Args:
|
| 32 |
-
fault_type: One of 'overheat', 'vibration', 'stall', 'drift', or None.
|
| 33 |
-
"""
|
| 34 |
-
self.fault_mode = fault_type
|
| 35 |
-
|
| 36 |
-
def read(self) -> Dict[str, float]:
|
| 37 |
-
"""
|
| 38 |
-
Simulate a sensor reading, possibly with injected fault.
|
| 39 |
-
Returns:
|
| 40 |
-
Dictionary with sensor keys: temperature, vibration, motor_current, position_error.
|
| 41 |
-
"""
|
| 42 |
-
self.step += 1
|
| 43 |
-
# Normal variation
|
| 44 |
-
temp = self.base_temp + random.gauss(0, 0.5)
|
| 45 |
-
vib = self.base_vibration + random.gauss(0, 0.02)
|
| 46 |
-
current = self.base_current + random.gauss(0, 0.1)
|
| 47 |
-
pos_err = self.base_position_error + random.gauss(0, 0.005)
|
| 48 |
-
|
| 49 |
-
# Inject fault
|
| 50 |
-
if self.fault_mode == 'overheat':
|
| 51 |
-
temp += 5 + 0.1 * self.step
|
| 52 |
-
elif self.fault_mode == 'vibration':
|
| 53 |
-
vib += 0.5 + 0.02 * self.step
|
| 54 |
-
elif self.fault_mode == 'stall':
|
| 55 |
-
current += 1.0 + 0.1 * self.step
|
| 56 |
-
elif self.fault_mode == 'drift':
|
| 57 |
-
pos_err += 0.02 + 0.002 * self.step
|
| 58 |
-
|
| 59 |
-
return {
|
| 60 |
-
'temperature': max(0, temp),
|
| 61 |
-
'vibration': max(0, vib),
|
| 62 |
-
'motor_current': max(0, current),
|
| 63 |
-
'position_error': max(0, pos_err)
|
| 64 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|