Rework slam streamer for easier testing

This commit is contained in:
Piv
2020-03-19 23:35:23 +10:30
parent b16b0900cd
commit 872073352b
3 changed files with 13 additions and 3 deletions

View File

@@ -0,0 +1,10 @@
from tracking.devices.mock_lidar import MockLidar
from rplidar import RPLidar
import tracking.lidar_loader as loader
def get_lidar(connection: str):
if connection == 'TEST':
return MockLidar(loader.load_scans_bytes_file("tracking/out.pickle"))
else:
return RPLidar(connection)

View File

@@ -0,0 +1,43 @@
"""
This module contains a MockLidar class, for use in place of RPLidar.
Importantly, it implements iter_scans, so it can be substituted for RPLidar
in the lidar_cache for testing (or anywhere else the rplidar may be used)
"""
import tracking.lidar_loader as loader
class MockLidar:
def __init__(self, scan_iter=None):
"""
Create mock lidar with an iterator that can be used as fake (or reused) scan data.
Examples
--------
lidar = MockLidar(scans)
first_scan = next(lidar.iter_scans(measurements=100))
Parameters
----------
scan_iter: Iterable
An iterator that will generate/provide the fake/old scan data.
"""
self._iter = scan_iter
def iter_scans(self, min_len=100):
return iter(self._iter)
def get_health(self):
return "Mock Lidar has scans" if self._iter is not None else "Mock lidar won't work properly!"
def get_info(self):
return self.get_health()
def stop(self):
pass
def disconnect(self):
pass