-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnavigator.py
147 lines (131 loc) · 5.19 KB
/
navigator.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
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
144
145
146
147
from copy import copy
import numpy as np
from matplotlib import pyplot as plt
from logger import get_logger
from navigation.data.raw.sample import FRAMES
logger = get_logger(__name__)
def get_robo_position(frame, robo_obj_name="robot vacuum"):
# Get a list of all vaccum robot objects
robo_objs = []
for obj in frame:
if obj["label"] == robo_obj_name:
robo_objs.append(obj)
# Calculate the average brightness of each object
for obj in robo_objs:
obj["brightness"] = np.mean(obj["average_color"])
# Get the brightest object
if len(robo_objs) == 0:
return None
robo_obj = max(robo_objs, key=lambda x: x["brightness"])
return robo_obj
class Navigator:
def __init__(self):
self.last_robo_position = None
self.pose_history = []
logger.info("Navigator initialized")
def next_frame(self, current_frame):
self.pose_history.append(
[self.last_robo_position, self.get_current_direction(current_frame)]
)
self.last_robo_position = copy(get_robo_position(current_frame))
logger.info(f"Last robot position:\n{self.last_robo_position}")
def get_distance_target(self, current_frame, target_position):
robo_position = get_robo_position(current_frame)
if robo_position is None:
return None
return np.linalg.norm(
np.array(robo_position["centroid"]) - np.array(target_position["centroid"])
)
def get_current_direction(self, current_frame):
robo_position = get_robo_position(current_frame)
if robo_position is None or self.last_robo_position is None:
return None
direction = np.array(robo_position["centroid"]) - np.array(
self.last_robo_position["centroid"]
)
direction_deg = np.degrees(np.arctan2(direction[1], direction[0]))
return direction_deg
def get_distance_angular_difference(self, current_frame, target_position):
robo_position = get_robo_position(current_frame)
if robo_position is None or self.last_robo_position is None:
return None
# Calculate angular difference
target_vector = np.array(target_position["centroid"]) - np.array(
robo_position["centroid"]
)
robo_vector = np.array(robo_position["centroid"]) - np.array(
self.last_robo_position["centroid"]
)
difference = np.arccos(
np.dot(target_vector, robo_vector)
/ (np.linalg.norm(target_vector) * np.linalg.norm(robo_vector))
)
cross_product = np.cross(target_vector, robo_vector)
if cross_product < 0:
difference = -difference
difference_deg = np.degrees(difference)
if np.isnan(difference_deg):
return 0
return difference_deg
def plot_target_approach(self, current_frame, target_position):
"""Plot the current position of the robot, the direction vector, the current direction of the robot, and the target position, using matplotlib"""
if self.last_robo_position is None:
return
robo_position = get_robo_position(current_frame)
if robo_position is None:
return
target_vector = np.array(target_position["centroid"]) - np.array(
robo_position["centroid"]
)
plt.quiver(
*robo_position["centroid"],
*target_vector,
angles="xy",
scale_units="xy",
scale=1,
)
# Current direction of the robot
direction = self.get_current_direction(current_frame)
if direction is not None:
vector_length = 100
vector = (
np.array([np.cos(np.radians(direction)), np.sin(np.radians(direction))])
* vector_length
)
plt.quiver(
*robo_position["centroid"],
*vector,
angles="xy",
scale_units="xy",
scale=1,
color="green",
label="Current direction",
)
plt.scatter(
*self.last_robo_position["centroid"],
color="lightblue",
label="Last robot position",
)
plt.scatter(
*robo_position["centroid"], color="blue", label="Current robot position"
)
plt.scatter(*target_position["centroid"], color="red", label="Target position")
plt.legend()
if __name__ == "__main__":
data = FRAMES
target_position = {"centroid": (111.5, 187.0)}
navigator = Navigator()
for frame in data:
distance = navigator.get_distance_target(frame, target_position)
direction = navigator.get_current_direction(frame)
angular_difference = navigator.get_distance_angular_difference(
frame, target_position
)
if distance is not None and angular_difference is not None:
print(
f"Direction: {direction:.2f}, Distance to target: {distance:.2f}, Angular difference in deg: {angular_difference:.2f}"
)
navigator.plot_target_approach(frame, target_position)
plt.equal()
plt.show()
navigator.next_frame(frame)