def __init__(self, address=("localhost", 9999)): self.address = address self.shutdown_event = threading.Event() self.should_shutdown = False self.socket = socket.socket(self.address_family, self.socket_type) print "bound to: ", address self.socket.bind(self.address) self.address = self.socket.getsockname() self.launcher = USBMissileLauncher() self.last_status_time = 0
class LauncherServer(object): address_family = socket.AF_INET socket_type = socket.SOCK_DGRAM max_packet_size = 1024 def __init__(self, address=("localhost", 9999)): self.address = address self.shutdown_event = threading.Event() self.should_shutdown = False self.socket = socket.socket(self.address_family, self.socket_type) print "bound to: ", address self.socket.bind(self.address) self.address = self.socket.getsockname() self.launcher = USBMissileLauncher() self.last_status_time = 0 def fileno(self): return self.socket.fileno() def serve(self, sleep=0.01): self.shutdown_event.clear() try: while not self.should_shutdown: t = time.time() if t - self.last_status_time > STATUS_INTERVAL: # heartbeat self.launcher.status() r, w, e = select.select([self], [], [], sleep) if self in r: self.handle_request() # sleep time.sleep(max(0.001, POLL_RATE - (time.time() - t))) finally: self.socket.close() self.should_shutdown = False self.shutdown_event.set() def handle_request(self): try: data, address = self.socket.recvfrom(self.max_packet_size) print "processing: ", data if data == LAUNCHER_EXIT: self.should_shutdown = True elif int(data) == FIRE: # don't respond to requests self.launcher.fire() elif int(data) == PRIME: # don't respond to requests self.launcher.prime() else: self.launcher.send_command(int(data)) except socket.error: return
# print # print x_timeout, y_timeout, (x_timeout > cur_time), (y_timeout > cur_time), cur_time, x_cmd, y_cmd # print # print cmd = 0 if x_timeout > cur_time: cmd |= x_cmd if y_timeout > cur_time: cmd |= y_cmd if not cmd: cmd = STOP send_command(cmd) launcher = USBMissileLauncher() def add_controller_command(timeout_x, timeout_y, cmd): # given a launcher, execute this command completely cur_time = time.time() x_cmd = cmd & (LEFT | RIGHT) y_cmd = cmd & (UP | DOWN) cmds = [(timeout_x, x_cmd), (timeout_y, y_cmd)] while True: cmd = 0 for t, c in cmds: if t > time.time(): cmd |= c if not cmd: break
# print x_timeout, y_timeout, (x_timeout > cur_time), (y_timeout > cur_time), cur_time, x_cmd, y_cmd # print # print cmd = 0 if x_timeout > cur_time: cmd |= x_cmd if y_timeout > cur_time: cmd |= y_cmd if not cmd: cmd = STOP send_command(cmd) launcher = USBMissileLauncher() def add_controller_command(timeout_x, timeout_y, cmd): # given a launcher, execute this command completely cur_time = time.time() x_cmd = cmd & (LEFT | RIGHT) y_cmd = cmd & (UP | DOWN) cmds = [(timeout_x, x_cmd), (timeout_y, y_cmd)] while True: cmd = 0 for t, c in cmds: if t > time.time(): cmd |= c
import time from usbturret import LEFT, RIGHT, UP, DOWN, STOP, USBMissileLauncher, FIRE launcher = USBMissileLauncher() def reset_to_lower_left(): down_done = False left_done = False while not down_done or not left_done: cmd = 0 if not down_done: cmd = cmd | DOWN if not left_done: cmd = cmd | LEFT status = launcher.send_command(cmd) if status & DOWN: down_done = True if status & LEFT: left_done = True def move_for_time(secs, cmd): start = time.time() # while time.time() - start < secs: # status = launcher.send_command(cmd) status = launcher.send_command(cmd) time.sleep(max(0, secs - (time.time() - start))) launcher.send_command(STOP)