forked from MicroPythonOS/MicroPythonOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sensor_manager.py
More file actions
431 lines (343 loc) · 14.5 KB
/
test_sensor_manager.py
File metadata and controls
431 lines (343 loc) · 14.5 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
# Unit tests for SensorManager service
import unittest
import sys
# Mock hardware before importing SensorManager
class MockI2C:
"""Mock I2C bus for testing."""
def __init__(self, bus_id, sda=None, scl=None):
self.bus_id = bus_id
self.sda = sda
self.scl = scl
self.memory = {} # addr -> {reg -> value}
def readfrom_mem(self, addr, reg, nbytes):
"""Read from memory (simulates I2C read)."""
if addr not in self.memory:
raise OSError("I2C device not found")
if reg not in self.memory[addr]:
return bytes([0] * nbytes)
return bytes(self.memory[addr][reg])
def writeto_mem(self, addr, reg, data):
"""Write to memory (simulates I2C write)."""
if addr not in self.memory:
self.memory[addr] = {}
self.memory[addr][reg] = list(data)
class MockQMI8658:
"""Mock QMI8658 IMU sensor."""
def __init__(self, i2c_bus, address=0x6B, accel_scale=0b10, gyro_scale=0b100):
self.i2c = i2c_bus
self.address = address
self.accel_scale = accel_scale
self.gyro_scale = gyro_scale
@property
def temperature(self):
"""Return mock temperature."""
return 25.5 # Mock temperature in °C
@property
def acceleration(self):
"""Return mock acceleration (in G)."""
return (0.0, 0.0, 1.0) # At rest, Z-axis = 1G
@property
def gyro(self):
"""Return mock gyroscope (in deg/s)."""
return (0.0, 0.0, 0.0) # Stationary
class MockWsenIsds:
"""Mock WSEN_ISDS IMU sensor."""
def __init__(self, i2c, address=0x6B, acc_range="8g", acc_data_rate="104Hz",
gyro_range="500dps", gyro_data_rate="104Hz"):
self.i2c = i2c
self.address = address
self.acc_range = acc_range
self.gyro_range = gyro_range
self.acc_sensitivity = 0.244 # mg/digit for 8g
self.gyro_sensitivity = 17.5 # mdps/digit for 500dps
self.acc_offset_x = 0
self.acc_offset_y = 0
self.acc_offset_z = 0
self.gyro_offset_x = 0
self.gyro_offset_y = 0
self.gyro_offset_z = 0
def get_chip_id(self):
"""Return WHO_AM_I value."""
return 0x6A
def _read_raw_accelerations(self):
"""Return mock acceleration (in mg)."""
return (0.0, 0.0, 1000.0) # At rest, Z-axis = 1000 mg
def read_angular_velocities(self):
"""Return mock gyroscope (in mdps)."""
return (0.0, 0.0, 0.0)
def acc_calibrate(self, samples=None):
"""Mock calibration."""
pass
def gyro_calibrate(self, samples=None):
"""Mock calibration."""
pass
# Mock constants from drivers
_QMI8685_PARTID = 0x05
_REG_PARTID = 0x00
_ACCELSCALE_RANGE_8G = 0b10
_GYROSCALE_RANGE_256DPS = 0b100
# Mock SharedPreferences to prevent loading real calibration
class MockSharedPreferences:
"""Mock SharedPreferences for testing."""
def __init__(self, package, filename=None):
self.package = package
self.filename = filename
self.data = {}
def get_list(self, key):
"""Get list value."""
return self.data.get(key)
def edit(self):
"""Return editor."""
return MockEditor(self.data)
class MockEditor:
"""Mock SharedPreferences editor."""
def __init__(self, data):
self.data = data
def put_list(self, key, value):
"""Put list value."""
self.data[key] = value
return self
def commit(self):
"""Commit changes."""
pass
mock_config = type('module', (), {
'SharedPreferences': MockSharedPreferences
})()
# Create mock modules
mock_machine = type('module', (), {
'I2C': MockI2C,
'Pin': type('Pin', (), {})
})()
mock_qmi8658 = type('module', (), {
'QMI8658': MockQMI8658,
'_QMI8685_PARTID': _QMI8685_PARTID,
'_REG_PARTID': _REG_PARTID,
'_ACCELSCALE_RANGE_8G': _ACCELSCALE_RANGE_8G,
'_GYROSCALE_RANGE_256DPS': _GYROSCALE_RANGE_256DPS
})()
mock_wsen_isds = type('module', (), {
'Wsen_Isds': MockWsenIsds
})()
# Mock esp32 module
def _mock_mcu_temperature(*args, **kwargs):
"""Mock MCU temperature sensor."""
return 42.0
mock_esp32 = type('module', (), {
'mcu_temperature': _mock_mcu_temperature
})()
# Inject mocks into sys.modules
sys.modules['machine'] = mock_machine
sys.modules['mpos.hardware.drivers.qmi8658'] = mock_qmi8658
sys.modules['mpos.hardware.drivers.wsen_isds'] = mock_wsen_isds
sys.modules['esp32'] = mock_esp32
sys.modules['mpos.config'] = mock_config
# Mock _thread for thread safety testing
try:
import _thread
except ImportError:
mock_thread = type('module', (), {
'allocate_lock': lambda: type('lock', (), {
'acquire': lambda self: None,
'release': lambda self: None
})()
})()
sys.modules['_thread'] = mock_thread
# Now import the module to test
from mpos import SensorManager
class TestSensorManagerQMI8658(unittest.TestCase):
"""Test cases for SensorManager with QMI8658 IMU."""
def setUp(self):
"""Set up test fixtures before each test."""
# Reset SensorManager singleton instance
SensorManager._instance = None
# Reset SensorManager class state
SensorManager._initialized = False
SensorManager._imu_driver = None
SensorManager._sensor_list = []
SensorManager._has_mcu_temperature = False
SensorManager._i2c_bus = None
SensorManager._i2c_address = None
# Create mock I2C bus with QMI8658
self.i2c_bus = MockI2C(0, sda=48, scl=47)
# Set QMI8658 chip ID
self.i2c_bus.memory[0x6B] = {_REG_PARTID: [_QMI8685_PARTID]}
def test_initialization_qmi8658(self):
"""Test that SensorManager initializes with QMI8658."""
result = SensorManager.init(self.i2c_bus, address=0x6B)
self.assertTrue(result)
self.assertTrue(SensorManager.is_available())
def test_sensor_list_qmi8658(self):
"""Test getting sensor list for QMI8658."""
SensorManager.init(self.i2c_bus, address=0x6B)
sensors = SensorManager.get_sensor_list()
# QMI8658 provides: Accelerometer, Gyroscope, IMU Temperature, MCU Temperature
self.assertGreaterEqual(len(sensors), 3)
# Check sensor types present
sensor_types = [s.type for s in sensors]
self.assertIn(SensorManager.TYPE_ACCELEROMETER, sensor_types)
self.assertIn(SensorManager.TYPE_GYROSCOPE, sensor_types)
self.assertIn(SensorManager.TYPE_IMU_TEMPERATURE, sensor_types)
def test_get_default_sensor(self):
"""Test getting default sensor by type."""
SensorManager.init(self.i2c_bus, address=0x6B)
accel = SensorManager.get_default_sensor(SensorManager.TYPE_ACCELEROMETER)
self.assertIsNotNone(accel)
self.assertEqual(accel.type, SensorManager.TYPE_ACCELEROMETER)
gyro = SensorManager.get_default_sensor(SensorManager.TYPE_GYROSCOPE)
self.assertIsNotNone(gyro)
self.assertEqual(gyro.type, SensorManager.TYPE_GYROSCOPE)
def test_get_nonexistent_sensor(self):
"""Test getting a sensor type that doesn't exist."""
SensorManager.init(self.i2c_bus, address=0x6B)
# Type 999 doesn't exist
sensor = SensorManager.get_default_sensor(999)
self.assertIsNone(sensor)
def test_read_accelerometer(self):
"""Test reading accelerometer data."""
SensorManager.init(self.i2c_bus, address=0x6B)
accel = SensorManager.get_default_sensor(SensorManager.TYPE_ACCELEROMETER)
data = SensorManager.read_sensor(accel)
self.assertTrue(data is not None, f"read_sensor returned None, expected tuple")
self.assertEqual(len(data), 3) # (x, y, z)
ax, ay, az = data
# At rest, Z should be ~9.8 m/s² (1G converted to m/s²)
self.assertAlmostEqual(az, 9.80665, places=2)
def test_read_gyroscope(self):
"""Test reading gyroscope data."""
SensorManager.init(self.i2c_bus, address=0x6B)
gyro = SensorManager.get_default_sensor(SensorManager.TYPE_GYROSCOPE)
data = SensorManager.read_sensor(gyro)
self.assertTrue(data is not None, f"read_sensor returned None, expected tuple")
self.assertEqual(len(data), 3) # (x, y, z)
gx, gy, gz = data
# Stationary, all should be ~0 deg/s
self.assertAlmostEqual(gx, 0.0, places=1)
self.assertAlmostEqual(gy, 0.0, places=1)
self.assertAlmostEqual(gz, 0.0, places=1)
def test_read_temperature(self):
"""Test reading temperature data."""
SensorManager.init(self.i2c_bus, address=0x6B)
# Try IMU temperature
imu_temp = SensorManager.get_default_sensor(SensorManager.TYPE_IMU_TEMPERATURE)
if imu_temp:
temp = SensorManager.read_sensor(imu_temp)
self.assertIsNotNone(temp)
self.assertIsInstance(temp, (int, float))
# Try MCU temperature
mcu_temp = SensorManager.get_default_sensor(SensorManager.TYPE_SOC_TEMPERATURE)
if mcu_temp:
temp = SensorManager.read_sensor(mcu_temp)
self.assertIsNotNone(temp)
self.assertEqual(temp, 42.0) # Mock value
def test_read_sensor_without_init(self):
"""Test reading sensor without initialization."""
accel = SensorManager.get_default_sensor(SensorManager.TYPE_ACCELEROMETER)
self.assertIsNone(accel)
def test_is_available_before_init(self):
"""Test is_available before initialization."""
self.assertFalse(SensorManager.is_available())
class TestSensorManagerWsenIsds(unittest.TestCase):
"""Test cases for SensorManager with WSEN_ISDS IMU."""
def setUp(self):
"""Set up test fixtures before each test."""
# Reset SensorManager singleton instance
SensorManager._instance = None
# Reset SensorManager class state
SensorManager._initialized = False
SensorManager._imu_driver = None
SensorManager._sensor_list = []
SensorManager._has_mcu_temperature = False
SensorManager._i2c_bus = None
SensorManager._i2c_address = None
# Create mock I2C bus with WSEN_ISDS
self.i2c_bus = MockI2C(0, sda=9, scl=18)
# Set WSEN_ISDS WHO_AM_I
self.i2c_bus.memory[0x6B] = {0x0F: [0x6A]}
def test_initialization_wsen_isds(self):
"""Test that SensorManager initializes with WSEN_ISDS."""
result = SensorManager.init(self.i2c_bus, address=0x6B)
self.assertTrue(result)
self.assertTrue(SensorManager.is_available())
def test_sensor_list_wsen_isds(self):
"""Test getting sensor list for WSEN_ISDS."""
SensorManager.init(self.i2c_bus, address=0x6B)
sensors = SensorManager.get_sensor_list()
# WSEN_ISDS provides: Accelerometer, Gyroscope, MCU Temperature
# (no IMU temperature)
self.assertGreaterEqual(len(sensors), 2)
# Check sensor types
sensor_types = [s.type for s in sensors]
self.assertIn(SensorManager.TYPE_ACCELEROMETER, sensor_types)
self.assertIn(SensorManager.TYPE_GYROSCOPE, sensor_types)
def test_read_accelerometer_wsen_isds(self):
"""Test reading accelerometer from WSEN_ISDS."""
SensorManager.init(self.i2c_bus, address=0x6B)
accel = SensorManager.get_default_sensor(SensorManager.TYPE_ACCELEROMETER)
data = SensorManager.read_sensor(accel)
self.assertTrue(data is not None, f"read_sensor returned None, expected tuple")
self.assertEqual(len(data), 3)
ax, ay, az = data
# WSEN_ISDS mock returns 1000mg = 1G = 9.80665 m/s²
self.assertAlmostEqual(az, 9.80665, places=2)
class TestSensorManagerNoHardware(unittest.TestCase):
"""Test cases for SensorManager without hardware (desktop mode)."""
def setUp(self):
"""Set up test fixtures before each test."""
# Reset SensorManager singleton instance
SensorManager._instance = None
# Reset SensorManager class state
SensorManager._initialized = False
SensorManager._imu_driver = None
SensorManager._sensor_list = []
SensorManager._has_mcu_temperature = False
SensorManager._i2c_bus = None
SensorManager._i2c_address = None
# Create mock I2C bus with no devices
self.i2c_bus = MockI2C(0, sda=48, scl=47)
# No chip ID registered - simulates no hardware
def test_no_imu_detected(self):
"""Test behavior when no IMU is present."""
result = SensorManager.init(self.i2c_bus, address=0x6B)
# Returns True if MCU temp is available (even without IMU)
self.assertTrue(result)
def test_graceful_degradation(self):
"""Test graceful degradation when no sensors available."""
SensorManager.init(self.i2c_bus, address=0x6B)
# Should have at least MCU temperature
sensors = SensorManager.get_sensor_list()
self.assertGreaterEqual(len(sensors), 0)
# Reading non-existent sensor should return None
accel = SensorManager.get_default_sensor(SensorManager.TYPE_ACCELEROMETER)
if accel is None:
# Expected when no IMU
pass
else:
# If somehow initialized, reading should handle gracefully
data = SensorManager.read_sensor(accel)
# Should either work or return None, not crash
self.assertTrue(data is None or len(data) == 3)
class TestSensorManagerMultipleInit(unittest.TestCase):
"""Test cases for multiple initialization calls."""
def setUp(self):
"""Set up test fixtures before each test."""
# Reset SensorManager singleton instance
SensorManager._instance = None
# Reset SensorManager class state
SensorManager._initialized = False
SensorManager._imu_driver = None
SensorManager._sensor_list = []
SensorManager._has_mcu_temperature = False
SensorManager._i2c_bus = None
SensorManager._i2c_address = None
# Create mock I2C bus with QMI8658
self.i2c_bus = MockI2C(0, sda=48, scl=47)
self.i2c_bus.memory[0x6B] = {_REG_PARTID: [_QMI8685_PARTID]}
def test_multiple_init_calls(self):
"""Test that multiple init calls are handled gracefully."""
result1 = SensorManager.init(self.i2c_bus, address=0x6B)
self.assertTrue(result1)
# Second init should return True but not re-initialize
result2 = SensorManager.init(self.i2c_bus, address=0x6B)
self.assertTrue(result2)
# Should still work normally
self.assertTrue(SensorManager.is_available())