-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update various logic for leaderboard run
- Loading branch information
MDP G14 RPI
committed
Oct 10, 2019
1 parent
441470a
commit ef0a69e
Showing
13 changed files
with
430 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_Store | ||
__pycache__/ | ||
logs/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,4 @@ def warning(self, msg): | |
logging.warning(msg) | ||
|
||
def error(self, msg): | ||
logging.error(msg) | ||
logging.error(msg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.