-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
138 lines (114 loc) · 4.57 KB
/
main.py
File metadata and controls
138 lines (114 loc) · 4.57 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
136
137
138
import pygame
import math
import numpy as np
import time
from src import map, raycast, mcl, lidar
#TODO upload code to github
pygame.init()
i = 0
j = 0
init_time = time.time()
PLAYER = 1
TEST_PARTICLE = 2
SCREEN_HEIGHT = 720
SCREEN_WIDTH = 1280
SCREEN_COLOR = 'black'
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
clock = pygame.time.Clock()
running = True
dt = 0
field = map.load_map(screen, 'maps/map3.jpeg')
particles = mcl.Particles(1000, field, screen)
player_pos_x = (field.starting_rect[0] + field.starting_rect[1]) / 2
player_pos_y = (field.starting_rect[2] + field.starting_rect[3]) / 2
player_pos = pygame.Vector2(player_pos_x, player_pos_y)
test_pos = pygame.Vector2(player_pos_x, player_pos_x)
player_angle = math.radians(-90)
test_angle = math.radians(-90)
lidar = lidar.Lidar(np.array([player_pos_x, player_pos_y, player_angle]), field, 12, 4)
player_radius = 20
X_t_minus_1 = particles.state_estimation
X_t = np.copy(X_t_minus_1)
player = PLAYER
while running:
keys = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT or keys[pygame.K_q]:
running = False
if keys[pygame.K_1]:
player = PLAYER
if keys[pygame.K_2]:
player = TEST_PARTICLE
screen.fill(SCREEN_COLOR)
if player == PLAYER:
# handle keyboard input
if keys[pygame.K_w]:
player_pos.y += 200 * dt * math.sin(player_angle)
player_pos.x += 200 * dt * math.cos(player_angle)
X_t[1] += 200 * dt * math.sin(X_t[2])
X_t[0] += 200 * dt * math.cos(X_t[2])
if keys[pygame.K_s]:
player_pos.y -= 200 * dt * math.sin(player_angle)
player_pos.x -= 200 * dt * math.cos(player_angle)
X_t[1] -= 200 * dt * math.sin(X_t[2])
X_t[0] -= 200 * dt * math.cos(X_t[2])
if keys[pygame.K_a]:
player_pos.x += 200 * dt * math.sin(player_angle)
player_pos.y -= 200 * dt * math.cos(player_angle)
X_t[0] += 200 * dt * math.sin(X_t[2])
X_t[1] -= 200 * dt * math.cos(X_t[2])
if keys[pygame.K_d]:
player_pos.x -= 200 * dt * math.sin(player_angle)
player_pos.y += 200 * dt * math.cos(player_angle)
X_t[0] -= 200 * dt * math.sin(X_t[2])
X_t[1] += 200 * dt * math.cos(X_t[2])
if keys[pygame.K_j]:
player_angle -= (math.pi) * 1.2 * dt
X_t[2] -= (math.pi) * 1.2 * dt
if keys[pygame.K_k]:
player_angle += (math.pi) * 1.2 * dt
X_t[2] += (math.pi) * 1.2 * dt
else: # player is test particle
if keys[pygame.K_w]:
test_pos.y += 200 * dt * math.sin(player_angle)
test_pos.x += 200 * dt * math.cos(player_angle)
if keys[pygame.K_s]:
test_pos.y -= 200 * dt * math.sin(player_angle)
test_pos.x -= 200 * dt * math.cos(player_angle)
if keys[pygame.K_a]:
test_pos.x += 200 * dt * math.sin(player_angle)
test_pos.y -= 200 * dt * math.cos(player_angle)
if keys[pygame.K_d]:
test_pos.x -= 200 * dt * math.sin(player_angle)
test_pos.y += 200 * dt * math.cos(player_angle)
if keys[pygame.K_j]:
test_angle -= (math.pi) * 1.2 * dt
if keys[pygame.K_k]:
test_angle += (math.pi) * 1.2 * dt
pygame.draw.circle(screen, "red", player_pos, player_radius)
# draw player's orientation
player_head_x = player_pos.x + player_radius * math.cos(player_angle)
player_head_y = player_pos.y + player_radius * math.sin(player_angle)
pygame.draw.line(screen, "purple", player_pos, pygame.Vector2(player_head_x, player_head_y), 5)
# rays = raycast.compute_rays(5, player_pos, player_angle, map)
# raycast.draw_rays(screen, "yellow", player_pos, rays, 3)
map.draw_map(screen, "blue", field, 3)
control = np.stack((X_t_minus_1, X_t))
lidar.state = np.array([player_pos.x, player_pos.y, player_angle])
measurement = lidar.measurements
particles.update_particles(control, measurement, screen, field)
lidar.draw_measurements(screen, "yellow", 2)
particles.draw_particles(screen, "red", 10)
particles.draw_state_estimation(screen, "green")
X_t_minus_1 = np.copy(X_t)
pygame.display.flip()
dt = clock.tick(60) / 1000
d_time = time.time() - init_time
j += 1
d_i = j - i
if d_time >= 1:
init_time = time.time()
i = j
refresh_rate = d_i / d_time
print(f'Refresh_rate = {refresh_rate} Hz')
pygame.quit()