Skip to content

Commit

Permalink
Various refactors, mainly new multiprocess script
Browse files Browse the repository at this point in the history
  • Loading branch information
MDP G14 RPI committed Nov 7, 2019
1 parent a0626ae commit d2f96e6
Show file tree
Hide file tree
Showing 16 changed files with 300 additions and 142 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ Ensure that OpenCV 3.3.0 is installed

Begins a multithread session that will establish communications with N7 Tablet, Arduino and PC. Also starts a video stream that will attempt to detect symbols in front of it. Program will conclude the ID of the detected symbol depending on an arbitrary threshold value.
Still a work in progress - multithread communication is commented out for now to test the detection alone.

## Connecting a new bluetooth device
`sudo hciconfig hci0 piscan`

`hcitool scan`
24 changes: 10 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
import os

# Using Multiprocess instead of Multithread to employ all 4 cores of RPI
# from src.communicator.MultiThread import MultiThread
from src.communicator.MultiProcess import MultiProcess
# from src.detector.SymbolDetector import SymbolDetector
import argparse
from src.communicator.MultiThread import MultiThread
# from src.communicator.MultiProcess import MultiProcess
from src.communicator.MultiProcess_v2 import MultiProcess
from src.Logger import Logger

log = Logger()

parser = argparse.ArgumentParser(description='Main Program for MDP')
parser.add_argument('-v', '--verbose', const=True, default=False, nargs='?')

def init():
args = parser.parse_args()
verbose = args.verbose
os.system("sudo hciconfig hci0 piscan")
try:
multi_thread = MultiProcess()
multi_thread = MultiProcess(verbose)
multi_thread.start()

# Currently SymbolDetector seems to be running really slow
# on a thread. So if just testing SymbolDetector, comment and
# uncomment respectively the code above and below

# detector = SymbolDetector()
# detector.detect()
except KeyboardInterrupt:
multi_thread.end()
# detector.end()

if __name__ == '__main__':
init()
63 changes: 19 additions & 44 deletions playgrounds/Multiprocess_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,63 +5,38 @@
import time

from src.Logger import Logger
from src.communicator.PC import PC
from src.detector.VideoStream import VideoStream

log = Logger()

class MultiThread:
def __init__(self):
log.info('Initializing Multithread Communication')
self.pc = PC()
self.pc.connect()

self.pc_queue = Queue()
self.vid = VideoStream((540, 480), 30)

def start(self):

read_pc = Process(target=self.read_pc, args=(self.pc_queue,))
read_pc.start()
self.vid.start()
time.sleep(3)

Process(target=self.detect_symbols, args=()).start()
while True:
# but this is fine
print('ff: ' + str(self.vid.read()[0][0]))
time.sleep(2)

write_pc = Process(target=self.write_pc, args=(self.pc_queue,))
write_pc.start()

def end(self):
log.info('Multithread Communication Session Ended')

def read_pc(self, pc_queue):
print('read read')
while True:
msg = self.pc.read()
if msg is not None:
print('Received: ' + str(msg))
pc_queue.put_nowait(msg['payload'])
'''
log.info('Read PC: ' + str(msg['target']) + '; ' + str(msg['payload']))
if msg['target'] == 'android':
android_queue.put_nowait(msg['payload'])
elif msg['target'] == 'arduino':
arduino_queue.put_nowait(msg['payload'])
elif msg['target'] == 'both':
android_queue.put_nowait(msg['payload']['android'])
arduino_queue.put_nowait(msg['payload']['arduino'])
'''

def write_pc(self, pc_queue):
print('write write')
while True:
if not pc_queue.empty():
msg = pc_queue.get_nowait()
print('Write: ' + str(msg))
self.pc.write(msg)
log.info('Write PC: ' + str(msg))

def detect_symbols(self, android_queue):
def detect_symbols(self):
while True:
frame = self.detector.get_frame()
symbol_match = self.detector.detect(frame)
if symbol_match is not None:
print('Symbol Match ID: ' + str(symbol_match))
android_queue.put_nowait('SID|' + str(symbol_match))
# frame = self.vid.read()
# Weird problem that .read() does not update frame
print('frame:' + str(self.vid.read()[0][0]))
time.sleep(2)
# symbol_match = self.detector.detect(frame)
# if symbol_match is not None:
# print('Symbol Match ID: ' + str(symbol_match))
# android_queue.put_nowait('SID|' + str(symbol_match))

