Enhance mujoco_server.py and UR5 environment for improved teleoperation and episode control
Browse files- Introduced new mechanisms for handling teleoperation commands, including a snapshot of the last command for better state management.
- Updated the reward calculation in the UR5 environment to prioritize task rewards, with a fallback to distance-based rewards.
- Added gym-style control message handling over the unified WebSocket, allowing for richer interactions and state updates.
- Implemented a robust episode control flag system to manage episode termination and truncation without interfering with the gym client's lifecycle.
- Enhanced documentation in README.md to clarify server startup options and the unified WebSocket API for trainers and clients.
- Added unit tests to verify the functionality of the episode control mechanism, ensuring thread safety and correct behavior across multiple episodes.
Files changed (5)
- README.md+50-23
- mujoco_server.py+180-55
- robots/ur5/ur5_env.py+12-4
- tests/test_api.py+2-2
- tests/test_episode_control_fix.pyAdded+167
114 unmodified lines115116117118119120121122123124125126254 unmodified lines3813823833843853863873883893903913924 unmodified lines39739839940040140240340440540686 unmodified lines49349449549649749849950050113 unmodified lines5155165175185195209 unmodified lines530531532533534535536537538539540541542543544545546547548549550252 unmodified lines803804805806807808809810114 unmodified linescd Quadruped-PyMPCpip install -e .# Start the serverpython mujoco_server.py# Open browser at http://localhost:3004/nova-sim/api/v1```## Docker Deployment### Getting Started254 unmodified lines// Receive messagesws.onmessage = (event) => {const msg = JSON.parse(event.data);if (msg.type === 'state') {console.log(msg.data);}};```#### Client → Server Messages4 unmodified lines| `command` | `{vx, vy, vyaw}` | Set velocity command || `reset` | `{}` | Reset robot to standing pose || `switch_robot` | `{robot, scene?}` | Switch active robot and optional scene || `camera` | `{action, ...}` | Camera control || `camera_follow` | `{follow}` | Toggle camera follow mode || `teleop_command` | `{dx, dy, dz}` | Apply incremental cartesian jog command (UI teleop) |**`command`:**```json{"type": "command", "data": {"vx": 0.5, "vy": 0.0, "vyaw": 0.0}}86 unmodified lines"base_height": 0.46,"upright": 0.98,"steps": 1234,"vx": 0.5,"vy": 0.0,"vyaw": 0.0}}```13 unmodified lines"control_mode": "ik","use_orientation": true,"steps": 1234,"nova_api": {"connected": true,"state_streaming": true,9 unmodified lines- `connected`: Whether Nova API client is connected- `state_streaming`: Whether using Nova API for robot state streaming (vs. internal)- `ik`: Whether using Nova API for inverse kinematics (vs. internal)### Gym WebSocket API (RL/IL)The gym-style API is exposed at `ws://localhost:3004/nova-sim/api/v1/gym/ws`.It supports `reset`, `step`, `configure`, and `get_spaces`.Example request payloads:```json{"type": "configure", "data": {"robot": "ur5_t_push"}}{"type": "reset"}{"type": "step", "data": {"action": [0,0,0,0,0,0,0], "render": false}}```The server responds with `gym_reset`, `gym_step`, `gym_spaces`, or `gym_configured` messages.### HTTP Endpoints252 unmodified lines2. Keep it running at `http://localhost:3004` so the HTTP/websocket endpoints stay reachable.3. Run `pytest nova-sim/tests` to exercise:- API endpoints (`/metadata`, `/camera/<name>/video_feed`, `/video_feed`)- WebSocket control (`/ws`)- Gym-style websocket (`/gym/ws`)- Auxiliary MJPEG overlays after switching to the T-push UR5 sceneThe tests assume the server is accessible via `http://localhost:3004/nova-sim/api/v1` and will skip automatically if the API is unreachable.114 unmodified lines115116117118119120121122123124125126127128129130131132133134254 unmodified lines3893903913923933943953963973983994004014024 unmodified lines40740840941041141241341441541641741841942042142242342442542642742842986 unmodified lines51651751851952052152252352452513 unmodified lines5395405415425435445455465475489 unmodified lines558559560561562563564565566567568569570571572573574575576577578252 unmodified lines831832833834835836837114 unmodified linescd Quadruped-PyMPCpip install -e .# Start the server (default reward threshold: -0.1)python mujoco_server.py# Or with custom reward threshold for auto episode terminationpython mujoco_server.py --reward-threshold -0.05 # Stricter (5cm from target)python mujoco_server.py --reward-threshold -0.2 # Lenient (20cm from target)# Open browser at http://localhost:3004/nova-sim/api/v1```**Reward Threshold**: Episodes automatically terminate when the robot reaches within the specified distance of the target. See [REWARD_THRESHOLD.md](REWARD_THRESHOLD.md) for details.```## Docker Deployment### Getting Started254 unmodified lines// Receive messagesws.onmessage = (event) => {const msg = JSON.parse(event.data);if (msg.type === 'state') {console.log(msg.data);}};```Nova-Sim uses `/ws` as the shared control channel for the browser UI, trainers, and any RL clients. Every UI interaction (teleop, camera controls, robot switching) and the trainer handshake/notifications flows through this single socket; the UI `state` messages shown below now also carry the action deltas, integrated reward, and trainer connection status that RL agents need. The legacy `/nova-sim/api/v1/gym/ws` endpoint remains for backwards compatibility, but new clients should talk to `/ws` instead.#### Client → Server Messages4 unmodified lines| `command` | `{vx, vy, vyaw}` | Set velocity command || `reset` | `{}` | Reset robot to standing pose || `switch_robot` | `{robot, scene?}` | Switch active robot and optional scene || `configure` | `{robot, scene?}` | Reconfigure the active robot/scene (reuses the UI configure flow) || `camera` | `{action, ...}` | Camera control || `camera_follow` | `{follow}` | Toggle camera follow mode || `teleop_command` | `{dx, dy, dz}` | Apply incremental cartesian jog command (UI teleop) |RL agents, trainers, and CLI tools connect to `/ws` as well by sending gym-style control packets that include a request `id`. The server replies with `gym_*` responses so your existing clients keep working while also benefiting from the richer state stream.**Gym-style RL/RL control messages** (use `id` and expect a `gym_*` response):```{"type": "reset", "id": 42}{"type": "step", "data": {"action": [0,0,0,0,0,0,0], "render": false}, "id": 43}{"type": "configure", "data": {"robot": "ur5_t_push"}, "id": 44}{"type": "get_spaces", "id": 45}{"type": "close", "id": 46}```The server replies with `gym_reset`, `gym_step`, `gym_configured`, `gym_spaces`, or `gym_closed` (matching the `id` you supplied). Every message still flows over `/ws`, so UI teleop commands, state updates, and gym responses share the same connection.**`command`:**```json{"type": "command", "data": {"vx": 0.5, "vy": 0.0, "vyaw": 0.0}}86 unmodified lines"base_height": 0.46,"upright": 0.98,"steps": 1234,"command": {"vx": 0.5, "vy": 0.0, "vyaw": 0.0},"reward": 0.0,"teleop_command": {"dx": 0.05, "dy": 0.0, "dz": 0.0},"trainer_connected": true}}```13 unmodified lines"control_mode": "ik","use_orientation": true,"steps": 1234,"reward": -0.25,"command": {"vx": 0.1, "vy": 0.0, "vyaw": 0.0},"teleop_command": {"dx": 0.02, "dy": 0.0, "dz": 0.0},"trainer_connected": true,"nova_api": {"connected": true,"state_streaming": true,9 unmodified lines- `connected`: Whether Nova API client is connected- `state_streaming`: Whether using Nova API for robot state streaming (vs. internal)- `ik`: Whether using Nova API for inverse kinematics (vs. internal)- `command`: The latest velocity command (`vx`, `vy`, `vyaw`) that drives locomotion or arm movement.- `teleop_command`: The most recent UI teleop delta (`dx`, `dy`, `dz`) so trainers know how the UI nudged the robot.- `reward`: The integrated task reward from the simulator that remote trainers can consume.- `trainer_connected`: Whether a trainer handshake is active on `/ws` (useful for status LEDs).### State broadcasts and trainer notificationsEvery `/ws` client receives a `state` message roughly every 100 ms. The examples above show the locomotion (`spot`) and arm (`ur5`) payloads; the payload also now includes:- `command`: The last velocity command that drives locomotion or arm motion (`vx`, `vy`, `vyaw`).- `teleop_command`: The latest UI teleop delta (`dx`, `dy`, `dz`) so trainers know how the browser nudged the robot.- `reward`: The integrated task reward that trainers can consume without sending a separate `step`.- `trainer_connected`: Whether a trainer handshake is active on `/ws` (used to update the UI indicator).Trainers announce themselves by sending a `trainer_identity` payload when the socket opens. The server mirrors that information into the `trainer_status` broadcasts (`trainer_status` messages flow to every UI client) and lets trainers emit `notification` payloads that the UI receives as `trainer_notification` events.### HTTP Endpoints252 unmodified lines2. Keep it running at `http://localhost:3004` so the HTTP/websocket endpoints stay reachable.3. Run `pytest nova-sim/tests` to exercise:- API endpoints (`/metadata`, `/camera/<name>/video_feed`, `/video_feed`)- Unified WebSocket control (`/ws`)- Auxiliary MJPEG overlays after switching to the T-push UR5 sceneThe tests assume the server is accessible via `http://localhost:3004/nova-sim/api/v1` and will skip automatically if the API is unreachable.
8 unmodified lines910111213141582 unmodified lines98991001011021031 unmodified line10510610710810911088 unmodified lines199200201202203204205278 unmodified lines48448548648748848943 unmodified lines53353453553653753853917 unmodified lines55755855956056156256356456510 unmodified lines57657757857958058168 unmodified lines650651652653654655172 unmodified lines82882983083183283383483583683783883928 unmodified lines86886987087187287339 unmodified lines913914915916917918138 unmodified lines10571058105910601061106271 unmodified lines11341135113611371138113911404 unmodified lines1145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119729 unmodified lines122712281229123012311232568 unmodified lines1801180218031804180518061807180818091810181118128 unmodified linesimport numpy as npimport mujocofrom pathlib import Pathfrom typing import Anyfrom flask import Flask, Response, render_template_string, request, jsonifyfrom flask_sock import Sock82 unmodified lines}episode_control_lock = threading.Lock()# WebSocket clientsws_clients = set()ws_clients_lock = threading.Lock()1 unmodified linetrainer_ws_clients_lock = threading.Lock()trainer_client_metadata: dict = {}trainer_client_metadata_lock = threading.Lock()# Camera state for orbit controlscam = mujoco.MjvCamera()88 unmodified linesOVERLAY_CAMERA_PRESETS = {"ur5_t_push": UR5_T_PUSH_OVERLAY_PRESETS,"scene_t_push": UR5_T_PUSH_OVERLAY_PRESETS,}CAMERA_FEEDS = [278 unmodified linesobs = env._get_obs()cmd = env.get_command()steps = env.stepswith trainer_ws_clients_lock:trainer_connected = len(trainer_ws_clients) > 043 unmodified lines'control_mode': control_mode,'use_orientation': use_orientation,'steps': int(steps),'reward': env.get_task_reward(),'nova_api': {'connected': nova_connected,'state_streaming': nova_state_streaming,17 unmodified lines'base_height': base_height,'upright': upright,'steps': int(steps),'vx': float(cmd[0]),'vy': float(cmd[1]),'vyaw': float(cmd[2]),'trainer_connected': trainer_connected}})10 unmodified linesws_clients.difference_update(dead_clients)def _build_trainer_status_payload():"""Build a summary payload describing connected trainer clients."""with trainer_ws_clients_lock:68 unmodified linesws_clients.difference_update(dead_clients)def _signal_episode_control(action: str):"""Set episode control flags and notify trainer/UI clients."""action = (action or "").lower()172 unmodified linestime.sleep(0.04)def handle_ws_message(data):"""Handle incoming WebSocket message."""global needs_robot_switch, camera_followmsg_type = data.get('type')if msg_type == 'command':payload = data.get('data', {})vx = payload.get('vx', 0.0)28 unmodified linesprint(f"Robot switch requested: {robot} / scene: {scene}")needs_robot_switch = {"robot": robot, "scene": scene}elif msg_type == 'camera':payload = data.get('data', {})a = cam.azimuth * np.pi / 180.039 unmodified lineselse:updated_target = Nonebroadcast_to_trainer("teleop_command",{138 unmodified linessys.path.insert(0, ur5_dir)from ur5_env import UR5Envsys.path.pop(0)if scene:return UR5Env(render_mode="rgb_array", width=RENDER_WIDTH, height=RENDER_HEIGHT, scene_name=scene)return UR5Env(render_mode="rgb_array", width=RENDER_WIDTH, height=RENDER_HEIGHT)71 unmodified linesbreaktry:data = json.loads(message)handle_ws_message(data)except json.JSONDecodeError:print(f"Invalid JSON received: {message}")except Exception as e:4 unmodified lines# Unregister clientwith ws_clients_lock:ws_clients.discard(ws)print('WebSocket client disconnected')@sock.route(f'{API_PREFIX}/trainer/ws')def trainer_websocket_handler(ws):"""Handle trainer WebSocket clients (teleop commands + notifications)."""print('Trainer client connected')with trainer_ws_clients_lock:trainer_ws_clients.add(ws)_register_trainer_client(ws)broadcast_trainer_connection_status()broadcast_state()try:while True:message = ws.receive()if message is None:breaktry:data = json.loads(message)_handle_trainer_message(ws, data)except json.JSONDecodeError:print(f"Trainer sent invalid JSON: {message}")except Exception as exc:print(f"Error handling trainer message: {exc}")finally:with trainer_ws_clients_lock:trainer_ws_clients.discard(ws)_unregister_trainer_client(ws)print('Trainer client disconnected')broadcast_state()broadcast_trainer_connection_status()def _handle_trainer_message(ws, data):"""Process messages sent from the training client."""msg_type = data.get("type")if msg_type == "trainer_identity":payload = data.get("data", {}) or {}identity = payload.get("trainer_id") or payload.get("trainer_name") or payload.get("name") or "trainer"_set_trainer_identity(ws, identity)broadcast_trainer_connection_status()returnif msg_type == "notification":payload = data.get("data", {})payload.setdefault("timestamp", time.time())broadcast_notification_to_ui(payload)@sock.route(f'{API_PREFIX}/gym/ws')29 unmodified lineselif msg_type == "step":action = payload.get("action", [])obs, reward, terminated, truncated, info = session.step(action)response = {"type": "gym_step","data": {568 unmodified lines<span>Z nudge</span></span></li><li><span class="hint-key"><kbd>Enter</kbd><span>End episode</span></span></li></ul></div><div class="robot-info" id="robot_info">8 unmodified lines910111213141582 unmodified lines98991001011021031041051061071 unmodified line10911011111211311411511688 unmodified lines205206207208209210278 unmodified lines48949049149249349449549649749849950050150250350443 unmodified lines54854955055155255355455555617 unmodified lines57457557657757857958058158258358458510 unmodified lines59659759859960060160260360460560660760860961061161261361461561661761861962068 unmodified lines689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794172 unmodified lines96796896997097197297397497597697797897998098198298398498528 unmodified lines1014101510161017101810191020102110221023102410251026102739 unmodified lines10671068106910701071107210731074107510761077107810791080108110821083138 unmodified lines122212231224122512261227122871 unmodified lines13001301130213031304130513064 unmodified lines1311131213131314131513161317131813191320132113221323132413251326132729 unmodified lines1357135813591360136113621363568 unmodified lines1932193319341935193619378 unmodified linesimport numpy as npimport mujocofrom pathlib import Pathfrom typing import Any, Optionalfrom flask import Flask, Response, render_template_string, request, jsonifyfrom flask_sock import Sock82 unmodified lines}episode_control_lock = threading.Lock()# Latest teleoperation command (for trainer state)last_teleop_command: Optional[dict[str, Any]] = Noneteleop_lock = threading.Lock()# WebSocket clientsws_clients = set()ws_clients_lock = threading.Lock()1 unmodified linetrainer_ws_clients_lock = threading.Lock()trainer_client_metadata: dict = {}trainer_client_metadata_lock = threading.Lock()gym_sessions: dict = {}gym_sessions_lock = threading.Lock()# Camera state for orbit controlscam = mujoco.MjvCamera()88 unmodified linesOVERLAY_CAMERA_PRESETS = {"ur5_t_push": UR5_T_PUSH_OVERLAY_PRESETS,}CAMERA_FEEDS = [278 unmodified linesobs = env._get_obs()cmd = env.get_command()steps = env.stepscommand = {"vx": float(cmd[0]) if len(cmd) > 0 else 0.0,"vy": float(cmd[1]) if len(cmd) > 1 else 0.0,"vyaw": float(cmd[2]) if len(cmd) > 2 else 0.0,}with teleop_lock:teleop_snapshot = last_teleop_command.copy() if last_teleop_command else Nonereward_value = Noneif hasattr(env, "get_task_reward"):reward_value = env.get_task_reward()with trainer_ws_clients_lock:trainer_connected = len(trainer_ws_clients) > 043 unmodified lines'control_mode': control_mode,'use_orientation': use_orientation,'steps': int(steps),'reward': reward_value,'command': command,'teleop_command': teleop_snapshot,'nova_api': {'connected': nova_connected,'state_streaming': nova_state_streaming,17 unmodified lines'base_height': base_height,'upright': upright,'steps': int(steps),'command': command,'teleop_command': teleop_snapshot,'vx': float(cmd[0]),'vy': float(cmd[1]),'vyaw': float(cmd[2]),'reward': reward_value,'trainer_connected': trainer_connected}})10 unmodified linesws_clients.difference_update(dead_clients)def _handle_trainer_message(ws, data):"""Process message payloads originating from trainers."""msg_type = data.get("type")if msg_type == "trainer_identity":payload = data.get("data", {}) or {}identity = payload.get("trainer_id") or payload.get("trainer_name") or payload.get("name") or "trainer"with trainer_ws_clients_lock:trainer_ws_clients.add(ws)_register_trainer_client(ws)_set_trainer_identity(ws, identity)broadcast_state()broadcast_trainer_connection_status()returnif msg_type == "notification":payload = data.get("data", {})payload.setdefault("timestamp", time.time())broadcast_notification_to_ui(payload)def _build_trainer_status_payload():"""Build a summary payload describing connected trainer clients."""with trainer_ws_clients_lock:68 unmodified linesws_clients.difference_update(dead_clients)def _safe_ws_send(ws, message: dict):"""Send JSON message over WebSocket without raising."""try:ws.send(json.dumps(message))except Exception:passdef _get_or_create_gym_session(ws):with gym_sessions_lock:session = gym_sessions.get(ws)if session is None:session = GymSession()gym_sessions[ws] = sessionreturn sessiondef _remove_gym_session(ws):with gym_sessions_lock:session = gym_sessions.pop(ws, None)if session:session.close()def _handle_gym_ws_message(ws, data):"""Handle gym-style control messages routed over `/ws`."""msg_type = data.get("type")if msg_type not in {"reset", "step", "configure", "get_spaces", "close"}:return Falsemsg_id = data.get("id")if msg_id is None:return Falsepayload = data.get("data", {}) or {}session = _get_or_create_gym_session(ws)try:if msg_type == "reset":seed = payload.get("seed")obs, info = session.reset(seed=seed)response = {"type": "gym_reset","data": {"obs": obs.tolist(),"info": _serialize_value(info),},}elif msg_type == "step":action = payload.get("action", [])render = bool(payload.get("render", False))obs, reward, terminated, truncated, info = session.step(action)response = {"type": "gym_step","data": {"obs": obs.tolist(),"reward": float(reward),"terminated": bool(terminated),"truncated": bool(truncated),"info": _serialize_value(info),},}if render:frame_jpeg = session.render_jpeg()if frame_jpeg:response["data"]["frame_jpeg"] = frame_jpegelif msg_type == "configure":robot = payload.get("robot", "ur5")scene = payload.get("scene")session.configure(robot, scene)response = {"type": "gym_configured","data": {"robot": session.robot, "scene": session.scene},}elif msg_type == "get_spaces":response = {"type": "gym_spaces","data": {"action_space": _serialize_space(session.env.action_space),"observation_space": _serialize_space(session.env.observation_space),},}elif msg_type == "close":response = {"type": "gym_closed"}_remove_gym_session(ws)else:response = {"type": "gym_error","message": f"Unknown message type: {msg_type}",}if msg_id is not None:response["id"] = msg_id_safe_ws_send(ws, response)except Exception as exc:error_response = {"type": "gym_error", "message": str(exc)}if msg_id is not None:error_response["id"] = msg_id_safe_ws_send(ws, error_response)return Truedef _signal_episode_control(action: str):"""Set episode control flags and notify trainer/UI clients."""action = (action or "").lower()172 unmodified linestime.sleep(0.04)def handle_ws_message(ws, data):"""Handle incoming WebSocket message."""global needs_robot_switch, camera_follow, last_teleop_commandmsg_type = data.get('type')if _handle_gym_ws_message(ws, data):returnif msg_type in ("trainer_identity", "notification"):_handle_trainer_message(ws, data)returnif msg_type == 'command':payload = data.get('data', {})vx = payload.get('vx', 0.0)28 unmodified linesprint(f"Robot switch requested: {robot} / scene: {scene}")needs_robot_switch = {"robot": robot, "scene": scene}elif msg_type == 'configure':payload = data.get('data', {})robot = payload.get('robot')scene = payload.get('scene')if robot:print(f"Configure requested: {robot} / scene: {scene}")needs_robot_switch = {"robot": robot, "scene": scene}elif msg_type == 'camera':payload = data.get('data', {})a = cam.azimuth * np.pi / 180.039 unmodified lineselse:updated_target = Nonewith teleop_lock:last_teleop_command = {"dx": dx,"dy": dy,"dz": dz,"robot": current_robot,"scene": getattr(env, "scene_name", None) if env is not None else None,"target": updated_target.tolist() if updated_target is not None else None,"timestamp": timestamp,}broadcast_to_trainer("teleop_command",{138 unmodified linessys.path.insert(0, ur5_dir)from ur5_env import UR5Envsys.path.pop(0)# scene is already resolved by _resolve_robot_scene (e.g., "scene_t_push")if scene:return UR5Env(render_mode="rgb_array", width=RENDER_WIDTH, height=RENDER_HEIGHT, scene_name=scene)return UR5Env(render_mode="rgb_array", width=RENDER_WIDTH, height=RENDER_HEIGHT)71 unmodified linesbreaktry:data = json.loads(message)handle_ws_message(ws, data)except json.JSONDecodeError:print(f"Invalid JSON received: {message}")except Exception as e:4 unmodified lines# Unregister clientwith ws_clients_lock:ws_clients.discard(ws)with trainer_ws_clients_lock:was_trainer = ws in trainer_ws_clientstrainer_ws_clients.discard(ws)if was_trainer:_unregister_trainer_client(ws)broadcast_state()broadcast_trainer_connection_status()_remove_gym_session(ws)print('WebSocket client disconnected')@sock.route(f'{API_PREFIX}/gym/ws')29 unmodified lineselif msg_type == "step":action = payload.get("action", [])obs, reward, terminated, truncated, info = session.step(action)print(f"[GYM WS] step reward={reward:.4f}, terminated={terminated}, truncated={truncated}", flush=True)response = {"type": "gym_step","data": {568 unmodified lines<span>Z nudge</span></span></li></ul></div><div class="robot-info" id="robot_info">
799 unmodified lines800801802803804805806807808809810811812813799 unmodified linesobservation = self._get_obs()# Reward: distance to targetee_pos = self.get_end_effector_pos()dist = np.linalg.norm(ee_pos - self._target_pos)reward = -distterminated = Falsetruncated = self.steps >= self.max_stepsinfo = {"ee_pos": ee_pos,"target_pos": self._target_pos,799 unmodified lines800801802803804805806807808809810811812813814815816817818819820821799 unmodified linesobservation = self._get_obs()# Reward: Use task reward if available, otherwise distance to targettask_reward = self.get_task_reward()if task_reward is not None:reward = task_rewardelse:# Fallback: distance to target (for non-task scenes)ee_pos = self.get_end_effector_pos()dist = np.linalg.norm(ee_pos - self._target_pos)reward = -distterminated = Falsetruncated = self.steps >= self.max_steps# Compute info with distance for debuggingee_pos = self.get_end_effector_pos()dist = np.linalg.norm(ee_pos - self._target_pos)info = {"ee_pos": ee_pos,"target_pos": self._target_pos,
27 unmodified lines282930313233343527 unmodified linesresp = requests.get(f"{api_base}/metadata", timeout=5)data = resp.json()presets = data.get('overlay_camera_presets', {})assert 'ur5_t_push' in presets or 'scene_t_push' in presetstarget_presets = presets.get('ur5_t_push') or presets.get('scene_t_push') or []names = {item.get('name') for item in target_presets}assert {'aux_top', 'aux_side', 'aux_flange'}.issubset(names)27 unmodified lines282930313233343527 unmodified linesresp = requests.get(f"{api_base}/metadata", timeout=5)data = resp.json()presets = data.get('overlay_camera_presets', {})assert 'ur5_t_push' in presetstarget_presets = presets.get('ur5_t_push', [])names = {item.get('name') for item in target_presets}assert {'aux_top', 'aux_side', 'aux_flange'}.issubset(names)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167"""Unit test for episode control flag mechanism.
This test verifies that episode control flags work correctly across multiple episodeswithout the problematic env.reset() call."""
import threadingimport time
class MockEpisodeControlSystem: """ Minimal reproduction of the episode control system from mujoco_server.py.
This tests the core flag-based signaling mechanism without needing the full MuJoCo environment or WebSocket infrastructure. """
def __init__(self): self.episode_control_state = { "terminate": False, "truncate": False, } self.episode_control_lock = threading.Lock()
def signal_episode_control(self, action: str): """ Simulates _signal_episode_control() from mujoco_server.py.
This is the FIXED version without the env.reset() call. """ action = (action or "").lower() if action not in ("terminate", "truncate"): return
with self.episode_control_lock: self.episode_control_state[action] = True
# NOTE: The bug was here - the original code called env.reset() # The fix is to NOT reset the environment, as it interferes with # the gym client's episode lifecycle management.
def consume_episode_control_flags(self): """Simulates _consume_episode_control_flags() from mujoco_server.py.""" with self.episode_control_lock: terminate = self.episode_control_state.get("terminate", False) truncate = self.episode_control_state.get("truncate", False) self.episode_control_state["terminate"] = False self.episode_control_state["truncate"] = False return terminate, truncate
def test_episode_control_multiple_episodes(): """ Test that episode control works correctly for multiple consecutive episodes.
This simulates the scenario where a user presses Enter in the Nova-Sim UI during multiple episodes to terminate them early. """ system = MockEpisodeControlSystem()
num_episodes = 5 steps_per_episode = 10 step_to_terminate = 3
print(f"Testing {num_episodes} episodes with episode control...")
all_episodes_succeeded = True
for ep in range(num_episodes): print(f"\n=== Episode {ep + 1}/{num_episodes} ===")
# Simulate episode reset episode_terminated = False
for step in range(steps_per_episode): # Simulate user pressing Enter at a specific step if step == step_to_terminate: print(f" Step {step + 1}: User presses Enter (UI episode control)") system.signal_episode_control("terminate")
# Simulate gym client calling step() term, trunc = system.consume_episode_control_flags()
if term or trunc: print(f" Step {step + 1}: Episode ended (terminated={term}, truncated={trunc})") episode_terminated = True break else: print(f" Step {step + 1}: Continuing...")
if not episode_terminated: print(f" ✗ FAILED: Episode {ep + 1} did not terminate!") all_episodes_succeeded = False elif not episode_terminated: print(f" ✗ FAILED: Episode {ep + 1} ended too early!") all_episodes_succeeded = False else: print(f" ✓ SUCCESS: Episode {ep + 1} terminated correctly")
print("\n" + "=" * 50) if all_episodes_succeeded: print("✓ ALL TESTS PASSED") print(f"Episode control worked correctly for all {num_episodes} episodes") return True else: print("✗ SOME TESTS FAILED") return False
def test_episode_control_threading(): """ Test that episode control is thread-safe.
This simulates the scenario where the UI thread signals episode control while the gym client thread is consuming flags. """ system = MockEpisodeControlSystem()
print("\nTesting thread safety...")
# Simulate concurrent UI and gym client activity def ui_thread(): for i in range(10): time.sleep(0.01) system.signal_episode_control("terminate")
def gym_thread(): terminate_count = 0 for i in range(100): time.sleep(0.001) term, _ = system.consume_episode_control_flags() if term: terminate_count += 1 return terminate_count
ui = threading.Thread(target=ui_thread) gym = threading.Thread(target=gym_thread)
ui.start() gym.start()
ui.join() gym.join()
print("✓ Thread safety test completed without crashes") return True
if __name__ == "__main__": success = True
print("=" * 50) print("Episode Control Fix - Unit Tests") print("=" * 50)
success = test_episode_control_multiple_episodes() and success success = test_episode_control_threading() and success
print("\n" + "=" * 50) if success: print("✓ ALL UNIT TESTS PASSED") exit(0) else: print("✗ SOME UNIT TESTS FAILED") exit(1)