/
Marvin_Pi_v0.31.py
181 lines (156 loc) · 4.44 KB
/
Marvin_Pi_v0.31.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
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
import wiringpi2
import time
import subprocess
from threading import *
from subprocess import call
#THIS IS THE PRIORITY IT WILL RUN AT
#wiringpi2.piHiPri(99)
fileLock = Lock()
arduinoOutLock = Lock()
arduinoInReady = Lock()
arduinoListenerReady = Event()
fileLocation = ""
arduinoIn = -1
arduinoOut = -1
flightPath = []
def fileOpen():
global fileLocation
fileLocation = "/home/pi/python/logs/" \
+ str(time.strftime('%c')) + "myLog.txt"
fileLock.acquire()
file = open(fileLocation,"w")
file.write("Flight Log (")
file.write(time.strftime("%c"))
file.write(")\n")
file.close()
fileLock.release()
def fileAppend(loc, message):
fileLock.acquire()
file = open(loc, "a")
file.write(message)
file.close()
fileLock.release()
def arduinoListen():
global wiringpi2
global arduinoIn
global arduino
wiringpi2.wiringPiSetup()
arduino = wiringpi2.serialOpen("/dev/ttyACM0", 9600)
wiringpi2.delay(250)
wiringpi2.pinMode(0,1)
wiringpi2.digitalWrite(0, 1)
arduinoInReady.acquire()
arduinoListenerReady.set()
while(True):
while(True):
if(wiringpi2.serialDataAvail(arduino) > 0):
arduinoIn = chr(wiringpi2.serialGetchar(arduino))
arduinoInReady.release()
time.sleep(0.5)
break
else:
time.sleep(0.5)
arduinoInReady.acquire()
def outputArduino(mes):
arduinoOutLock.acquire()
wiringpi2.serialPutchar(arduino, mes)
arduinoOutLock.release()
def handleFlight():
arduinoListenerReady.wait()
launch()
global arduinoIn
for command in flightPath:
command = command.split(":")
mode = command[0]
end = command[1]
if(mode == "HOVER_STEADY"):
arduino_Out = Thread(target = outputArduino, args = (72,)) #'H'
arduino_Out.start()
elif(mode == "LAND"):
arduino_Out = Thread(target = outputArduino, args = (76,)) #'L'
arduino_Out.start()
file_Out = Thread(target = fileAppend,
args = (fileLocation,"Landing Sequence begun at " \
+ str(time.time()) + "\n"))
file_Out.start()
elif(mode == "END"):
file_Out = Thread(target = fileAppend,
args = (fileLocation,"Shutdown Proceeding at " \
+ str(time.time()) + "\n"))
file_Out.start()
time.sleep(2)
subprocess.call(["sudo","shutdown","-h","now"])
if(end == "TIMER"):
time.sleep(eval(command[2]))
next
else:
while(True):
arduinoInReady.acquire()
if(arduinoIn == end):
arduinoIn = -1
arduinoInReady.release()
break
else:
arduinoIn = -1
arduinoInReady.release()
def launch():
global arduinoIn
while(True):
arduinoInReady.acquire()
if(arduinoIn == '!'):
arduino_Out = Thread(target = outputArduino, args = (82,)) #'R'
arduino_Out.start()
arduinoIn = -1
file_Out = Thread(target = fileAppend,
args = (fileLocation,"Handshake received at " \
+ str(time.time()) + "\n"))
file_Out.start()
arduinoInReady.release()
break
else:
arduinoIn = -1
arduinoInReady.release()
while(True):
arduinoInReady.acquire()
if(arduinoIn == 'r'):
arduino_Out = Thread(target = outputArduino, args = (84,)) #'T'
arduino_Out.start()
arduinoIn = -1
file_Out = Thread(target = fileAppend,
args = (fileLocation,"Takeoff Sequence begun at " \
+ str(time.time()) + "\n"))
file_Out.start()
arduinoInReady.release()
break
else:
arduinoIn = -1
arduinoInReady.release()
while(True):
arduinoInReady.acquire()
if(arduinoIn == 't'):
arduino_Out = Thread(target = outputArduino, args = (72,)) #'H'
arduino_Out.start()
arduinoIn = -1
file_Out = Thread(target = fileAppend,
args = (fileLocation,"Takeoff Sequence completed at " \
+ str(time.time()) + "\n"))
file_Out.start()
arduinoInReady.release()
break
else:
arduinoIn = -1
arduinoInReady.release()
def main():
#Setup
fileCreator = Thread(target = fileOpen)
fileCreator.start()
arduinoListenerReady.clear()
global flightPath
#change as desired
flightPath = ["HOVER_STEADY:TIMER:20", "LAND:l", "END:TIMER:5"]
inputHandler = Thread(target = handleFlight)
inputHandler.start()
arduinoListener = Thread(target = arduinoListen)
arduinoListener.start()
#----------------------------------------------------------------------------------------------------------------------------
main()