ME 405 Romi
Loading...
Searching...
No Matches
line_sensor.py
Go to the documentation of this file.
17
18from pyb import Pin, Timer, ADC
19from os import listdir, remove
20import array
21
22DATA_AMT = 5 # Number of samples on multi read to be sampled
23PITCH = 8 # mm
24
25
28
29
33 def __init__(self, ir1, ir2, ir3, ir4, ir5, ir6, ir7, ir8, ir9, ir10, ir11, ir12, ir13):
34 self.ir1 = ir1
35 self.ir2 = ir2
36 self.ir3 = ir3
37 self.ir4 = ir4
38 self.ir5 = ir5
39 self.ir6 = ir6
40 self.ir7 = ir7
41 self.ir8 = ir8
42 self.ir9 = ir9
43 self.ir10 = ir10
44 self.ir11 = ir11
45 self.ir12 = ir12
46 self.ir13 = ir13
47
48 self.tim = Timer(6, freq = 20000)
49
50 self.calibrated = False
51
52 #Create a buffer to hold information from each sensor
53 self.buf1 = array.array('H', (0 for i in range(DATA_AMT)))
54 self.buf2 = array.array('H', (0 for i in range(DATA_AMT)))
55 self.buf3 = array.array('H', (0 for i in range(DATA_AMT)))
56 self.buf4 = array.array('H', (0 for i in range(DATA_AMT)))
57 self.buf5 = array.array('H', (0 for i in range(DATA_AMT)))
58 self.buf6 = array.array('H', (0 for i in range(DATA_AMT)))
59 self.buf7 = array.array('H', (0 for i in range(DATA_AMT)))
60 self.buf8 = array.array('H', (0 for i in range(DATA_AMT)))
61 self.buf9 = array.array('H', (0 for i in range(DATA_AMT)))
62 self.buf10 = array.array('H', (0 for i in range(DATA_AMT)))
63 self.buf11 = array.array('H', (0 for i in range(DATA_AMT)))
64 self.buf12 = array.array('H', (0 for i in range(DATA_AMT)))
65 self.buf13 = array.array('H', (0 for i in range(DATA_AMT)))
66
67 #Create array of ir sensor objects for easily looping over entire line
68 self.ir_array = [self.ir1, self.ir2, self.ir3, self.ir4, self.ir5, self.ir6, self.ir7, self.ir8, self.ir9, self.ir10, self.ir11, self.ir12, self.ir13]
69 self.ir_adc_array = [self.ir1.adc, self.ir2.adc, self.ir3.adc, self.ir4.adc, self.ir5.adc, self.ir6.adc, self.ir7.adc, self.ir8.adc, self.ir9.adc, self.ir10.adc, self.ir11.adc, self.ir12.adc, self.ir13.adc]
70 self.data_buffer = [self.buf1, self.buf2, self.buf3,self.buf4,self.buf5,self.buf6,self.buf7,self.buf8,self.buf9,self.buf10,self.buf11,self.buf12,self.buf13 ]
71 self.data_avg = [0]*13
72
73 # White value first, Black value second
74 self.act_data = [0.0]*13
75
76 # Location of each sensor in mm
77 self.sens_dist = [0, 5, 4, 3, 2, 1, 0, -1, -2, -3, -4, -5, 0] * PITCH
78
79 # Data array to hold calibration constants
80 self.black=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
81 self.white=[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
82
83
85 def check_Calibrate(self):
86 return self.calibrated
87
88
91 def calibrate(self):
92 try:
93 with open('/flash/IR_cal.txt') as f:
94 file_exists = True
95 except OSError:
96 file_exists = False
97
98 if file_exists:
99 with open("/flash/IR_cal.txt", "r") as file:
100 idx = 0
101 for line in file:
102 text=line.strip().split(',')
103 self.white[idx] = (float(text[1]))
104 self.black[idx] = (float(text[0]))
105 idx += 1
106 self.calibrated = True
107 else:
108 self.calibrated = False
109
110 return file_exists
111
112
116 def getCentroid(self):
117 sum = 0
118 idx = 0
119 self.readSensors()
120 for val in self.act_data:
121 sum += (val * self.sens_dist[idx])
122 idx += 1
123 idx += 1
124 return sum/(idx-2)
125
126
128 def readSensors(self):
129 ADC.read_timed_multi(self.ir_adc_array, self.data_buffer, self.tim)
130 idx = 0
131 for array in self.data_buffer:
132 sum = 0
133 for number in array:
134 sum += number
135 self.data_avg[idx] = sum/DATA_AMT
136 idx+=1
137 idx = 0
138 for val in self.data_avg:
139 self.act_data[idx] = (val - self.black[idx])/(self.white[idx]-self.black[idx])
140 idx += 1
141
142 return self.act_data
143
144
147 ADC.read_timed_multi(self.ir_adc_array, self.data_buffer, self.tim)
148 idx = 0
149 for array in self.data_buffer:
150 sum = 0
151 for number in array:
152 sum += number
153 self.data_avg[idx] = sum/DATA_AMT
154 idx+=1
155 return self.data_avg
156
157
159 def check_Line(self):
160 check_list = list(self.readSensors())
161 for item in check_list:
162 if item > 15:
163 return True
164 return False
165
166
168 def check_Indv(self, which_sensor):
169 return self.ir_adc_array[which_sensor].read()
Line_Sensor class which will calibrate the sensors, read data from the entire line,...
read_Calibrate_Data(self)
readSensors will read every ir sensor with a time multi read but will return the raw unnormalized dat...
readSensors(self)
readSensors will read every ir sensor with a time multi read and then normalize with the calibration ...
getCentroid(self)
getCentroid will return the value of the centroid of the darkest spot the sensors can see along the l...
__init__(self, ir1, ir2, ir3, ir4, ir5, ir6, ir7, ir8, ir9, ir10, ir11, ir12, ir13)
init is the Line_Sensor object initializer which takes 13 arguments.
check_Indv(self, which_sensor)
check_Indv is a debugging function to look at the data from a single IR sensor if you think it is rea...
calibrate(self)
calibrate will try to read the calibration text IR_cal.txt and return whether or not the file exists.
check_Line(self)
check_Line is a debugging function to check that the line sensor is correctly setup with all the sens...
check_Calibrate(self)
check_Calibrate returns the status of the calibration variable.