def __init__(self): os.system('xset r off') self.tango = Tango() # Tango instance to issue commands self.root = Tk() self.root.bind('<KeyPress>', self.char_pressed) self.root.bind('<KeyRelease>', self.char_released) self.root.bind('<Left>', self.left_pressed) self.root.bind('<Right>', self.right_pressed) self.root.bind('<Up>', self.up_pressed) self.root.bind('<Down>', self.down_pressed)
def __init__(self): self.tango = Tango() self.queue = [] # queue of actions self.degree = [ ] # degree contains amounts by which to turn in the order they were added self.distance = [ ] # distance contains distances to travel in the order they were added self.pause = 1 # sleep time between all queued actions # feed this ActionQueue to the server and kick it off in the background self.server = Server(self) self.server.start()
class Playerbot: # changes to a 'current' location to go a specific direction TODO: these are copies from adventure; instead, break into a separate 'cardinal' file NORTH = [0, -1] EAST = [1, 0] SOUTH = [0, 1] WEST = [-1, 0] def __init__(self): self.tango = Tango() # the robot that will interact with the player self.cur_dir = Playerbot.NORTH # always start facing north self.tango.reset() def same_dir(dir1, dir2): for i, n in enumerate(dir1): if not (n == dir2[i]): return False return True def add_dir(dir1, dir2): ''' adds dir2 to dir1 ''' result = [] result.append(dir1[0]+dir2[0]) result.append(dir1[1]+dir2[1]) return result def diff_dir(dir1, dir2): ''' subtracts dir2 from dir1 ''' result = [] result.append(dir1[0]-dir2[0]) result.append(dir1[1]-dir2[1]) return result def encounter(self): ''' encounter makes tango look around like it has encountered an enemy ''' self.tango.head(True, Tango.SIDE) self.tango.head(True, Tango.SIDE) time.sleep(.5) self.tango.head(False, Tango.SIDE) self.tango.head(False, Tango.SIDE) time.sleep(.25) self.tango.head(False, Tango.SIDE) self.tango.head(False, Tango.SIDE) time.sleep(.5) self.tango.head(True, Tango.SIDE) self.tango.head(True, Tango.SIDE) def hit(self): ''' hit makes the tango simulate dealing combat damage ''' self.tango.head(True, Tango.LEFT_SHOULDER) self.tango.head(True, Tango.LEFT_SHOULDER) self.tango.head(True, Tango.LEFT_SHOULDER) self.tango.twist(True) self.tango.twist(True) time.sleep(.25) self.tango.twist(False) self.tango.twist(False) self.tango.head(False, Tango.LEFT_SHOULDER) self.tango.head(False, Tango.LEFT_SHOULDER) self.tango.head(False, Tango.LEFT_SHOULDER) def move(self, direction): ''' move makes tango drive in the given direction ''' self.turn(direction) # move forward for 1s then stop self.tango.drive(True) time.sleep(.5) self.tango.stop() def turn(self, direction): ''' turn causes the tango to turn towards the given direction ''' while not Playerbot.same_dir(self.cur_dir, direction): self.turn_90(direction) self.cur_dir = direction def turn_90(self, direction): ''' turn 90 makes a turn in 90 degree increments ''' change = Playerbot.diff_dir(direction, self.cur_dir) # determine the change Tango must make to face the correct direction turn_dir = 0 # direction to turn (-1 for left, 1 for right) # based on the if Playerbot.same_dir(self.cur_dir, Playerbot.NORTH) or Playerbot.same_dir(self.cur_dir, Playerbot.SOUTH): if change[1] == 2: change[0] = 1 change[1] = 1 elif change[1] == -2: change[0] = -1 change[1] = -1 turn_dir = change[0] if Playerbot.same_dir(self.cur_dir, Playerbot.SOUTH): turn_dir = -turn_dir elif Playerbot.same_dir(self.cur_dir, Playerbot.EAST) or Playerbot.same_dir(self.cur_dir, Playerbot.WEST): if change[0] == 2: change[1] = 1 change[0] = 1 elif change[0] == -2: change[1] = -1 change[0] = -1 turn_dir = change[1] if Playerbot.same_dir(self.cur_dir, Playerbot.WEST): turn_dir = -turn_dir else: print("ERROR:", direction, "does not match an expected direction") if turn_dir < 0: turn_dir = False elif turn_dir > 0: turn_dir = True else: print("ERROR:", turn_dir, "does not imply a direction for turning") self.tango.turn(turn_dir) time.sleep(1.6) self.tango.stop() self.cur_dir = Playerbot.add_dir(change, self.cur_dir)
def __init__(self): self.tango = Tango() # the robot that will interact with the player self.cur_dir = Playerbot.NORTH # always start facing north self.tango.reset()
import time from tango import Tango t = Tango() # move Tango forward to the first speed, go for 1 second, and stop t.drive(True) time.sleep(1) t.stop() # move back to 2nd speed, go for 1 sec, stop t.drive(False) t.drive(False) time.sleep(1) t.stop() # twist left, then right t.twist(False) time.sleep(1) t.twist(True) t.twist(True) time.sleep(1) # look up to max, look down to maximum t.head(t.UPDOWN, True) t.head(t.UPDOWN, True) time.sleep(1) t.head(t.UPDOWN, False) t.head(t.UPDOWN, False) t.head(t.UPDOWN, False) t.head(t.UPDOWN, False)
from tango import Tango, Page app = Tango(__name__) @app.route('/') def index(): return "Hello Tango." if __name__ == "__main__": app.run()
import sys sys.path.append('..') import os import subprocess import itertools import numpy as N from enthought.mayavi import mlab from tango import Tango from simulation import SlitSystem # Tango color palette tango = Tango(return_format='float') def rotations(iterable): """Generator that returns permutations of an iterable in order""" for n in range(len(iterable)): yield iterable[n:] + iterable[:n] mlab.figure(1, bgcolor=tango.white, fgcolor=tango.black, size=(800, 800)) mlab.clf() # Display a semi-transparent sphere sphere = mlab.points3d(0, 0, 0, scale_mode='none', scale_factor=2, color=tango.aluminum3, resolution=50,
class ActionQueue: def __init__(self): self.tango = Tango() self.queue = [] # queue of actions self.degree = [ ] # degree contains amounts by which to turn in the order they were added self.distance = [ ] # distance contains distances to travel in the order they were added self.pause = 1 # sleep time between all queued actions # feed this ActionQueue to the server and kick it off in the background self.server = Server(self) self.server.start() # add enables an action to be added to the queue # (actions will be added as either [function, direction] pairs or as [function] elements) def add(self, function, direction=None, port=None): self.queue.append(self.buildAction(function, direction, port)) def pop(self): ''' pop removes the first action in the queue ''' self.queue.pop(0) def push(self, function, direction=None, port=None): ''' push adds an action to the front of the queue. if adding a series of actions, push in the reverse order of desired execution ex. to make the robot MOVE_FORWARD->TURN_HEAD->MOVE_BACKWARD, first push MOVE_BACKWARD, then TURN_HEAD, and finally MOVE_FORWARD ''' self.queue.insert(0, self.buildAction(function, direction, port)) def buildAction(self, function, direction=None, port=None): ''' buildAction returns the action formatted as a list to be added to the queue ''' print(' adding:', function, direction, port) action = [ function ] # form the action by first adding the function to be invoked # if a direction is provided, tack it on to the action (this allows us to add calls with no arguments to the queue) if direction != None: action.append(direction) # if a port is provided, tack it on to the action if port != None: action.append(port) return action def execute( self ): # TODO: incrementally remove actions from queue directly following execution ''' execute goes through the queued actions and executes them in-order ''' for i, action in enumerate(self.queue): func = action[0] # pull out the function name # if arguments must be provided to call the function, do so. Otherwise, don't provide any. if len(action) > 2: func(action[1], action[2]) elif len(action) > 1: func(action[1]) # if executing a wheel-related action, sleep for the necessary amount of time if func == self.tango.drive: time.sleep(self.distance.pop(0)) elif func == self.tango.turn: time.sleep(self.degree.pop(0)) self.tango.stop( ) # stop after driving or turning (no effect if not moving) else: # if nothing was given, the robot must pause to listen if func == None: self.queue = self.queue[ i + 1:] # remove any actions already executed return # halt execution immediately func() time.sleep(self.pause) # pause for the specified time self.tango.reset( ) # avoid out of control bot after the execution of the user's commands self.queue = [] # remove all actions from queue # remove takes the action at the given index out of the action queue def remove(self, index): # disallow attempted removal of items outside the list if len(self.queue) < index + 1: return self.queue.pop(index) # set_distance sets the distance of turn rotation def set_degree(self, deg): self.degree.append(self.angle(deg)) # set_distance sets the distance of drive travel def set_distance(self, num): self.distance.append(self.foot(num)) # foot calculates the amount of sleep time for the robot to drive num feet def foot(self, num): return .025 * (num**2) + .3 * num - .125 # angle calculates the amount of sleep time for the robot to turn a given angle def angle(self, deg): return -.00002304527 * (deg**2) + .0144444444 * deg - .3833333333
class InputHandler: def __init__(self): os.system('xset r off') self.tango = Tango() # Tango instance to issue commands self.root = Tk() self.root.bind('<KeyPress>', self.char_pressed) self.root.bind('<KeyRelease>', self.char_released) self.root.bind('<Left>', self.left_pressed) self.root.bind('<Right>', self.right_pressed) self.root.bind('<Up>', self.up_pressed) self.root.bind('<Down>', self.down_pressed) # Determine what to move from the key pressed. def char_pressed(self, key): if key.char is 'w': self.tango.drive(True) elif key.char is 's': self.tango.drive(False) elif key.char is ' ': self.tango.reset() elif key.char is 'q': self.tango.twist(False) elif key.char is 'e': self.tango.twist(True) elif key.char is 'a': self.tango.turn(False) elif key.char is 'd': self.tango.turn(True) # Slow down the robot on key release def char_released(self, key): if key.char is 'a': self.tango.turn(True) elif key.char is 'd': self.tango.turn(False) def left_pressed(self, key): self.tango.head(True, self.tango.SIDE) def right_pressed(self, key): self.tango.head(False, self.tango.SIDE) def up_pressed(self, key): self.tango.head(True, self.tango.UPDOWN) def down_pressed(self, key): self.tango.head(False, self.tango.UPDOWN)