Exemplo n.º 1
0
    def __init__(self, verbose):
        log.info('Initializing Multiprocessing Communication')
        self.verbose = verbose
        self.android = Android()
        self.arduino = Arduino()
        self.pc = PC()
        self.detector = SymbolDetector()

        self.msg_queue = Queue()
        self.img_queue = Queue()
Exemplo n.º 2
0
    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)
        self.detector_queue = queue.Queue(maxsize=0)
Exemplo n.º 3
0
    def __init__(self, image_processing_server_url: str = None):
        """
        Instantiates a MultiProcess Communications session and set up the necessary variables.

        Upon instantiation, RPi begins connecting to
        - Arduino
        - Algorithm
        - Android
        in this exact order.

        Also instantiates the queues required for multiprocessing.
        """
        print('Initializing Multiprocessing Communication')

        self.arduino = Arduino()  # handles connection to Arduino
        self.algorithm = Algorithm()  # handles connection to Algorithm
        self.android = Android()  # handles connection to Android

        self.manager = DequeManager()
        self.manager.start()

        # messages from Arduino, Algorithm and Android are placed in this queue before being read
        self.message_deque = self.manager.DequeProxy()
        self.to_android_message_deque = self.manager.DequeProxy()

        self.read_arduino_process = Process(target=self._read_arduino)
        self.read_algorithm_process = Process(target=self._read_algorithm)
        self.read_android_process = Process(target=self._read_android)

        self.write_process = Process(target=self._write_target)
        self.write_android_process = Process(target=self._write_android)

        # the current action / status of the robot
        self.status = Status.IDLE  # robot starts off being idle

        self.dropped_connection = Value('i', 0)  # 0 - arduino, 1 - algorithm

        self.image_process = None

        if image_processing_server_url is not None:
            self.image_process = Process(target=self._process_pic)

            # pictures taken using the PiCamera are placed in this queue
            self.image_deque = self.manager.DequeProxy()

            self.image_processing_server_url = image_processing_server_url
            self.image_count = Value('i', 0)
Exemplo n.º 4
0
from src.communicator.Android import Android
import time
import json
import random

statuses = ['moving', 'stopped', 'turning', 'ready', 'completed']

symbol_ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

android = Android()
android.connect()

# 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}
        android.write(json.dumps(message))
        time.sleep(1)
    '''
    '''
    for symbol_id in symbol_ids:
        rand_x = random.randint(0, 15)
        rand_y = random.randint(0, 20)
        message = {
            'symbol_id' : symbol_id,
            'coordinate' : str(rand_x) + "," + str(rand_y)
        }
