-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.py
More file actions
85 lines (70 loc) · 2.71 KB
/
robot.py
File metadata and controls
85 lines (70 loc) · 2.71 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
import math
import pygame
from config import *
from ray import *
from mcl import MCL
class Robot:
def __init__(self, x, y, w, h, theta, sensors):
self.x = x
self.y = y
self.w = w
self.h = h
self.theta = theta
self.v = 0.0
self.omega = 0.0
self.sensors = sensors
self.left_wheel_delta = 0.0
self.right_wheel_delta = 0.0
self.mcl = MCL()
def update(self, dt):
self.x += self.v * math.cos(self.theta) * dt
self.y += self.v * math.sin(self.theta) * dt
self.theta += self.omega * dt
self.update_gaussian(dt)
self.update_rays()
self.mcl.update(self, dt)
def update_rays(self):
for sensor in self.sensors:
sensor.update(self)
def update_gaussian(self, dt):
self.left_wheel_delta = (self.v - self.omega * self.h / 2) * dt
self.right_wheel_delta = (self.v + self.omega * self.h / 2) * dt
self.left_wheel_delta += random.gauss(0, WHEEL_NOISE_STD * abs(self.left_wheel_delta))
self.right_wheel_delta += random.gauss(0, WHEEL_NOISE_STD * abs(self.right_wheel_delta))
# ---------------- DRAWING FUNCTIONS ----------------
def corners(self):
hw, hh = self.w / 2, self.h / 2
local_corners = [(-hw, -hh), (hw, -hh), (hw, hh), (-hw, hh)]
c, s = math.cos(self.theta), math.sin(self.theta)
return [
(self.x + px * c - py * s, self.y + px * s + py * c)
for px, py in local_corners
]
def draw(self, screen, font):
pygame.draw.polygon(
screen, (50, 100, 220), [world_to_screen(x, y) for x, y in self.corners()]
)
x_text, y_text = 10, 10
for sensor in self.sensors:
pygame.draw.line(
screen,
(220, 50, 50),
world_to_screen(sensor.sx, sensor.sy),
world_to_screen(sensor.ex, sensor.ey),
2
)
text = font.render(
f"{sensor.name}: {sensor.d_noisy:.1f} in",
True,
(0, 0, 0)
)
screen.blit(text, (x_text, y_text))
y_text += 20
pygame.draw.circle(screen, (200, 0, 0), world_to_screen(self.x, self.y), 5)
pygame.draw.circle(screen, (0, 200, 0), world_to_screen(self.mcl.prediction[0], self.mcl.prediction[1]), 5)
self.mcl.draw_particles(screen)
text = font.render(f"Left Wheel Delta: {self.left_wheel_delta:.4f} in", True, (0, 0, 0))
screen.blit(text, (10, y_text))
y_text += 20
text = font.render(f"Right Wheel Delta: {self.right_wheel_delta:.4f} in", True, (0, 0, 0))
screen.blit(text, (10, y_text))