microduck-spin / run_local.py
Swenlon's picture
Fix local inference timing and add reproducible spin configuration
bf84d6c verified
Raw History Blame Contribute Delete
2.89 kB
"""Run the documented CPU rehearsal, never training or a hardware controller.
Use Python's standard library to verify the model and construct an explicit
command for the project's existing simulator. --check-only starts nothing.
"""
import argparse
import hashlib
import json
from pathlib import Path
import shlex
import subprocess
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('--repo',type=Path,required=True,help='Local microduck_rl checkout with its configured .venv')
parser.add_argument('--check-only',action='store_true',help='Verify paths and hash, print command, do not start simulation')
parser.add_argument('--save-csv',type=Path,help='Optional observations/actions CSV; existing files are refused')
args=parser.parse_args()
directory=Path(__file__).resolve().parent
config=json.loads((directory/'inference_config.json').read_text())
repo=args.repo.expanduser().resolve();policy=directory/config['policy_file']
if hashlib.sha256(policy.read_bytes()).hexdigest()!=config['policy_sha256']:
raise SystemExit('Model hash mismatch; refusing inference')
interpreter=repo/'.venv/bin/mjpython';script=repo/config['simulator_script'];scene=repo/config['scene']
for path in (interpreter,script,scene):
if not path.is_file():raise SystemExit('Required local file missing: '+str(path))
if (config['physics_timestep_s'],config['control_period_s'],config['actuator_delay']['control_steps'])!=(.005,.02,1):
raise SystemExit('Configuration differs from the measured reference')
command=[str(interpreter),str(script),'--walking',str(policy),'--scene',str(scene),
'--new-cmd-obs','--lin-vel-x','1','--lin-vel-y','0','--ang-vel-z','0',
'--action-scale','1','--delay','1','--vin','7.4','--vin-drop-gain','0.1']
if args.save_csv:
csv=args.save_csv.expanduser().resolve()
if csv.exists():raise SystemExit('Refusing to overwrite CSV: '+str(csv))
if not csv.parent.is_dir():raise SystemExit('CSV parent directory does not exist: '+str(csv.parent))
command+=['--save-csv',str(csv)]
print('Model: checkpoint 19999 / completed update 20000; SHA-256 verified.',flush=True)
print('SIMULATION ONLY. Fixed behavior command [1,0,0], 20 ms actuator delay.',flush=True)
print('Do not change arrow/space/head/body commands initially. Press Q IN THIS TERMINAL to exit.',flush=True)
print('No safe-stop, fall recovery or hardware validation is provided.',flush=True)
print('cd '+shlex.quote(str(repo))+' && '+shlex.join(command),flush=True)
if args.check_only:return
# The subprocess inherits the terminal for keyboard input. No shell or
# robot/network action is involved; only the local simulator is started.
raise SystemExit(subprocess.call(command,cwd=repo))
if __name__=='__main__':main()