Spaces:
Runtime error
Runtime error
Create iot_simulator.py
Browse files- iot_simulator.py +46 -0
iot_simulator.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import time
|
| 3 |
+
import numpy as np
|
| 4 |
+
from typing import Dict, List
|
| 5 |
+
|
| 6 |
+
class IoTSimulator:
|
| 7 |
+
"""Simulates a robotic arm with sensors."""
|
| 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)
|
| 27 |
+
vib = self.base_vibration + random.gauss(0, 0.02)
|
| 28 |
+
current = self.base_current + random.gauss(0, 0.1)
|
| 29 |
+
pos_err = self.base_position_error + random.gauss(0, 0.005)
|
| 30 |
+
|
| 31 |
+
# Inject fault
|
| 32 |
+
if self.fault_mode == 'overheat':
|
| 33 |
+
temp += 5 + 0.1 * self.step
|
| 34 |
+
elif self.fault_mode == 'vibration':
|
| 35 |
+
vib += 0.5 + 0.02 * self.step
|
| 36 |
+
elif self.fault_mode == 'stall':
|
| 37 |
+
current += 1.0 + 0.1 * self.step
|
| 38 |
+
elif self.fault_mode == 'drift':
|
| 39 |
+
pos_err += 0.02 + 0.002 * self.step
|
| 40 |
+
|
| 41 |
+
return {
|
| 42 |
+
'temperature': max(0, temp),
|
| 43 |
+
'vibration': max(0, vib),
|
| 44 |
+
'motor_current': max(0, current),
|
| 45 |
+
'position_error': max(0, pos_err)
|
| 46 |
+
}
|