-
Notifications
You must be signed in to change notification settings - Fork 1
/
executor_raspberry.py
156 lines (131 loc) · 3.79 KB
/
executor_raspberry.py
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
import picamera
import io
import time
from config import config_get
from threading import Lock, Condition, Thread
class CameraWorker:
def __init__(self):
self.frame_cd = Condition()
self.frame = False
self.keep_working = True
self.worker = Thread(target=self.work)
self.worker.start()
def work(self):
camera = picamera.PiCamera()
camera.hflip = config_get("raspberry_horizontal_flip")
camera.vflip = config_get("raspberry_vertical_flip")
camera.resolution = (
config_get("raspberry_resolution_x"),
config_get("raspberry_resolution_y")
)
camera.start_preview()
time.sleep(2)
stream = io.BytesIO()
released = True
try:
for notUsed in camera.capture_continuous(stream, format='jpeg', resize=None, quality=config_get("raspberry_base_quality")):
self.frame_cd.acquire()
released = False
self.frame = stream.getvalue()
self.frame_cd.notify_all()
self.frame_cd.release()
released = True
stream.seek(0)
stream.truncate()
if not self.keep_working:
break
finally:
camera.close()
if not released: self.frame_cd.release()
def close(self):
self.keep_working = False
self.worker.join()
def get_picture(self):
self.frame_cd.acquire()
self.frame_cd.wait(5)
frame_cpy = self.frame
self.frame_cd.release()
return frame_cpy
def stream(self):
try:
self.frame_cd.acquire()
while self.keep_working:
self.frame_cd.wait(5)
yield self.frame
finally:
self.frame_cd.release()
class LazySingleton:
def __init__(self, builder, destroyer = False):
self.instance = False
self.references = 0
self.builder = builder
self.destroyer = destroyer
self.mutex = Lock()
def get(self):
self.references += 1
if not self.instance:
self.mutex.acquire()
try:
if not self.instance:
self.instance = self.builder()
finally:
self.mutex.release()
return self.instance
def release(self):
self.references -= 1
if self.references <= 0:
self.mutex.acquire()
try:
if self.references <= 0:
if self.destroyer: self.destroyer(self.instance)
self.instance = False
finally:
self.mutex.release()
class ExecutorRaspberry:
def __init__(self, panServoId=False, tiltServoId=False):
self.panServoId = panServoId
self.tiltServoId = tiltServoId
self.camera = LazySingleton(
builder = lambda: CameraWorker(),
destroyer = lambda w: w.close()
)
def video_stream(self):
cam = self.camera.get()
try:
for frame in cam.stream():
yield "Content-Type: image/jpeg\r\n"
yield "Content-length: "+ str(len(frame)) + "\r\n\r\n"
yield frame
yield "\r\n\r\n\r\n"
yield "--BoundaryString\r\n"
finally:
self.camera.release()
def snapshot(self):
cam = self.camera.get()
try:
return cam.get_picture()
finally:
self.camera.release()
def stop(self):
pass
def center(self):
self.servo_write(self.panServoId, config_get("pan_center"))
self.servo_write(self.tiltServoId, config_get("tilt_center"))
def up(self):
self.servo_write(self.tiltServoId, "-" + config_get("tilt_step"))
def down(self):
self.servo_write(self.tiltServoId, "+" + config_get("tilt_step"))
def left(self):
self.servo_write(self.panServoId, "+" + config_get("pan_step"))
def right(self):
self.servo_write(self.panServoId, "-" + config_get("pan_step"))
def vertical_patrol(self):
pass
def horizontal_patrol(self):
pass
def servo_write(self, servoId, value):
if not servoId or not value: return
f = open('/dev/servoblaster', 'w')
if f:
f.write('%d=%s\n' % (servoId, value))
f.close()