Buckets:
| """Start a fresh simulator, calibrate from RGB, sort three blocks, and verify.""" | |
| import argparse | |
| import json | |
| import os | |
| from pathlib import Path | |
| import socket | |
| import subprocess | |
| import sys | |
| import time | |
| import urllib.error | |
| import urllib.request | |
| ROOT = Path(__file__).resolve().parent | |
| def request(port, endpoint, payload=None): | |
| url = f'http://127.0.0.1:{port}{endpoint}' | |
| data = None if payload is None else json.dumps(payload).encode() | |
| req = urllib.request.Request(url, data=data, headers={'Content-Type': 'application/json'}) | |
| with urllib.request.urlopen(req, timeout=5) as response: | |
| result = json.load(response) | |
| if 'error' in result: | |
| raise RuntimeError(result['error']) | |
| return result | |
| def main(): | |
| parser = argparse.ArgumentParser(description=__doc__) | |
| parser.add_argument('--headless', action='store_true', help='Run without a viewer window.') | |
| parser.add_argument('--fast', action='store_true', help='Run physics without real-time delays.') | |
| parser.add_argument('--record', action='store_true', help='Record the camera and create the captioned video; requires ffmpeg.') | |
| parser.add_argument('--port', type=int, default=int(os.environ.get('SO101_PORT', '8877'))) | |
| parser.add_argument('--close-after-run', action='store_true', help='Close the viewer automatically after completion.') | |
| args = parser.parse_args() | |
| if not 1 <= args.port <= 65535: | |
| parser.error('--port must be between 1 and 65535') | |
| with socket.socket() as probe: | |
| if probe.connect_ex(('127.0.0.1', args.port)) == 0: | |
| parser.error(f'Port {args.port} is already in use. Close that simulator or choose another --port.') | |
| env = dict(os.environ, SO101_PORT=str(args.port)) | |
| work = ROOT / 'work' | |
| work.mkdir(exist_ok=True) | |
| (ROOT / 'results').mkdir(exist_ok=True) | |
| subprocess.run([sys.executable, str(ROOT / 'scripts/build_lego_scene.py')], cwd=ROOT, env=env, check=True) | |
| python = sys.executable | |
| if sys.platform == 'darwin' and not args.headless: | |
| python = str(Path(sys.executable).with_name('mjpython')) | |
| if not Path(python).exists(): | |
| raise RuntimeError('mjpython is missing from the uv environment; run uv sync --locked.') | |
| command = [python, str(ROOT / 'scripts/lego_simulator.py'), '--port', str(args.port)] | |
| if args.headless: | |
| command.append('--headless') | |
| if args.fast: | |
| command.append('--fast') | |
| log_path = work / 'simulator.log' | |
| with log_path.open('w') as log: | |
| simulator = subprocess.Popen(command, cwd=ROOT, env=env, stdout=log, stderr=subprocess.STDOUT) | |
| try: | |
| deadline = time.monotonic() + 60 | |
| while True: | |
| if simulator.poll() is not None: | |
| raise RuntimeError(f'Simulator exited before startup. See {log_path}') | |
| try: | |
| if request(args.port, '/state').get('scene') == 'lego': | |
| break | |
| except (OSError, urllib.error.URLError, ValueError): | |
| pass | |
| if time.monotonic() > deadline: | |
| raise RuntimeError(f'Simulator startup timed out. See {log_path}') | |
| time.sleep(.15) | |
| print('Simulator ready. Calibrating and sorting from the front camera...', flush=True) | |
| if args.record: | |
| subprocess.run([sys.executable, str(ROOT / 'video/record_run.py')], cwd=ROOT, env=env, check=True) | |
| subprocess.run([sys.executable, str(ROOT / 'video/edit_short_video.py')], cwd=ROOT, env=env, check=True) | |
| else: | |
| for script in ('calibrate_camera.py', 'sort_blocks.py'): | |
| subprocess.run([sys.executable, str(ROOT / 'scripts' / script)], cwd=ROOT, env=env, check=True) | |
| subprocess.run([sys.executable, str(ROOT / 'verify_result.py')], cwd=ROOT, env=env, check=True) | |
| print('Success: see results/blocks-in-box.png and results/verification.json.', flush=True) | |
| if not args.headless and not args.close_after_run: | |
| print('Close the viewer window or press Ctrl+C to stop the simulator.', flush=True) | |
| simulator.wait() | |
| finally: | |
| if simulator.poll() is None: | |
| try: | |
| request(args.port, '/shutdown', {}) | |
| simulator.wait(timeout=5) | |
| except (OSError, urllib.error.URLError, ValueError, subprocess.TimeoutExpired): | |
| simulator.terminate() | |
| try: | |
| simulator.wait(timeout=5) | |
| except subprocess.TimeoutExpired: | |
| simulator.kill() | |
| simulator.wait() | |
| if __name__ == '__main__': | |
| main() | |
Xet Storage Details
- Size:
- 4.76 kB
- Xet hash:
- 948be72ade1c43b56bcb467d91ecbc79893fb7d3308c3ae9937ffa9b7ceec235
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.