Python: Getting Coordinates from a micromouse

Viewed 56

im trying to make a micromouse route tracer in python so eventually i can run A* on it and find the most efficient route for the mouse to take

the on board microcontroller will take measurements of distance and turns in the following format:

FD - 1 movement forward (length of mouse)
R90 - Turn right 90 degrees
L90 - Turn left 90 degrees

I currently have this stored as a list:

list = ["FD","R90","FD", ...]

So far I have it converting all FD to 10 and L90 to -90 and R90 to +90

I am trying to get this converted into coordinates to display them with pygame

(I know its possible with turtle but i still need the coords as will be using them as nodes for A* search)

Program so far:

#imports pygame, used to trace the path of mouse
import pygame


##LIST RECIEVED FROM CONTROLLER
list = ["FD","FD","R90","FD","FD","FD","FD","FD","FD","L90","FD","FD","FD"]

#defines window width and baclground colour
(width, height) = (300, 300)
background_colour = (255,255,255)

#initalizes screen
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Micromouse Tracer')
screen.fill(background_colour)


#draws the line
def draw(start, end):
    pygame.draw.line(screen, (0,0,0), start, end, 5)

    
# converts list from controller into steps
def convert(list):
    calcdlist = []
    i = 0
    while i < len(list):
        if list[i] == "FD":
            calcdlist.append(10)
        elif list[i] == "R90":
            calcdlist.append(-90)
        elif list[i] == "L90":
            calcdlist.append(90)
        i += 1
    return calcdlist

    
#makes path from converted data
def makepath(list):
    pathlist = convert(list) #converts list into path
    x, y = (150,150)
    path = [(x,y)]
    current = "y"
    degree = 0


    i = 0

    while i < len(pathlist):

        if pathlist[i] == 10:

            if current == "y" and degree >= 90:
                y += 10
                path.append((x,y))

            elif current == "x" and degree >= 90:
                x += 10
                path.append((x,y))

            elif current == "y" and degree <= 90:
                y -= 10
                path.append((x,y))

            elif current == "x" and degree <= 90:
                x -= 10
                path.append((x,y))

        else:

            degree += pathlist[i]


        i += 1
    return path


j = makepath(list)
i = 0
print(j)
while i < len(j):
    if i == 0:
        draw((150,150),(150,150))
    else:
        draw((j[i-1][0],j[i-1][1]),(j[i][0],j[i][1]))
    i += 1

#flips display for correct orientation
pygame.display.flip()


#allows the user to press red x to exit program
running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False
1 Answers

Solution:

as user3386109 stated, the solution lay in having a list of N, S, E and W

then if a L was found in the list it +1 to the index to select the correct direction and then i just had to look if the letter was N S E or W when making the path.

Sorry ive done a poor job of explaining it, my working code is below for anyone else who has this issue.

#imports pygame, used to trace the path of mouse
import pygame

#defines window width and baclground colour
(width, height) = (300, 300)
background_colour = (255,255,255)

#initalizes screen
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Micromouse Tracer')
screen.fill(background_colour)


##LIST RECIEVED FROM ESP
list = ["FD","FD","R","FD","FD","FD","FD","FD","FD","L","FD","FD","FD"]

def convert(list):
    direction = ["N","E","s","W"]
    directionlistpos = 0

    x, y = (150, 150)
    startpos = (x, y)
    pathlist = []
    i = 0

    while i < len(list):

        if list[i] == "FD":
            pathlist.append(direction[directionlistpos])

        else:
            if list[i] == "L":
                directionlistpos += 1
            else:
                directionlistpos -= 1

        i += 1
    return pathlist



def makepath(list):
    pathlist = convert(list)


    startpos = (150, 150)
    x, y = startpos
    finallist = [(startpos)]

    i = 0

    while i < len(pathlist):

        if pathlist[i] == "N":
            y -= 10
            finallist.append((x,y))

        elif pathlist[i] == "E":
            x -= 10
            finallist.append((x,y))

        elif pathlist[i] == "S":
            y += 10
            finallist.append((x,y))

        else:
            x += 10
            finallist.append((x,y))

        i += 1
    return finallist


def draw(start, end):
    pygame.draw.line(screen, (0,0,0), start, end, 5)


def run(list):
    j = makepath(list)
    i = 0

    while i < len(j):
        if i == 0:
            draw((150,150),(150,150))
        else:
            draw((j[i-1][0],j[i-1][1]),(j[i][0],j[i][1]))
        i += 1


run(list)


#flips display for correct orientation
pygame.display.flip()
#allows the user to press red x to exit program
running = True
while running:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      running = False
Related