-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauto_control.py
More file actions
335 lines (237 loc) · 11.9 KB
/
auto_control.py
File metadata and controls
335 lines (237 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import datetime
from gradient import *
import sys, time
sys.path.append("../catkin_ws/src/scr_control/scripts/lights")
sys.path.append("../catkin_ws/src/scr_control/scripts/time_of_flight")
sys.path.append("../catkin_ws/src/scr_control/scripts/color_sensors")
import SCR_OctaLight_client as light_control
import SCR_TOF_client as tof
import SCR_COS_client as cos
# This function will detect the sensor pixel
# with a high noise level. Then, these pixels
# will be ignored in the auto_control algorithm.
def tof_pixel_check(tof_id, loop_count=10, freq=0.1, threshold=500, bar_chart=True, grid=True, bar_fn="ToF_bar_chart.png", grid_fn="ToF_grid_figure.png"):
tof_data_list = tof.get_distances(tof_id)
change_freq_matrix = [[[] for j in range(len(tof_data_list[0]))] for i in range(len(tof_data_list))]
# store the current data in the ToF matrix
tof_origin = tof_data_list
# detect the new data from the ToF sensor in the loop
# and return the changes of data
for itr_number in range(0, loop_count):
# create a new matrix to store the changes of data
data_change_matrix = [[0]*len(tof_data_list[0]) for i in range(len(tof_data_list))]
# read the new data matrix
tof_data_list = tof.get_distances(int(tof_id))
# calculate and store the changes
for i in range(0, len(tof_data_list)):
for j in range(0, len(tof_data_list[0])):
data_change_matrix[i][j] = abs(tof_data_list[i][j] - tof_origin[i][j])
if itr_number != 0:
(change_freq_matrix[i][j]).append(data_change_matrix[i][j])
tof_origin = tof_data_list
time.sleep(freq)
# calcualte the average changes for each pixel
mean_change = []
for i in range(0, len(change_freq_matrix)):
for j in range(0, len(change_freq_matrix[0])):
each_pixel_change = []
mean_change_one_pixel = sum(change_freq_matrix[i][j])/len(change_freq_matrix[i][j])
maximum_change = max(change_freq_matrix[i][j])
each_pixel_change.append(mean_change_one_pixel)
each_pixel_change.append(maximum_change)
each_pixel_change.append(i)
each_pixel_change.append(j)
mean_change.append(each_pixel_change)
# sort the mean_change matrix
sorted_mean_change = sorted(mean_change, key=lambda x: -x[0])
# Plot the bar chart
if bar_chart:
# plot the maximum_change and mean_change amoung these pixels
plot_max_list_x = []
plot_max_list_y = []
for d in sorted_mean_change:
plot_max_list_x.append(str(d[2])+str(d[3]))
plot_max_list_y.append(int(d[1])) # insert the max_changes
plt.bar(plot_max_list_x, plot_max_list_y)
# Add labels
plt.xlabel('Pixel locations')
plt.ylabel('Average changes')
plt.title('Average changes of ToF sensor {}'.format(tof_id))
# Save the chart
plt.savefig(bar_fn)
# Plot the grid
if grid:
# Create a new martix to store the max changes values
max_change_matrix = [[0 for j in range(20)] for i in range(25)]
for i in range(0, len(change_freq_matrix)):
for j in range(0, len(change_freq_matrix[0])):
max_change_matrix[i][j] = max(change_freq_matrix[i][j])
# Create a new figure and axis object
fig, ax = plt.subplots()
# Plot the matrix as an image
im = ax.imshow(max_change_matrix, cmap='coolwarm')
# Add a colorbar
cbar = ax.figure.colorbar(im, ax=ax)
# Loop over the matrix and add text annotations
for i in range(25):
for j in range(20):
text = ax.text(j, i, max_change_matrix[i][j],
ha="center", va="center",
fontsize = 5,
fontweight='bold' if max_change_matrix[i][j] >= threshold else 'normal',
color="black" if max_change_matrix[i][j] < threshold else "white")
# Set the axis labels
ax.set_xticks(range(20))
ax.set_yticks(range(25))
# Set the tick labels
ax.set_xticklabels(range(1, 21))
ax.set_yticklabels(range(1, 26))
# Set the axis labels
ax.set_xlabel("Column")
ax.set_ylabel("Row")
# Set the title
ax.set_title("ToF Matrix Visualization")
# Add the threshold value as text outside the matrix
ax.text(1.32, 1.0, "Threshold:\n {}".format(threshold),
transform=ax.transAxes,
fontsize=10,
ha='left',
va='center')
# Save the figure to a file
plt.savefig(grid_fn)
# Return the matrix of selected pixel
selected_pixel = np.zeros((25,20))
for i in range(0, len(max_change_matrix)):
for j in range(0, len(max_change_matrix[0])):
if max_change_matrix[i][j] < threshold:
selected_pixel[i, j] = True
else:
selected_pixel[i, j] = False
# Set pixels to False manually
for i in range(22, 25):
selected_pixel[i, 19] = False
selected_pixel[23, 18] = False
return selected_pixel
def check_neighbors(matrix, row, col):
rows, cols = len(matrix), len(matrix[0])
#top = matrix[row-1][col] if row > 0 else 2000
bottom = matrix[row+1][col] if row < rows-1 else 2000
left = matrix[row][col-1] if col > 0 else 2000
right = matrix[row][col+1] if col < cols-1 else 2000
return bottom <= 2700 and left <= 2700 and right <= 2700
def entry_detect(pixel_pick_mat_0, pixel_pick_mat_1, start=False):
with open("auto_control_log.txt", "w") as log_file:
# read the new data
tof_data_list_0 = tof.get_distances(0)
tof_data_list_1 = tof.get_distances(1)
trigger = False
for i in range(4, 20):
if tof_data_list_0[24][i] <= 2400 and pixel_pick_mat_0[24][i]:
trigger = True
break
if not trigger:
return False
# If the data read by door-side pixels is lower than 2500
# This means someone may get into the room.
# Then, in 1 second period, detect the change of each pixel,
# If there are more than 5 pixels have a change lower than
# -500, the lights will turn on.
tof_data_origin_0 = tof_data_list_0 # Store the old values
tof_data_origin_1 = tof_data_list_1
start_time = time.time() # Get the start time
pixel_change_count = 0 # Count the number of pixels with a high change
#print("Enter 2s while loop")
while (time.time() - start_time) < 2: # Keep running in 2 seconds
tof_data_list_0 = tof.get_distances(0) # Update the values
# create a new matrix to store the changes of data
#data_change_matrix = np.zeros((25,20))
# calculate and compare the changes in ToF sensor #0
for i in range(22, 25):
for j in range(0, 20):
change = tof_data_list_0[i][j] - tof_data_origin_0[i][j]
#data_change_matrix[i, j] = change
if (change <= -500)\
and (pixel_pick_mat_0[i][j]):
pixel_change_count += 1
print("ToF #0: distance change <= -500 detected on: {} {} at {}".format(i, j, datetime.datetime.now()))
log_file.write("ToF #0: distance change <= -500 detected on: {} {} at {}".format(i, j, datetime.datetime.now()))
if pixel_change_count >= 3:
return True
tof_data_list_1 = tof.get_distances(1)
# calculate and compare the changes in ToF sensor #1
for i in range(19, 25):
for j in range(0, 5):
change = tof_data_list_1[i][j] - tof_data_origin_1[i][j]
if (change <= -500)\
and (pixel_pick_mat_1[i][j]):
pixel_change_count += 1
print("ToF #1: distance change <= -500 detected on: {} {} at {}".format(i, j, datetime.datetime.now()))
log_file.write("ToF #1: distance change <= -500 detected on: {} {} at {}".format(i, j, datetime.datetime.now()))
if pixel_change_count >= 3:
return True
tof_data_origin_0 = tof_data_list_0
tof_data_origin_1 = tof_data_list_1
def turn_lights_on(gradient_trigger):
if gradient_trigger:
turn_on_light_gradually()
#light_control.cct(2,0,3500,10)
else:
light_control.cct(0, 2, 3500, 1000)
light_control.cct(0, 0, 3500, 1000)
light_control.cct(1, 1, 3500, 1000)
light_control.cct(2, 0, 3500, 1000)
light_control.cct(2, 2, 3500, 1000)
light_control.cct(3, 0, 3500, 500)
light_control.cct(3, 2, 3500, 500)
light_control.cct(4, 1, 3500, 500)
light_control.cct(5, 2, 3500, 500)
light_control.cct(5, 0, 3500, 500)
def sleep_helper():
with open("auto_control_log.txt", "w") as log_file:
sleep_timer = time.time()
sleep_duration = 60
while(time.time() - sleep_timer) < sleep_duration:
# If the light is turned on manually
if not (all(value == 0.0 for value in light_control.get_sources(2, 0))):
print("Lights are turned on during sleep, auto_control enabled {}".format(datetime.datetime.now()))
log_file.write("Lights are turned on during sleep, auto_control enabled {}".format(datetime.datetime.now()))
return True
print("1 min is up, auto_control enabled {}".format(datetime.datetime.now()))
log_file.write("1 min is up, auto_control enabled {}".format(datetime.datetime.now()))
return False
def main():
print("System is initializing, please make sure there's no movement near the door")
pixel_pick_mat_0 = tof_pixel_check(0, bar_chart=False)
pixel_pick_mat_1 = tof_pixel_check(1, bar_chart=False)
sleep_flag = False
print("System Initialized")
print("auto_control system is running")
#light_control.cct(2,0,3500,0)
# Open the file for writing
with open("auto_control_log.txt", "w") as log_file:
# While the light is off
while True:
light_is_off = all(value == 0.0 for value in light_control.get_sources(2, 0))
# Check if the light is manually turned on and set sleep_flag to True
if not light_is_off:
sleep_flag = True
# sleep for 60 second:
if light_is_off and sleep_flag:
print("Lights are turned off, disable auto_control for 1 minute {}".format(datetime.datetime.now()))
log_file.write("Lights are turned off, disable auto_control for 1 minute {}".format(datetime.datetime.now()))
sleep_flag = sleep_helper()
while light_is_off:
turn_on_lights = entry_detect(pixel_pick_mat_0, pixel_pick_mat_1, True)
if turn_on_lights:
turn_lights_on(True)
sleep_flag = True
current_time = datetime.datetime.now()
log_file.write("Turn on the lights at: {}\n".format(current_time))
print("Turn lights on at {}\n".format(current_time))
light_is_off = all(value == 0.0 for value in light_control.get_sources(2, 0))
if __name__ == "__main__":
main()