mp = MultiThread()
mp.start()
Expand Down
36 changes: 15 additions & 21 deletions playgrounds/Multithread_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,17 @@

log = Logger()

'''
Multithreading essentially refers to running multiple processes in parallel
Communications between Rpi and other devices involve a session, which means
the Rpi will be waiting for a trigger. Hence if single threaded, Rpi can only
do one thing at one time. With multhreading, Rpi can have multiple sessions
simultaneuously. Image Recognition will have to be run as a thread as well, but
it seems to be running really slow at the moment. There's also an occasional
error: Camera component couldn't be enabled: Out of resources. If hit that error
just restart the process, wait a little for the resources to be released, then re
run the main program
'''

class MultiThread:
def __init__(self):
log.info('Initializing Multithread Communication')
# self.android = Android()
self.arduino = Arduino()
self.pc = PC()
# self.arduino = Arduino()
# self.pc = PC()
self.detector = SymbolDetector()

# self.android.connect()
self.arduino.connect()
self.pc.connect()
# self.arduino.connect()
# self.pc.connect()

self.android_queue = queue.Queue(maxsize= 0)
self.arduino_queue = queue.Queue(maxsize=0)
Expand All @@ -44,14 +32,14 @@ def start(self):
self.detector.start()

# _thread.start_new_thread(self.read_android, (self.pc_queue,))
_thread.start_new_thread(self.read_arduino, (self.pc_queue,))
_thread.start_new_thread(self.read_pc,(self.android_queue, self.arduino_queue,))
# _thread.start_new_thread(self.read_arduino, (self.pc_queue,))
# _thread.start_new_thread(self.read_pc,(self.android_queue, self.arduino_queue,))

# _thread.start_new_thread(self.write_android, (self.android_queue,))
_thread.start_new_thread(self.write_arduino, (self.arduino_queue,))
_thread.start_new_thread(self.write_pc, (self.pc_queue,))
# _thread.start_new_thread(self.write_arduino, (self.arduino_queue,))
# _thread.start_new_thread(self.write_pc, (self.pc_queue,))

# _thread.start_new_thread(self.detect_symbols, (self.android_queue,))
_thread.start_new_thread(self.detect_symbols, (self.android_queue,))

log.info('Multithread Communication Session Started')

Expand Down Expand Up @@ -124,8 +112,14 @@ def write_pc(self, pc_queue):
def detect_symbols(self, android_queue):
while True:
frame = self.detector.get_frame()
print(frame[0][0])
time.sleep(2)
'''
symbol_match = self.detector.detect(frame)
if symbol_match is not None:
print('Symbol Match ID: ' + str(symbol_match))
android_queue.put_nowait('SID|' + str(symbol_match))
'''

mt = MultiThread()
mt.start()
12 changes: 7 additions & 5 deletions playgrounds/android_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
android = Android()
android.connect()

# android.write("MDF|FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF|010002000400000000000043F0800100000003C08081000200080020F880100000000000040|N|18|1|0")
android.write("F|LLS4RS5RS4LS9S3")
# android.write("MDF|FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF|010002000400000000000043F0800100000003C08081000200080020F880100000000000040|N|18|1|15")
# android.write("F|LLS4RS5RS4LS9S3")
while True:
msg = input("Send to android: ")
android.write(msg)
'''
for status in statuses:
message = {'bot_status' : status}
Expand All @@ -31,6 +33,6 @@
android.write(json.dumps(message))
time.sleep(1)
'''
msg = android.read()
if msg is not None:
print('message from android: ' + str(msg))
# msg = android.read()
# if msg is not None:
# print('message from android: ' + str(msg))
13 changes: 8 additions & 5 deletions playgrounds/arduino_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
arduino = Arduino()
arduino.connect()

msg_read = "FP|(1,1,N);(2,1,N);(2,2,E)"
pc_msg = pcMsgParser(msg_read)

print(pc_msg)
# msg_read = "FP|(1,1,N);(2,1,N);(2,2,E)"
# pc_msg = pcMsgParser(msg_read)
# print(pc_msg)

while True:
'''
try:
msg = arduino.read()
if msg is not None:
print(msg)
except Exception as e:
pass
'''
command = input("Enter command to send to Arduino:")
'''
if command == 'demo':
print('Init demo')
arduino.write(pc_msg['payload']['arduino'])
else:
arduino.write(command)
'''
arduino.write(command)
13 changes: 9 additions & 4 deletions playgrounds/camera_test.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from picamera import PiCamera
from picamera.array import PiRGBArray
from time import sleep

