Spaces:
Runtime error
Runtime error
Update iot_simulator.py
Browse files- iot_simulator.py +27 -9
iot_simulator.py
CHANGED
|
@@ -1,26 +1,44 @@
|
|
| 1 |
import random
|
| 2 |
-
import
|
| 3 |
-
import numpy as np
|
| 4 |
-
from typing import Dict, List
|
| 5 |
|
| 6 |
class IoTSimulator:
|
| 7 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def __init__(self, seed: int = 42):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
random.seed(seed)
|
| 10 |
self.step = 0
|
| 11 |
self.base_temp = 35.0
|
| 12 |
self.base_vibration = 0.1
|
| 13 |
self.base_current = 2.0
|
| 14 |
self.base_position_error = 0.01
|
| 15 |
-
self.fault_mode = None # 'overheat', 'vibration', 'stall', 'drift'
|
| 16 |
|
| 17 |
-
def set_fault(self, fault_type: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
self.fault_mode = fault_type
|
| 19 |
|
| 20 |
-
def clear_fault(self):
|
| 21 |
-
self.fault_mode = None
|
| 22 |
-
|
| 23 |
def read(self) -> Dict[str, float]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
self.step += 1
|
| 25 |
# Normal variation
|
| 26 |
temp = self.base_temp + random.gauss(0, 0.5)
|
|
|
|
| 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)
|