40 lines
2.1 KiB
Python
40 lines
2.1 KiB
Python
"""Virtual-clock supervisor demo. No robot connection and no --execute option."""
|
|
import argparse
|
|
import json
|
|
import tempfile
|
|
from pathlib import Path
|
|
from trajectory_supervisor import Trajectory,Supervisor
|
|
from elbow_raise_preflight import ROOT,DEFAULT_PLAN
|
|
|
|
|
|
def main():
|
|
parser=argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument('--fault',choices=['none','feedback-timeout','stop-timeout'],default='none')
|
|
args=parser.parse_args()
|
|
plan=json.loads(DEFAULT_PLAN.read_text());trajectory=Trajectory(plan['waypoints'])
|
|
directory=Path(tempfile.mkdtemp(prefix='elbow_supervisor_sim_',dir=ROOT/'data'))
|
|
with (directory/'events.jsonl').open('w') as log:
|
|
def emit(event):
|
|
line=json.dumps(event,ensure_ascii=False)
|
|
log.write(line+'\n');log.flush();print(line,flush=True)
|
|
supervisor=Supervisor(trajectory,emit)
|
|
for step in range(int((trajectory.duration+5)/.02)+1):
|
|
now=round(step*.02,8)
|
|
reference=trajectory.sample(max(0,now-.02))
|
|
velocity=reference['velocity']
|
|
if supervisor.state=='STOPPING' and args.fault!='stop-timeout':velocity=[0.]*7
|
|
observation=dict(seq=step,received=now,position=reference['position'],velocity=velocity)
|
|
if args.fault=='feedback-timeout' and now>=.5:observation=None
|
|
supervisor.tick(now,observation,acknowledgement=True if step==1 else None,
|
|
stop_ack=True if supervisor.state=='STOPPING' and args.fault!='stop-timeout' else None,
|
|
cancel=args.fault=='stop-timeout' and now==.5)
|
|
if supervisor.state in supervisor.TERMINAL:break
|
|
result=dict(mode='simulation_only_virtual_clock',hardware_connected=False,motion_sent=False,
|
|
state=supervisor.state,reason=supervisor.reason,fault_injected=args.fault,
|
|
trajectory_duration_s=trajectory.duration,events=str(directory/'events.jsonl'))
|
|
(directory/'result.json').write_text(json.dumps(result,indent=2))
|
|
print(json.dumps(result,ensure_ascii=False),flush=True)
|
|
|
|
|
|
if __name__=='__main__':main()
|