You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					272 lines
				
				8.0 KiB
			
		
		
			
		
	
	
					272 lines
				
				8.0 KiB
			| 
											4 years ago
										 | #!/usr/bin/env python3
 | ||
| 
											3 years ago
										 | import os
 | ||
| 
											4 years ago
										 | import time
 | ||
|  | import unittest
 | ||
| 
											3 years ago
										 | import numpy as np
 | ||
| 
											3 years ago
										 | from collections import namedtuple, defaultdict
 | ||
| 
											4 years ago
										 | 
 | ||
|  | import cereal.messaging as messaging
 | ||
| 
											3 years ago
										 | from cereal import log
 | ||
| 
											3 years ago
										 | from system.hardware import TICI, HARDWARE
 | ||
| 
											3 years ago
										 | from selfdrive.manager.process_config import managed_processes
 | ||
| 
											4 years ago
										 | 
 | ||
| 
											3 years ago
										 | BMX = {
 | ||
|  |   ('bmx055', 'acceleration'),
 | ||
|  |   ('bmx055', 'gyroUncalibrated'),
 | ||
|  |   ('bmx055', 'magneticUncalibrated'),
 | ||
|  |   ('bmx055', 'temperature'),
 | ||
|  | }
 | ||
|  | 
 | ||
|  | LSM = {
 | ||
|  |   ('lsm6ds3', 'acceleration'),
 | ||
|  |   ('lsm6ds3', 'gyroUncalibrated'),
 | ||
|  |   ('lsm6ds3', 'temperature'),
 | ||
|  | }
 | ||
|  | LSM_C = {(x[0]+'trc', x[1]) for x in LSM}
 | ||
|  | 
 | ||
|  | MMC = {
 | ||
|  |   ('mmc5603nj', 'magneticUncalibrated'),
 | ||
|  | }
 | ||
|  | 
 | ||
