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