From 894a093d05dbd4cc5cdb31c638771e75f6af6319 Mon Sep 17 00:00:00 2001
From: Tim Stadtmann <tim.stadtmann@rwth-aachen.de>
Date: Fri, 16 Dec 2016 17:55:32 +0100
Subject: [PATCH] Resetting the devices will now never throw errors

This fixes the bug where an error occured if disconnect is called while
motor is running. Motors are now explicitly stopped if they are busily
moving when resetting the devices
---
 source/Motor.m  | 27 +++++++++++++++++++++++----
 source/Sensor.m |  8 ++++++--
 2 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/source/Motor.m b/source/Motor.m
index fc0de1b..27633f4 100755
--- a/source/Motor.m
+++ b/source/Motor.m
@@ -1148,7 +1148,7 @@ classdef Motor < MaskedHandle & dynamicprops
                 error('Motor::releaseBrake: No physical motor connected to Port %s',...
                        port2str('Motor', motor.port));
             elseif motor.currentSpeed~=0
-                error('Motor::releaseBrake: Can''t releaseBrake brake because Motor is moving');
+                error('Motor::releaseBrake: Can''t release brake because Motor is moving');
             end
             
             if motor.speedRegulation 
@@ -1257,15 +1257,34 @@ classdef Motor < MaskedHandle & dynamicprops
         end
         
         function resetPhysicalMotor(motor)
-            %
+            % Do nothing if their is either no connection or no motor connected to port
             if ~motor.connectedToBrick || ~motor.physicalMotorConnected
                 return; 
             end
             
+            % If motor is *busily* running, stop it. That avoids suicidal stuff like: 
+            %     b = EV3(); b.connect('usb');
+            %     b.motorA.start();
+            %     b.disconnect(); -> Motor still running and cannot directly be stopped anymore
+            if motor.isRunning
+                motor.stop(); 
+            end
+            
+            % Reset tacho values
             motor.resetTachoCount();
             motor.internalReset();
-            motor.setBrake(0);
-            %motor.stop();
+            
+            % setBrake (correctly) throws an error if currentSpeed ~= 0. In this case, it
+            % has already been checked if motor is busily running. At this point, the only
+            % things that provoke a currentSpeed~=0 are coasting into a stop or a user manually
+            % spinning the motor. In both cases, there is no active brake set which means that
+            % setBrake(0) unnecessary either way. Therefore, if setBrake throws an error, it
+            % can be safely ignored.
+            try
+                motor.setBrake(0);
+            catch ME
+                % Safely ignore this...
+            end
         end
     end
 end
diff --git a/source/Sensor.m b/source/Sensor.m
index 53949fd..d775869 100755
--- a/source/Sensor.m
+++ b/source/Sensor.m
@@ -527,8 +527,12 @@ classdef Sensor < MaskedHandle
                 return
             end
             
-            sensor.mode = DeviceMode(sensor.type, uint8(0));
-            sensor.reset;
+            try
+                sensor.mode = DeviceMode(sensor.type, uint8(0));
+                sensor.reset;
+            catch ME
+                % For now: ignore...
+            end
         end
     end
 end
-- 
GitLab