Move root car to pycar, put other pycar back to car.
This commit is contained in:
0
pycar/src/car/tracking/devices/__init__.py
Normal file
0
pycar/src/car/tracking/devices/__init__.py
Normal file
26
pycar/src/car/tracking/devices/factory.py
Normal file
26
pycar/src/car/tracking/devices/factory.py
Normal file
@@ -0,0 +1,26 @@
|
||||
from .mock_lidar import MockLidar
|
||||
from .. import lidar_loader as loader
|
||||
import os
|
||||
|
||||
MOCK_DEVICE = "LIDAR_MOCK"
|
||||
|
||||
|
||||
def get_lidar(device=None):
|
||||
actual_device = None
|
||||
try:
|
||||
actual_device = device if device is not None else os.environ["CAR_LIDAR"]
|
||||
except KeyError:
|
||||
print(
|
||||
'No lidar device specified and the CAR_LIDAR environment variable is not set.')
|
||||
if actual_device == MOCK_DEVICE:
|
||||
return MockLidar(loader.load_scans_bytes_file("car/src/car/tracking/out.pickle"))
|
||||
elif actual_device != '':
|
||||
try:
|
||||
from rplidar import RPLidar
|
||||
return RPLidar(device)
|
||||
except ImportError:
|
||||
print('Could not import RPLidar. Have you downloaded rplidar?')
|
||||
else:
|
||||
print('No valid lidar device found. Please choose ' +
|
||||
MOCK_DEVICE + ' or a dn address for the lidar device.')
|
||||
return None
|
||||
43
pycar/src/car/tracking/devices/mock_lidar.py
Normal file
43
pycar/src/car/tracking/devices/mock_lidar.py
Normal 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 car.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
|
||||
52
pycar/src/car/tracking/devices/recording_lidar.py
Normal file
52
pycar/src/car/tracking/devices/recording_lidar.py
Normal file
@@ -0,0 +1,52 @@
|
||||
import datetime
|
||||
|
||||
|
||||
class RecordingLidarDecorator:
|
||||
|
||||
def __init__(self, lidar):
|
||||
self._lidar = lidar
|
||||
self._scans = []
|
||||
self._record = False
|
||||
|
||||
@property
|
||||
def record(self):
|
||||
return self._record
|
||||
|
||||
@record.setter
|
||||
def record(self, value):
|
||||
self.record = value
|
||||
|
||||
def save_data(self, filename):
|
||||
with open(filename, 'w') as f:
|
||||
for scan in self._scans:
|
||||
f.write("%s\n" % scan)
|
||||
|
||||
def iter_scans(self, min_len=100):
|
||||
# Need to customise the iterable.
|
||||
return RecordingIterator(self._lidar.iter_scans(min_len), self._scans)
|
||||
|
||||
def get_health(self):
|
||||
return self._lidar.get_health()
|
||||
|
||||
def get_info(self):
|
||||
return self._lidar.get_info()
|
||||
|
||||
def stop(self):
|
||||
return self._lidar.stop()
|
||||
|
||||
def disconnect(self):
|
||||
return self._lidar.disconnect()
|
||||
|
||||
|
||||
class RecordingIterator:
|
||||
def __init__(self, iterator, scan_list):
|
||||
self._iterator = iterator
|
||||
self._scans = scan_list
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
def __next__(self):
|
||||
nextIter = next(self._iterator)
|
||||
self._scans.append((nextIter, str(datetime.datetime.now())))
|
||||
return nextIter
|
||||
Reference in New Issue
Block a user