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 pathTestFollow.py
More file actions
872 lines (670 loc) · 32.4 KB
/
TestFollow.py
File metadata and controls
872 lines (670 loc) · 32.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
# TestFollow.py
# shotmanager
#
# Unit tests for the follow smart shot.
#
# Created by Will Silva and Eric Liao on 1/19/2015.
# Mostly overhauled 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 follow
from follow import *
import shotManager
from shotManager import ShotManager
import unittest
import mock
from mock import call
from mock import Mock
from mock import MagicMock
from mock import patch
class TestFollowShotInit(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
vehicle.attitude.yaw = math.radians(30)
vehicle.mount_status[0] = -45
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
shotmgr.getParam.return_value = 0 # so mock doesn't do lazy binds
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
def testInit(self):
'''Test that the shot initialized properly'''
# vehicle object should be created (not None)
assert self.shot.vehicle is not None
# shotManager object should be created (not None)
assert self.shot.shotmgr is not None
# pathHandler should be None
assert self.shot.pathHandler is not None
# pathHandler should be None
assert self.shot.pathController is None
# filtered roi should be None
self.assertEqual(self.shot.filteredROI, None)
self.assertEqual(self.shot.rawROI, None)
# rawROI queue should be created
assert self.shot.rawROIQueue is not None
# filteredROI queue should be created
assert self.shot.filteredROIQueue is not None
# roiVelocity should be initialized to None
self.assertEqual(self.shot.roiVelocity, None)
# init shot state
self.assertEqual(self.shot.followState, 0)
self.assertEqual(self.shot.followPreference, 0)
# initialize curTranslateVel to an empty Vector3
self.assertEqual(self.shot.translateVel,Vector3())
# shotmgr.getParam should be called thrice
# once for maxClimbRate and once for maxAlt and once for FENCE_ENABLE
calls = [call("PILOT_VELZ_MAX", DEFAULT_PILOT_VELZ_MAX_VALUE), call("FENCE_ALT_MAX", DEFAULT_FENCE_ALT_MAX), call("FENCE_ENABLE", DEFAULT_FENCE_ENABLE)]
self.shot.shotmgr.getParam.assert_has_calls(calls)
# initialize shot in altitudeOffset
self.assertEqual(self.shot.followControllerAltOffset, 0)
# initialize shot with ROIAltitudeOffset 0
self.assertEqual(self.shot.ROIAltitudeOffset, 0)
# enter Guided
self.assertEqual(self.shot.vehicle.mode.name, "GUIDED")
class TestHandleRCs(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
shotmgr.getParam.return_value = 0 # so mock doesn't do lazy binds
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
# Mock the functions
self.shot.checkSocket = Mock()
self.shot.filterROI = Mock()
# Mock Attributes
self.shot.rawROI = LocationGlobalRelative(37.873168,-122.302062, 0)
self.shot.filteredROI = LocationGlobalRelative(37.873168,-122.302062, 0)
self.shot.roiVelocity = Vector3(0,0,0)
self.shot.camYaw = 0.0
# Mock the pointing functions
self.shot.handleFreeLookPointing = Mock()
self.shot.handleFollowPointing = Mock()
self.shot.handleLookAtMePointing = Mock()
#Mock the pathHandler object
self.shot.pathHandler = mock.create_autospec(pathHandler.PathHandler)
self.shot.pathHandler.cruiseSpeed = 0
#Mock the pathController object
self.shot.pathController = mock.create_autospec(OrbitController)
self.shot.pathController.move.return_value = (LocationGlobalRelative(37.873168,-122.302062, 0),Vector3(1,1,1))
#Neutral sticks
throttle = 0.0
roll = 0.0
pitch = 0.0
yaw = 0.0
self.channels = [throttle, roll, pitch, yaw, 0.0, 0.0, 0.0, 0.0]
def testCallcheckSocket(self):
'''Test that we call checkSocket()'''
self.shot.handleRCs(self.channels)
self.shot.checkSocket.assert_called_with()
def testNoRawROI(self):
'''If raw ROI is not set then do NOT continue'''
self.shot.rawROI = None
self.shot.handleRCs(self.channels)
assert not self.shot.filterROI.called
def testCallFilterROI(self):
'''Test that we call filteRROI()'''
self.shot.handleRCs(self.channels)
self.shot.filterROI.assert_called_with()
def testFollowWaitReturns(self):
'''If followState == FOLLOW_WAIT, do NOT continue'''
self.shot.followState = FOLLOW_WAIT
self.shot.handleRCs(self.channels)
# assert not self.shot.vehicle.send_mavlink.called # TODO - CAN"T FIGURE OUT WHY THIS IS FAILING
def testLookAtMePathControllerCalled(self):
'''Test that correct path controller gets called'''
self.shot.followState = FOLLOW_LOOKAT
self.shot.pathController = mock.create_autospec(FlyController)
self.shot.pathController.move.return_value = (LocationGlobalRelative(37.873168,-122.302062, 0),Vector3(1,1,1))
self.shot.handleRCs(self.channels)
self.shot.pathController.move.assert_called_with(self.channels)
def testFreeLookPathControllerCalled(self):
'''Test that correct path controller gets called'''
self.shot.followState = FOLLOW_FREELOOK
self.shot.pathController = mock.create_autospec(FlyController)
self.shot.pathController.move.return_value = (LocationGlobalRelative(37.873168,-122.302062, 0),Vector3(1,1,1))
self.shot.camPitch = 45.0
self.shot.handleRCs(self.channels)
self.shot.pathController.move.assert_called_with(self.channels, newHeading = self.shot.camYaw, newOrigin = self.shot.filteredROI, roiVel=self.shot.roiVelocity)
def testOrbitPathControllerCalled(self):
'''Test that correct path controller gets called'''
self.shot.followState = FOLLOW_ORBIT
# pathController created in setup
self.shot.pathHandler.cruiseSpeed = 0.0
self.shot.handleRCs(self.channels)
self.shot.pathController.move.assert_called_with(self.channels, cruiseSpeed = self.shot.pathHandler.cruiseSpeed, newroi = self.shot.filteredROI, roiVel=self.shot.roiVelocity)
def testLeashPathControllerCalled(self):
'''Test that correct path controller gets called'''
self.shot.followState = FOLLOW_LEASH
self.shot.pathController = mock.create_autospec(LeashController)
self.shot.pathController.move.return_value = (LocationGlobalRelative(37.873168,-122.302062, 0),Vector3(1,1,1))
self.shot.handleRCs(self.channels)
self.shot.pathController.move.assert_called_with(self.channels, newroi = self.shot.filteredROI, roiVel=self.shot.roiVelocity)
def testSettingAltitudeLimit(self):
self.shot.maxAlt = 100.0
self.shot.handleRCs(self.channels)
def testFreelookPointing(self):
self.followState = FOLLOW_FREELOOK
self.shot.manualPitch = Mock()
self.shot.manualYaw = Mock()
self.shot.handleFreeLookPointing = Mock()
self.shot.handleRCs(self.channels)
# self.shot.manualPitch.assert_called_with(self.channels) # TODO - CAN"T FIGURE OUT WHY THIS IS FAILING
# self.shot.manualYaw.assert_called_with(self.channels) # TODO - CAN"T FIGURE OUT WHY THIS IS FAILING
# self.shot.handleFreeLookPointing.assert_called_with(mock.ANY) # TODO - CAN"T FIGURE OUT WHY THIS IS FAILING
def testNonFreelookPointing(self):
# may need mock
self.followState = FOLLOW_ORBIT
self.updateaROIAltOffset = Mock()
self.shot.handleRCs(self.channels)
#self.updateaROIAltOffset.assert_called_with(self.channels[RAW_PADDLE]) # TODO - CAN"T FIGURE OUT WHY THIS IS FAILING
def testMavlinkMessageDidSend(self):
self.shot.handleRCs(self.channels)
#Test that the expected message got encoded:
expectedControlMask = 0b0000110111000000 # pos-vel mask
# TODO - CAN"T FIGURE OUT WHY THIS IS FAILING:
# self.shot.vehicle.message_factory.set_position_target_global_int_encode.assert_called_with(
# 0, # time_boot_ms (not used)
# 0, 1, # target system, target component
# mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, # frame
# expectedControlMask, # type_mask
# int(37.873168*1E7), int(-122.302062*1E7), 0, # x, y, z positions (ignored)
# 1, 1, 1, # x, y, z velocity in m/s
# 0, 0, 0, # x, y, z acceleration (not used)
# 0, 0) # yaw, yaw_rate (not used)
self.shot.vehicle.send_mavlink.assert_called_with(mock.ANY)
class TestUpdateROIAltOffset(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
self.shot.pathHandler = mock.create_autospec(pathHandler.PathHandler)
self.shot.pathHandler.cruiseSpeed = 0
#Mock the pathController object
self.shot.followState = FOLLOW_ORBIT
self.shot.pathController = mock.create_autospec(OrbitController)
self.shot.ROIAltitudeOffset = 0.0
self.shot.ROIAltitudeOffset = 0.0
def testWithinDeadZone(self):
self.shot.updateROIAltOffset(0.01)
self.assertEqual(self.shot.ROIAltitudeOffset, 0.0)
def testNotWithinDeadZone(self):
self.shot.updateROIAltOffset(0.9)
self.assertNotEqual(self.shot.ROIAltitudeOffset, 0.0)
class TestHandleButton(unittest.TestCase):
'''TODO Nick - after UI shakes out'''
pass
class TestSetButtonMappings(unittest.TestCase):
'''TODO Nick - after UI shakes out'''
pass
class TestHandleOptions(unittest.TestCase):
'''Handles follow options from app. TODO Nick - after UI shakes out'''
pass
class TestInitState(unittest.TestCase):
ARBITRARY_HEADING = 34.8
DISTANCE = 97.5
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
#Mock Methods
self.shot.updateMountStatus = Mock()
self.shot.initLookAtMeController = Mock()
self.shot.initOrbitController = Mock()
self.shot.initLeashController = Mock()
self.shot.initFreeLookController = Mock()
self.shot.updateMountStatus = Mock()
self.shot.updateAppOptions = Mock()
self.shot.setButtonMappings = Mock()
#Setup Attributes
self.shot.followState = FOLLOW_WAIT
self.shot.rawROI = location_helpers.newLocationFromAzimuthAndDistance(self.shot.vehicle.location.global_relative_frame, self.ARBITRARY_HEADING, self.DISTANCE)
def testNoROINoInit(self):
self.shot.rawROI = None
self.shot.initState(FOLLOW_ORBIT)
assert not self.shot.updateMountStatus.called
def testLookatControllerDidInit(self):
self.shot.initState(FOLLOW_LOOKAT)
assert self.shot.initLookAtMeController.called
def testOrbitControllerDidInit(self):
self.shot.initState(FOLLOW_ORBIT)
assert self.shot.initOrbitController.called
def testLeashControllerDidInit(self):
self.shot.initState(FOLLOW_LEASH)
assert self.shot.initLeashController.called
def testFreeLookControllerDidInit(self):
self.shot.initState(FOLLOW_FREELOOK)
assert self.shot.initFreeLookController.called
def testUpdateFunctionsCalled(self):
self.shot.initState(FOLLOW_ORBIT)
assert self.shot.updateMountStatus.called
assert self.shot.updateAppOptions.called
assert self.shot.setButtonMappings.called
class TestUpdateAppOptions(unittest.TestCase):
'''TODO Nick - after UI shakes out'''
pass
class TestSetupSocket(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
@mock.patch('socket.socket')
def testSocketCreation(self, socket_socket):
'''Test that the socket is created'''
self.shot.setupSocket()
socket_socket.assert_called_with(socket.AF_INET, socket.SOCK_DGRAM)
@mock.patch('socket.socket', return_value = Mock())
def testSocketOptions(self, socket_socket):
'''Test that socket options are set'''
self.shot.setupSocket()
self.shot.socket.setsockopt.assert_called_with(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
@mock.patch('socket.socket', return_value = Mock())
def testSocketBlocking(self, socket_socket):
'''Test that socket blocking is set to 0'''
self.shot.setupSocket()
self.shot.socket.setblocking.assert_called_with(0)
@mock.patch('socket.socket', return_value = Mock())
def testSocketBind(self, socket_socket):
'''Test that socket tries to bind'''
self.shot.setupSocket()
self.shot.socket.bind.assert_called_with(("",FOLLOW_PORT))
@mock.patch('socket.socket', return_value = Mock())
def testSocketTimeout(self, socket_socket):
'''Test that socket sets a timeout'''
self.shot.setupSocket()
self.shot.socket.settimeout.assert_called_with(SOCKET_TIMEOUT)
class TestCheckSocket(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
sampleData = struct.pack('<IIddf', app_packet.SOLO_MESSAGE_LOCATION, 20, 37.873168,-122.302062, 0)
self.shot.socket.recvfrom = Mock(side_effect = [(sampleData,'127.0.0.1'), socket.timeout])
def testRecvFrom(self):
'''Test recvFrom functionality'''
self.shot.checkSocket()
self.shot.socket.recvfrom.assert_called_with(28)
@mock.patch('monotonic.monotonic', return_value = 333)
def testWhenROIisNone(self, monotonic_monotonic):
'''Test first data unpack'''
self.shot.rawROI = None
self.shot.checkSocket()
self.assertEqual(self.shot.roiDeltaTime, None)
self.assertEqual(self.shot.previousROItime, 333)
self.assertEqual(self.shot.rawROI.lat, 37.873168)
self.assertEqual(self.shot.rawROI.lon, -122.302062)
self.assertEqual(self.shot.rawROI.alt, 0)
@mock.patch('monotonic.monotonic', return_value = 333)
def testNewData(self, monotonic_monotonic):
'''Test subsequent data unpack'''
self.shot.previousROItime = 222
self.shot.rawROI = LocationGlobalRelative(37.873168,-122.302062, 0)
self.shot.checkSocket()
self.assertEqual(self.shot.roiDeltaTime, 111)
self.assertEqual(self.shot.previousROItime, 333)
self.assertEqual(self.shot.rawROI.lat, 37.873168)
self.assertEqual(self.shot.rawROI.lon, -122.302062)
self.assertEqual(self.shot.rawROI.alt, 0)
class TestFilterROI(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
shotmgr.buttonManager = Mock()
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
# rawROI
self.ROI = LocationGlobalRelative(37.873168,-122.302062, 0) #sample ROI, used throughout
self.shot.rawROI = self.ROI
# mock methods
self.shot.initState = Mock()
location_helpers.calcYawPitchFromLocations = Mock()
location_helpers.calcYawPitchFromLocations.return_value = (0.0,0.0)
#roiDeltaTime
self.shot.roiDeltaTime = 0.04 # 25Hz (guess)
#init vars
self.shot.roiVelocity = Vector3()
def testFilteredROIQueueGreaterThanZero(self):
'''Test that beginFollow is called if roiVelocity is None and the filteredROIQueue is long enough'''
self.shot.filterROI()
self.assertEqual(self.shot.filteredROI, self.shot.rawROI)
self.assertEqual(self.shot.roiVelocity, Vector3(0,0,0))
assert self.shot.initState.called
def testNoQueue(self):
'''run through the method with empty queues (First item will be added in this queue)'''
# Filter gets skipped b/c queues are empty
# initialization block executes because queue now has 1 item
# end of method executes
self.shot.roiDeltaTime = None
self.shot.rawROIQueue = collections.deque(maxlen=MIN_RAW_ROI_QUEUE_LENGTH)
self.shot.filteredROIQueue = collections.deque(maxlen=MIN_FILT_ROI_QUEUE_LENGTH)
self.shot.filterROI()
self.assertEqual(self.shot.filteredROI, self.shot.rawROI)
assert not location_helpers.calcYawPitchFromLocations.called
def testFilterFullQueue(self):
'''run through the method with a pre-filled queue'''
# filter executes because queues are full
# init is skipped. len(queue) > 1
# else block executes. len(queue) > 1 AND roiDeltaTime exists.
for i in range(5):
self.shot.rawROIQueue.append(self.ROI)
self.shot.filteredROIQueue.append(self.ROI)
self.shot.filterROI()
assert location_helpers.calcYawPitchFromLocations.called
def testAccelerationLimitVariations_x(self):
'''For different combinations of roiVeloctiy, translateVel, and components X,Y,Z: Verify that the code is executed and that the values are as expected.'''
self.shot.roiVelocity.x = 0.0 # gets overwritten in filterROI() anyway
# (much) Greater than case
self.shot.translateVel.x = -1.0
self.shot.filterROI()
assert self.shot.translateVel.x == -1.0 + ACCEL_PER_TICK
# equal case
self.shot.translateVel.x = 0.0
self.shot.filterROI()
assert self.shot.translateVel.x == self.shot.roiVelocity.x
# (much) Less than case
self.shot.translateVel.x = 1.0
self.shot.filterROI()
assert self.shot.translateVel.x == 1.0 - ACCEL_PER_TICK
def testAccelerationLimitVariations_y(self):
'''For different combinations of roiVeloctiy, translateVel, and components X,Y,Z: Verify that the code is executed and that the values are as expected.'''
self.shot.roiVelocity.y = 0.0 # gets overwritten in filterROI() anyway
# (much) Greater than case
self.shot.translateVel.y = -1.0
self.shot.filterROI()
assert self.shot.translateVel.y == -1.0 + ACCEL_PER_TICK
# equal case
self.shot.translateVel.y = 0.0
self.shot.filterROI()
assert self.shot.translateVel.x == self.shot.roiVelocity.y
# (much) Less than case
self.shot.translateVel.y = 1.0
self.shot.filterROI()
assert self.shot.translateVel.y == 1.0 - ACCEL_PER_TICK
def testAccelerationLimitVariations_z(self):
'''For different combinations of roiVeloctiy, translateVel, and components X,Y,Z: Verify that the code is executed and that the values are as expected.'''
self.shot.roiVelocity.z = 0.0 # gets overwritten in filterROI() anyway
# (much) Greater than case
self.shot.translateVel.z = -1.0
self.shot.filterROI()
assert self.shot.translateVel.z == -1.0 + ACCEL_PER_TICK
# equal case
self.shot.translateVel.z = 0.0
self.shot.filterROI()
assert self.shot.translateVel.z == self.shot.roiVelocity.z
# (much) Less than case
self.shot.translateVel.z = 1.0
self.shot.filterROI()
assert self.shot.translateVel.z == 1.0 - ACCEL_PER_TICK
def testQueueLengthsAreNotTooLong(self):
'''make sure (at steady state) the length of the queue is correct.'''
# should be defined as
# MIN_RAW_ROI_QUEUE_LENGTH = 5, MIN_FILT_ROI_QUEUE_LENGTH = 4
for i in range(10):
self.shot.rawROIQueue.append(self.ROI)
self.shot.filteredROIQueue.append(self.ROI)
self.shot.filterROI()
self.assertEqual(len(self.shot.rawROIQueue),5)
self.assertEqual(len(self.shot.filteredROIQueue),4)
class TestInitControllersParent(unittest.TestCase):
'''
Parent class to enable testing of multiple methods
'''
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
# mock methods
location_helpers.getDistanceFromPoints = Mock()
location_helpers.calcAzimuthFromPoints = Mock()
location_helpers.getDistanceFromPoints.return_value = 10.0
location_helpers.calcAzimuthFromPoints.return_value = 0.0
class TestInitOrbitController(TestInitControllersParent):
''' ORBIT '''
def setup(self):
super(TestInitOrbitController,self).setup()
controller = mock.create_autospec(OrbitController)
def didExecute_initOrbitController(self):
self.shot.initOrbitController()
assert location_helpers.getDistanceFromPoints.called
assert location_helpers.calcAzimuthFromPoints.called
assert controller.setOptions.called
class TestInitLeashController(TestInitControllersParent):
''' LEASH '''
def setup(self):
super(TestInitLeashController,self).setup()
controller = mock.create_autospec(LeashController)
def didExecute_initLeashController(self):
self.shot.initLeashController()
assert location_helpers.getDistanceFromPoints.called
assert location_helpers.calcAzimuthFromPoints.called
assert controller.setOptions.called
class TestInitFreeLookController(TestInitControllersParent):
''' FREE LOOK '''
def setup(self):
super(TestInitFreeLookController,self).setup()
location_helpers.getVectorFromPoints = Mock()
location_helpers.getVectorFromPoints.return_value = Vector3(5,5,5)
controller = mock.create_autospec(FlyController)
def didExecute_initFreeLookController(self):
self.shot.initFreeLookController()
assert location_helpers.getDistanceFromPoints.called
assert location_helpers.calcAzimuthFromPoints.called
assert controller.setOptions.called
# Didn't check if getYaw, getPitch were called. It's probably fine
class TestInitLookAtMeController(TestInitControllersParent):
''' LOOK AT ME '''
def setup(self):
super(TestInitLookAtMeController,self).setup()
controller = mock.create_autospec(LookAtController)
self.shot.pathHandler.pause = Mock()
def didExecute_initLookAtMeController(self):
self.shot.initLookAtMeController()
assert self.shot.pathHandler.pause.called
assert controller.setOptions.called
class TestUpdateMountStatus(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
def testFreeLookMessage(self):
self.shot.followState = FOLLOW_FREELOOK
self.shot.updateMountStatus()
self.shot.vehicle.message_factory.mount_configure_encode.assert_called_with(
0, 1, # target system, target component
mavutil.mavlink.MAV_MOUNT_MODE_MAVLINK_TARGETING, #mount_mode
1, # stabilize roll
1, # stabilize pitch
1, # stabilize yaw
)
assert self.shot.vehicle.send_mavlink.called
def testNonFreeLookMessage(self):
self.shot.followState = FOLLOW_ORBIT
self.shot.updateMountStatus()
self.shot.vehicle.message_factory.mount_configure_encode.assert_called_with(
0, 1, # target system, target component
mavutil.mavlink.MAV_MOUNT_MODE_GPS_POINT, #mount_mode
1, # stabilize roll
1, # stabilize pitch
1, # stabilize yaw
)
assert self.shot.vehicle.send_mavlink.called
class TestManualPitch(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
#Neutral sticks
throttle = 0.0
roll = 0.0
pitch = 0.0
yaw = 0.0
self.channels = [throttle, roll, pitch, yaw, 0.0, 0.0, 0.0, 0.0]
def testPositiveValue(self):
self.shot.camPitch = 1.0
self.shot.manualPitch(self.channels[THROTTLE])
assert self.shot.camPitch == 0.0
def testZeroValue(self):
self.shot.camPitch = 0.0
self.shot.manualPitch(self.channels[THROTTLE])
assert self.shot.camPitch == 0.0
def testSlightlyNegativeValue(self):
self.shot.camPitch = -1.0
self.shot.manualPitch(self.channels[THROTTLE])
assert self.shot.camPitch == -1.0
def testNegative90Value(self):
self.shot.camPitch = -90.0
self.shot.manualPitch(self.channels[THROTTLE])
assert self.shot.camPitch == -90.0
def testHugeNegativeValue(self):
self.shot.camPitch = -100.0
self.shot.manualPitch(self.channels[THROTTLE])
assert self.shot.camPitch == -90.0
class TestManualYaw(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
self.shot.camYaw = 0 #init to zero. Can be permuted below.
#Neutral sticks, unless permuted in the methods below
throttle = 0.0
roll = 0.0
pitch = 0.0
yaw = 0.0
self.channels = [throttle, roll, pitch, yaw, 0.0, 0.0, 0.0, 0.0]
def testZeroValue(self):
''' Make sure Function returns if no yaw command'''
self.channels[YAW] = 0.0
self.shot.camYaw = -999 #Dummy value to make sure it is unchanged in the function
self.shot.manualYaw(self.channels)
assert self.shot.camYaw == -999
def testPositiveYawPositiveDirectionOfSpin(self):
self.channels[YAW] = 0.5
self.shot.manualYaw(self.channels)
assert self.shot.camDir == 1
def testNegativeYawNegativeDirectionOfSpin(self):
self.channels[YAW] = -0.5
self.shot.manualYaw(self.channels)
assert self.shot.camDir == -1
def testLargeValue(self):
self.channels[YAW] = -0.1
self.shot.camYaw = 380
self.shot.manualYaw(self.channels)
assert self.shot.camYaw <= 20
def testNegativeValue(self):
self.channels[YAW] = 0.1
self.shot.camYaw = -50
self.shot.manualYaw(self.channels)
assert self.shot.camYaw >= 310
# TODO - Needed to test edge cases of 0 and 360? Can camYaw be both 0 and 360 on the dot? Hard to test.
class TestHandleFreeLookPointing(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
self.shot.camYaw = float( 1.111333 )
self.shot.camPitch = float (2.55556666 )
self.shot.camDir = 1
def testWithGimbal(self):
''' With a gimbal, use mount_control to control pitch/yaw '''
self.shot.vehicle.mount_status = [20.0, 0.0, 1.3]
self.shot.handleFreeLookPointing()
self.shot.vehicle.message_factory.mount_control_encode.assert_called_with(
0, 1, # target system, target component
self.shot.camPitch * 100, # pitch
0.0, # roll
self.shot.camYaw * 100, # yaw
0 # save position
)
def testNoGimbal(self):
''' Without a gimbal, we only use condition_yaw to control '''
# no gimbal
self.shot.vehicle.mount_status = [None, None, None]
yawDir = 1
self.shot.handleFreeLookPointing()
self.shot.vehicle.message_factory.command_long_encode.assert_called_with(
0, 0, # target system, target component
mavutil.mavlink.MAV_CMD_CONDITION_YAW, #command
0, #confirmation
self.shot.camYaw, # param 1 - target angle
follow.YAW_SPEED, # param 2 - yaw speed
yawDir, # param 3 - direction
0.0, # relative offset
0, 0, 0 # params 5-7 (unused)
)
class testHandleLookAtMePointing(unittest.TestCase):
def setUp(self):
#Create a mock vehicle object
vehicle = mock.create_autospec(Vehicle)
#Create a mock shotManager object
shotmgr = mock.create_autospec(ShotManager)
shotmgr.rcMgr = Mock(specs=['remapper'])
#Run the shot constructor
self.shot = follow.FollowShot(vehicle, shotmgr)
#roi
self.tempROI = LocationGlobalRelative(37.873168,-122.302062, 0)
def didExecute_handleLookAtPointing(self):
self.vehicle.message_factory.command_int_encode.assert_called_with(
0, 1, # target system, target component
mavutil.mavlink.MAV_FRAME_GLOBAL_RELATIVE_ALT, #frame
mavutil.mavlink.MAV_CMD_DO_SET_ROI, #command
0, #current
0, #autocontinue
0, 0, 0, 0, #params 1-4
self.tempROI.lat*1.E7,
self.tempROI.lon*1.E7,
self.tempROI.alt + self.shot.ROIAltitudeOffset #offset for ROI
)
assert self.shot.vehicle.send_mavlink.called