-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathtest_search_pattern.py
219 lines (194 loc) · 7.48 KB
/
test_search_pattern.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""
Tests the SearchPattern class
"""
from typing import Generator
import pytest
from modules import decision_command
from modules import odometry_and_time
from modules.common.modules import orientation
from modules.common.modules import position_local
from modules.common.modules.mavlink import drone_odometry_local
from modules.decision import search_pattern
DISTANCE_SQUARED_THRESHOLD = 2.0
SMALL_ADJUSTMENT = 1
# Test functions use test fixture signature names and access class privates
# pylint: disable=redefined-outer-name
@pytest.fixture()
def search_pattern_width_greater_depth() -> Generator[search_pattern.SearchPattern, None, None]:
"""
Initializes a SearchPattern instance.
"""
search_pattern_instance = search_pattern.SearchPattern(
camera_fov_forwards=30.0,
camera_fov_sideways=60.0,
search_height=100.0,
search_overlap=0.2,
current_position_x=100.0,
current_position_y=50.0,
distance_squared_threshold=DISTANCE_SQUARED_THRESHOLD,
small_adjustment=SMALL_ADJUSTMENT,
)
yield search_pattern_instance
@pytest.fixture()
def search_pattern_depth_greater_width() -> Generator[search_pattern.SearchPattern, None, None]:
"""
Initializes a SearchPattern instance.
"""
search_pattern_instance = search_pattern.SearchPattern(
camera_fov_forwards=60.0,
camera_fov_sideways=30.0,
search_height=50.0,
search_overlap=0.2,
current_position_x=75.0,
current_position_y=150.0,
distance_squared_threshold=DISTANCE_SQUARED_THRESHOLD,
small_adjustment=SMALL_ADJUSTMENT,
)
yield search_pattern_instance
@pytest.fixture()
def drone_odometry() -> Generator[odometry_and_time.OdometryAndTime, None, None]:
"""
Pytest Fixture for coordinates of drone
"""
def create_drone_odometry(
pos_x: float, pos_y: float, depth: float
) -> odometry_and_time.OdometryAndTime:
"""
Creates an OdometryAndTime instance representing the drone's current position.
"""
drone_position = position_local.PositionLocal.create(pos_y, pos_x, -depth)[1]
drone_orientation = orientation.Orientation.create(0.0, 0.0, 0.0)[1]
odometry_data = drone_odometry_local.DroneOdometryLocal.create(
drone_position, drone_orientation
)[1]
odometry = odometry_and_time.OdometryAndTime.create(odometry_data)[1]
return odometry
return create_drone_odometry
def assert_move_command(
move: decision_command.DecisionCommand,
expected_new_pos: bool,
expected_target_x: float,
expected_target_y: float,
expected_target_z: float,
) -> None:
"""
Compares the passed in move command with the expected values for this command
"""
expected_command_type = decision_command.DecisionCommand.CommandType.MOVE_TO_ABSOLUTE_POSITION
actual_new_pos = move[0]
actual_target_pos = move[1].get_command_position()
actual_targetx = actual_target_pos[0]
actual_targety = actual_target_pos[1]
actual_targetz = actual_target_pos[2]
actual_command_type = move[1].get_command_type()
assert actual_new_pos == expected_new_pos
assert actual_targetx == pytest.approx(expected_target_x, 0.1)
assert actual_targety == pytest.approx(expected_target_y, 0.1)
assert actual_targetz == pytest.approx(expected_target_z, 0.1)
assert actual_command_type == expected_command_type
class TestSearchPattern:
"""
Tests for the SearchPattern class methods
"""
def test_width_greater_depth_normal(
self,
search_pattern_width_greater_depth: search_pattern.SearchPattern,
drone_odometry: odometry_and_time.OdometryAndTime,
) -> None:
"""
Test first 20 positions of search pattern where drone has reached point before being called
"""
expected_coordinates = [
[100, 50, True],
[123.75, 92.87, False],
[124.75, 92.87, True],
[142.87, 118.62, False],
[142.87, 117.62, True],
[142.87, 74.75, True],
[168.62, 7.13, False],
[167.62, 7.13, True],
[124.75, 7.13, True],
[57.13, -18.62, False],
[57.13, -17.62, True],
[57.13, 18.00, True],
[57.13, 53.62, True],
[57.13, 89.25, True],
[57.13, 124.87, True],
[31.38, 185.25, False],
[32.38, 185.25, True],
[68.00, 185.25, True],
[103.62, 185.25, True],
[139.25, 185.25, True],
[174.87, 185.25, True],
]
for i in range(len(expected_coordinates) - 1):
current_position = drone_odometry(
expected_coordinates[i][0], expected_coordinates[i][1], 100
)
move_command = search_pattern_width_greater_depth.continue_search(current_position)
assert_move_command(
move_command,
expected_coordinates[i + 1][2],
expected_coordinates[i + 1][0],
expected_coordinates[i + 1][1],
-100,
)
def test_depth_greater_width_normal(
self,
search_pattern_depth_greater_width: search_pattern.SearchPattern,
drone_odometry: odometry_and_time.OdometryAndTime,
) -> None:
"""
Test first 20 positions of search pattern where drone has reached point before being called
"""
expected_coordinates = [
[75, 150, True],
[74.00, 171.44, False],
[75.00, 171.44, True],
[96.44, 172.44, False],
[96.44, 171.44, True],
[96.44, 143.81, True],
[97.44, 128.56, False],
[96.44, 128.56, True],
[68.81, 128.56, True],
[53.56, 127.56, False],
[53.56, 128.56, True],
[53.56, 166.91, True],
[52.56, 192.87, False],
[53.56, 192.87, True],
[91.91, 192.87, True],
[117.87, 193.87, False],
[117.87, 192.87, True],
[117.87, 160.17, True],
[117.87, 127.46, True],
[118.87, 107.13, False],
[117.87, 107.13, True],
]
for i in range(len(expected_coordinates) - 1):
current_position = drone_odometry(
expected_coordinates[i][0], expected_coordinates[i][1], 50
)
move_command = search_pattern_depth_greater_width.continue_search(current_position)
assert_move_command(
move_command,
expected_coordinates[i + 1][2],
expected_coordinates[i + 1][0],
expected_coordinates[i + 1][1],
-50,
)
def test_return_to_pattern(
self,
search_pattern_width_greater_depth: search_pattern.SearchPattern,
drone_odometry: odometry_and_time.OdometryAndTime,
) -> None:
"""
Test behaviour when drone has not reached the next waypoint
"""
current_position = drone_odometry(100, 50, 100)
move_command = search_pattern_width_greater_depth.continue_search(current_position)
assert_move_command(move_command, False, 125.75, 92.87, -100)
# Pretend drone goes off to search
current_position = drone_odometry(200, 150, 100)
move_command = search_pattern_width_greater_depth.continue_search(current_position)
# Should still have same target location
assert_move_command(move_command, False, 125.75, 92.87, -100)