126 lines
4.4 KiB
Python
126 lines
4.4 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import os
|
|
import signal
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
PYTHON_ROOT = Path(__file__).resolve().parent
|
|
UNOQ_ROOT = PYTHON_ROOT.parent
|
|
if str(PYTHON_ROOT) not in sys.path:
|
|
sys.path.insert(0, str(PYTHON_ROOT))
|
|
|
|
from core.config import load_hardware_profile, load_robot_manifest # noqa: E402
|
|
from core.controller import MicroduckController # noqa: E402
|
|
from core.deploy_service import DeployService # noqa: E402
|
|
from core.policy_runner import PolicySet # noqa: E402
|
|
from hal.base import HardwareBridge # noqa: E402
|
|
from hal.mock_hal import MockHal # noqa: E402
|
|
from hal.router_hal import RouterBridgeHal # noqa: E402
|
|
from web.fallback_server import FallbackWebServer # noqa: E402
|
|
|
|
|
|
def _select_hal(hal_mode: str) -> HardwareBridge:
|
|
if hal_mode == "mock":
|
|
return MockHal()
|
|
if hal_mode == "router":
|
|
return RouterBridgeHal()
|
|
# auto: prefer RouterBridge when running on UNO Q App Lab, otherwise ask explicitly for --hal mock
|
|
try:
|
|
from arduino.app_utils import Bridge # type: ignore # noqa: F401
|
|
|
|
return RouterBridgeHal()
|
|
except ImportError as exc:
|
|
raise RuntimeError(
|
|
"arduino.app_utils is not available in this Python environment. "
|
|
"Launch inside UNO Q App Lab, or pass '--hal mock' for PC pre-hardware testing."
|
|
) from exc
|
|
|
|
|
|
def main(argv: list[str] | None = None) -> int:
|
|
parser = argparse.ArgumentParser(description="Microduck UNO Q 50Hz Controller & Deploy Server")
|
|
parser.add_argument("--base-dir", type=Path, default=UNOQ_ROOT, help="UNO Q config/policies directory")
|
|
parser.add_argument(
|
|
"--hal",
|
|
choices=("auto", "router", "mock"),
|
|
default=os.environ.get("MICRODUCK_HAL", "auto"),
|
|
help="Hardware backend: 'router' (UNO Q STM32), 'mock' (PC simulator), or 'auto'",
|
|
)
|
|
parser.add_argument(
|
|
"--policy-backend",
|
|
choices=("auto", "onnx", "dummy"),
|
|
default=os.environ.get("MICRODUCK_POLICY_BACKEND", "auto"),
|
|
help="Inference backend: 'onnx' / 'auto', or explicit 'dummy' dry-run",
|
|
)
|
|
parser.add_argument("--host", default="0.0.0.0")
|
|
parser.add_argument("--port", type=int, default=8000)
|
|
args = parser.parse_args(argv)
|
|
|
|
base_dir = Path(args.base_dir).resolve()
|
|
manifest = load_robot_manifest(base_dir / "config" / "robot_manifest.json")
|
|
profile = load_hardware_profile(base_dir / "config" / "hardware_profile.json", joint_names=manifest["joint_names"])
|
|
|
|
custom_script = None
|
|
script_path = base_dir / "config" / "custom_move_script.json"
|
|
if script_path.is_file():
|
|
import json
|
|
|
|
custom_script = json.loads(script_path.read_text(encoding="utf-8"))
|
|
|
|
hal = _select_hal(args.hal)
|
|
|
|
boot_error: str | None = None
|
|
try:
|
|
policies = PolicySet.load_from_manifest(base_dir, manifest, backend=args.policy_backend)
|
|
except FileNotFoundError as exc:
|
|
# Allow web server & /api/deploy to boot on a fresh board before first push from website
|
|
boot_error = f"Awaiting first policy deploy: {exc}"
|
|
policies = PolicySet({}, backend="awaiting_deploy")
|
|
|
|
controller = MicroduckController(
|
|
base_dir=base_dir,
|
|
manifest=manifest,
|
|
profile=profile,
|
|
hal=hal,
|
|
policies=policies,
|
|
custom_script=custom_script,
|
|
)
|
|
if boot_error:
|
|
controller._last_error = boot_error
|
|
|
|
deploy_backend = args.policy_backend
|
|
deploy_service = DeployService(base_dir=base_dir, controller=controller, policy_backend=deploy_backend)
|
|
|
|
controller.start()
|
|
|
|
def _shutdown(*_args: object) -> None:
|
|
raise SystemExit(0)
|
|
|
|
signal.signal(signal.SIGTERM, _shutdown)
|
|
|
|
srv: FallbackWebServer | None = None
|
|
try:
|
|
try:
|
|
import uvicorn # type: ignore
|
|
|
|
from web.server import create_fastapi_app
|
|
|
|
app = create_fastapi_app(controller, deploy_service)
|
|
uvicorn.run(app, host=args.host, port=args.port, log_level="info")
|
|
except ImportError:
|
|
srv = FallbackWebServer(args.host, args.port, controller, deploy_service)
|
|
print(f"[microduck-unoq] FastAPI/uvicorn not installed; serving stdlib HTTP/WS on {args.host}:{srv.port}")
|
|
srv.serve_forever()
|
|
except (KeyboardInterrupt, SystemExit):
|
|
pass
|
|
finally:
|
|
if srv is not None:
|
|
srv.server_close()
|
|
controller.stop()
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|