def loadMap(self, fileName): tempSet = set() with open(fileName, 'r') as fd: lines = fd.readlines() for m, line in enumerate(lines): for n, char in enumerate(line): v1 = Matrix._makeMatrix([[m / 3], [n / 3]]) if char == '+' or char == '^' or char == '<' or char == 'v' or char == '>': if (n + 1) < len(line): v2 = Matrix._makeMatrix([[0], [1]]) if line[n + 1] == '-': tempSet.add(Connection(v1, v2)) elif line[n + 1] == '=': tempSet.add(Connection(v1, v2, "R")) if (m + 1) < len(lines): v2 = Matrix._makeMatrix([[1], [0]]) if lines[m + 1][n] == '|': tempSet.add(Connection(v1, v2)) elif lines[m + 1][n] == '=': tempSet.add(Connection(v1, v2, "R")) # check for connections if not char == '+': self.registerRobot(copy.deepcopy(v1), char) self.connections = list(tempSet) self.draw()
def registerRobot(self, position, orientationAscii): rotLocToGlob = Matrix._makeMatrix([[1, 0], [0, 1]]) rotGlobToLoc = Matrix._makeMatrix([[1, 0], [0, 1]]) if orientationAscii == '^': rotLocToGlob = Matrix._makeMatrix([[1, 0], [0, 1]]) rotGlobToLoc = Matrix._makeMatrix([[1, 0], [0, 1]]) elif orientationAscii == '<': rotLocToGlob = Matrix._makeMatrix([[0, 1], [-1, 0]]) rotGlobToLoc = Matrix._makeMatrix([[0, -1], [1, 0]]) elif orientationAscii == 'v': rotLocToGlob = Matrix._makeMatrix([[-1, 0], [0, -1]]) rotGlobToLoc = Matrix._makeMatrix([[-1, 0], [0, -1]]) elif orientationAscii == '>': rotLocToGlob = Matrix._makeMatrix([[0, -1], [1, 0]]) rotGlobToLoc = Matrix._makeMatrix([[0, 1], [-1, 0]]) self.robots.append( Robot.Robot(self, position, rotLocToGlob, rotGlobToLoc))
def __init__(self, start, direction, color="B"): if direction[0][0] < 0 or direction[1][0] < 0: self.start = start + direction self.direction = Matrix._makeMatrix([[0], [0]]) self.direction[0][0] = -direction[0][0] self.direction[1][0] = -direction[1][0] else: self.start = start self.direction = direction self.color = color
def driveDirections(self, direction): logger.debug("Got drive Direction: %s" % (direction)) self.sendBusy() v = Matrix._makeMatrix([[1], [0]]) if direction == "N": v = Matrix._makeMatrix([[1], [0]]) elif direction == "E": v = Matrix._makeMatrix([[0], [-1]]) elif direction == "S": v = Matrix._makeMatrix([[-1], [0]]) elif direction == "W": v = Matrix._makeMatrix([[0], [1]]) if self.moveLocal(v): logger.info("moving %s" % (direction)) timer = threading.Timer(Robot.MOVING_TIME_S, self.onDriveDirections) timer.start() else: logger.warn("moving failed") self.sendError("Not a valid direction: %s" % (direction))
if color: v.append(["N", color]) color = self.map.getColor( self.globalPosition, self.rotateLocalToGlobal(Matrix.fromList([[0], [-1]]))) if color: v.append(["E", color]) color = self.map.getColor( self.globalPosition, self.rotateLocalToGlobal(Matrix.fromList([[-1], [0]]))) if color: v.append(["S", color]) color = self.map.getColor( self.globalPosition, self.rotateLocalToGlobal(Matrix.fromList([[0], [1]]))) if color: v.append(["W", color]) return v if __name__ == "__main__": rotLocToGlob = Matrix._makeMatrix([[0, 1], [-1, 0]]) rotGlobToLoc = Matrix._makeMatrix([[0, -1], [1, 0]]) r = Robot(None, Matrix._makeMatrix([[0], [0]]), rotLocToGlob, rotGlobToLoc) m = Matrix.fromList([[1], [0]]) print(r.rotateLocalToGlobal(m)) #print(r.makeAvailableDirectionsJson([["N","B"],["E","B"]])) #print(r.makeAvailableDirectionsJson([]))
return "" else: return self.connections[index].color def isConnection(self, fromGlobal, directionGlobal): c = Connection(fromGlobal, directionGlobal) if c in self.connections: return True return False if __name__ == "__main__": map = Map() map.loadMap("default.map") map.draw() r = map.getRobots()[0] #print(r.globalPosition) #print(r.scan()) vlocal = Matrix._makeMatrix([[-1], [0]]) r.moveLocal(vlocal) #print(r.globalPosition) #print(r.scan()) r.moveLocal(vlocal) #print(r.globalPosition) #print(r.scan())