This repository was archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTestFlyController.py
More file actions
330 lines (238 loc) · 13.1 KB
/
TestFlyController.py
File metadata and controls
330 lines (238 loc) · 13.1 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# TestFlyController.py
# shotmanager
#
# Unit tests for the orbit controller.
#
# Created by Will Silva and Eric Liao on 1/19/2015.
# Modified by Nick Speal on 3/16/2016
# Copyright (c) 2016 3D Robotics.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import flyController
from flyController import *
from dronekit import LocationGlobalRelative
import unittest
import mock
from mock import call
from mock import Mock
from mock import MagicMock
from mock import patch
class TestSetOptions(unittest.TestCase):
def setUp(self):
origin = LocationGlobalRelative(37.873168,-122.302062, 0) # lat,lon,alt
self.startxOffset = 20 # meters
self.startyOffset = 15 # meters
self.startzOffset = 10 # meters
self.startHeading = 0 # deg
self.startLocation = location_helpers.addVectorToLocation(origin,Vector3(self.startxOffset, self.startyOffset, self.startzOffset))
#Run the controller constructor
self.controller = flyController.FlyController(origin, self.startxOffset, self.startyOffset, self.startzOffset, self.startHeading)
def testSetBothOptions(self):
'''Tests setting both options'''
self.controller.setOptions(maxClimbRate = 3, maxAlt = 75)
self.assertEqual(self.controller.maxClimbRate, 3)
self.assertEqual(self.controller.maxAlt, 75)
def testSetOptionsNoAltLimit(self):
'''Tests setting both options, with disabled altitude limit'''
self.controller.setOptions(maxClimbRate = 3, maxAlt = None)
self.assertEqual(self.controller.maxClimbRate, 3)
self.assertEqual(self.controller.maxAlt, None)
class TestMove(unittest.TestCase):
def setUp(self):
origin = LocationGlobalRelative(37.873168,-122.302062, 0) # lat,lon,alt
self.startxOffset = 20 # meters
self.startyOffset = 15 # meters
self.startzOffset = 130 # meters
self.startHeading = 0 # deg
self.startLocation = location_helpers.addVectorToLocation(origin,Vector3(self.startxOffset, self.startyOffset, self.startzOffset))
#Run the controller constructor
self.controller = flyController.FlyController(origin, self.startxOffset, self.startyOffset, self.startzOffset, self.startHeading)
self.controller._approach = Mock(return_value = Vector3())
self.controller._strafe = Mock(return_value = Vector3())
self.controller._climb = Mock(return_value = Vector3())
#Set options
self.controller.maxClimbRate = 5 # m/s
self.controller.maxAlt = 100 #m
#Arbirary sticks
throttle = 0.3
roll = -0.4
pitch = 0.5
yaw = -0.6
raw_paddle = 0.7
self.channels = [throttle, roll, pitch, yaw, 0.0, 0.0, 0.0, raw_paddle]
self.controller.move(self.channels, newHeading = 0, newOrigin = LocationGlobalRelative(37.873168,-122.302062, 20))
def testCallApproach(self):
'''Tests that _approach() is called'''
self.controller._approach.assert_called_with(-0.5)
def testCallStrafe(self):
'''Tests that _strafe() is called'''
self.controller._strafe.assert_called_with(-0.4)
def testCallClimb(self):
'''Tests that _climb() is called'''
self.controller._climb.assert_called_with(0.7)
def testAltitudeLimit(self):
'''Test that maximum altitude is enforced when climbing'''
self.assertEqual(self.controller.offset.z, self.controller.maxAlt)
def testNeworigin(self):
'''Tests that a new origin is set when passed via optional argument'''
self.assertEqual(self.controller.origin.alt, 20)
@mock.patch('location_helpers.addVectorToLocation')
def testCallAddVectorToLocation(self, location_helpers_addVectorToLocation):
'''Tests that location_helpers.addVectorToLocation() is called'''
self.controller.move(self.channels)
location_helpers_addVectorToLocation.assert_called_with(mock.ANY, mock.ANY)
class TestApproach(unittest.TestCase):
def setUp(self):
origin = LocationGlobalRelative(37.873168,-122.302062, 0) # lat,lon,alt
self.startxOffset = 20 # meters
self.startyOffset = 15 # meters
self.startzOffset = 10 # meters
self.startHeading = 0 # deg
self.startLocation = location_helpers.addVectorToLocation(origin,Vector3(self.startxOffset, self.startyOffset, self.startzOffset))
#Run the controller constructor
self.controller = flyController.FlyController(origin, self.startxOffset, self.startyOffset, self.startzOffset, self.startHeading)
# set options
self.controller.setOptions(maxClimbRate=1.5 ,maxAlt=45)
def testPitchForwards(self):
'''Stick/pitch forwards to move forwards'''
approachVel = self.controller._approach(1.0)
expectedXVel = math.cos(self.controller.heading) * self.controller.approachSpeed
expectedYVel = math.sin(self.controller.heading) * self.controller.approachSpeed
self.assertEqual(self.controller.approachSpeed, ACCEL_PER_TICK)
self.assertEqual(approachVel, Vector3(expectedXVel, expectedYVel, 0))
def testPitchBackwards(self):
'''Stick/pitch backwards to move backwards'''
approachVel = self.controller._approach(-1.0)
expectedXVel = math.cos(self.controller.heading) * self.controller.approachSpeed
expectedYVel = math.sin(self.controller.heading) * self.controller.approachSpeed
self.assertEqual(self.controller.approachSpeed, -ACCEL_PER_TICK)
self.assertEqual(approachVel, Vector3(expectedXVel, expectedYVel, 0))
def testApproachSpeedDoNotExceed(self):
'''Test that the incremented speed doesn't exceed approachSpeed'''
self.controller.approachSpeed = 3.99
self.controller._approach(0.5) #desired approach speed is 4 but an increment will put it over 4
self.assertEqual(self.controller.approachSpeed,4) #make sure we're limited to 4
def testApproachSpeedDoNotFallBelow(self):
'''Test that the incremented speed doesn't fall below approachSpeed'''
self.controller.approachSpeed = -3.99
self.controller._approach(-0.5) #desired approach speed is 4 but an increment will put it under 4
self.assertEqual(self.controller.approachSpeed,-4) #make sure we're limited to 4
class TestStrafe(unittest.TestCase):
def setUp(self):
origin = LocationGlobalRelative(37.873168,-122.302062, 0) # lat,lon,alt
self.startxOffset = 20 # meters
self.startyOffset = 15 # meters
self.startzOffset = 10 # meters
self.startHeading = 0 # deg
self.startLocation = location_helpers.addVectorToLocation(origin,Vector3(self.startxOffset, self.startyOffset, self.startzOffset))
#Run the controller constructor
self.controller = flyController.FlyController(origin, self.startxOffset, self.startyOffset, self.startzOffset, self.startHeading)
# set options
self.controller.setOptions(maxClimbRate=1.5 ,maxAlt=45)
def testStrafeRight(self):
'''Stick/roll right to move right'''
strafeVel = self.controller._strafe(1.0)
expectedXVel = math.cos(self.controller.heading + math.pi/2.) * self.controller.strafeSpeed
expectedYVel = math.sin(self.controller.heading + math.pi/2.) * self.controller.strafeSpeed
self.assertEqual(self.controller.strafeSpeed, ACCEL_PER_TICK)
self.assertEqual(strafeVel, Vector3(expectedXVel, expectedYVel, 0))
def testStrafeLeft(self):
'''Stick/roll left to move left'''
strafeVel = self.controller._strafe(-1.0)
expectedXVel = math.cos(self.controller.heading + math.pi/2.) * self.controller.strafeSpeed
expectedYVel = math.sin(self.controller.heading + math.pi/2.) * self.controller.strafeSpeed
self.assertEqual(self.controller.strafeSpeed, -ACCEL_PER_TICK)
self.assertEqual(strafeVel, Vector3(expectedXVel, expectedYVel, 0))
def testApproachSpeedDoNotExceed(self):
'''Test that the incremented speed doesn't exceed approachSpeed'''
self.controller.strafeSpeed = 3.99
self.controller._strafe(0.5) #desired approach speed is 4 but an increment will put it over 4
self.assertEqual(self.controller.strafeSpeed,4) #make sure we're limited to 4
def testApproachSpeedDoNotFallBelow(self):
'''Test that the incremented speed doesn't fall below approachSpeed'''
self.controller.strafeSpeed = -3.99
self.controller._strafe(-0.5) #desired approach speed is 4 but an increment will put it under 4
self.assertEqual(self.controller.strafeSpeed,-4) #make sure we're limited to 4
class TestClimb(unittest.TestCase):
def setUp(self):
origin = LocationGlobalRelative(37.873168,-122.302062, 0) # lat,lon,alt
self.startxOffset = 20 # meters
self.startyOffset = 15 # meters
self.startzOffset = 10 # meters
self.startHeading = 0 # deg
self.startLocation = location_helpers.addVectorToLocation(origin,Vector3(self.startxOffset, self.startyOffset, self.startzOffset))
#Run the controller constructor
self.controller = flyController.FlyController(origin, self.startxOffset, self.startyOffset, self.startzOffset, self.startHeading)
# set options
self.controller.setOptions(maxClimbRate=1.5 ,maxAlt=45)
def testClimbUp(self):
'''Test climbing up with stick up'''
climbVel = self.controller._climb(1.0)
self.assertEqual(climbVel, Vector3(0,0,1.0*self.controller.maxClimbRate))
def testClimbDown(self):
'''Test descending with stick down'''
climbVel = self.controller._climb(-1.0)
self.assertEqual(climbVel, Vector3(0,0,-1.0*self.controller.maxClimbRate))
class TestMaxApproachSpeed(unittest.TestCase):
def setUp(self):
origin = LocationGlobalRelative(37.873168,-122.302062, 0) # lat,lon,alt
self.startxOffset = 20 # meters
self.startyOffset = 15 # meters
self.startzOffset = 10 # meters
self.startHeading = 0 # deg
self.startLocation = location_helpers.addVectorToLocation(origin,Vector3(self.startxOffset, self.startyOffset, self.startzOffset))
#Run the controller constructor
self.controller = flyController.FlyController(origin, self.startxOffset, self.startyOffset, self.startzOffset, self.startHeading)
# set options
self.controller.setOptions(maxClimbRate=1.5 ,maxAlt=45)
def testZeroStrafeSpeed(self):
'''Test that approachSpeed gets all of MAX_SPEED if strafeSpeed is zero'''
strafeSpeed = 0.0
maxSpeed = self.controller._maxApproachSpeed(strafeSpeed)
self.assertEqual(maxSpeed, MAX_SPEED)
def testHalfStrafeSpeed(self):
'''Test that approachSpeed is equal to strafeSpeed if strafeSpeed is sqrt(MAX_SPEED*4)'''
strafeSpeed = math.sqrt(MAX_SPEED*4)
maxSpeed = self.controller._maxApproachSpeed(strafeSpeed)
self.assertAlmostEqual(maxSpeed, strafeSpeed)
def testAllStrafeSpeed(self):
'''Test that approachSpeed is zero if strafeSpeed is MAX_SPEED'''
strafeSpeed = MAX_SPEED
maxSpeed = self.controller._maxApproachSpeed(strafeSpeed)
self.assertEqual(maxSpeed, 0.0)
class TestMaxStrafeSpeed(unittest.TestCase):
def setUp(self):
origin = LocationGlobalRelative(37.873168,-122.302062, 0) # lat,lon,alt
self.startxOffset = 20 # meters
self.startyOffset = 15 # meters
self.startzOffset = 10 # meters
self.startHeading = 0 # deg
self.startLocation = location_helpers.addVectorToLocation(origin,Vector3(self.startxOffset, self.startyOffset, self.startzOffset))
#Run the controller constructor
self.controller = flyController.FlyController(origin, self.startxOffset, self.startyOffset, self.startzOffset, self.startHeading)
# set options
self.controller.setOptions(maxClimbRate=1.5 ,maxAlt=45)
def testZeroApproachSpeed(self):
'''Test that strafeSpeed gets all of MAX_SPEED if approachSpeed is zero'''
approachSpeed = 0.0
maxSpeed = self.controller._maxStrafeSpeed(approachSpeed)
self.assertEqual(maxSpeed, MAX_SPEED)
def testHalfApproachSpeed(self):
'''Test that strafeSpeed is equal to approachSpeed if approachSpeed is sqrt(MAX_SPEED*4)'''
approachSpeed = math.sqrt(MAX_SPEED*4)
maxSpeed = self.controller._maxStrafeSpeed(approachSpeed)
self.assertAlmostEqual(maxSpeed, approachSpeed)
def testAllApproachSpeed(self):
'''Test that strafeSpeed is zero if approachSpeed is MAX_SPEED'''
approachSpeed = MAX_SPEED
maxSpeed = self.controller._maxStrafeSpeed(approachSpeed)
self.assertEqual(maxSpeed, 0.0)