camera = PiCamera()
camera.vflip = True
camera.start_preview()
sleep(10)
camera.stop_preview()
camera.resolution = (540, 480)
camera.framerate = 30
camera.vflip = False
camera.hflip = False

# camera.start_preview()
# sleep(10)
# camera.stop_preview()
4 changes: 2 additions & 2 deletions playgrounds/symbol_detect_test.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from src.detector.SymbolDetector import SymbolDetector
from imutils.video import FPS
detector = SymbolDetector()
detector = SymbolDetector(width=650, height=480, framerate=32)
detector.start()
fps = FPS().start()

frame_count = 0

while frame_count < 100:
while frame_count < 10000:
frame = detector.get_frame()
symbol_match = detector.detect(frame)
if symbol_match is not None:
Expand Down
2 changes: 1 addition & 1 deletion src/communicator/Android.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def read(self):
def write(self, message):
try:
self.client_sock.send(message)
log.info('Successfully wrote to Android: ' + str(message))
# log.info('Successfully wrote to Android: ' + str(message))
except Exception as error:
raise

Expand Down
57 changes: 37 additions & 20 deletions src/communicator/MultiProcess.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,40 @@ def __init__(self):
self.android_queue = Queue()
self.arduino_queue = Queue()
self.pc_queue = Queue()
self.detector_queue = Queue()

self.frame_count = 0

def start(self):
# self.detector.start()
self.detector.start()
time.sleep(3)

r_android = Process(target=self.read_android, args=(self.pc_queue, self.detector_queue,)).start()
w_android = Process(target=self.write_android, args=(self.android_queue,)).start()

r_arduino = Process(target=self.read_arduino, args=(self.pc_queue,)).start()
w_arduino = Process(target=self.write_arduino, args=(self.arduino_queue,)).start()

r_android = Process(target=self.read_android, args=(self.pc_queue,))
r_android.start()
w_android = Process(target=self.write_android, args=(self.android_queue,))
w_android.start()

r_arduino = Process(target=self.read_arduino, args=(self.pc_queue,))
r_arduino.start()
w_arduino = Process(target=self.write_arduino, args=(self.arduino_queue,))
w_arduino.start()

r_pc = Process(target=self.read_pc, args=(self.android_queue, self.arduino_queue,))
r_pc.start()
w_pc = Process(target=self.write_pc, args=(self.pc_queue,))
w_pc.start()

# symbol_detect = Process(target=self.detect_symbols, args=(self.android_queue,))
r_pc = Process(target=self.read_pc, args=(self.android_queue, self.arduino_queue,)).start()
w_pc = Process(target=self.write_pc, args=(self.pc_queue,)).start()

symbol_detect = Process(target=self.detect_symbols, args=(self.detector_queue, self.android_queue,)).start()
log.info('Multithread Communication Session Started')

def end(self):
self.detector.end()
log.info('Multithread Communication Session Ended')

def read_android(self, pc_queue):
def read_android(self, pc_queue, detector_queue):
while True:
try:
msg = self.android.read()
if msg is not None:
log.info('Read Android:' + str(msg))
pc_queue.put_nowait(msg)
if msg == 'TP':
detector_queue.put_nowait(msg)
else:
pc_queue.put_nowait(msg)

except Exception as e:
log.error("Android read failed: " + str(e))
Expand Down Expand Up @@ -119,11 +121,26 @@ def write_pc(self, pc_queue):
self.pc.write(msg)
log.info('Write PC: ' + str(msg))

def detect_symbols(self, android_queue):
def detect_symbols(self, detector_queue, android_queue):
while True:
if not detector_queue.empty():
msg = detector_queue.get_nowait()
if msg == 'TP':
print('Take photo')
frame = self.detector.get_frame()
self.frame_count = self.frame_count + 1
print(frame[0][0], self.frame_count)

# frame = self.detector.get_frame()
# self.frame_count = self.frame_count + 1
# print(frame[0][0], self.frame_count)
# cv.imshow("Video Stream", frame)
# key = cv.waitKey(1) & 0xFF
'''
frame = self.detector.get_frame()
symbol_match = self.detector.detect(frame)
if symbol_match is not None:
print('Symbol Match ID: ' + str(symbol_match))
android_queue.put_nowait('SID|' + str(symbol_match))
'''

Loading

0 comments on commit d2f96e6

Please sign in to comment.