Exemplo n.º 5
0
class MultiProcessComms:
    """
    This class handles multi-processing communications between Arduino, Algorithm and Android.
    """
    def __init__(self, image_processing_server_url: str = None):
        """
        Instantiates a MultiProcess Communications session and set up the necessary variables.

        Upon instantiation, RPi begins connecting to
        - Arduino
        - Algorithm
        - Android
        in this exact order.

        Also instantiates the queues required for multiprocessing.
        """
        print('Initializing Multiprocessing Communication')

        self.arduino = Arduino()  # handles connection to Arduino
        self.algorithm = Algorithm()  # handles connection to Algorithm
        self.android = Android()  # handles connection to Android

        self.manager = DequeManager()
        self.manager.start()

        # messages from Arduino, Algorithm and Android are placed in this queue before being read
        self.message_deque = self.manager.DequeProxy()
        self.to_android_message_deque = self.manager.DequeProxy()

        self.read_arduino_process = Process(target=self._read_arduino)
        self.read_algorithm_process = Process(target=self._read_algorithm)
        self.read_android_process = Process(target=self._read_android)

        self.write_process = Process(target=self._write_target)
        self.write_android_process = Process(target=self._write_android)

        # the current action / status of the robot
        self.status = Status.IDLE  # robot starts off being idle

        self.dropped_connection = Value('i', 0)  # 0 - arduino, 1 - algorithm

        self.image_process = None

        if image_processing_server_url is not None:
            self.image_process = Process(target=self._process_pic)

            # pictures taken using the PiCamera are placed in this queue
            self.image_deque = self.manager.DequeProxy()

            self.image_processing_server_url = image_processing_server_url
            self.image_count = Value('i', 0)

    def start(self):
        try:
            self.arduino.connect()
            self.algorithm.connect()
            self.android.connect()

            print('Connected to Arduino, Algorithm and Android')

            self.read_arduino_process.start()
            self.read_algorithm_process.start()
            self.read_android_process.start()
            self.write_process.start()
            self.write_android_process.start()

            if self.image_process is not None:
                self.image_process.start()

            print(
                'Started all processes: read-arduino, read-algorithm, read-android, write, image'
            )

            print('Multiprocess communication session started')

        except Exception as error:
            raise error

        self._allow_reconnection()

    def end(self):
        # children processes should be killed once this parent process is killed
        self.algorithm.disconnect_all()
        self.android.disconnect_all()
        print('Multiprocess communication session ended')

    def _allow_reconnection(self):
        print('You can reconnect to RPi after disconnecting now')

        while True:
            try:
                if not self.read_arduino_process.is_alive():
                    self._reconnect_arduino()

                if not self.read_algorithm_process.is_alive():
                    self._reconnect_algorithm()

                if not self.read_android_process.is_alive():
                    self._reconnect_android()

                if not self.write_process.is_alive():
                    if self.dropped_connection.value == 0:
                        self._reconnect_arduino()
                    elif self.dropped_connection.value == 1:
                        self._reconnect_algorithm()

                if not self.write_android_process.is_alive():
                    self._reconnect_android()

                if self.image_process is not None and not self.image_process.is_alive(
                ):
                    self.image_process.terminate()

            except Exception as error:
                print("Error during reconnection: ", error)
                raise error

    def _reconnect_arduino(self):
        self.arduino.disconnect()

        self.read_arduino_process.terminate()
        self.write_process.terminate()
        self.write_android_process.terminate()

        self.arduino.connect()

        self.read_arduino_process = Process(target=self._read_arduino)
        self.read_arduino_process.start()

        self.write_process = Process(target=self._write_target)
        self.write_process.start()

        self.write_android_process = Process(target=self._write_android)
        self.write_android_process.start()

        print('Reconnected to Arduino')

    def _reconnect_algorithm(self):
        self.algorithm.disconnect()

        self.read_algorithm_process.terminate()
        self.write_process.terminate()
        self.write_android_process.terminate()

        self.algorithm.connect()

        self.read_algorithm_process = Process(target=self._read_algorithm)
        self.read_algorithm_process.start()

        self.write_process = Process(target=self._write_target)
        self.write_process.start()

        self.write_android_process = Process(target=self._write_android)
        self.write_android_process.start()

        print('Reconnected to Algorithm')

    def _reconnect_android(self):
        self.android.disconnect()

        self.read_android_process.terminate()
        self.write_process.terminate()
        self.write_android_process.terminate()

        self.android.connect()

        self.read_android_process = Process(target=self._read_android)
        self.read_android_process.start()

        self.write_process = Process(target=self._write_target)
        self.write_process.start()

        self.write_android_process = Process(target=self._write_android)
        self.write_android_process.start()

        print('Reconnected to Android')

    def _read_arduino(self):
        while True:
            try:
                raw_message = self.arduino.read()

                if raw_message is None:
                    continue
                message_list = raw_message.splitlines()

                for message in message_list:

                    if len(message) <= 0:
                        continue

                    self.message_deque.append(
                        self._format_for(ALGORITHM_HEADER, message + NEWLINE))

            except Exception as error:
                print('Process read_arduino failed: ' + str(error))
                break

    def _read_algorithm(self):
        while True:
            try:
                raw_message = self.algorithm.read()

                if raw_message is None:
                    continue

                message_list = raw_message.splitlines()

                for message in message_list:

                    if len(message) <= 0:
                        continue

                    elif message[0] == AlgorithmToRPi.TAKE_PICTURE:

                        if self.image_count.value >= 5:
                            self.message_deque.append(
                                self._format_for(
                                    ALGORITHM_HEADER,
                                    RPiToAlgorithm.DONE_IMG_REC + NEWLINE))

                        else:

                            message = message[2:-1]  # to remove 'C[' and ']'
                            # self.to_android_message_deque.append(
                            # RPiToAndroid.STATUS_TAKING_PICTURE + NEWLINE
                            # )
                            image = self._take_pic()
                            print('Picture taken')
                            self.message_deque.append(
                                self._format_for(
                                    ALGORITHM_HEADER,
                                    RPiToAlgorithm.DONE_TAKING_PICTURE +
                                    NEWLINE))
                            self.image_deque.append([image, message])

                    elif message == AlgorithmToRPi.EXPLORATION_COMPLETE:
                        # to let image processing server end all processing and display all images
                        self.status = Status.IDLE
                        self.image_deque.append(
                            [cv2.imread(STOPPING_IMAGE), "-1,-1|-1,-1|-1,-1"])

                        # self.to_android_message_deque.append(
                        # RPiToAndroid.STATUS_IDLE + NEWLINE
                        # )

                    elif message[0] == AlgorithmToAndroid.MDF_STRING:
                        self.to_android_message_deque.append(message[1:] +
                                                             NEWLINE)

                    else:  # (message[0]=='W' or message in ['D|', 'A|', 'Z|']):
                        #self._forward_message_algorithm_to_android(message)
                        self.message_deque.append(
                            self._format_for(ARDUINO_HEADER,
                                             message + NEWLINE))

            except Exception as error:
                print('Process read_algorithm failed: ' + str(error))
                break

    def _forward_message_algorithm_to_android(self, message):
        messages_for_android = message.split(MESSAGE_SEPARATOR)

        for message_for_android in messages_for_android:

            if len(message_for_android) <= 0:
                continue

            elif message_for_android[0] == AlgorithmToAndroid.TURN_LEFT:
                self.to_android_message_deque.append(RPiToAndroid.TURN_LEFT +
                                                     NEWLINE)

                # self.to_android_message_deque.append(
                # RPiToAndroid.STATUS_TURNING_LEFT + NEWLINE
                # )

            elif message_for_android[0] == AlgorithmToAndroid.TURN_RIGHT:
                self.to_android_message_deque.append(RPiToAndroid.TURN_RIGHT +
                                                     NEWLINE)

                # self.to_android_message_deque.append(
                # RPiToAndroid.STATUS_TURNING_RIGHT + NEWLINE
                # )

            # elif message_for_android[0] == AlgorithmToAndroid.CALIBRATING_CORNER:
            # self.to_android_message_deque.append(
            # RPiToAndroid.STATUS_CALIBRATING_CORNER + NEWLINE
            # )

            # elif message_for_android[0] == AlgorithmToAndroid.SENSE_ALL:
            # self.to_android_message_deque.append(
            # RPiToAndroid.STATUS_SENSE_ALL + NEWLINE
            # )

            # elif message_for_android[0] == AlgorithmToAndroid.ALIGN_RIGHT:
            # self.to_android_message_deque.append(
            # RPiToAndroid.STATUS_ALIGN_RIGHT + NEWLINE
            # )

            # elif message_for_android[0] == AlgorithmToAndroid.ALIGN_FRONT:
            # self.to_android_message_deque.append(
            # RPiToAndroid.STATUS_ALIGN_FRONT + NEWLINE
            # )

            elif message_for_android[0] == AlgorithmToAndroid.MOVE_FORWARD:
                # if self.status == Status.EXPLORING:
                # self.to_android_message_deque.append(
                # RPiToAndroid.STATUS_EXPLORING + NEWLINE
                # )

                # elif self.status == Status.FASTEST_PATH:
                # self.to_android_message_deque.append(
                # RPiToAndroid.STATUS_FASTEST_PATH + NEWLINE
                # )
                num_steps_forward = int(message_for_android.decode()[1:])

                # TODO
                print('Number of steps to move forward:', num_steps_forward)
                for _ in range(num_steps_forward):
                    self.to_android_message_deque.append(RPiToAndroid.MOVE_UP +
                                                         NEWLINE)

                    # self.to_android_message_deque.append(
                    # RPiToAndroid.STATUS_MOVING_FORWARD + NEWLINE
                    # )

    def _read_android(self):
        while True:
            try:
                raw_message = self.android.read()

                if raw_message is None:
                    continue

                message_list = raw_message.splitlines()

                for message in message_list:
                    if len(message) <= 0:
                        continue

                    elif message in (AndroidToArduino.ALL_MESSAGES +
                                     [AndroidToRPi.CALIBRATE_SENSOR]):
                        if message == AndroidToRPi.CALIBRATE_SENSOR:
                            self.message_deque.append(
                                self._format_for(
                                    ARDUINO_HEADER,
                                    RPiToArduino.CALIBRATE_SENSOR + NEWLINE))

                        else:
                            self.message_deque.append(
                                self._format_for(ARDUINO_HEADER,
                                                 message + NEWLINE))

                    else:  # if message in ['ES|', 'FS|', 'SendArena']:
                        if message == AndroidToAlgorithm.START_EXPLORATION:
                            self.status = Status.EXPLORING
                            self.message_deque.append(
                                self._format_for(
                                    ARDUINO_HEADER,
                                    RPiToArduino.START_EXPLORATION + NEWLINE))

                        elif message == AndroidToAlgorithm.START_FASTEST_PATH:
                            self.status = Status.FASTEST_PATH
                            self.message_deque.append(
                                self._format_for(
                                    ARDUINO_HEADER,
                                    RPiToArduino.START_FASTEST_PATH + NEWLINE))

                        self.message_deque.append(
                            self._format_for(ALGORITHM_HEADER,
                                             message + NEWLINE))

            except Exception as error:
                print('Process read_android failed: ' + str(error))
                break

    def _write_target(self):
        while True:
            target = None
            try:
                if len(self.message_deque) > 0:
                    message = self.message_deque.popleft()
                    target, payload = message['target'], message['payload']

                    if target == ARDUINO_HEADER:
                        self.arduino.write(payload)

                    elif target == ALGORITHM_HEADER:
                        self.algorithm.write(payload)

                    else:
                        print("Invalid header", target)

            except Exception as error:
                print('Process write_target failed: ' + str(error))

                if target == ARDUINO_HEADER:
                    self.dropped_connection.value = 0

                elif target == ALGORITHM_HEADER:
                    self.dropped_connection.value = 1

                self.message_deque.appendleft(message)

                break

    def _write_android(self):
        while True:
            try:
                if len(self.to_android_message_deque) > 0:
                    message = self.to_android_message_deque.popleft()

                    self.android.write(message)

            except Exception as error:
                print('Process write_android failed: ' + str(error))
                self.to_android_message_deque.appendleft(message)
                break

    def _take_pic(self):
        try:
            start_time = datetime.now()

            # initialize the camera and grab a reference to the raw camera capture
            camera = PiCamera(resolution=(IMAGE_WIDTH,
                                          IMAGE_HEIGHT))  # '1920x1080'
            rawCapture = PiRGBArray(camera)

            # allow the camera to warmup
            time.sleep(0.1)

            # grab an image from the camera
            camera.capture(rawCapture, format=IMAGE_FORMAT)
            image = rawCapture.array
            camera.close()

            print('Time taken to take picture: ' +
                  str(datetime.now() - start_time) + 'seconds')

            # to gather training images
            # os.system("raspistill -o images/test"+
            # str(start_time.strftime("%d%m%H%M%S"))+".png -w 1920 -h 1080 -q 100")

        except Exception as error:
            print('Taking picture failed: ' + str(error))

        return image

    def _process_pic(self):
        # initialize the ImageSender object with the socket address of the server
        image_sender = imagezmq.ImageSender(
            connect_to=self.image_processing_server_url)
        image_id_list = []
        while True:
            try:
                if not self.image_deque.empty():
                    start_time = datetime.now()

                    image_message = self.image_deque.popleft()
                    # format: 'x,y|x,y|x,y'
                    obstacle_coordinates = image_message[1]

                    reply = image_sender.send_image('image from RPi',
                                                    image_message[0])
                    reply = reply.decode('utf-8')

                    if reply == 'End':
                        break  # stop sending images

                    # example replies
                    # "1|2|3" 3 symbols in order from left to right
                    # "1|-1|3" 2 symbols, 1 on the left, 1 on the right
                    # "1" 1 symbol either on the left, middle or right
                    else:
                        detections = reply.split(MESSAGE_SEPARATOR)
                        obstacle_coordinate_list = obstacle_coordinates.split(
                            MESSAGE_SEPARATOR)

                        for detection, coordinates in zip(
                                detections, obstacle_coordinate_list):

                            if coordinates == '-1,-1':
                                continue  # if no obstacle, skip mapping of symbol id
                            elif detection == '-1':
                                continue  # if no symbol detected, skip mapping of symbol id
                            else:
                                id_string_to_android = '{"image":[' + coordinates + \
                                ',' + detection + ']}'
                                print(id_string_to_android)

                                if detection not in image_id_list:
                                    self.image_count.value += 1
                                    image_id_list.append(detection)

                                self.to_android_message_deque.append(
                                    id_string_to_android + NEWLINE)

                    print('Time taken to process image: ' + \
                        str(datetime.now() - start_time) + ' seconds')

            except Exception as error:
                print('Image processing failed: ' + str(error))

    def _format_for(self, target, payload):
        return {
            'target': target,
            'payload': payload,
        }
