ME 405 Romi
Loading...
Searching...
No Matches
line_task.py
Go to the documentation of this file.
20import line_sensor
21import os
22import gc
23SENSOR_START = 0
24SENSOR_END = 12
25
39 def __init__(self, lineSensor, l_state, need_Calibrate, ready_Black, ready_White, centroid):
40 self.lineSensor = lineSensor
41 self.l_state = l_state
42 self.state = 0
43 self.need_Cal = need_Calibrate
44 self.ready_Black = ready_Black
45 self.ready_White = ready_White
46 self.centroid = centroid
47 self.black_data = [0.0]*13
48 self.white_data = [0.0]*13
49 self.got_black = False
50 self.got_white = False
51
58 def run(self):
59
60 while (True):
61 # Check calibration state
62 if self.state == 0:
63 ans = self.lineSensor.calibrate()
64 print(ans)
65 if ans:
66 print("sensor calibrated")
67 self.state = 2
68 else:
69 print("Sensor not calibrated")
70 self.state = 1
71 self.need_Cal.put(1)
72 yield 0
73
74 # Calibrating state
75 elif self.state == 1:
76 if self.ready_Black.get():
77 #print("Getting Black")
78 self.black_data = list(self.lineSensor.read_Calibrate_Data())
79 self.ready_Black.put(0)
80 self.got_black = True
81 #print(self.black_data)
82 if self.ready_White.get():
83 #print("Getting White")
84 self.white_data = list(self.lineSensor.read_Calibrate_Data())
85 self.ready_White.put(0)
86 self.got_white = True
87 #print(self.white_data)
88 if self.got_black and self.got_white == True:
89 #print("Got my data trying to make a file")
90 self.need_Cal.put(0)
91 self.state = 3
92 # # Get the folder where *this script* is located
93 # current_folder = os.path.dirname(os.path.abspath(__file__))
94 # # Build the full path to the new text file
95 # file_path = os.path.join(current_folder, "IR_cal.txt")
96 file_path = "/flash/IR_cal.txt"
97 #print("file creation complete")
98 # 1) See where a relative path points
99
100 # Write both arrays to the file
101 with open(file_path, "w") as file:
102 # for w, b in zip(self.white_data, self.black_data):
103 # file.write(f"{w},{b}\n")
104 combined = [f"{a},{b}" for a, b in zip(self.black_data, self.white_data)]
105 for line in combined:
106 file.write(line + "\n")
107 # for x, y in zip(self.black_data, self.white_data):
108 # file.write(f"{x}\t{y}\n")
109 yield 1
110
111 # Reading state
112 elif self.state == 2:
113 gc.collect()
114 self.centroid.put(self.lineSensor.getCentroid())
115 yield 2
116
117 elif self.state == 3:
118 ans = self.lineSensor.calibrate()
119 print(ans)
120 if ans:
121 self.state = 2
122 yield 3
123 else:
124 raise ValueError("Not that many states")
125 yield self.state
line_task class that can be initialized to be run with the cotask scheduler.
Definition line_task.py:27
__init__(self, lineSensor, l_state, need_Calibrate, ready_Black, ready_White, centroid)
Definition line_task.py:28
run(self)
run is the generator that the cotask scheduler will run since our tasks are objects.
Definition line_task.py:58