"""TRON1 task program; the bridge owns the official LimX SDK and policy."""
import argparse
import json
import time
import urllib.request


def main():
    parser=argparse.ArgumentParser(description=__doc__)
    parser.add_argument('--bridge',default='http://127.0.0.1:8765')
    args=parser.parse_args()
    try:
        for _ in range(100):
            # move() is a platform helper around the public policy, not a LimX API.
            # Implement your own image recognition and task decisions here.
            move(args.bridge,0.,0.,0.)
            time.sleep(.05)
    finally:
        for _ in range(60):
            move(args.bridge,0.,0.,0.)
            time.sleep(.05)


def move(bridge,vx,vy,yaw):
    request=urllib.request.Request(bridge+'/v1/locomotion',data=json.dumps({'command':[vx,vy,yaw]}).encode(),headers={'Content-Type':'application/json'})
    with urllib.request.urlopen(request,timeout=2) as response:return json.load(response)


if __name__=='__main__':main()
