From 147c4269fc6c388c0fceb9997943e439dfe05204 Mon Sep 17 00:00:00 2001
From: Tim Stadtmann <tim.stadtmann@rwth-aachen.de>
Date: Tue, 18 Oct 2016 13:29:08 +0200
Subject: [PATCH] Add first version of Motor/Sensor-tests

---
 tools/private/connectWires.m    | 11 ++++
 tools/private/motorTest.m       | 62 ++++++++++++++++++++++
 tools/private/sensorTest.m      | 48 +++++++++++++++++
 tools/testAllMotorsAndSensors.m | 94 +++++++++++++++++++++++++++++++++
 4 files changed, 215 insertions(+)
 create mode 100644 tools/private/connectWires.m
 create mode 100644 tools/private/motorTest.m
 create mode 100644 tools/private/sensorTest.m
 create mode 100644 tools/testAllMotorsAndSensors.m

diff --git a/tools/private/connectWires.m b/tools/private/connectWires.m
new file mode 100644
index 0000000..28958ca
--- /dev/null
+++ b/tools/private/connectWires.m
@@ -0,0 +1,11 @@
+function [] = connectWires()
+%Connect motors to ports
+disp(sprintf('Please connect front motor to port A, left wheel to port B,\nright wheel to port C. Then press any key to continue.\n\n'));
+pause();
+
+%Connect sensors to ports
+disp(sprintf('Please connect touch sensor to port 1, color sensor to port 3,\nultrasonic sensor to port 4. Then press any key to continue.\n\n'));
+pause();
+
+end
+
diff --git a/tools/private/motorTest.m b/tools/private/motorTest.m
new file mode 100644
index 0000000..c9415b2
--- /dev/null
+++ b/tools/private/motorTest.m
@@ -0,0 +1,62 @@
+function ret = motorTest(EV3, mode)
+ret = 0;
+%Start test
+disp(sprintf(['--------------------------------------------------------------\nStart motor test ', mode]));
+%Test motor A
+EV3.motorA.power=20;
+try EV3.motorA.start()
+catch all
+end;
+pause(1);
+try EV3.motorA.stop()
+catch all
+end;
+EV3.motorA.power=-20;
+try EV3.motorA.start()
+catch all
+end;
+pause(1);
+try EV3.motorA.stop()
+catch all
+end;
+
+answerA = input('Did motor A move? [y/n]', 's');
+
+%Test motor B
+EV3.motorB.power=20;
+EV3.motorB.limitValue=1000;
+try EV3.motorB.start
+catch all
+end;
+
+answerB = input('Did motor B move? [y/n]', 's');
+
+%Test motor C
+EV3.motorC.power=20;
+EV3.motorC.limitValue=1000;
+try EV3.motorC.start
+catch all
+end;
+
+answerC = input('Did motor C move? [y/n]', 's');
+
+%Warnings for defect motors
+if (~isempty(strfind(answerA, 'n')))
+    warning('Test for motor A failed');
+end;
+
+if (~isempty(strfind(answerB, 'n')))
+    warning('Test for motor B failed');
+end;
+
+if (~isempty(strfind(answerC, 'n')))
+    warning('Test for motor C failed');
+end;
+
+if (~isempty(strfind(answerA, 'y'))) && (~isempty(strfind(answerB, 'y'))) && (~isempty(strfind(answerC, 'y')))
+    disp(sprintf('All motors work correctly.\n'));
+    ret = 1;
+end;
+
+end
+
diff --git a/tools/private/sensorTest.m b/tools/private/sensorTest.m
new file mode 100644
index 0000000..bde6056
--- /dev/null
+++ b/tools/private/sensorTest.m
@@ -0,0 +1,48 @@
+function ret = sensorTest(EV3, mode)
+
+ret = 0;
+
+%Start test
+disp(sprintf(['--------------------------------------------------------------\nStart sensor test ', mode]));
+
+%Test sensor 1
+if EV3.sensor1.type == DeviceType.Touch
+    answer1 = 1;
+else
+    answer1 = 0;
+end;
+
+%Test sensor 3
+if EV3.sensor3.type == DeviceType.Color
+    answer3 = 1;
+else
+    answer3 = 0;
+end;
+
+%Test sensor 4
+if EV3.sensor4.type == DeviceType.UltraSonic
+    answer4 = 1;
+else
+    answer4 = 0;
+end;
+
+%Warnings for defect motors
+if answer1 == 0
+    warning('Test for sensor 1 failed');
+end;
+
+if answer3 == 0
+    warning('Test for sensor 3 failed');
+end;
+
+if answer4 == 0
+    warning('Test for sensor 4 failed');
+end;
+
+if answer1==1 && answer3==1 && answer4==1
+    disp(sprintf('All sensors work correctly.\n'));
+    ret = 1;
+end;
+
+end
+
diff --git a/tools/testAllMotorsAndSensors.m b/tools/testAllMotorsAndSensors.m
new file mode 100644
index 0000000..5725175
--- /dev/null
+++ b/tools/testAllMotorsAndSensors.m
@@ -0,0 +1,94 @@
+function [] = testAllMotorsAndSensors()
+
+%Connect the Brick via USB
+clear all;
+b=EV3;
+connected = 0;
+i=0;
+while connected == 0
+    disp(sprintf('Please connect the brick via USB and press any key to continue.\n\n'));
+    pause();
+    try 
+        b.connect('usb')
+    catch all
+        disp('connection failed, try again...')
+        % discard errors
+    end;
+    connected = b.isConnected();
+    i=i+1;
+    if i==3
+        error('USB connection failed. You can not continue the test. Restart Matlab and the brick, then try again. If the error occurs again, please contact us.');
+    end;
+end;
+
+%Connect motors and sensors to ports
+connectWires();
+
+%Test motors
+USBresultMotor = motorTest(b, 'USB');
+
+if USBresultMotor == 0
+    warning('The USB motor test failed. Please contact us.');
+end;
+
+%Test sensors
+USBresultSensor = sensorTest(b, 'USB');
+
+if USBresultSensor == 0
+    warning('The USB sensor test failed. Please contact us.');
+end;
+
+if (USBresultMotor == 1) && (USBresultSensor == 1)
+    disp(sprintf('\n<strong>--------------All USB tests passed successfully---------------</strong>\n'));
+else
+    disp(sprintf('\n<strong>-----End of USB tests. Some tests failed, notice the warnings.-----</strong>\n'));
+end;
+
+b.disconnect;
+pause(1);
+%Connect the Brick via BT
+b=EV3;
+connected = 0;
+i=0;
+while connected==0
+    disp(sprintf('Please remove the USB wire, so that the brick can be\nautomatically connected via BT and press any key to continue.\n\n'));
+    pause();
+    try b.connect('bt', 'serPort', '/dev/rfcomm0')
+    catch all
+    end;
+    try b.connect('bt', 'serPort', '/dev/rfcomm1')
+    catch all
+    end;
+    connected = b.isConnected();
+    pause(1);
+    i=i+1;
+    if i==3
+        error('BT connection failed. You can not continue the test. Did you connect the brick with btconnect? If no, start btconnect in the terminal. If yes, restart Matlab and try again. If the error occurs again, please contact us.');
+    end;
+end;
+
+%Test motors
+BTresultMotor = motorTest(b, 'BT');
+
+if BTresultMotor == 0
+    warning('The BT motor test failed. Please contact us.');
+end;
+
+%Test sensors
+BTresultSensor = sensorTest(b, 'BT');
+
+if BTresultSensor == 0
+    warning('The BT sensor test failed. Please contact us.');
+end;
+
+if (BTresultMotor == 1) && (BTresultSensor == 1)
+    disp(sprintf('\n<strong>---------------All BT tests passed successfully---------------</strong>\n'));
+else
+    disp(sprintf('\n<strong>-----End of BT tests. Some tests failed, notice the warnings.-----</strong>\n'));
+end;
+
+disp(sprintf('\n------------------------End of the test------------------------\n'));
+clear all;
+
+end
+
-- 
GitLab