Skip to content

Commit cd509a1

Browse files
author
Charles Weir
committed
Added smoothing to UltrasonicSensor
1 parent a1d77e3 commit cd509a1

3 files changed

Lines changed: 23 additions & 8 deletions

File tree

BrickPython/Sensor.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,26 @@ class UltrasonicSensor(Sensor):
103103
'''
104104
#: The reading when no object is in sight:
105105
MAX_VALUE = 30
106+
#: Round readings to nearest centimeters.
107+
ROUND_TO = 5
108+
#: How many readings to smooth over.
109+
SMOOTHING_RANGE=10
106110

107111
def __init__(self, port):
112+
self.recentRawValues = [0]
108113
Sensor.__init__(self, port, Sensor.ULTRASONIC_CONT)
109114

110115
def cookValue(self, rawValue):
111-
result = int(5 * round(float(rawValue)/5)) # Round to nearest 5
116+
self.recentRawValues.append( rawValue )
117+
if len(self.recentRawValues) > UltrasonicSensor.SMOOTHING_RANGE:
118+
del self.recentRawValues[0]
119+
smoothedValue = min(self.recentRawValues)
120+
result = int(self.ROUND_TO * round(float(smoothedValue)/self.ROUND_TO)) # Round to nearest 5
112121
return min(result, UltrasonicSensor.MAX_VALUE)
113122

123+
# def displayValue(self):
124+
# return self.recentRawValues
125+
114126
class LightSensor(Sensor):
115127
'''Represents my NXT color sensor.
116128
The BrickPi_Python COLOR_FULL setting didn't work for me at all - always has value 1.

ExamplePrograms/TrialApp.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def __init__(self):
2020
self.root.wm_title("Trial running")
2121
for c in "ABCD":
2222
self.motor(c).zeroPosition()
23+
self.addActionCoroutine(self.motor(c).runAtConstantSpeed(180))
2324
for c in settings:
2425
self.addSensorCoroutine(self.showChanges(c))
2526

@@ -35,12 +36,6 @@ def showSensorValues(self, sensorId):
3536
for i in self.waitMilliseconds(1000): yield
3637
print sensor
3738

38-
## for c in "ABCD":
39-
## motor= self.motor(c)
40-
## for i in motor.moveTo(90*2): yield
41-
## for i in motor.moveTo(0): yield
42-
##
43-
4439
if __name__ == "__main__":
4540
logging.basicConfig(format='%(message)s', level=logging.DEBUG) # All log messages printed to console.
4641
logging.info( "Starting" )

test/TestSensor.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,17 @@ def testUltrasonicSensor(self):
6868
for input, output in {0:0, 2:0, 3:5, 4:5, 9:10, 11:10, 14:15, 16:15, 22:20, 23:25, 26:25,
6969
255: UltrasonicSensor.MAX_VALUE
7070
}.items():
71-
sensor.updateValue( input )
71+
for i in xrange(0,100):
72+
sensor.updateValue( input ) # Remove effects of smoothing.
7273
self.assertEquals( sensor.value(), output )
7374

75+
def testUltrasonicSensorSmoothing(self):
76+
sensor = UltrasonicSensor( '1' )
77+
for input in [ 24,14,10,8,10,10,50,10,50,18,50]:
78+
sensor.updateValue( input )
79+
print sensor
80+
self.assertEquals( sensor.value(), 10 )
81+
7482
def testLightSensor(self):
7583
#Light is 680, dark about 800
7684
sensor = LightSensor('4')

0 commit comments

Comments
 (0)