-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenPathViewServer.py
More file actions
executable file
·376 lines (322 loc) · 12.6 KB
/
openPathViewServer.py
File metadata and controls
executable file
·376 lines (322 loc) · 12.6 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#!/usr/bin/python3.2
import RPi.GPIO as gpio
import os
import sys
import json
import time
import battery
import threading
sys.path.append(os.path.realpath(__file__)[:os.path.realpath(__file__).rfind("/")]+"/includes")
sys.path.append(os.path.realpath(__file__)[:os.path.realpath(__file__).rfind("/")]+"/includes/quick2wire-python-api/")
import gps, goPro, compas, color, config
from plugins import Manager
socketport=9876
EARTH_RADIUS = 6367.445 #found on da internet m
class OpenPathViewServer(threading.Thread):
"""
main class of the project, manage the whole thing
"""
def __init__(self):
"""
init the server
"""
print(color.OKBLUE+"Initializing main server...",color.ENDC)
threading.Thread.__init__(self)
self.keepAlive = threading.Event()
self.keepAlive.set()
self.config = config.Config()
if not os.path.isfile("picturesInfo.csv"):
os.system("""echo "time; lat; lon; alt; rad; goProFailed" >> picturesInfo.csv""")
if not os.path.isfile("picturesInfo_secondaryGPS.csv"):
os.system("""echo "time; lat; lon; alt; rad; goProFailed" >> picturesInfo_secondaryGPS.csv""")
self.autoMode = threading.Event()
self.autoModeTimed = threading.Event()
self.autoModeTimeIntervalSec = 2.5 # default 2.5 seconds
self.distPhoto = 5
self.configAutoModeLock = threading.Lock()
self.configAutoModeTimedLock = threading.Lock()
self.configOnOffLock = threading.Lock()
self.gps_secondary = gps.Gps(self, "/dev/ttyAMA0", baudrate=115200, trackfull_path="track_full_secondary",debug=False)
self.gps = gps.Gps(self, self.config.get("MAIN_GPS_SERIAL"), baudrate=115200, trackfull_path="track_full_main",debug=False)
self.lastLatLon = self.gps.getDegCoord()
self.lastLatLon_secondary = self.gps_secondary.getDegCoord()
self.compas = compas.Compas(self)
self.gopro = goPro.GoPro(self)
self.battery = battery.Battery(self)
self.interfaces = Manager(self)
self.geoInfoUpdateThread = threading.Thread(target = self.sendGeoInfoUpdateThread)
self.geoInfoUpdateThread.daemon = True
self.geoInfoUpdateThread.start()
print(color.OKGREEN+"Main server initialized",color.ENDC)
def isAutoMode(self):
"""
return True if autoMode is enabled
"""
return self.autoMode.isSet()
def takePic(self):
"""
take a pictures
"""
print(color.OKBLUE+"Taking picture",color.ENDC)
if not self.gps.isConnected():
raise gps.gpsError("gps not connected")
self.gopro.takePhoto()
# def rafale(self):
# """
# Takes 10 pictures, only for test purposes.
# """
# self.gopro.rafale()
def __turnOnThread(self):
"""
the Thread that turn the GoPro on
"""
print(color.OKBLUE+"Turning GoPro on",color.ENDC)
if self.configOnOffLock.acquire(blocking=False):#if the Lock is unlocked
if self.gopro.turnOn():
self.interfaces.notif(True)
else:
self.interfaces.notif(False)
self.configOnOffLock.release()
return
else:
return
def __turnOffThread(self):
"""
the Thread that turn the GoPro off
"""
print(color.OKBLUE+"Turning GoPro off",color.ENDC)
if self.configOnOffLock.acquire(blocking=False):#if the Lock is unlocked
if self.gopro.turnOff():
self.interfaces.notif(True)
else:
self.interfaces.notif(False)
self.configOnOffLock.release()
else:
return
def turnOn(self):
"""
turn all GoPro On
"""
thread = threading.Thread(target = self.__turnOnThread)
thread.daemon=True
thread.start()
def turnOff(self):
"""
turn all GoPro Off
"""
thread = threading.Thread(target = self.__turnOffThread)
thread.daemon=True
thread.start()
def goproPowerOn(self):
"""
Power all GoPro On
"""
gpio.output(26, gpio.HIGH)
def goproPowerOff(self):
"""
Power all GoPro Off
"""
gpio.output(26, gpio.LOW)
def statut(self,succes,goProFailed="000000"):
"""
act depending of the succes of photo taking
goProFailed give the list of goPro wich failed to take a picture
0 means no fail, the left one is goPro6 and the right is goPro1
"""
rad = self.compas.getHeading()
lat,lon = self.gps.getDegCoord()
lat_secondary, lon_secondary = self.gps_secondary.getDegCoord()
alt = self.gps.getAltitude()
alt_secondary = self.gps_secondary.getAltitude()
os.system(
"""echo "{}; {:f}; {:f}; {:f}; {}; {}" >> picturesInfo.csv""".format(time.asctime(), lat, lon, alt, rad,
goProFailed))
os.system("""echo "{}; {:f}; {:f}; {:f}; {}; {}" >> picturesInfo_secondaryGPS.csv""".format(time.asctime(),
lat_secondary,
lon_secondary,
alt_secondary, rad,
goProFailed))
if succes:
self.interfaces.newPanorama(True,latitude=lat,longitude=lon,altitude=alt,heading=rad)
else:
self.interfaces.newPanorama(False,goProFailed = goProFailed)
def canMove(self):
"""
tell all the interfaces the user can move
"""
self.interfaces.canMove()
def __automodeThread(self):
"""
a thread taking picture when autoMode is set
"""
print(color.OKBLUE+"starting autoMode"+color.ENDC)
if not self.autoMode.isSet():
self.autoMode.set()
while self.autoMode.isSet() and self.keepAlive.isSet():
lat, lon = self.lastLatLon
distance = self.gps.calculateDist(lat,lon)
if distance != None:
if distance>=self.distPhoto:
lastLatLon=self.gps.getDegCoord()
if not None in lastLatLon:
self.takePic()
self.lastLatLon = lastLatLon
print(color.OKBLUE+"stoping autoMode"+color.ENDC)
else:
print(color.WARNING+"Error : autoMode already set"+color.ENDC)
def __automodeTimedThread(self):
"""
Timed auto mode thread.
:return:
"""
print(color.OKBLUE + "starting Timed Auto Mode" + color.ENDC)
if not self.autoModeTimed.isSet():
self.autoModeTimed.set()
while self.autoModeTimed.isSet() and self.keepAlive.isSet():
self.takePic()
time.sleep(self.autoModeTimeIntervalSec)
print(color.OKBLUE + "stoping Timed auto mode" + color.ENDC)
else:
print(color.WARNING + "Error : timed auto mode already set" + color.ENDC)
def run(self):
"""
control thread : display information and allow command in the same time
"""
while self.keepAlive.isSet():
time.sleep(1)
def setAuto(self,frequMetre=None):
"""
set auto mode parameters, None mean Off
"""
self.configAutoModeLock.acquire()
if frequMetre:
print("Setting auto mode for",frequMetre,"metres")
self.distPhoto = frequMetre
if not self.autoMode.isSet():
automodeThread = threading.Thread(target=self.__automodeThread)
automodeThread.daemon=True
automodeThread.start()
else:
print("Disabling automode")
self.autoMode.clear()
self.configAutoModeLock.release()
def setAutoTimed(self,intervalSec=None):
"""
set auto mode parameters, None mean Off
"""
self.configAutoModeTimedLock.acquire()
if intervalSec:
print("Setting timed auto mode for",intervalSec," seconds")
self.autoModeTimeIntervalSec = intervalSec
if not self.autoModeTimed.isSet():
automodeTimedThread = threading.Thread(target=self.__automodeTimedThread)
automodeTimedThread.daemon=True
automodeTimedThread.start()
else:
print("Disabling timed auto mode")
self.autoModeTimed.clear()
self.configAutoModeTimedLock.release()
def socketHandler(self,socketSource,msg):
"""
will be called by socket when they receiving a message
"""
try:
msg = json.loads(msg)
except ValueError:
print(color.FAIL, "Error decoding json from socket", color.ENDC)
return
if "action" in msg:
if msg["action"]=="takepic":
self.takePic()
elif msg["action"]=="turnon":
self.turnOn()
elif msg["action"]=="turnoff":
self.turnOff()
elif msg["action"]=="gopropoweron":
self.goproPowerOn()
elif msg["action"]=="gopropoweroff":
self.goproPowerOff()
# elif msg["rafale"]=="rafale":
# self.rafale()
else:
print(color.WARNING,"WARNING : no correct action found in socket json",color.ENDC)
if "set" in msg:
if msg["set"]=="automode":
try:
if msg["dist"]:
self.setAuto(int(msg["dist"]))
else:
self.setAuto()
except KeyError:
print(color.FAIL,"Error : automode configuration fail",color.ENDC)
elif msg["set"]=="automodetimed":
try:
if msg["intervalSec"]:
self.setAutoTimed(float(msg["intervalSec"]))
else:
self.setAutoTimed()
except KeyError:
print(color.FAIL,"Error : automode timed configuration fail",color.ENDC)
else:
print(color.WARNING,"WARNING : no correct setting field found in socket json",color.ENDC)
def sendGeoInfoUpdateThread(self):
"""
keep interfaces up to date with geolocalisation info
"""
while self.keepAlive.isSet():
lat,lon,alt,rad = self.getLatLonAltRad()
battery_volatge = self.battery.getBatteryVoltage()
self.interfaces.setGeoInfo(lat,lon,alt,rad)
self.interfaces.setBatteryInfo(battery_volatge)
time.sleep(0.05)
def getLatLonAltRad(self):
"""
return latitude, longitude and heading in degree
"""
lat, lon = self.gps.getDegCoord()
alt = self.gps.getAltitude()
return lat,lon,alt,self.compas.getHeading().replace("°"," deg")
def updateAllConfigInfo(self):
"""
send config info to all connected devices
"""
self.interfaces.setModeInfo(self.isAutoMode(),self.distPhoto)
def stop(self):
"""
stop all subThread
"""
print(color.OKBLUE+"Stopping server...",color.ENDC)
self.gps.stop()
self.gps_secondary.stop()
self.gopro.stop()
self.interfaces.stop()
self.keepAlive.clear()
print(color.OKGREEN+"Server stopped",color.ENDC)
def __del__(self):
"""
destroyed need object
"""
print(color.WARNING+"Destroying server and its component...",color.ENDC)
for interface in self.interfaces:
interface.__del__()
self.gps.__del__()
self.gps_secondary.__del__()
self.gopro.__del__()
print(color.WARNING+"Server destroyed",color.ENDC)
if __name__ == "__main__":
os.system("clear")
# GPIO Test (Quentin)
gpio.setwarnings(False)
gpio.setmode(gpio.BCM)
gpio.setup(26, gpio.OUT)
server=OpenPathViewServer()
try:
server.start()
server.join()
except KeyboardInterrupt:
print("""Ctrl-C detected, you could have typed "stop",it would be less brutal...""")
os.system("reset")
# curses.endwin()
finally:
server.stop()
server.__del__()