Exemplo n.º 6
0
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)
        self.detector_queue = queue.Queue(maxsize=0)

    def start(self):
        # self.detector.start()

        _thread.start_new_thread(self.read_android,
                                 (self.pc_queue, self.detector_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(target=self.read_android, args=(self.pc_queue, self.detector_queue,)).start()
        Thread(target=self.read_arduino, args=(self.pc_queue,)).start()
        Thread(target=self.read_pc, args=(self.android_queue, self.arduino_queue,)).start()

        Thread(target=self.write_android, args=(self.android_queue,)).start()
        Thread(target=self.write_arduino, args=(self.arduino_queue,)).start()
        Thread(target=self.write_pc, args=(self.pc_queue,)).start()
        '''
        # _thread.start_new_thread(self.detect_symbols, (self.detector_queue, 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, detector_queue):
        while True:
            try:
                msg = self.android.read()
                if msg is not None:
                    log.info('Read Android:' + str(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))
                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)
                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, detector_queue, android_queue):
        while True:
            frame = self.detector.get_frame()
            # print(frame[0][0])
            if not detector_queue.empty():
                # Try match confidence of 1 frame
                # If not good enough, will have to find a way to bump match_confidence
                msg = detector_queue.get_nowait()
                log.info('Detecting Symbols')
                symbol_match = self.detector.detect(frame)
                if symbol_match is not None:
                    log.info('Symbol Match ID: ' + str(symbol_match))
                    android_queue.put_nowait('SID|' + str(symbol_match))
                else:
                    log.info('No symbols detected in frame')
Exemplo n.º 7
0
class MultiProcess:
    def __init__(self, verbose):
        log.info('Initializing Multiprocessing Communication')
        self.verbose = verbose
        self.android = Android()
        self.arduino = Arduino()
        self.pc = PC()
        self.detector = SymbolDetector()

        self.msg_queue = Queue()
        self.img_queue = Queue()

    def start(self):
        try:
            self.android.connect()
            self.arduino.connect()
            self.pc.connect()

            Process(target=self.read_android, args=(self.msg_queue, )).start()
            Process(target=self.read_arduino, args=(self.msg_queue, )).start()
            Process(target=self.read_pc,
                    args=(
                        self.msg_queue,
                        self.img_queue,
                    )).start()

            Process(target=self.write_target, args=(self.msg_queue, )).start()

            log.info('Launching Symbol Detector')
            self.detector.start()

            log.info('Multiprocess Communication Session Started')

            while True:
                if not self.img_queue.empty():
                    msg = self.img_queue.get_nowait()
                    if msg == 'TP':
                        log.info('Detecting for Symbols')
                        frame = self.detector.get_frame()
                        symbol_match = self.detector.detect(frame)
                        if symbol_match is not None:
                            log.info('Symbol Match ID: ' + str(symbol_match))
                            self.pc.write('TC|' + str(symbol_match))
                        else:
                            log.info('No Symbols Detected')
                            self.pc.write('TC|0')
        except KeyboardInterrupt:
            raise

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

    def read_android(self, msg_queue):
        while True:
            try:
                msg = self.android.read()
                if msg is not None:
                    if self.verbose:
                        log.info('Read Android: ' + str(msg))
                    if msg in ['w1', 'a', 'd', 'h']:
                        msg_queue.put_nowait(format_for('ARD', msg))
                    else:
                        msg_queue.put_nowait(format_for('PC', msg))

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

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

    def read_pc(self, msg_queue, img_queue):
        while True:
            msg = self.pc.read()
            if msg is not None:
                if self.verbose:
                    log.info('Read PC: ' + str(msg['target']) + '; ' +
                             str(msg['payload']))
                if msg['target'] == 'android':
                    msg_queue.put_nowait(format_for('AND', msg['payload']))
                elif msg['target'] == 'arduino':
                    msg_queue.put_nowait(format_for('ARD', msg['payload']))
                elif msg['target'] == 'rpi':
                    img_queue.put_nowait(msg['payload'])
                elif msg['target'] == 'both':
                    msg_queue.put_nowait(
                        format_for('AND', msg['payload']['android']))
                    msg_queue.put_nowait(
                        format_for('ARD', msg['payload']['arduino']))

    def write_target(self, msg_queue):
        while True:
            if not msg_queue.empty():
                msg = msg_queue.get_nowait()
                msg = json.loads(msg)
                payload = msg['payload']

                if msg['target'] == 'PC':
                    if self.verbose:
                        log.info('Write PC:' + str(payload))
                    self.pc.write(payload)

                elif msg['target'] == 'AND':
                    if self.verbose:
                        log.info('Write Android:' + str(payload))
                    self.android.write(payload)

                elif msg['target'] == 'ARD':
                    if self.verbose:
                        log.info('Write Arduino:' + str(payload))
                    self.arduino.write(payload)
Exemplo n.º 8
0
class MultiProcess:
    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()
        self.arduino_queue = Queue()
        self.pc_queue = Queue()
        self.detector_queue = Queue()

        self.frame_count = 0

    def start(self):
        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_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, detector_queue):
        while True:
            try:
                msg = self.android.read()
                if msg is not None:
                    log.info('Read Android:' + str(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))
                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, 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
            '''