-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.py
54 lines (44 loc) · 1.41 KB
/
sensor.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
import numpy as np
def collision_check(x0, y0, x1, y1, ground_truth, robot_belief):
x0 = x0.round()
y0 = y0.round()
x1 = x1.round()
y1 = y1.round()
dx, dy = abs(x1 - x0), abs(y1 - y0)
x, y = x0, y0
error = dx - dy
x_inc = 1 if x1 > x0 else -1
y_inc = 1 if y1 > y0 else -1
dx *= 2
dy *= 2
collision_flag = 0
max_collision = 10
while 0 <= x < ground_truth.shape[1] and 0 <= y < ground_truth.shape[0]:
k = ground_truth.item(y, x)
if k == 1 and collision_flag < max_collision:
collision_flag += 1
if collision_flag >= max_collision:
break
if k != 1 and collision_flag > 0:
break
if x == x1 and y == y1:
break
robot_belief.itemset((y, x), k)
if error > 0:
x += x_inc
error -= dy
else:
y += y_inc
error += dx
return robot_belief
def sensor_work(robot_position, sensor_range, robot_belief, ground_truth):
sensor_angle_inc = 0.5 / 180 * np.pi
sensor_angle = 0
x0 = robot_position[0]
y0 = robot_position[1]
while sensor_angle < 2 * np.pi:
x1 = x0 + np.cos(sensor_angle) * sensor_range
y1 = y0 + np.sin(sensor_angle) * sensor_range
robot_belief = collision_check(x0, y0, x1, y1, ground_truth, robot_belief)
sensor_angle += sensor_angle_inc
return robot_belief