"""Everything the node reads from the environment, parsed once at startup. Parsing happens here rather than where each value is used, so an unusable setting stops the node before it joins the dataflow instead of surfacing the first time somebody picks up the gamepad. """ import os from dataclasses import dataclass, field def env_float(name: str, default: float) -> float: """Read a number, naming the variable if it cannot be read as one.""" value = os.environ.get(name, "").strip() if not value: return default try: return float(value) except ValueError: raise ValueError(f"{name} must be a number, not {value!r}") from None @dataclass class DeadmanConfig: """How long a source may go quiet before the arm is told to stop. Four layers, outermost last. `joy_ttl_s` is how long a pad reading stays worth acting on; this node re-sends the current velocity every tick; `jog_ttl_s` is how long it will keep doing so without hearing from the source; and behind all of them sits the arm node's own `VELOCITY_COMMAND_TTL_S`, which the controller enforces whether or not this process is alive. Keep them in that order — each shorter than the one behind it — or a backstop fires before the thing it is backing up. """ jog_ttl_s: float = 0.4 # How long the `joy` stream may go quiet before the pad reads as gone. The # pad lives in another node now, and that node dying looks like silence # rather than like an unplugged pad, so silence has to mean the same thing. # `dora-joy` publishes every tick, so it means nothing else. joy_ttl_s: float = 0.2 # How long a gamepad may sit untouched before it gives the arm back, so the # browser can take over without anyone having to press anything. gamepad_idle_release_s: float = 2.0 # How often to ask the arm for the things telemetry does not carry: whether # the tool is engaged, whether freedrive is on, whether it is ready. state_poll_s: float = 0.5 @classmethod def from_env(cls) -> "DeadmanConfig": """Build the timings from the environment.""" return cls( jog_ttl_s=env_float("JOG_TTL_S", 0.4), joy_ttl_s=env_float("JOY_TTL_S", 0.2), gamepad_idle_release_s=env_float("GAMEPAD_IDLE_RELEASE_S", 2.0), state_poll_s=env_float("STATE_POLL_S", 0.5), ) @dataclass class Config: """The node's settings.""" # Full-deflection speeds. A stick is analog, so these are the maximum and # anything less moves proportionally slower — which is the whole reason a # gamepad beats the console's dial pad. speed_mm_s: float speed_deg_s: float # Stick travel to ignore. A worn stick that does not centre would otherwise # drive the arm across the table on its own. deadzone: float # How much of the curve is cubic: 0 is linear, 1 is fully cubic. Fine # positioning happens near centre, and expo is what buys resolution there. expo: float deadman: DeadmanConfig = field(default_factory=DeadmanConfig) @classmethod def from_env(cls) -> "Config": """Build the configuration from the environment, refusing anything unusable.""" deadzone = env_float("DEADZONE", 0.12) if not 0.0 <= deadzone < 1.0: raise ValueError(f"DEADZONE must be at least 0 and below 1, not {deadzone}") expo = env_float("EXPO", 0.4) if not 0.0 <= expo <= 1.0: raise ValueError(f"EXPO must be between 0 and 1, not {expo}") deadman = DeadmanConfig.from_env() # The layers have to fire outermost last, and each one is a backstop for # the one inside it. Behind both of these sits the arm node's own # VELOCITY_COMMAND_TTL_S, which no setting here can see. if deadman.joy_ttl_s >= deadman.jog_ttl_s: raise ValueError( f"JOY_TTL_S must be below JOG_TTL_S ({deadman.jog_ttl_s}), not {deadman.joy_ttl_s}" ) return cls( speed_mm_s=env_float("SPEED_MM_S", 60.0), speed_deg_s=env_float("SPEED_DEG_S", 30.0), deadzone=deadzone, expo=expo, deadman=deadman, )