from abc import ABC, abstractmethod
from typing import Any, Dict, List, Tuple, get_args, get_origin
from .printing import TCOL
from .time import Time
default_param: List[Tuple[str, type, Any]] = [
("leg_number", int, 0),
("std_movement_time", float, 0.5),
("control_rate", float, 30.0),
("mvmt_update_rate", float, 10.0),
("ignore_limits", bool, False),
("limit_margin", float, 0.0),
("speed_mode", bool, False),
("add_joints", List[str], [""]),
("start_coord", List[float], [0.0, 0.0, 0.0]),
# ("mirror_angles", bool, False),
("urdf_path", str, ""),
("urdf", str, ""),
("start_effector_name", str, ""),
("end_effector_name", str, ""),
]
[docs]
class Spinner(ABC):
alias: str = ""
[docs]
@abstractmethod
def now(self) -> Time: ...
[docs]
@abstractmethod
def error(self, *args, **kwargs) -> None: ...
[docs]
@abstractmethod
def warn(self, *args, **kwargs) -> None: ...
[docs]
@abstractmethod
def info(self, *args, **kwargs) -> None: ...
[docs]
@abstractmethod
def debug(self, *args, **kwargs) -> None: ...
[docs]
@abstractmethod
def get_parameter(self, name: str, value_type: type, default=None) -> Any: ...
[docs]
class PythonSpinner(Spinner):
def __init__(self) -> None:
self.__now: Time = Time(0)
[docs]
def now(self) -> Time:
return self.__now
[docs]
def change_time(self, time: Time):
self.__now = Time(time)
[docs]
def error(self, *args) -> None:
print(TCOL.FAIL + str(*args) + TCOL.ENDC)
[docs]
def warn(self, *args) -> None:
print(TCOL.WARNING + str(*args) + TCOL.ENDC)
[docs]
def info(self, *args) -> None:
print(*args)
[docs]
def debug(self, *args) -> None:
print(TCOL.OKBLUE + str(*args) + TCOL.ENDC)
[docs]
class FlexNode:
spinner: Spinner #: must be initialized and spinning already
startup_time: Time
def __init__(self, spinner: Spinner):
self.spinner = spinner
self.startup_time = self.now()
self.__ms_param = None
[docs]
def now(self) -> Time:
return self.spinner.now()
[docs]
def error(self, *args):
self.spinner.error(*args)
[docs]
def warn(self, *args):
self.spinner.warn(*args)
[docs]
def info(self, *args):
self.spinner.info(*args)
[docs]
def debug(self, *args):
self.spinner.debug(*args)
@property
def ms_param(self) -> Dict[str, Any]:
if self.__ms_param is None:
self.__ms_param = self._make_ms_param()
return self.__ms_param
def _make_ms_param(self) -> Dict[str, Any]:
params: Dict[str, Any] = {}
for p, t, v in default_param:
if p in params.keys():
self.error(f"Parameter {p} defined twice. Second one ignored.")
params[p] = self.spinner.get_parameter(p, t, v)
return params