class Control(cmd.Cmd): """Parse robot commands and apply them. Does no real error checking.""" def __init__(self): cmd.Cmd.__init__(self); self.points = []; self.ready = False; self.motor = None; def do_move(self, line): """move x, y, z, power Go to lego coordinates. """ x, y, z, p = line.split(); coord = LegoCoord(int(x), int(y), int(z)).to_delta(); if self.motor is None: print "No connection."; else: self.delta.move_to_coord(coord, int(p)); def do_set_points(self, line): """set_points [x, y, z; ...] Set the points to draw at. """ try: tmp_points = []; c = 0; # Parse tuple and create lego coordinate for xyz in line[1:-1].split(';'): x, y, z = ast.literal_eval(xyz.strip()); print x; tmp_points.append(LegoCoord(x, y, z)); c += 1; # Store new list and set ready to run self.points = tmp_points; self.ready = True; print repr(c) + " Point[s] Set"; except Exception as err: print "Invalid Syntax"; def do_clear_points(self, line): """clear_points Clear all set points. """ self.points = []; self.ready = False; print "Points Cleared"; def do_run(self, line): """run [time] Travel through the requested positions. """ if not self.ready or self.motor is None: print "Missing connection or points." return # Convert points to targets targets = map(lambda x: Target(x, self.points), self.points); # Sort targets to reduce travel distance sorted = target.sort(targets, START); # Map each target into its steps steps = range(len(self.points)); for x in range(len(self.points)): if x == 0: steps[x] = sorted[x].get_moves(START, 1); else: steps[x] = sorted[x].get_moves(sorted[x-1].delta_coord, 1); steps2 = range(len(self.points)); for x in range(len(self.points)): if x == 0: steps2[x] = sorted[x].get_moves(sorted[-1].delta_coord, 1); else: steps2[x] = sorted[x].get_moves(sorted[x-1].delta_coord, 1); for x in range(2): if x == 0: for sublist in steps: for coord, power in sublist: self.delta.move_to_coord(coord, power); print coord else: for sublist in steps2: for coord, power in sublist: self.delta.move_to_coord(coord, power); print coord self.delta.move_to_angles([-45, -45, -45]); def do_connect(self, line): """connect Connect to the delta. """ self.motor = Motors(); self.delta = Delta(self.motor); def do_disconnect(self, line): self.motor.close(); self.motor = None; def do_exit(self, line): if self.motor: self.motor.close(); return True; def do_EOF(self, line): if self.motor: self.motor.close(); return True;
def do_connect(self, line): """connect Connect to the delta. """ self.motor = Motors(); self.delta = Delta(self.motor);