Skip to content

Commit e31c509

Browse files
author
Charles Weir
committed
Changed Motor.PIDSetting to match.
1 parent 2441d68 commit e31c509

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

BrickPython/Motor.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ class PIDSetting():
1111
Distances are in clicks, times in ms, motor power between -255 and +255, the sum of distance is added 20 times/sec.
1212
Speeds in clicks per second.
1313
'''
14-
def __init__(self,distanceMultiplier=0.9,speedMultiplier=0.23,sumDistanceMultiplier=0.05,
14+
def __init__(self,distanceMultiplier=0.9,speedMultiplier=0.23,integratedDistanceMultiplier=(0.05/20.0),
1515
closeEnoughPosition=4, closeEnoughSpeed=10.0):
16-
#: Factor for motor power as function of distance.
16+
#: Factor for motor power as function of distance - in power units per click distance.
1717
self.distanceMultiplier = distanceMultiplier
18-
#: Factor for motor power as function of speed.
18+
#: Factor for motor power as function of speed - in power units per (click per millisecond)
1919
self.speedMultiplier = speedMultiplier
20-
#: Factor for motor power as function of sum of distances.
21-
self.sumDistanceMultiplier = sumDistanceMultiplier
20+
#: Factor for motor power as a function of the integrated distance - in power units per (click-millisecond)
21+
self.integratedDistanceMultiplier = integratedDistanceMultiplier
2222
#: Distance in clicks from the target that we'll accept as got there.
2323
self.closeEnoughPosition = closeEnoughPosition
2424
#: Speed that we'll consider as close enough to zero.
@@ -138,7 +138,7 @@ def positionUsingPIDAlgorithmWithoutTimeout( self, target ):
138138

139139
power = (self.pidSetting.distanceMultiplier * delta
140140
- self.pidSetting.speedMultiplier * speed
141-
+ self.pidSetting.sumDistanceMultiplier * distanceIntegratedOverTime )
141+
+ self.pidSetting.integratedDistanceMultiplier * distanceIntegratedOverTime )
142142
self.setPower( power )
143143

144144
yield

CHANGES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# BrickPython Changelog
22

3+
## BrickPython v.4
4+
5+
- Updated PID algorithm so it's independent of the work cycle time. NOTE: One parameter to the constructor of
6+
Motor.PIDSetting has changed.
7+
38
## BrickPython v0.3.1
49

510
- Reduced sampling frequency so TkWindow shows correctly.

ExamplePrograms/MotorController.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ def onKeyPress(self, event):
7171
elif char == 'Y':
7272
self.pidSetting.speedMultiplier /= 1.1
7373
elif char == 'z':
74-
self.pidSetting.sumDistanceMultiplier *= 1.1
74+
self.pidSetting.integratedDistanceMultiplier *= 1.1
7575
elif char == 'Z':
76-
self.pidSetting.sumDistanceMultiplier /= 1.1
76+
self.pidSetting.integratedDistanceMultiplier /= 1.1
7777
logging.info( "%r" % (self.pidSetting) )
7878
self.motor('A').setPIDSetting( self.pidSetting )
7979

test/TestMotor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ def testPowerIsFunctionOfSumDistance(self):
106106
motor = self.motor
107107
# When the motor isn't moving and is some distance from the target
108108
co = motor.positionUsingPIDAlgorithmWithoutTimeout( 100 )
109+
# And we have quite long work cycles:
110+
motor.timeMillis.side_effect = range(0,990,20)
109111
# The motor power increases with time
110112
motor.updatePosition( 0 )
111113
co.next()
@@ -116,10 +118,10 @@ def testPowerIsFunctionOfSumDistance(self):
116118
self.assertGreater( p2, p1 )
117119

118120
# And the motor power depends on time between readings, so if we do it all again
119-
# with with longer between readings and a different motor
121+
# with with longer work cycles and a different motor
120122
motorB = self.bp.motor( 'B' )
121123
motorB.timeMillis = Mock()
122-
motorB.timeMillis.side_effect = range(0,99,2)
124+
motorB.timeMillis.side_effect = range(0,990,40)
123125
co = motorB.positionUsingPIDAlgorithmWithoutTimeout( 100 )
124126
motorB.updatePosition( 0 )
125127
co.next()

0 commit comments

Comments
 (0)