Skip to content

Commit 5821906

Browse files
author
Charles Weir
committed
Made UltrasonicSensor default to MAX_VALUE, not zero.
1 parent e3d2e15 commit 5821906

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

BrickPython/Sensor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,11 @@ class UltrasonicSensor(Sensor):
109109
SMOOTHING_RANGE=10
110110

111111
def __init__(self, port):
112-
self.recentRawValues = [0]
112+
self.recentRawValues = [255]
113113
Sensor.__init__(self, port, Sensor.ULTRASONIC_CONT)
114114

115115
def cookValue(self, rawValue):
116+
rawValue = 255 if rawValue == 0 else rawValue
116117
self.recentRawValues.append( rawValue )
117118
if len(self.recentRawValues) > UltrasonicSensor.SMOOTHING_RANGE:
118119
del self.recentRawValues[0]

test/TestSensor.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ def testUltrasonicSensor(self):
6565
sensor = UltrasonicSensor( '1' )
6666
self.assertEquals(sensor.port, 0)
6767
self.assertEquals( sensor.idChar, '1' )
68-
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,
68+
for input, output in {0:UltrasonicSensor.MAX_VALUE, 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-
for i in xrange(0,100):
71+
for i in xrange(0,UltrasonicSensor.SMOOTHING_RANGE+1):
7272
sensor.updateValue( input ) # Remove effects of smoothing.
73-
self.assertEquals( sensor.value(), output )
73+
self.assertEquals( sensor.value(), output, "Failed with input %d: got %d" %(input,sensor.value()) )
7474

7575
def testUltrasonicSensorSmoothing(self):
7676
sensor = UltrasonicSensor( '1' )

0 commit comments

Comments
 (0)