Skip to content

Commit

Permalink
Update various logic for leaderboard run
Browse files Browse the repository at this point in the history
  • Loading branch information
MDP G14 RPI committed Oct 10, 2019
1 parent 441470a commit ef0a69e
Show file tree
Hide file tree
Showing 13 changed files with 430 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
__pycache__/
logs/*
6 changes: 4 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os

from src.communicator.MultiThread import MultiThread
# 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
from src.Logger import Logger

Expand All @@ -9,7 +11,7 @@
def init():
os.system("sudo hciconfig hci0 piscan")
try:
multi_thread = MultiThread()
multi_thread = MultiProcess()
multi_thread.start()

# Currently SymbolDetector seems to be running really slow
Expand Down
68 changes: 68 additions & 0 deletions playgrounds/Multiprocess_comm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from multiprocessing import Process, Queue
import queue
import os
import cv2 as cv
import time

from src.Logger import Logger
from src.communicator.PC import PC

log = Logger()

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

self.pc_queue = Queue()

def start(self):

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

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):
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))

mp = MultiThread()
mp.start()

131 changes: 131 additions & 0 deletions playgrounds/Multithread_backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import _thread
import queue
import os
import cv2 as cv
import time

from src.Logger import Logger
from src.detector.SymbolDetector import SymbolDetector
from src.communicator.Arduino import Arduino
from src.communicator.PC import PC
from src.communicator.Android import Android

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.detector = SymbolDetector()

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

self.android_queue = queue.Queue(maxsize= 0)
self.arduino_queue = queue.Queue(maxsize=0)
self.pc_queue = queue.Queue(maxsize=0)

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.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.detect_symbols, (self.android_queue,))

log.info('Multithread Communication Session Started')

while True:
pass

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

def read_android(self, pc_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 == 'SV':
# arduino_queue.put_nowait(msg)

except Exception as e:
log.error("Android read failed: " + str(e))
self.android.connect()

def write_android(self, android_queue):
while True:
if not android_queue.empty():
try:
msg = android_queue.get_nowait()
self.android.write(msg)
# log.info('Write Android: ' + str(msg))
except Exception as e:
log.error("Android write failed " + str(e))
self.android.connect()

def read_arduino(self, pc_queue):
while True:
msg = self.arduino.read()
if msg is not None and msg != "Connected":
log.info('Read Arduino: ' + str(msg))
pc_queue.put_nowait(msg)

def write_arduino(self, arduino_queue):
while True:
if not arduino_queue.empty():
msg = arduino_queue.get_nowait()
self.arduino.write(msg)
log.info('Write Arduino: ' + str(msg))

def read_pc(self, android_queue, arduino_queue):
while True:
msg = self.pc.read()
if msg is not None:
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):
while True:
if not pc_queue.empty():
msg = pc_queue.get_nowait()
self.pc.write(msg)
log.info('Write PC: ' + str(msg))

def detect_symbols(self, android_queue):
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))

6 changes: 6 additions & 0 deletions playgrounds/arduino_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
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')
Expand Down
53 changes: 53 additions & 0 deletions playgrounds/multiprocess_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import _thread
import queue
import os
import time

from multiprocessing import Process

'''
Multithreading: _thread.start_new_thread(function, args[, kwargs])
'''

def test_thread(name, delay):
while True:
time.sleep(delay)
print('%s Ping' % (name))


class Multiprocess:
def __init__(self):
print('Init Multithread')
self.run = False

def start(self):
print('Begin Multithread Session')
self.run = True
# _thread.start_new_thread(test_thread, ('Thread 1', 1))
# _thread.start_new_thread(test_thread, ('Thread 2', 2))
# _thread.start_new_thread(test_thread, ('Thread 3', 4))
x1 = Process(target=test_thread, args=('Thread 1', 1))
x2 = Process(target=test_thread, args=('Thread 2', 2))
x3 = Process(target=test_thread, args=('Thread 3', 4))

x1.start()
x2.start()
x3.start()

print('Multiprocess Started')

def end(self):
print('End Multithread Session')
self.run = False

mp = Multiprocess()
mp.start()

while mp.run:
try:
pass
except KeyboardInterrupt:
mt.end()



1 change: 1 addition & 0 deletions playgrounds/multithread_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def start(self):
_thread.start_new_thread(test_thread, ('Thread 1', 1))
_thread.start_new_thread(test_thread, ('Thread 2', 2))
_thread.start_new_thread(test_thread, ('Thread 3', 4))
print('Multithread Started')

def end(self):
print('End Multithread Session')
Expand Down
9 changes: 9 additions & 0 deletions playgrounds/pc_command.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from src.communicator.PC import PC
import time

pc = PC()
pc.connect()

while True:
command = input("Enter command to send to PC:")
pc.write(command)
2 changes: 1 addition & 1 deletion src/Logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def warning(self, msg):
logging.warning(msg)

def error(self, msg):
logging.error(msg)
logging.error(msg)
3 changes: 2 additions & 1 deletion src/communicator/Arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from src.config import SERIAL_PORT
from src.config import BAUD_RATE
from src.config import LOCALE
from src.communicator.utils import ardMsgParser

log = Logger()

Expand Down Expand Up @@ -57,7 +58,7 @@ def read(self):
try:
msg = self.connection.readline().strip().decode(LOCALE)
if len(msg) > 0:
return msg
return ardMsgParser(msg)
return None
except Exception as error:
log.error('Arduino read failed: ' + str(error))
Loading

0 comments on commit ef0a69e

Please sign in to comment.