Add client.py
Browse files
client.py
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Space Robotics Lab, SnT, University of Luxembourg, SpaceR
|
| 2 |
+
# RANS: arXiv:2310.07393 — OpenEnv-compatible implementation
|
| 3 |
+
|
| 4 |
+
"""
|
| 5 |
+
RANSEnv — OpenEnv client for the RANS spacecraft navigation environment.
|
| 6 |
+
|
| 7 |
+
Usage (async)::
|
| 8 |
+
|
| 9 |
+
import asyncio
|
| 10 |
+
from rans_env import RANSEnv, SpacecraftAction
|
| 11 |
+
|
| 12 |
+
async def main():
|
| 13 |
+
async with RANSEnv(base_url="http://localhost:8000") as env:
|
| 14 |
+
obs = await env.reset()
|
| 15 |
+
print("Task:", obs.task)
|
| 16 |
+
print("Observation:", obs.state_obs)
|
| 17 |
+
|
| 18 |
+
# Zero-thrust step
|
| 19 |
+
n = len(obs.thruster_masks)
|
| 20 |
+
result = await env.step(SpacecraftAction(thrusters=[0.0] * n))
|
| 21 |
+
print("Reward:", result.reward)
|
| 22 |
+
print("Done:", result.done)
|
| 23 |
+
|
| 24 |
+
asyncio.run(main())
|
| 25 |
+
|
| 26 |
+
Usage (synchronous)::
|
| 27 |
+
|
| 28 |
+
from rans_env import RANSEnv, SpacecraftAction
|
| 29 |
+
|
| 30 |
+
with RANSEnv(base_url="http://localhost:8000").sync() as env:
|
| 31 |
+
obs = env.reset()
|
| 32 |
+
result = env.step(SpacecraftAction(thrusters=[1, 0, 0, 0, 0, 0, 0, 0]))
|
| 33 |
+
|
| 34 |
+
Docker::
|
| 35 |
+
|
| 36 |
+
env = RANSEnv.from_docker_image(
|
| 37 |
+
"rans-env:latest",
|
| 38 |
+
env={"RANS_TASK": "GoToPose"},
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
HuggingFace Spaces::
|
| 42 |
+
|
| 43 |
+
env = RANSEnv.from_env("openenv/rans-env")
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
from __future__ import annotations
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
from openenv.core.env_client import EnvClient
|
| 50 |
+
except ImportError:
|
| 51 |
+
EnvClient = object # type: ignore[assignment,misc]
|
| 52 |
+
|
| 53 |
+
from rans_env.models import SpacecraftAction, SpacecraftObservation, SpacecraftState
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class RANSEnv(EnvClient):
|
| 57 |
+
"""
|
| 58 |
+
Client for the RANS spacecraft navigation OpenEnv environment.
|
| 59 |
+
|
| 60 |
+
All functionality (``reset``, ``step``, ``state``, ``sync``,
|
| 61 |
+
``from_docker_image``, ``from_env``) is provided by the ``EnvClient``
|
| 62 |
+
base class from openenv-core.
|
| 63 |
+
|
| 64 |
+
The client is typed: it sends ``SpacecraftAction`` objects and receives
|
| 65 |
+
``SpacecraftObservation`` objects.
|
| 66 |
+
|
| 67 |
+
Parameters
|
| 68 |
+
----------
|
| 69 |
+
base_url:
|
| 70 |
+
Base URL of the running RANS server, e.g. ``"http://localhost:8000"``.
|
| 71 |
+
"""
|
| 72 |
+
|
| 73 |
+
action_type = SpacecraftAction
|
| 74 |
+
observation_type = SpacecraftObservation
|
| 75 |
+
state_type = SpacecraftState
|