-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsim.py
More file actions
143 lines (107 loc) · 3.75 KB
/
sim.py
File metadata and controls
143 lines (107 loc) · 3.75 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
139
140
141
142
143
import pygame
import numpy as np
import random
import vis
import sys
from controller import Controller
from de2bot import DE2Bot, DE2Config
def meas_prob(x, o, meas_angle):
ddir = o - x[:2, :]
dist = np.linalg.norm(ddir)
ddir = ddir / dist
abs_angle = x[2, 0] + meas_angle
adir = np.asmatrix([[np.cos(abs_angle), np.sin(abs_angle)]]).T
cos_th = ddir.T.dot(adir)
cos = np.arccos(cos_th)
return np.exp(-35 * dist * cos[0, 0] * cos[0, 0])
# deg = 30
# rad = np.radians(deg)
# return 1 if np.abs(cos) < rad else 0
def sensor_model(x, obstacles, meas_angle):
by_prox = sorted(obstacles, key=lambda o: np.linalg.norm(x[:2, :] - o))
for obs in by_prox:
prob = meas_prob(x, obs, meas_angle)
if random.random() < prob:
return np.linalg.norm(obs - x[:2, 0])
return None
def wait():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_f:
return
def main():
clock = pygame.time.Clock()
visualizer = vis.Visualizer()
fps = 60
dt = 1.0 / fps
t = 0.0
i = 0
robot = DE2Bot()
dead_reckon = DE2Bot()
# obstacles = [
# np.asmatrix([[0.5, 0]]).T,
# np.asmatrix([[1, 1]]).T,
# np.asmatrix([[0.5, 1.5]]).T,
# np.asmatrix([[-0.5, 0]]).T,
# ]
obstacles = [
np.asmatrix([[-1.5, -0.5]]).T,
np.asmatrix([[1.5, -0.5]]).T,
]
angles_deg = [-144, -90, -44, -12, 12, 44, 90, 144]
angles_rad = [np.deg2rad(a) for a in angles_deg]
controller = Controller(robot.state.pose, angles_rad, DE2Config(), obstacles)
next_sensor = None
framerate = 60.
# sensor_update_time = 1. / 16.
sensor_update_time = 1. / 30.
last_sensor_update = pygame.time.get_ticks()
hits = []
while not visualizer.close:
visualizer.update_events()
pos = robot.state.pose
# for a in angles_rad:
# dist = sensor_model(pos, obstacles, a)
# if dist is not None:
# angle = a + pos[2, 0]
# hits.append((pos[0, 0] + dist * np.cos(angle), pos[1, 0] + dist * np.sin(angle)))
sense = None
if next_sensor:
a = angles_rad[next_sensor]
sense = sensor_model(pos, obstacles, a)
for b in angles_rad:
if b != a:
angle = b + pos[2, 0]
hits.append((pos[0, 0] + 0.1 * np.cos(angle), pos[1, 0] + 0.1 * np.sin(angle)))
if sense is not None:
angle = a + pos[2, 0]
hits.append((pos[0, 0] + sense * np.cos(angle), pos[1, 0] + sense * np.sin(angle)))
can_sense = pygame.time.get_ticks() > (last_sensor_update + sensor_update_time * 1000)
if can_sense:
hits = []
encoder_noise = 0.10 * np.asmatrix(np.random.normal(size=(2, 1)))
dead_reckon.apply(robot.left_right() + encoder_noise, dt)
controls, next_sensor, R = controller.update(
robot.left_right() + encoder_noise,
sense,
can_sense,
1. / framerate
)
controls_noise = 0.5 * np.asmatrix(np.random.normal(size=(2, 1))) * np.linalg.norm(controls)
robot.apply(controls, dt)
if can_sense and next_sensor:
last_sensor_update = pygame.time.get_ticks()
visualizer.draw(
robot.state.pose,
dead_reckon.state.pose,
controller.estimated_position(),
R,
hits,
[(mat[0, 0], mat[1, 0]) for mat in obstacles])
# wait()
clock.tick(framerate)
if __name__ == '__main__':
main()