-
Notifications
You must be signed in to change notification settings - Fork 8
/
simmer.py
148 lines (119 loc) · 4.42 KB
/
simmer.py
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
136
137
138
139
140
141
142
143
144
145
146
147
148
'''
This is the main file of SimMeR.
'''
# This file is part of SimMeR, an educational mechatronics robotics simulator.
# Initial development funded by the University of Toronto MIE Department.
# Copyright (C) 2023 Ian G. Bennett
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# Imports
import numpy as np
import pygame
from maze import Maze
from robot import Robot
from block import Block
from interface.hud import Hud
from interface.communication import TCPServer
import config as CONFIG
import utilities
### Initialization
print('SimMeR Loading...')
# Set random error seed
if not CONFIG.rand_error:
np.random.seed(CONFIG.floor_seed)
# Load maze walls and floor pattern
MAZE = Maze()
MAZE.import_walls()
MAZE.generate_floor()
CANVAS_WIDTH = MAZE.size_x * CONFIG.ppi + CONFIG.border_pixels * 2
CANVAS_HEIGHT = MAZE.size_y * CONFIG.ppi + CONFIG.border_pixels * 2
# Load robot
ROBOT = Robot()
# List of sensors to simulate every frame (for testing only)
if hasattr(CONFIG, 'simulate_list'):
SIMULATE_LIST = CONFIG.simulate_list
else:
SIMULATE_LIST = []
# Create the block
BLOCK = Block()
# Create a copy of the environment objects to pass to simulation functions
environment = {'BLOCK': BLOCK, 'MAZE': MAZE, 'ROBOT': ROBOT}
# Load the Heads Up Display
HUD = Hud()
# Load TCP Communication
COMM = TCPServer()
COMM.start()
# Initialize graphics
pygame.init()
canvas = pygame.display.set_mode([CANVAS_WIDTH, CANVAS_HEIGHT])
### Main Loop ###
RUNNING = True
try:
while RUNNING:
##########################
##### USER INTERFACE #####
##########################
# Check for keyboard input
game_events = pygame.event.get()
RUNNING = HUD.check_input(game_events)
keypress = pygame.key.get_pressed()
# Get the command information from the tcp buffer
cmds = COMM.get_buffer_rx()
################################################
##### ROBOT AND DEVICE UPDATES AND ACTIONS #####
################################################
# Act on commands and respond
if cmds:
responses = ROBOT.command(cmds, environment)
COMM.set_buffer_tx(responses)
# Move the robot, either from keypress commands or from the movement buffers
if True in keypress:
ROBOT.move_manual(keypress, [*BLOCK.block_square, *MAZE.reduced_walls])
else:
ROBOT.move_from_command([*BLOCK.block_square, *MAZE.reduced_walls])
# Recalculate global positions of the robot and its devices
ROBOT.update_outline()
ROBOT.update_device_positions()
# Manually simulate a specific sensor or sensors
utilities.simulate_sensors(environment, SIMULATE_LIST)
# Update the sensors that need to be updated every frame
for sensor in ROBOT.sensors.values():
if callable(getattr(sensor, "update", None)):
sensor.update(environment)
###########################################
##### DRAW RELEVANT OBJECTS ON CANVAS #####
###########################################
# Fill the background with the background color
canvas.fill(CONFIG.background_color)
# Draw the maze checkerboard pattern
MAZE.draw_floor(canvas)
# Draw the maze walls
MAZE.draw_walls(canvas)
# Draw the block
BLOCK.draw(canvas)
# Draw the robot onto the maze
ROBOT.draw(canvas)
ROBOT.draw_devices(canvas)
# Update the various HUD elements
HUD.draw_frame_indicator(canvas)
HUD.draw_keys(canvas, keypress)
# Limit the framerate
HUD.clock.tick(CONFIG.frame_rate)
# Flip the display (update the canvas)
pygame.display.flip()
except KeyboardInterrupt:
pass
# Done! Time to quit.
print('Execution finished. Closing SimMeR.')
pygame.quit()