forked from charlesweir/BrickPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMotor.py.bak
More file actions
188 lines (165 loc) · 6.81 KB
/
TestMotor.py.bak
File metadata and controls
188 lines (165 loc) · 6.81 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
# Tests for Motor
#
# Copyright (c) 2014 Charles Weir. Shared under the MIT Licence.
from BrickPython.BrickPiWrapper import BrickPiWrapper
from BrickPython.Scheduler import Scheduler, StopCoroutineException
import unittest
from mock import Mock
class TestMotor(unittest.TestCase):
''' Tests for the Motor class, especially for PID Servo Motor functionality'''
def setUp(self):
self.saveTime = Scheduler.currentTimeMillis
Scheduler.currentTimeMillis = Mock(side_effect = xrange(1,10000))
self.bp = BrickPiWrapper()
motor = self.motor = self.bp.motor( 'A' )
#motor.position = Mock()
motor.timeMillis = Mock()
motor.timeMillis.side_effect = range(0,9999)
def tearDown(self):
Scheduler.currentTimeMillis = self.saveTime
unittest.TestCase.tearDown(self)
def testZeroPosition( self ):
motor = self.motor
# When the motor is zeroed at absolute position 1
motor.updatePosition(1)
motor.zeroPosition()
# then absolute position 2 is read as position 1
motor.updatePosition(2)
self.assertEquals( motor.position(), 1 )
# When the motor is zeroed again at absolute position 2
motor.zeroPosition()
# then absolute position 3 is read as position 1
motor.updatePosition(3)
self.assertEquals( motor.position(), 1 )
def testSpeedCalculation(self):
motor = self.motor
# When the motor is moving at 1 click per call (every millisecond)
motor.updatePosition(1)
motor.updatePosition(2)
# The speed is 1000 clicks per second.
print motor.speed()
assert( int(motor.speed()) == 1000)
# Tests for positionUsingPIDAlgorithm:
def testGeneratorFunctionWorks(self):
motor = self.motor
# When we try to reposition the motor
positions = [0,5,11,10,10,10].__iter__()
generator = motor.positionUsingPIDAlgorithmWithoutTimeout( 10 )
# it keeps going
for i in generator:
motor.updatePosition( positions.next() )
assert( motor.enabled() )
# until it reaches the target position
assert( motor.position() == 10 )
# then switches off
assert( not motor.enabled() )
assert( motor.power() == 0 )
def testFinishesIfNearEnough(self):
motor = self.motor
# When the motor gets near enough to the target
positions = [0,99,99,99].__iter__()
generator = motor.positionUsingPIDAlgorithmWithoutTimeout( 100 )
for i in generator:
motor.updatePosition( positions.next() )
# it still completes :
self.assertEquals( motor.position(), 99 )
self.assertFalse( motor.enabled() )
def testChecksSpeedOnFinishing(self):
motor = self.motor
# When the motor gets to the right position, but is still going fast:
positions = [0,50,100,150].__iter__()
co = motor.positionUsingPIDAlgorithm( 100 )
for i in range(3):
motor.updatePosition( positions.next() )
co.next()
# it doesn't stop (no StopIteration exception has been thrown)
def testPowerIsFunctionOfDistance(self):
motor = self.motor
# When it's some distance from the target, but not moving
co = motor.positionUsingPIDAlgorithmWithoutTimeout( 100 )
motor.updatePosition( 0 )
co.next()
motor.updatePosition( 0 )
co.next()
# There's power to the motor in the right direction.
assert( motor.power() > 0 )
def testPowerIsFunctionOfSpeed(self):
motor = self.motor
# When the motor is going fast
co = motor.positionUsingPIDAlgorithmWithoutTimeout( 100 )
# when it reaches the target
motor.updatePosition( 0 )
co.next()
motor.updatePosition( 100 )
co.next()
# It doesn't finish and the power is in reverse.
assert( motor.power() < 0 )
def testPowerIsFunctionOfSumDistance(self):
motor = self.motor
# When the motor isn't moving and is some distance from the target
co = motor.positionUsingPIDAlgorithmWithoutTimeout( 100 )
# And we have quite long work cycles:
motor.timeMillis.side_effect = range(0,990,20)
# The motor power increases with time
motor.updatePosition( 0 )
co.next()
p1 = motor.power()
motor.updatePosition( 0 )
co.next()
p2 = motor.power()
self.assertGreater( p2, p1 )
# And the motor power depends on time between readings, so if we do it all again
# with with longer work cycles and a different motor
motorB = self.bp.motor( 'B' )
motorB.timeMillis = Mock()
motorB.timeMillis.side_effect = range(0,990,40)
co = motorB.positionUsingPIDAlgorithmWithoutTimeout( 100 )
motorB.updatePosition( 0 )
co.next()
motorB.updatePosition( 0 )
co.next()
# then we get a larger power reading than before
self.assertGreater( motorB.power(), p2 )
def testTimesOutIfNeverReachesTarget(self):
motor = self.motor
co = motor.positionUsingPIDAlgorithm( 100 )
# If the motor never reaches the target in 6 seconds:
for i in xrange(0,6000/50):
self.bp.doWork()
# It terminates
self.assertFalse( self.bp.stillRunning( co ) )
def testAlternateNameAndUseOfScheduler(self):
motor = self.motor
# When we use the other name for the operation
generator = motor.moveTo( 10 )
# and invoke the scheduler - disabling normal update processing - while the motor moves to the position
self.bp.setUpdateCoroutine(Scheduler.nullCoroutine())
positions = [0,5,11,10,10,10]
for i in range(len(positions)):
motor.updatePosition( positions[i] )
self.bp.doWork()
# Then we complete correctly
self.assertFalse( self.bp.stillRunning( generator ))
self.assertEquals( motor.position(), 10 )
def testCanCancelOperation(self):
motor = self.motor
# if we start the motor
co = motor.positionUsingPIDAlgorithmWithoutTimeout( 100 )
co.next()
assert( motor.enabled() )
# then stop the coroutine
try:
co.throw(StopCoroutineException)
# the coroutine stops
assert( False )
except (StopCoroutineException, StopIteration): # The exception should stop the coroutine
pass
# and switch off the motor.
assert( not motor.enabled() )
assert( motor.power() == 0 )
def testMotorTextRepresentation(self):
self.assertRegexpMatches( repr(self.motor), 'Motor.*location=.*speed=.*')
def testPIDIntegratedDistanceMultiplierBackwardCompatibility(self):
pass
if __name__ == '__main__':
unittest.main()