This repository has been 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
/
TestRCManager.py
185 lines (151 loc) · 7.33 KB
/
TestRCManager.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
import shotManager
import unittest
import mock
from mock import patch, Mock
import socket
from dronekit import Vehicle
import struct
import rcManager
class TestParse(unittest.TestCase):
@patch.object(socket.socket, 'bind')
def setUp(self, mock_bind):
self.mgr = shotManager.ShotManager()
v = mock.create_autospec(Vehicle)
self.mgr.Start(v)
self.sock = Mock()
self.channels = [1500, 5, 9, 8, 7, 1, 1, 1]
self.value = struct.pack('<dHHHHHHHHH', 2.3, 3, 1500, 5, 9, 8, 7, 1, 1, 1)
self.mgr.rcMgr.server.recv = Mock(return_value = self.value)
def testCacheSocketData(self):
""" incoming RC data should be written to self.channels """
# should feed return value of sock.recv into remap
self.mgr.rcMgr.parse()
self.assertEqual( self.mgr.rcMgr.channels, self.channels )
def testResetTicks(self):
""" handleRCSock should reset numTicksSinceRCUpdate """
self.mgr.numTicksSinceRCUpdate = 17
self.mgr.rcMgr.parse()
self.assertEqual( self.mgr.rcMgr.numTicksSinceRCUpdate, 0 )
def testDontCacheData(self):
""" if incoming data is malformed, do not cache it """
self.mgr.channels = 111
self.sock.recv = Mock(return_value='333')
self.mgr.rcMgr.parse()
self.assertNotEqual( self.mgr.rcMgr.channels, '333' )
class TestNormalizeRC(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.rcManager = rcManager.rcManager(shotmgr)
sock = Mock()
def testOutOfBoundsHighDefaultMinMax(self):
""" PWM values above max should return 0.0 """
result = self.rcManager.normalizeRC(2050, rcManager.DEFAULT_RC_MIN, rcManager.DEFAULT_RC_MAX)
self.assertEqual( result, 0.0 )
def testOutOfBoundsLowDefaultMinMax(self):
""" PWM values below min should return 0.0 """
result = self.rcManager.normalizeRC(957, rcManager.DEFAULT_RC_MIN, rcManager.DEFAULT_RC_MAX)
self.assertEqual( result, 0.0 )
def test1DefaultMinMax(self):
""" pwm of 2000 should result in 1.0 """
result = self.rcManager.normalizeRC(2000, rcManager.DEFAULT_RC_MIN, rcManager.DEFAULT_RC_MAX)
self.assertEqual( result, 1.0 )
def testneg1DefaultMinMax(self):
""" pwm of 1000 should result in -1.0 """
result = self.rcManager.normalizeRC(1000, rcManager.DEFAULT_RC_MIN, rcManager.DEFAULT_RC_MAX)
self.assertEqual( result, -1.0 )
def testMidDefaultMinMax(self):
""" pwm of 1500 should result in 0.0 """
result = self.rcManager.normalizeRC(1500, rcManager.DEFAULT_RC_MIN, rcManager.DEFAULT_RC_MAX)
self.assertEqual( result, 0.0 )
def testOutOfBoundsHighCustomMinMax(self):
""" Values above a custom max should return 0.0 """
result = self.rcManager.normalizeRC(1521, 1000, 1520)
self.assertEqual( result, 0.0 )
def testOutOfBoundsLowCustomMinMax(self):
""" Values below a custom min should return 0.0 """
result = self.rcManager.normalizeRC(-1, 0, 1000)
self.assertEqual( result, 0.0 )
def test1CustomMinMax(self):
""" Value matching max should result in 1.0 """
result = self.rcManager.normalizeRC(1333, 650, 1333)
self.assertEqual( result, 1.0 )
def testneg1CustomMinMax(self):
""" Value matching min should result in -1.0 """
result = self.rcManager.normalizeRC(333, 333, 666)
self.assertEqual( result, -1.0 )
def testMidCustomMinMax(self):
""" Mid value should result in 0.0 """
result = self.rcManager.normalizeRC(1260, 1000, 1520)
self.assertEqual( result, 0.0 )
class TestRemap(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.rcManager = rcManager.rcManager(shotmgr)
sock = Mock()
def testStickNormalization(self):
""" Remap should call normalize for each channel """
self.rcManager.channels = [1600, 1200, 1500, 1300, 2000, 1000, 1237, 1889]
normChannels = self.rcManager.remap()
self.assertEqual( normChannels[0], .2 )
self.assertEqual( normChannels[1], -0.6 )
self.assertEqual( normChannels[2], 0.0)
self.assertEqual( normChannels[3], -0.4 )
self.assertEqual( normChannels[5], -1.0 )
def testChannel6Max(self):
""" Channel 6 (index 5) is a special case and goes from 1000-1520. Input of 1520 should return 1.0"""
self.rcManager.channels = [0, 0, 0, 0, 0, 1520, 0, 0]
normChannels = self.rcManager.remap()
self.assertEqual( normChannels[5], 1.0 )
def testChannel6Mid(self):
""" Channel 6 (index 5) is a special case and goes from 1000-1520. Input of 1260 should return 0.0"""
self.rcManager.channels = [0, 0, 0, 0, 0, 1260, 0, 0]
normChannels = self.rcManager.remap()
self.assertEqual( normChannels[5], 0.0 )
def testChannel8(self):
""" Channel 8 (index 7) is a special case and goes from 0-1000 """
self.rcManager.channels = [0, 0, 0, 0, 0, 0, 0, 777]
normChannels = self.rcManager.remap()
result = (777 - 500) / 500.0
self.assertEqual( normChannels[7], result )
def testNoRemappingSticks(self):
""" If self.remappingSticks is off, we should not forward sticks to pixRC """
self.rcManager.channels = [1100, 1500, 1500, 1500, 0, 0, 0, 777]
self.rcManager.remappingSticks = False
with patch('sololink.rc_ipc.put') as mock_put:
self.rcManager.remap()
self.assertFalse( mock_put.called )
def testRemappingSticks(self):
""" If self.remappingSticks is on, we should forward sticks to pixRC, if passing in True for sendToPixRC """
self.rcManager.channels = [1100, 0, 0, 1500,0, 0, 0, 777]
self.rcManager.remappingSticks = True
self.rcManager.timestamp = 140
self.rcManager.sequence = 0
with patch('sololink.rc_ipc.put') as mock_put:
self.rcManager.remap()
mock_put.assert_called_with((140, 0, [1100, 0, 0, 1500, 0, 0, 0, 777]))
def testRemappingSticksFalse(self):
""" If self.remappingSticks is off, we should enter into Failsafe, enable remapping and send RC to sendToPixRC """
self.rcManager.channels = [1100, 1500, 1500, 1500, 0, 0, 0, 777]
self.rcManager.remappingSticks = False
with patch('sololink.rc_ipc.put') as mock_put:
self.rcManager.remap()
self.assertFalse( mock_put.called )
def testRemap(self):
defaultChannels = [0.0, 0.0, 0.0, 0.0, 0, 0.0, 0, 0.0]
self.rcManager.channels = [rcManager.DEFAULT_RC_MID,
rcManager.DEFAULT_RC_MID,
rcManager.DEFAULT_RC_MID,
rcManager.DEFAULT_RC_MID,
rcManager.DEFAULT_RC_MIN,
rcManager.CHANNEL6_MID,
rcManager.DEFAULT_RC_MID,
rcManager.CHANNEL8_MID]
retVal = self.rcManager.remap()
self.assertEqual(retVal,defaultChannels)
class TestRemapperInit(unittest.TestCase):
def testInit(self):
""" Test initialization of RC remapper """
shotmgr = Mock()
self.rcManager = rcManager.rcManager(shotmgr)
sock = Mock()
self.assertFalse(self.rcManager.remappingSticks)