Skip to content

Commit

Permalink
stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas committed Jan 22, 2025
1 parent e566d4d commit f6bd052
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ out*/

# dev
ref/
.kos-sim/
18 changes: 0 additions & 18 deletions kos_sim/client.py

This file was deleted.

64 changes: 0 additions & 64 deletions kos_sim/config.py

This file was deleted.

39 changes: 28 additions & 11 deletions kos_sim/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,30 @@
import asyncio
import time
from concurrent import futures
from pathlib import Path

import colorlogging
import grpc
from kos_protos import actuator_pb2_grpc, imu_pb2_grpc, sim_pb2_grpc
from kscale import K
from kscale.web.gen.api import RobotURDFMetadataOutput

from kos_sim import logger
from kos_sim.config import SimulatorConfig
from kos_sim.services import ActuatorService, IMUService, SimService
from kos_sim.simulator import MujocoSimulator
from kos_sim.stepping import StepController, StepMode
from kos_sim.utils import get_sim_artifacts_path


class SimulationServer:
def __init__(
self,
model_path: str,
config_path: str | None = None,
model_path: str | Path,
model_metadata: RobotURDFMetadataOutput,
port: int = 50051,
step_mode: StepMode = StepMode.CONTINUOUS,
) -> None:
if config_path:
config = SimulatorConfig.from_file(config_path)
else:
config = SimulatorConfig.default()

self.simulator = MujocoSimulator(model_path, config=config, render=True)
self.simulator = MujocoSimulator(model_path, model_metadata, render=True)
self.step_controller = StepController(self.simulator, mode=step_mode)
self.port = port
self._stop_event = asyncio.Event()
Expand Down Expand Up @@ -102,9 +99,25 @@ async def stop(self) -> None:
self.simulator.close()


async def get_model_metadata(api: K, model_name: str) -> RobotURDFMetadataOutput:
model_path = get_sim_artifacts_path() / model_name / "metadata.json"
if model_path.exists():
return RobotURDFMetadataOutput.model_validate_json(model_path.read_text())
model_path.parent.mkdir(parents=True, exist_ok=True)
model_metadata = await api.get_robot_class(model_name)
model_path.write_text(model_metadata.model_dump_json())
return model_metadata


async def serve(model_name: str, config_path: str | None = None, port: int = 50051) -> None:
api = K()
model_dir = await api.download_and_extract_urdf(model_name)
model_dir, model_metadata = await asyncio.gather(
api.download_and_extract_urdf(model_name),
get_model_metadata(api, model_name),
)

breakpoint()

model_path = next(model_dir.glob("*.mjcf"))

server = SimulationServer(model_path, config_path=config_path, port=port)
Expand All @@ -123,6 +136,10 @@ async def run_server() -> None:
await serve(args.model_name, args.config_path, args.port)


def main() -> None:
asyncio.run(run_server())


if __name__ == "__main__":
# python -m kos_sim.server
asyncio.run(run_server())
main()
6 changes: 3 additions & 3 deletions kos_sim/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@
import mujoco_viewer
import numpy as np
from kscale import K
from kscale.web.gen.api import RobotURDFMetadataOutput

from kos_sim import logger
from kos_sim.config import SimulatorConfig


class MujocoSimulator:
def __init__(
self,
model_path: str | Path,
config: SimulatorConfig | None = None,
model_metadata: RobotURDFMetadataOutput,
render: bool = False,
gravity: bool = True,
suspended: bool = False,
) -> None:
# Load config or use default
self._config = config or SimulatorConfig.default()
self._metadata = model_metadata

# Load MuJoCo model and initialize data
self._model = mujoco.MjModel.from_xml_path(str(model_path))
Expand Down
9 changes: 9 additions & 0 deletions kos_sim/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Defines some utility functions."""

import os
from pathlib import Path


def get_sim_artifacts_path() -> Path:
base_path = os.getenv("KOS_SIM_CACHE_PATH", ".kos-sim")
return Path(base_path).expanduser().resolve()
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
tests_require=requirements_dev,
extras_require={"dev": requirements_dev},
packages=["kos_sim"],
# entry_points={
# "console_scripts": [
# "kos-sim.cli:main",
# ],
# },
entry_points={
"console_scripts": [
"kos-sim=kos_sim.server:main",
],
},
)

0 comments on commit f6bd052

Please sign in to comment.