-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStereoCapture-with-WatchDog.py
More file actions
314 lines (260 loc) · 8.59 KB
/
StereoCapture-with-WatchDog.py
File metadata and controls
314 lines (260 loc) · 8.59 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
import cv2
from camera import Camera1
from camera import Camera2
from utils import *
from datetime import datetime
import time
import threading
import sys
import Jetson.GPIO as GPIO
from threading import Thread, Lock
# Global variables:
fps = 15
focus = 0
output_path = "/home/user/Pictures/"
imagetime = ' '
restart_times = 0
selector = None
SnapTime = 2 # photos are snapped at this interval, seconds
width = 1920
height = 1080
Cam1PhotoTime = 0.0 # Time of last photo from camera1, for watchdog
Cam2PhotoTime = 0.0 # Time of last photo from camera2, for watchdog
WatchDogPollTime = 10 # How often watch dog times are checked, seconds
# Thread synchronization using condition variables
condition1 = threading.Condition() # Cam1Thread
condition2 = threading.Condition() # Cam2Thread
PrintMutex = Lock() # Used to clean up screen printing
# nameCurrTime
# Compute name for photos
def nameCurrTime():
now = datetime.now()
return now.strftime("%Y%m%d_%H%M%S")
# End of nameCurrTime
# WatchDogThread
# Send a signal through GPIO to an external MCU.
# If the External MCU does not received this signal
# over a period of time, the reset pin will be toggled
# on the NVIDIA Jetson.
def WatchDogThread():
print("WatchDogThread: starting")
# Wait for camera threads to initialize and snap photos.
time.sleep(WatchDogPollTime)
while True:
CurrentTime = time.time()
Cam1DiffTime = CurrentTime - Cam1PhotoTime
Cam2DiffTime = CurrentTime - Cam2PhotoTime
if (Cam1DiffTime < WatchDogPollTime) and (Cam2DiffTime < WatchDogPollTime):
# Pet the watch dog by sending a heartbeat signal
# to the watchdog MCU.
GPIO.output(12, GPIO.HIGH)
time.sleep(0.5) # Adjust as needed
GPIO.output(12, GPIO.LOW)
time.sleep(0.5) # Adjust as needed
PrintMutex.acquire()
print("WatchDogThread: sending heartbeat")
PrintMutex.release()
else:
PrintMutex.acquire()
print("WatchDogThread: missed heartbeat")
PrintMutex.release()
# End of Else
time.sleep(WatchDogPollTime)
# End of While
# Should never get here.
PrintMutex.acquire()
print("WatchDogThread: finished")
PrintMutex.release()
# End of WatchDogThread
# Cam1Thread
# Capture images from Camera1
def Cam1Thread():
global Cam1PhotoTime
PrintMutex.acquire()
print("Cam1Thread: starting")
PrintMutex.release()
cap = Camera1(0, selector)
cap.set_width(width)
cap.set_height(height)
cap.set_fps(fps)
cap.open()
while not cap.isOpened():
PrintMutex.acquire()
print("Cam1Thread: Can't open camera1, retry after 1 second")
PrintMutex.release()
time.sleep(1)
cap = Camera1(0, selector)
cap.set_width(width)
cap.set_height(height)
cap.set_fps(fps)
cap.open()
# End of While
while True:
with condition1:
condition1.wait()
ret, frame = cap.read()
if not ret:
if restart_times != 0:
PrintMutex.acquire()
print("Cam1Thread: Unable to read video frame")
PrintMutex.release()
success = False
for i in range(1, restart_times + 1):
PrintMutex.acquire()
print(f"Cam1Thread: reopen {i} times")
PrintMutex.release()
try:
cap.reStart()
success = True
break
except:
continue
# End of For
if success:
continue
else:
PrintMutex.acquire()
print("Cam1Thread: reopen failed")
PrintMutex.release()
# End of If - restart_times
# End of If - ret
cv2.imwrite(output_path + imagetime + "L.png", frame)
Cam1PhotoTime = time.time()
PrintMutex.acquire()
print("Cam1Thread: Captured " + imagetime)
PrintMutex.release()
key = cv2.waitKey(1)
if key == ord("q"):
break
# End of With
# End of While
cap.release()
PrintMutex.acquire()
print("Cam1Thread: finished")
PrintMutex.release()
# End of Cam1Thread
# Cam2Thread
# Capture images from Camera2
def Cam2Thread():
global Cam2PhotoTime
PrintMutex.acquire()
print("Cam2Thread: starting")
PrintMutex.release()
cap = Camera2(2, selector)
cap.set_width(width)
cap.set_height(height)
cap.set_fps(fps)
cap.open()
while not cap.isOpened():
PrintMutex.acquire()
print("Cam2Thread: Can't open camera1, retry after 1 second")
PrintMutex.release()
time.sleep(1)
cap = Camera2(2, selector)
cap.set_width(width)
cap.set_height(height)
cap.set_fps(fps)
cap.open()
# End of While
while True:
with condition2:
condition2.wait()
ret, frame = cap.read()
if not ret:
if restart_times != 0:
PrintMutex.acquire()
print("Cam2Thread: Unable to read video frame")
PrintMutex.release()
success = False
for i in range(1, restart_times + 1):
PrintMutex.acquire()
print(f"Cam2Thread: reopen {i} times")
PrintMutex.release()
try:
cap.reStart()
success = True
break
except:
continue
# End of For
if success:
continue
else:
PrintMutex.acquire()
print("Cam2Thread: reopen failed")
PrintMutex.release()
# End of If - restart_times
# End of If - ret
cv2.imwrite(output_path + imagetime + "R.png", frame)
Cam2PhotoTime = time.time()
PrintMutex.acquire()
print("Cam2Thread: Captured "+imagetime)
PrintMutex.release()
key = cv2.waitKey(1)
if key == ord("q"):
break
# End of With
# End of While
cap.release()
PrintMutex.acquire()
print("Cam2Thread: finished")
PrintMutex.release()
# End of Cam2Thread
# TimerThread
def TimerThread():
global imagetime
PrintMutex.acquire()
print("TimerThread: starting")
PrintMutex.release()
# Synchronize thread execution.
NextTime = time.time() + SnapTime # next photo snap time
while True:
imagetime = nameCurrTime()
# Notify both threads to snap a photo
with condition1:
condition1.notify() # notify Cam1Thread
# End of With
with condition2:
condition2.notify() # notify Cam2Thread
# End of With
# Sync to next photo snap time
while time.time() < NextTime:
time.sleep(0.1)
# End of While
NextTime = NextTime + SnapTime
# End of while
# Should not get here...
PrintMutex.acquire()
print("TimerThread: finished")
PrintMutex.release()
# End of TimerThread
# main()
# Main calling function.
def main():
# Setup GPIO to send heartbeat to watchdog MCU
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
# Create threads
t0 = Thread(target=WatchDogThread, args=())
t1 = Thread(target=Cam1Thread, args=())
t2 = Thread(target=Cam2Thread, args=())
t3 = Thread(target=TimerThread, args=())
# Start threads
print("main: starting threads")
t0.start() # Start WatchDogThread
t1.start() # Start Cam1Thread
t2.start() # Start Cam2Thread
t3.start() # Start TimerThread
# Wait for threads to finish
PrintMutex.acquire()
print("main: waiting for threads to finish")
PrintMutex.release()
#t0.join() # WatchDogThread
t1.join() # Cam1Thread
t2.join() # Cam2Thread
#t3.join() # TimerThread
print("main: exiting")
# End of main
# Execute main()
if __name__ == "__main__":
sys.exit(main())