-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControl_Emulator.py
More file actions
135 lines (114 loc) · 4.18 KB
/
Control_Emulator.py
File metadata and controls
135 lines (114 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame
import sys
from random import choice,randint
from Components.grid import Grid,NORTH,EAST,WEST,SOUTH
from Components.robot import DIRECTION_MAP,FORWARD,BACKWARD,TURN_BACK,TURN_LEFT,TURN_RIGHT,OPPOSITE_MAP,LEFT_MAP, RIGHT_MAP,Robot
from Graphic_Engine.Graphic_Engine import Graphic_Engine,calculate_origin
from MazeVis.main import get_grid
class QueryEnv:
def __init__(self, grid: Grid, position, orientation):
'''
grid->the envrionment the robot will be navigating in
position -> true position of the robot relative to the enviroment origin
orientatrion->true orientation of the robot relative to north of the grid
'''
self.env = grid
self.i = position[0]
self.j = position[1]
self.orientation = orientation
def get_dimensions(self):
return self.env.get_height()
def _update_delta(self, move_direction):
is_wall = self.env.get_bit(self.i, self.j, move_direction)
if(not is_wall):
di, dj = DIRECTION_MAP[move_direction]
self.i, self.j = self.i+di, self.j+dj
return
raise Exception("Robot has requested an illegal move")
def update(self, move):
if(move == FORWARD):
self._update_delta(self.orientation)
if(move == BACKWARD):
self._update_delta(OPPOSITE_MAP[self.orientation])
if(move == TURN_BACK):
self.orientation = OPPOSITE_MAP[self.orientation]
if(move == TURN_LEFT):
self.orientation = LEFT_MAP[self.orientation]
if(move == TURN_RIGHT):
self.orientation = RIGHT_MAP[self.orientation]
def query(self):
'''
returns the state of the walls in the following order
left ,front, right
'''
front = self.env.get_bit(self.i, self.j, self.orientation)
right = self.env.get_bit(self.i, self.j, RIGHT_MAP[self.orientation])
left = self.env.get_bit(self.i, self.j, LEFT_MAP[self.orientation])
return left, front, right
def query_dimensions(self):
'''
return the dimensions of the maze the robot is currently in
'''
return self.env.get_height()
def visualize(que:QueryEnv):
BACKGROUND_COLOR = (255,255,255)
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT =1000,600
screen = pygame.display.set_mode(SCREEN_SIZE)
origin = calculate_origin(screen,que.env.get_wdith(),que.env.get_height())
gfx = Graphic_Engine(screen,origin)
Running = True
while(Running):
for event in pygame.event.get():
if event.type == pygame.QUIT:
Running =False
screen.fill(BACKGROUND_COLOR)
gfx.draw_grid(que.env)
robot = Robot(que)
robot.i = que.i
robot.j = que.j
robot.orientation = que.orientation
gfx.draw_robot(robot)
pygame.display.flip()
pygame.quit()
command_map = {
"FORWARD":FORWARD,
"BACKWARD":BACKWARD,
"TURN_BACK":TURN_BACK,
"TURN_RIGHT":TURN_RIGHT,
"TURN_LEFT":TURN_LEFT
}
orientation_string_map = {
NORTH : "NORTH",
SOUTH : "SOUTH",
EAST : "EAST",
WEST : "WEST"
}
f = lambda x : 1 if x>0 else 0
def main():
grid = get_grid(sys.argv[1])
position = (choice((0,13)),choice((0,13)))
orientation = choice((EAST,NORTH,SOUTH,WEST))
position = (13,13)
orientation = WEST
que = QueryEnv(Grid(grid),position,orientation)
while True:
visualize(que)
command = input()
if(command == "?"):
print(*(f(i) for i in que.query()))
with open(sys.argv[2],"a") as fil:
fil.write(" ".join(str(f(i)) for i in que.query())+'\n')
elif(command == "end"):
with open(sys.argv[2],"a") as fil:
fil.write(f"{[que.i,que.j]} with initial position {position} and orietntation {orientation_string_map[orientation]} -- OK\n")
break
else:
try:
que.update(command_map[command])
except KeyError:
raise Exception(f"{position},{orientation}")
return 0
if __name__ == "__main__":
main()