nepyope commited on
Commit
f1e56e0
·
verified ·
1 Parent(s): 4c22ccb

Update env.py

Browse files
Files changed (1) hide show
  1. env.py +22 -58
env.py CHANGED
@@ -1,33 +1,18 @@
1
- # env.py
2
-
3
  from pathlib import Path
4
  import subprocess
5
  import sys
6
- import os
7
- import signal
8
-
9
  import gymnasium as gym
10
  from gymnasium import spaces
11
  import numpy as np
12
  from huggingface_hub import snapshot_download
13
-
14
- # ensure the repo snapshot exists (no-op if already cached)
15
- snapshot_download(repo_id="lerobot/unitree-g1-mujoco", revision=None, cache_dir=None)
16
 
17
 
18
- def make_env(
19
- n_envs=1,
20
- use_async_envs: bool = False,
21
- publish_images=True,
22
- camera_port=5555,
23
- cameras=None,
24
- **kwargs,
25
- ):
26
- """
27
- launch run_sim.py as a subprocess (non-blocking),
28
- then return a dummy gym env.
29
- """
30
 
 
31
  repo_dir = Path(__file__).parent
32
  run_sim = repo_dir / "run_sim.py"
33
 
@@ -36,13 +21,9 @@ def make_env(
36
  print("path:", run_sim)
37
  print("=" * 60)
38
 
39
- # start simulator in its own process group
40
- proc = subprocess.Popen(
41
- [sys.executable, str(run_sim)],
42
- preexec_fn=os.setsid, # new session / new pgid == pid
43
- )
44
-
45
- print(f"simulator started as pid={proc.pid}, pgid={os.getpgid(proc.pid)}")
46
 
47
  class DummyEnv(gym.Env):
48
  metadata = {"render_modes": []}
@@ -54,42 +35,25 @@ def make_env(
54
  self.observation_space = spaces.Box(-1, 1, shape=(1,), dtype=np.float32)
55
 
56
  def reset(self, seed=None, options=None):
57
- return np.zeros(1, dtype=np.float32), {}
 
 
 
58
 
59
  def step(self, action):
60
  return np.zeros(1, dtype=np.float32), 0.0, False, False, {}
61
 
62
  def close(self):
63
- # do nothing: lerobot will keep the sim across env lifetime
64
  pass
65
 
66
  def kill_sim(self):
67
- """kill the run_sim process group (parent + its children, e.g. image publisher)"""
68
- if self.process is None:
69
- return
70
-
71
- try:
72
- pgid = os.getpgid(self.process.pid)
73
- except ProcessLookupError:
74
- # already dead
75
- return
76
-
77
- print(
78
- f"killing simulator process group pgid={pgid} (pid={self.process.pid})..."
79
- )
80
-
81
- # send SIGTERM to the whole group
82
- try:
83
- os.killpg(pgid, signal.SIGTERM)
84
- except ProcessLookupError:
85
- return
86
-
87
- try:
88
- self.process.wait(timeout=2)
89
- except subprocess.TimeoutExpired:
90
- print("force killing simulator process group...")
91
- os.killpg(pgid, signal.SIGKILL)
92
-
93
- self.process = None
94
-
95
- return DummyEnv(proc)
 
 
 
1
  from pathlib import Path
2
  import subprocess
3
  import sys
 
 
 
4
  import gymnasium as gym
5
  from gymnasium import spaces
6
  import numpy as np
7
  from huggingface_hub import snapshot_download
8
+ import os
9
+ import signal
10
+ snapshot_download("lerobot/unitree-g1-mujoco")
11
 
12
 
13
+ def make_env(n_envs=1, use_async_envs=False, **kwargs):
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ # define run_sim FIRST
16
  repo_dir = Path(__file__).parent
17
  run_sim = repo_dir / "run_sim.py"
18
 
 
21
  print("path:", run_sim)
22
  print("=" * 60)
23
 
24
+ # now you can launch it
25
+ proc = subprocess.Popen([sys.executable, str(run_sim)])
26
+ print(f"simulator started as pid={proc.pid}")
 
 
 
 
27
 
28
  class DummyEnv(gym.Env):
29
  metadata = {"render_modes": []}
 
35
  self.observation_space = spaces.Box(-1, 1, shape=(1,), dtype=np.float32)
36
 
37
  def reset(self, seed=None, options=None):
38
+ super().reset(seed=seed, options=options)
39
+ obs = np.zeros(1, dtype=np.float32)
40
+ info = {}
41
+ return obs, info
42
 
43
  def step(self, action):
44
  return np.zeros(1, dtype=np.float32), 0.0, False, False, {}
45
 
46
  def close(self):
 
47
  pass
48
 
49
  def kill_sim(self):
50
+ if self.process.poll() is None:
51
+ print("killing simulator subprocess...")
52
+ self.process.terminate()
53
+ try:
54
+ self.process.wait(timeout=2)
55
+ except subprocess.TimeoutExpired:
56
+ print("force killing simulator subprocess...")
57
+ self.process.kill()
58
+
59
+ return DummyEnv(proc)