-
Notifications
You must be signed in to change notification settings - Fork 0
/
control_threading.py
224 lines (191 loc) · 5.75 KB
/
control_threading.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
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
# PI SPECIFIC
from threading import Condition, Thread
import time,random
import socket # for socket
import sys
import pickle
import os
from multiprocessing import Process, Pipe
import http.client
#Imports to communicate with Arduino
import serial
import RPi.GPIO as GPIO
import time
import datetime
IP = '192.168.4.1'
PORT = 22
PORT2 = 5900 # YOUDOSUIOAGKDSLAJG CHANGE THIS ALKSDJGIASODJGA
MAX_COMM = 5
# FOR ARDUINO COMMANDS
ser=serial.Serial("/dev/ttyACM0",115200, timeout = .75)
# FOR ARDUINO STATES
# ser=serial.Serial("/dev/ttyACM0",9600)
# CONDITION VARIABLES
given_instr = Condition()
given_state = Condition()
c = Condition() #This should be shared to all threads so that
running = 0 #in the case of phone disconnect, everything gets shut off
threads = []
dataObj = bytes(8)
def initGPIO():
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
GPIO.setup(12, GPIO.OUT)
GPIO.setup(19, GPIO.OUT)
class phone_Thread(Thread):
def __init__(self, name):
Thread.__init__(self)
self.name = name
def run(self):
global running
while True:
createSocket()
listen_tophone()
# SEND CAR STATE TO PHONE
def msg_tophone(state):
print("Message is being sent\n")
# SEND ITTTT
try:
t = socket.socket()
print("Socket successfully created")
except socket.error as err:
print ("socket creation failed with error %s" %(err))
# BINDS SOCKET
t.bind((IP,PORT2))
print("Socket successfully Binded")
t.send(state.encode('utf-8'))
# SEND INSTRUCTIONS TO CAR
def msg_tocar(data):
print("Message is being sent\n")
if (data == "11"):
do_goright()
while not [given_state]:
given_state.wait()
msg = "Turning right"
elif (data == "21"):
do_goleft()
while not [given_state]:
given_state.wait()
msg = "Turning left"
elif (data == "31"):
do_startcar()
while not [given_state]:
given_state.wait()
msg = "Starting"
elif (data == "30"):
do_stopcar()
while not [given_state]:
given_state.wait()
msg = "Stopping"
else:
msg = "Nothing"
# LISTENS FOR PHONE INSTRUCTIONS- shouldnt this run while phone is connected!?
# as soon as there are no instructions it breaks, we dont want to re run this all the time
def listen_tophone():
given_instr.acquire()
while not [given_instr]:
given_instr.wait()
while(True):
# SOCKET LISTEN MODE
s.listen(MAX_COMM)
print("Listening for Instructions\n")
conn, addr = s.accept()
dataObj = conn.recv(4096)
if(not dataObj):
running = 0
break
else:
data = (dataObj[3:4]) + (dataObj[4:5])
msg_tocar(data)
given_instr.wait()
# listens for state from the car
def listen_tocar():
print("Listening for Car State\n")
#state update given
given_state.acquire()
while not [given_state]:
given_state.wait()
# SEND STATE TO PHONE
# msg_to_phone(state)
given_state.notify()
# CREATES SOCKET FOR INSTRUCTION SENDING
def create_socket():
try:
s = socket.socket()
print("Socket successfully created")
running = 1
except socket.error as err:
print ("socket creation failed with error %s" %(err))
# BINDS SOCKET
s.bind((IP,PORT))
print("Socket successfully Binded")
# ACCEPT
conn, addr = s.accept()
print("Connected")
# CAR INSTRUCTIONSSSSSSSSSSSSSSSSSSSSS
def do_startcar():
print ("Starting Car")
ser.write('g'.encode('utf-8'))
def do_stopcar():
print ("Stoping Car")
ser.write('s'.encode('utf-8'))
def do_goleft():
print ("Turning Left")
ser.write('l'.encode('utf-8'))
def do_goright():
print ("Turning Right")
ser.write('r'.encode('utf-8'))
#Control Stream
def startStream(conn):
import os
pid = os.getpid()
conn.send(pid)
runStream = 0
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN)
while True:
runStream = conn.recv()
print("test")
if(runStream == 1):
from VideoStream import stream
elif(runStream == -1):
break
finally:
GPIO.cleanup()
#Send signal to Arduino to send data and main fucntion to collect data
def sendInterupt(conn):
import time
import os
import RPi.GPIO as GPIO
pid = os.getpid()
run = 0
prevMsg = ""
conn.send(pid)
try:
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(22, GPIO.IN)
GPIO.setup(9, GPIO.IN)
while(GPIO.input(22) == True):
while (GPIO.input(9) == True):
GPIO.output(18, GPIO.HIGH)
GPIO.output(18, GPIO.LOW)
time.sleep(.1)
print("FML")
finally:
GPIO.cleanup()
#main function to call threads
if __name__ == '__main__':
#Start socket and GPIO pins
initGPIO()
while True:
create_socket()
GPIO.output(25, GPIO.HIGH)
GPIO.output(12, GPIO.HIGH)
#pVS_conn.send(1)
while running:
GPIO.output(19, GPIO.HIGH)
# INITIALIZE CONDITION VARIABLES
# START THREADS