-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsensors.py
More file actions
57 lines (50 loc) · 2.15 KB
/
sensors.py
File metadata and controls
57 lines (50 loc) · 2.15 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
import pygame
import math
import numpy as np
def uncertainty_add(distance, angle, sigma):
mean = np.array([distance, angle])
covariance = np.diag(sigma ** 2)
distance, angle = np.random.multivariate_normal(mean, covariance)
distance = max(distance, 0)
angle = max(angle, 0)
return [distance, angle]
class LaserSensor:
def __init__(self, Range, map, uncertainty):
self.Range = Range
self.speed = 4 # rounds pr second
self.sigma = np.array([uncertainty[0], uncertainty[1]])
self.position = (0, 0)
self.map = map
self.W, self.H = pygame.display.get_surface().get_size()
self.sensedObstacles = []
def distance(self, obstaclePosition):
px = (obstaclePosition[0] - self.position[0]) ** 2
py = (obstaclePosition[1] - self.position[1]) ** 2
return math.sqrt(px + py)
def sense_obstacle(self):
data = []
x1, y1 = self.position[0], self.position[1]
for angle in np.linspace(0, 2 * math.pi, 60, False):
x2, y2 = (x1 + self.Range * math.cos(angle)), (y1 - self.Range * math.sin(angle))
for i in range(0, 100):
u = i / 100
x = int(x2 * u + x1 * (1 - u))
y = int(y2 * u + y1 * (1 - u))
if 0 < x < self.W and 0 < y < self.H:
color = self.map.get_at((x, y))
if (color[0], color[1], color[2]) == (0, 0, 0):
distance = self.distance((x, y))
output = uncertainty_add(distance, angle, self.sigma)
output = [distance, angle]
output.append(self.position[0]) # append to output
output.append(self.position[1]) # append to output
# print("Output", output)
# storing these laser and position measurements in our data
data.append(output)
print("Data: ", data)
break
# checking if the LIDAR recorded anything
if len(data) > 0:
return data
else:
return False