Skip to content

Commit

Permalink
Integrated lap time recorde in Context.
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC committed Feb 12, 2024
1 parent be16493 commit 3316c8e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
21 changes: 17 additions & 4 deletions leads/context.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections import deque as _deque
from time import time as _time
from copy import copy as _copy
from typing import TypeVar as _TypeVar, Generic as _Generic

Expand All @@ -14,21 +15,27 @@ def _check_data_type(data: T, superclass: type = DataContainer) -> None:


class Context(_Generic[T]):
def __init__(self, srw_mode: bool = True, initial_data: T | None = None, data_seq_size: int = 1000) -> None:
def __init__(self,
srw_mode: bool = True,
initial_data: T | None = None,
data_seq_size: int = 1000,
num_laps_recorded: int = 3) -> None:
"""
:param srw_mode: True: single rear wheel mode; False: double rear wheel mode
:param initial_data: initial data
:param data_seq_size: buffer size of previous data
"""
self._srw_mode: bool = srw_mode
superclass = SRWDataContainer if srw_mode else DRWDataContainer
if not initial_data:
if initial_data:
_check_data_type(initial_data, superclass)
else:
initial_data = superclass()
_check_data_type(initial_data, superclass)
self.__initial_data_type: type = type(initial_data)
if data_seq_size < 1:
raise ValueError("`data_seq_size` must be greater or equal to 1")
self._data_seq: _deque = _deque((initial_data,), maxlen=data_seq_size)
self._data_seq: _deque[superclass] = _deque((initial_data,), maxlen=data_seq_size)
self._lap_time_seq: _deque[int] = _deque((int(_time() * 1000),), maxlen=num_laps_recorded + 1)
self._dtcs: bool = True
self._abs: bool = True
self._ebi: bool = True
Expand Down Expand Up @@ -90,6 +97,12 @@ def set_atbs(self, enabled: bool) -> None:
def is_atbs_enabled(self) -> bool:
return self._atbs

def record_lap(self):
self._lap_time_seq.append(int(_time() * 1000))

def get_lap_time_list(self) -> list[int]:
return [self._lap_time_seq[i] - self._lap_time_seq[i - 1] for i in range(1, len(self._lap_time_seq))]

def brake(self, force: float) -> int:
# todo
return 0
2 changes: 1 addition & 1 deletion leads/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def reset_time_stamp(self) -> None:
"""
Reset the data's time stamp to now.
"""
self._time_stamp = _time() * 1000
self._time_stamp = int(_time() * 1000)

def get_time_stamp(self) -> int:
"""
Expand Down

0 comments on commit 3316c8e

Please sign in to comment.