diff --git a/test/frame_helper.py b/test/frame_helper.py new file mode 100644 index 0000000..d3abac4 --- /dev/null +++ b/test/frame_helper.py @@ -0,0 +1,37 @@ +""" This module acts as a helper script for putting a frame on the R2D2 Python +CAN bus. """ + +from client.comm import Comm +from common.frames import FrameQrcodeData + +STRING = "TEST" +WIDTH, HEIGHT = 200, 100 +X, Y = 5, 5 +DISTANCE = 20 + + +def create_frame(string, width, height, x, y, distance): + """ Helper function for creating a new CAN bus frame. + :param string: string message you want to send. + :param width: width of the qr code in pixels. + :param height: height of the qr code in pixels. + :param x: center pixel coordinate on the x axis. + :param y: center pixel coordinate on the y axis. + param distance: distance in milimeter to the QR code. + :return: Ret urns the created frame. """ + frame = FrameQrcodeData() + byte_string = bytes(string, 'utf-8') + frame.set_data(byte_string, width, height, x, y, distance) + return frame + + +def main(): + """ Main function that gets executed when we call the script, + probably from a subprocess. """ + comm = Comm() + frame = create_frame(STRING, WIDTH, HEIGHT, X, Y, DISTANCE) + comm.send(frame) + comm.stop() + + +main() diff --git a/test/test_vision_module.py b/test/test_vision_module.py new file mode 100644 index 0000000..0bc766f --- /dev/null +++ b/test/test_vision_module.py @@ -0,0 +1,60 @@ +""" This module describes the unit tests for our Vision module class. +Please connect a webcam and start the R2D2 Python bus manager. """ + +import subprocess +from client.comm import Comm +from common.frame_enum import FrameType +from modules.vision.module.mod import Vision +from modules.vision.module.qr_reader import QrReader +from modules.vision.test.frame_helper import create_frame +from modules.vision.module.video_feed import VideoFeedCV2 +from modules.vision.module.camera_properties import CameraProperties +from modules.vision.test.frame_helper import STRING, WIDTH, HEIGHT, X, Y, DISTANCE + +FRAME_TUPLE = (bytes(STRING, 'utf-8'), WIDTH, HEIGHT, X, Y, DISTANCE) + + +def catch(bus: Comm): + """ listens on the bus for a specific frametype. + :param frame_type: type of frame to listen for. + :return: None if it was unable to find a frame of the specified type on the bus. + """ + data = None + while bus.has_data(): + frame = bus.get_data() + data = frame.get_data() + return data + return data + + +class TestVisionModule: + """ Groups the tests for the vision class, please connect a webcam and + start the bus manager found in the Python build directory. """ + + def test_frame(self): + """ tests the frame for correctly setting and getting the values. """ + # create a frame and get the data + frame = create_frame(STRING, WIDTH, HEIGHT, X, Y, DISTANCE) + data = frame.get_data() + assert data[1:] == FRAME_TUPLE[1:] + + def test_module(self): + """ Tests if the module correctly puts a frame on the bus and retrieve it. + This test can fail if there is still old data on the bus, restart the manager + if this happens. """ + module = Vision(Comm(), VideoFeedCV2( + 0), QrReader(), CameraProperties(0, 0)) + + module.comm.listen_for([FrameType.QRCODE_DATA]) + + # put a frame on the bus from another process (the python build is setup in + # a way that you can't listen to frames sent by the same OS process, so we do + # it this way.) + subprocess.call(['python', 'test/frame_helper.py']) + + data = None + while data is None: + data = catch(module.comm) + module.comm.stop() + + assert data[1:] == FRAME_TUPLE[1:] \ No newline at end of file