Skip to content

Commit 6f000ef

Browse files
Make thresholds configurable for all 3 sensors if required
1 parent c04204a commit 6f000ef

1 file changed

Lines changed: 50 additions & 12 deletions

File tree

PicoAutonomousRobotics.py

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,34 @@ def getRawLFValue(self,whichSensor):
222222
raise Exception("INVALID SENSOR") #harsh, but at least you'll know
223223
return 0 #just in case
224224

225-
def setLFDarkValue(self,darkThreshold):
226-
self.darkVal = darkThreshold
225+
#These functions set the thresholds for light/dark sensing to return true / false
226+
#there should be a gap between light and dark thresholds, to give soem deadbanding.
227+
#if specified OptionalLeftThreshold and OptionalRightThreshold give you the ability to
228+
#specify 3 sets of values. If missing then all sensors use the same value.
229+
#initially all sensors are set to 30000 for light and 35000 for dark.
227230

228-
def setLFLightValue(self,lightThreshold):
229-
self.lightVal = lightThreshold
231+
def setLFDarkValue(self,darkThreshold, OptionalLeftThreshold = -1, OptionalRightThreshold = -1):
232+
self.centreDarkVal = darkThreshold
233+
if(OptionalLeftThreshold == -1):
234+
self.leftDarkVal = darkThreshold
235+
else:
236+
self.leftDarkVal = OptionalLeftThreshold
237+
if(OptionalRightThreshold == -1):
238+
self.rightDarkVal = darkThreshold
239+
else:
240+
self.rightDarkVal = OptionalRightThreshold
230241

242+
def setLFLightValue(self,lightThreshold, OptionalLeftThreshold = -1, OptionalRightThreshold = -1):
243+
self.centreLightVal = lightThreshold
244+
if(OptionalLeftThreshold == -1):
245+
self.leftLightVal = lightThreshold
246+
else:
247+
self.leftLightVal = OptionalLeftThreshold
248+
if(OptionalRightThreshold == -1):
249+
self.rightLightVal = lightThreshold
250+
else:
251+
self.rightLightVal = OptionalRightThreshold
252+
231253
#this returns True when sensor is over light and FALSE over Dark.
232254
#Light/Dark is determined by the thresholds.
233255
# This code will throw an exception if the value returned is in the 'gery' area.
@@ -236,18 +258,30 @@ def setLFLightValue(self,lightThreshold):
236258
def isLFSensorLight(self,whichSensor):
237259
if(whichSensor == "c"):
238260
sensorVal = self.CentreLF.read_u16()
261+
if(sensorVal >= self.centreDarkVal):
262+
return False
263+
elif(sensorVal < self.centreLightVal):
264+
return True
265+
else:
266+
raise Exception("Sensor value 'Grey'")
239267
elif (whichSensor == "l"):
240268
sensorVal = self.LeftLF.read_u16()
269+
if(sensorVal >= self.leftDarkVal):
270+
return False
271+
elif(sensorVal < self.leftLightVal):
272+
return True
273+
else:
274+
raise Exception("Sensor value 'Grey'")
241275
elif (whichSensor == "r"):
242276
sensorVal = self.RightLF.read_u16()
277+
if(sensorVal >= self.rightDarkVal):
278+
return False
279+
elif(sensorVal < self.rightLightVal):
280+
return True
281+
else:
282+
raise Exception("Sensor value 'Grey'")
243283
else:
244284
raise Exception("INVALID SENSOR") #harsh, but at least you'll know
245-
if(sensorVal>self.darkVal):
246-
return False
247-
elif(sensorVal<self.lightVal):
248-
return True
249-
else:
250-
raise Exception("Sensor value 'Grey'")
251285

252286
#Buzzer: functions will sound a horn or a required frequency. Option aswell to silence the buzzer
253287
def silence(self):
@@ -292,6 +326,10 @@ def __init__(self):
292326
self.RightLF = ADC(26)
293327
#The LF circuit is setup to give a high value when a dark (non reflective) surface is in view,and a low value when a light (reflective) surface is in view.
294328
#To aid there is a 'is it light or dark' function, and these values set the thresholds for determining that.
295-
self.LightVal = 30000
296-
self.DarkVal = 35000
329+
self.centreLightVal = 30000
330+
self.centreDarkVal = 35000
331+
self.leftLightVal = 30000
332+
self.leftDarkVal = 35000
333+
self.rightLightVal = 30000
334+
self.rightDarkVal = 35000
297335

0 commit comments

Comments
 (0)