Wall sensors
1 How they work
The robot's position in the maze is read by reading 3 wall sensorsIt needs to detect the presence of left, right and front walls.
It needs to steer straight down a corridor, and to detect gaps in the walls as they appear.
It can detect the end of a left turn, by seeing the re-appearance of the left wall.
Set up the inputs
from machine import Pin, ADC
Lsensor = ADC(27) # Left wall sensor
Msensor = ADC(26) # Front
Rsensor = ADC(28) # Right
tst = Pin(14, Pin.OUT) #Controls illuminating LEDsRead the three sensors with no illumination
tst.value(0) # switch off LED illumination time.sleep(0.001) # wait 1 millisecond LMin = Lsensor.read_u16() # read the dark values (Left) MMin = Msensor.read_u16() # Front RMin = Rsensor.read_u16() # Right
Read the sensors with illumination
tst.value(1) # switch on LED illumination time.sleep(0.001) # wait 1 millisecond LMax = Lsensor.read_u16() # read the light values (Left) MMax = Msensor.read_u16() # Front RMax = Rsensor.read_u16() # Right
Calculate the brightness increases
Left = LMax-LMinFront = MMax-MMin Right = RMax-RMin
Take an average
The code below uses the new reading to pull the average towards it
The factor controls how much it pulls the average. 1 = full 0= not at all.
The average has to be initialised at the start of the run:
Factor = 0.5 Laverage = 0 Faverage = 0 Raverage = 0
Factor = 0.5 Laverage = Laverage(1-*Factor) + Left *Factor Faverage = Faverage(1-*Factor) + Front*Factor Raverage = Raverage(1-*Factor) + Right*Factor
Saving the calibration
Writing dataNew file to write data
f = open("demofile.txt", "r")print("Saving")
f = open("calib.py", "w")
f.write("leftThresh =" + str(leftThresh)+ "\n")
f.write("frontThresh =" + str(frontThresh)+ "\n")
f.write("rightThresh =" + str(rightThresh)+ "\n")
f.close()
print("Saved")
Read existing file:
import calib as Ca return( (Ca.leftThresh,Ca.frontThresh,Ca.rightThresh) )
Writing data to a file Writing string (no end of line)
f.write("Start of data")
f.write("Start of data\r\n")
f.write("dfactor="+str(value)+"\r\n" )
4. Reading data
4.1 Read Only Parts of the File
You can also specify how much you want to read.
Example - the 5 first characters of the file:
f = open("demofile.txt", "r")
print(f.read(5))
Read one line of the file:
f = open("demofile.txt", "r")
print(f.readline())
Loop through the file line by line:
f = open("demofile.txt", "r")
for x in f:
print(x)
5 Closing the file
Close file
f.close()
Wall sensors
To delete a file, you must import the OS module, and run its os.remove() function: Example - Remove the file "demofile.txt":import os
os.remove("demofile.txt")To avoid getting an error, you might want to check if the file exists before you try to delete it:
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")Conclusion
Files can be used to store data permanently, or to record the results of a run.They are however a bit complicated to code.
They take a long time to read and write, and should not be used inside a tight control loop.
© Copyright St Albans Robots