|  | RPR = {
 | ||
|  |   ('rpr0521', 'light'),
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | SENSOR_CONFIGURATIONS = (
 | ||
| 
											3 years ago
										 |   (BMX | LSM | RPR),
 | ||
|  |   (MMC | LSM | RPR),
 | ||
|  |   (BMX | LSM_C | RPR),
 | ||
|  |   (MMC| LSM_C | RPR),
 | ||
| 
											4 years ago
										 | )
 | ||
|  | 
 | ||
| 
											3 years ago
										 | Sensor = log.SensorEventData.SensorSource
 | ||
| 
											3 years ago
										 | SensorConfig = namedtuple('SensorConfig', ['type', 'sanity_min', 'sanity_max'])
 | ||
| 
											3 years ago
										 | ALL_SENSORS = {
 | ||
|  |   Sensor.rpr0521: {
 | ||
| 
											3 years ago
										 |     SensorConfig("light", 0, 1023),
 | ||
| 
											3 years ago
										 |   },
 | ||
|  | 
 | ||
|  |   Sensor.lsm6ds3: {
 | ||
| 
											3 years ago
										 |     SensorConfig("acceleration", 5, 15),
 | ||
|  |     SensorConfig("gyroUncalibrated", 0, .2),
 | ||
|  |     SensorConfig("temperature", 0, 60),
 | ||
| 
											3 years ago
										 |   },
 | ||
|  | 
 | ||
|  |   Sensor.lsm6ds3trc: {
 | ||
| 
											3 years ago
										 |     SensorConfig("acceleration", 5, 15),
 | ||
|  |     SensorConfig("gyroUncalibrated", 0, .2),
 | ||
|  |     SensorConfig("temperature", 0, 60),
 | ||
| 
											3 years ago
										 |   },
 | ||
|  | 
 | ||
|  |   Sensor.bmx055: {
 | ||
| 
											3 years ago
										 |     SensorConfig("acceleration", 5, 15),
 | ||
|  |     SensorConfig("gyroUncalibrated", 0, .2),
 | ||
|  |     SensorConfig("magneticUncalibrated", 0, 300),
 | ||
|  |     SensorConfig("temperature", 0, 60),
 | ||
| 
											3 years ago
										 |   },
 | ||
|  | 
 | ||
|  |   Sensor.mmc5603nj: {
 | ||
| 
											3 years ago
										 |     SensorConfig("magneticUncalibrated", 0, 300),
 | ||
| 
											3 years ago
										 |   }
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											3 years ago
										 | LSM_IRQ = 336
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 | def get_irq_count(irq: int):
 | ||
|  |   with open(f"/sys/kernel/irq/{irq}/per_cpu_count") as f:
 | ||
|  |     per_cpu = map(int, f.read().split(","))
 | ||
|  |     return sum(per_cpu)
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 | def read_sensor_events(duration_sec):
 | ||
|  |   sensor_types = ['accelerometer', 'gyroscope', 'magnetometer', 'accelerometer2',
 | ||
|  |                   'gyroscope2', 'lightSensor', 'temperatureSensor']
 | ||
|  |   esocks = {}
 | ||
|  |   events = defaultdict(list)
 | ||
|  |   for stype in sensor_types:
 | ||
|  |     esocks[stype] = messaging.sub_sock(stype, timeout=0.1)
 | ||
|  | 
 | ||
|  |   start_time_sec = time.monotonic()
 | ||
|  |   while time.monotonic() - start_time_sec < duration_sec:
 | ||
|  |     for esock in esocks:
 | ||
|  |       events[esock] += messaging.drain_sock(esocks[esock])
 | ||
|  |     time.sleep(0.1)
 | ||
|  | 
 | ||
| 
											3 years ago
										 |   assert sum(map(len, events.values())) != 0, "No sensor events collected!"
 | ||
| 
											3 years ago
										 | 
 | ||
|  |   return events
 | ||
| 
											4 years ago
										 | 
 | ||
|  | class TestSensord(unittest.TestCase):
 | ||
|  |   @classmethod
 | ||
|  |   def setUpClass(cls):
 | ||
|  |     if not TICI:
 | ||
|  |       raise unittest.SkipTest
 | ||
|  | 
 | ||
| 
											3 years ago
										 |     # make sure gpiochip0 is readable
 | ||
|  |     HARDWARE.initialize_hardware()
 | ||
|  | 
 | ||
| 
											3 years ago
										 |     # enable LSM self test
 | ||
|  |     os.environ["LSM_SELF_TEST"] = "1"
 | ||
|  | 
 | ||
| 
											3 years ago
										 |     # read initial sensor values every test case can use
 | ||
| 
											3 years ago
										 |     os.system("pkill -f ./_sensord")
 | ||
| 
											3 years ago
										 |     try:
 | ||
|  |       managed_processes["sensord"].start()
 | ||
|  |       time.sleep(3)
 | ||
|  |       cls.sample_secs = 10
 | ||
|  |       cls.events = read_sensor_events(cls.sample_secs)
 | ||
|  |     finally:
 | ||
|  |       # teardown won't run if this doesn't succeed
 | ||
|  |       managed_processes["sensord"].stop()
 | ||
| 
											3 years ago
										 | 
 | ||
|  |   @classmethod
 | ||
|  |   def tearDownClass(cls):
 | ||
| 
											3 years ago
										 |     managed_processes["sensord"].stop()
 | ||
| 
											3 years ago
										 |     if "LSM_SELF_TEST" in os.environ:
 | ||
|  |       del os.environ['LSM_SELF_TEST']
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |   def tearDown(self):
 | ||
|  |     managed_processes["sensord"].stop()
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   def test_sensors_present(self):
 | ||
| 
											3 years ago
										 |     # verify correct sensors configuration
 | ||
| 
											4 years ago
										 | 
 | ||
|  |     seen = set()
 | ||
| 
											3 years ago
										 |     for etype in self.events:
 | ||
|  |       for measurement in self.events[etype]:
 | ||
|  |         m = getattr(measurement, measurement.which())
 | ||
|  |         seen.add((str(m.source), m.which()))
 | ||
| 
											4 years ago
										 | 
 | ||
|  |     self.assertIn(seen, SENSOR_CONFIGURATIONS)
 | ||
|  | 
 | ||
| 
											3 years ago
										 |   def test_lsm6ds3_timing(self):
 | ||
|  |     # verify measurements are sampled and published at 104Hz
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |     sensor_t = {
 | ||
|  |       1: [], # accel
 | ||
|  |       5: [], # gyro
 | ||
|  |     }
 | ||
| 
											3 years ago
										 | 
 | ||
|  |     for measurement in self.events['accelerometer']:
 | ||
|  |       m = getattr(measurement, measurement.which())
 | ||
|  |       sensor_t[m.sensor].append(m.timestamp)
 | ||
|  | 
 | ||
|  |     for measurement in self.events['gyroscope']:
 | ||
|  |       m = getattr(measurement, measurement.which())
 | ||
|  |       sensor_t[m.sensor].append(m.timestamp)
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |     for s, vals in sensor_t.items():
 | ||
|  |       with self.subTest(sensor=s):
 | ||
|  |         assert len(vals) > 0
 | ||
|  |         tdiffs = np.diff(vals) / 1e6 # millis
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |         high_delay_diffs = list(filter(lambda d: d >= 20., tdiffs))
 | ||
|  |         assert len(high_delay_diffs) < 15, f"Too many large diffs: {high_delay_diffs}"
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |         avg_diff = sum(tdiffs)/len(tdiffs)
 | ||
| 
											3 years ago
										 |         avg_freq = 1. / (avg_diff * 1e-3)
 | ||
|  |         assert 92. < avg_freq < 114., f"avg freq {avg_freq}Hz wrong, expected 104Hz"
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |         stddev = np.std(tdiffs)
 | ||
|  |         assert stddev < 2.0, f"Standard-dev to big {stddev}"
 | ||
| 
											3 years ago
										 | 
 | ||
|  |   def test_events_check(self):
 | ||
|  |     # verify if all sensors produce events
 | ||
|  | 
 | ||
|  |     sensor_events = dict()
 | ||
| 
											3 years ago
										 |     for etype in self.events:
 | ||
|  |       for measurement in self.events[etype]:
 | ||
|  |         m = getattr(measurement, measurement.which())
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |         if m.type in sensor_events:
 | ||
|  |           sensor_events[m.type] += 1
 | ||
| 
											3 years ago
										 |         else:
 | ||
| 
											3 years ago
										 |           sensor_events[m.type] = 1
 | ||
| 
											3 years ago
										 | 
 | ||
|  |     for s in sensor_events:
 | ||
|  |       err_msg = f"Sensor {s}: 200 < {sensor_events[s]}"
 | ||
|  |       assert sensor_events[s] > 200, err_msg
 | ||
|  | 
 | ||
|  |   def test_logmonottime_timestamp_diff(self):
 | ||
|  |     # ensure diff between the message logMonotime and sample timestamp is small
 | ||
|  | 
 | ||
|  |     tdiffs = list()
 | ||
| 
											3 years ago
										 |     for etype in self.events:
 | ||
|  |       for measurement in self.events[etype]:
 | ||
|  |         m = getattr(measurement, measurement.which())
 | ||
| 
											3 years ago
										 | 
 | ||
|  |         # check if gyro and accel timestamps are before logMonoTime
 | ||
| 
											3 years ago
										 |         if str(m.source).startswith("lsm6ds3") and m.which() != 'temperature':
 | ||
|  |           err_msg = f"Timestamp after logMonoTime: {m.timestamp} > {measurement.logMonoTime}"
 | ||
|  |           assert m.timestamp < measurement.logMonoTime, err_msg
 | ||
| 
											3 years ago
										 | 
 | ||
|  |         # negative values might occur, as non interrupt packages created
 | ||
|  |         # before the sensor is read
 | ||
| 
											3 years ago
										 |         tdiffs.append(abs(measurement.logMonoTime - m.timestamp) / 1e6)
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |     high_delay_diffs = set(filter(lambda d: d >= 15., tdiffs))
 | ||
|  |     assert len(high_delay_diffs) < 20, f"Too many measurements published : {high_delay_diffs}"
 | ||
| 
											3 years ago
										 | 
 | ||
|  |     avg_diff = round(sum(tdiffs)/len(tdiffs), 4)
 | ||
| 
											3 years ago
										 |     assert avg_diff < 4, f"Avg packet diff: {avg_diff:.1f}ms"
 | ||
| 
											3 years ago
										 | 
 | ||
|  |     stddev = np.std(tdiffs)
 | ||
| 
											3 years ago
										 |     assert stddev < 2, f"Timing diffs have too high stddev: {stddev}"
 | ||
| 
											3 years ago
										 | 
 | ||
|  |   def test_sensor_values_sanity_check(self):
 | ||
| 
											3 years ago
										 |     sensor_values = dict()
 | ||
|  |     for etype in self.events:
 | ||
|  |       for measurement in self.events[etype]:
 | ||
|  |         m = getattr(measurement, measurement.which())
 | ||
| 
											3 years ago
										 |         key = (m.source.raw, m.which())
 | ||
|  |         values = getattr(m, m.which())
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |         if hasattr(values, 'v'):
 | ||
|  |           values = values.v
 | ||
|  |         values = np.atleast_1d(values)
 | ||
|  | 
 | ||
|  |         if key in sensor_values:
 | ||
|  |           sensor_values[key].append(values)
 | ||
|  |         else:
 | ||
|  |           sensor_values[key] = [values]
 | ||
|  | 
 | ||
|  |     # Sanity check sensor values and counts
 | ||
|  |     for sensor, stype in sensor_values:
 | ||
|  |       for s in ALL_SENSORS[sensor]:
 | ||
|  |         if s.type != stype:
 | ||
|  |           continue
 | ||
|  | 
 | ||
|  |         key = (sensor, s.type)
 | ||
|  |         val_cnt = len(sensor_values[key])
 | ||
| 
											3 years ago
										 |         min_samples = self.sample_secs * 100  # Hz
 | ||
|  |         err_msg = f"Sensor {sensor} {s.type} got {val_cnt} measurements, expected {min_samples}"
 | ||
|  |         assert min_samples*0.9 < val_cnt < min_samples*1.1, err_msg
 | ||
| 
											3 years ago
										 | 
 | ||
|  |         mean_norm = np.mean(np.linalg.norm(sensor_values[key], axis=1))
 | ||
|  |         err_msg = f"Sensor '{sensor} {s.type}' failed sanity checks {mean_norm} is not between {s.sanity_min} and {s.sanity_max}"
 | ||
|  |         assert s.sanity_min <= mean_norm <= s.sanity_max, err_msg
 | ||
|  | 
 | ||
|  |   def test_sensor_verify_no_interrupts_after_stop(self):
 | ||
|  |     managed_processes["sensord"].start()
 | ||
| 
											3 years ago
										 |     time.sleep(3)
 | ||
| 
											3 years ago
										 | 
 | ||
|  |     # read /proc/interrupts to verify interrupts are received
 | ||
| 
											3 years ago
										 |     state_one = get_irq_count(LSM_IRQ)
 | ||
| 
											3 years ago
										 |     time.sleep(1)
 | ||
| 
											3 years ago
										 |     state_two = get_irq_count(LSM_IRQ)
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											3 years ago
										 |     error_msg = f"no interrupts received after sensord start!\n{state_one} {state_two}"
 | ||
|  |     assert state_one != state_two, error_msg
 | ||
| 
											3 years ago
										 | 
 | ||
|  |     managed_processes["sensord"].stop()
 | ||
| 
											3 years ago
										 |     time.sleep(1)
 | ||
| 
											3 years ago
										 | 
 | ||
|  |     # read /proc/interrupts to verify no more interrupts are received
 | ||
| 
											3 years ago
										 |     state_one = get_irq_count(LSM_IRQ)
 | ||
| 
											3 years ago
										 |     time.sleep(1)
 | ||
| 
											3 years ago
										 |     state_two = get_irq_count(LSM_IRQ)
 | ||
| 
											3 years ago
										 |     assert state_one == state_two, "Interrupts received after sensord stop!"
 | ||
|  | 
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											4 years ago
										 | if __name__ == "__main__":
 | ||
|  |   unittest.main()
 |