force_scale = 10.0 sizes = [2, 1] net = brain.loadBrain() INIT_ANG = 0.3 cart.setAngle(math.pi + INIT_ANG) while not gui.check_for_quit(): # loop until user hits escape # Test for falling over # if fallen then reset with a random angle input = copy.deepcopy(cart.getState()) input[0] = input[0] - math.pi out = net.ffwd(input) force = (out[0] - 0.5) * force_scale # step the car for a single GUI frame cart.step(force, dt) # draw the cart and display info draw(screen, cart, force) # slow the gui down to the given frameRate gui.tick(frameRate)
while not gui.check_for_quit(): # loop until user hits escape force=0.0 # check for user key press keyinput = gui.get_pressed() if keyinput[gui.keys.K_LEFT]: # push left force = -1.0 if keyinput[gui.keys.K_RIGHT]: # push right force = 1.0 # step the car for a single GUI frame cart.step(force,1.0/frameRate) ################ RESET CODE ############################## # Test for falling over # if fallen then reset with a random angle if abs(math.pi-cart.getAngle()) > math.pi/2: cart.setAngle(math.pi+0.02*(random.random()-0.5)) ############################################################ # draw the cart and display info draw(screen,cart,force) # slow the gui down to the given frameRate gui.tick(frameRate)
#### MAIN CODE ######################### # create a cart + inverted pendulum cart=cart.Cart() # set the initial angle cart.setAngle(math.pi+0.05) dt=0.1 # Time step for control gui=guisimple.Gui(10) while not gui.check_for_quit(): # loop until user hits escape # step the car for a single GUI frame cart.step(force,dt) gui.update(cart)
while not gui.check_for_quit(): # loop until user hits escape force = 0.0 # check for user key press keyinput = gui.get_pressed() if keyinput[gui.keys.K_LEFT]: # push left force = -1.0 if keyinput[gui.keys.K_RIGHT]: # push right force = 1.0 # step the car for a single GUI frame cart.step(force, 1.0 / frameRate) ################ RESET CODE ############################## # Test for falling over # if fallen then reset with a random angle if abs(math.pi - cart.getAngle()) > math.pi / 2: cart.setAngle(math.pi + 0.02 * (random.random() - 0.5)) ############################################################ # draw the cart and display info draw(screen, cart, force) # slow the gui down to the given frameRate gui.tick(frameRate)