diff --git a/source/Motor.m b/source/Motor.m index 62fb6bcef6d6bbcfdc1f3a78deecc2ea44301883..367d0eeb703ed79afa13629afa1bd0e20cd8b5a2 100755 --- a/source/Motor.m +++ b/source/Motor.m @@ -272,10 +272,7 @@ classdef Motor < MaskedHandle & dynamicprops end - if motor.debug - fprintf('\n(DEBUG) Motor::start: Calling ''outputStart''-command on Port %s...\n', port2str('Motor', motor.port)); - end - motor.commInterface.outputStart(0, motor.port); + motor.execute(@outputStart, 0, motor.port); motor.state.startedNotBusy = true; else @@ -287,41 +284,21 @@ classdef Motor < MaskedHandle & dynamicprops if strcmpi(motor.limitMode, 'Tacho') if motor.speedRegulation - if motor.debug - fprintf('\n(DEBUG) Motor::start: Calling ''outputStepSpeed''-command on Port %s...\n',... - port2str('Motor', motor.port)); - end - - motor.commInterface.outputStepSpeed(0, motor.port, motor.power,... + motor.execute(@outputStepSpeed, 0, motor.port, motor.power,... motor.smoothStart, limit, motor.smoothStop,... motor.brakeMode_); else - if motor.debug - fprintf('\n(DEBUG) Motor::start: Calling ''outputStepPower''-command on Port %s...\n',... - port2str('Motor', motor.port)); - end - - motor.commInterface.outputStepPower(0, motor.port, motor.power,... + motor.execute(@outputStepPower, 0, motor.port, motor.power,... motor.smoothStart, limit, motor.smoothStop,... motor.brakeMode_); end elseif strcmpi(motor.limitMode, 'Time') if motor.speedRegulation - if motor.debug - fprintf('\n(DEBUG) Motor::start: Calling ''outputTimeSpeed''-command on Port %s...\n',... - port2str('Motor', motor.port)); - end - - motor.commInterface.outputTimeSpeed(0, motor.port, motor.power,... + motor.execute(@outputTimeSpeed, 0, motor.port, motor.power,... motor.smoothStart, limit, motor.smoothStop,... motor.brakeMode_); else - if motor.debug - fprintf('\n(DEBUG) Motor::start: Calling ''outputTimePower''-command on Port %s...\n',... - port2str('Motor', motor.port)); - end - - motor.commInterface.outputTimePower(0, motor.port, motor.power,... + motor.execute(@outputTimePower, 0, motor.port, motor.power,... motor.smoothStart, limit, motor.smoothStop,... motor.brakeMode_); end @@ -349,10 +326,7 @@ classdef Motor < MaskedHandle & dynamicprops return; end - if motor.debug - fprintf('\n(DEBUG) Motor::stop: Calling ''outputStop''-command on Port %s...\n', port2str('Motor', motor.port)); - end - motor.commInterface.outputStop(0, motor.port, motor.brakeMode_); + motor.execute(@outputStop, 0, motor.port, motor.brakeMode_); motor.state.startedNotBusy = false; end @@ -473,19 +447,11 @@ classdef Motor < MaskedHandle & dynamicprops if strcmpi(motor.limitMode, 'Tacho') - if motor.debug - fprintf(['\n(DEBUG) Motor::syncedStart: Calling ''outputStepSync''-command on ' ,... - 'Ports %s and %s.\n'], port2str('Motor', motor.port), port2str('Motor', syncMotor.port)); - end - motor.commInterface.outputStepSync(0, motor.port+syncMotor.port, ... + motor.execute(@outputStepSync, 0, motor.port+syncMotor.port, ... motor.power, turnRatio, ... motor.limitValue, motor.brakeMode_); elseif strcmpi(motor.limitMode, 'Time') - if motor.debug - fprintf('\n(DEBUG) Motor::start: Calling ''outputStepSync''-command on Ports %s and %s.\n',... - port2str('Motor', motor.port), port2str('Motor', syncMotor.port)); - end - motor.commInterface.outputTimeSync(0, motor.port+syncMotor.port, ... + motor.execute(@outputTimeSync, 0, motor.port+syncMotor.port, ... motor.power, turnRatio, ... motor.limitValue, motor.brakeMode_); end @@ -527,11 +493,7 @@ classdef Motor < MaskedHandle & dynamicprops syncMotor.applyState(); % Synced stopping - if motor.debug - fprintf('\n(DEBUG) Motor::syncedStop: Calling ''outputStop''-command on Ports %s and %s.\n', ... - port2str('Motor', motor.port), port2str('Motor', syncMotor.port)); - end - motor.commInterface.outputStop(0, motor.port+syncMotor.port, motor.brakeMode_); + motor.execute(@outputStop, 0, motor.port+syncMotor.port, motor.brakeMode_); % On next start, both motors have to send power-opcode again if motor.state.sendPowerOnSet @@ -590,11 +552,7 @@ classdef Motor < MaskedHandle & dynamicprops port2str('Motor', motor.port)); end - if motor.debug - fprintf('\n(DEBUG) Motor::internalReset: Calling ''outputReset''-command on Port %s...\n',... - port2str('Motor', motor.port)); - end - motor.commInterface.outputReset(0, motor.port); + motor.execute(@outputReset, 0, motor.port); end function resetTachoCount(motor) @@ -609,11 +567,7 @@ classdef Motor < MaskedHandle & dynamicprops port2str('Motor', motor.port)); end - if motor.debug - fprintf('\n(DEBUG) Motor::resetTachoCount: Calling ''outputClrCount''-command on Port %s...\n',... - port2str('Motor', motor.port)); - end - motor.commInterface.outputClrCount(0, motor.port); + motor.execute(@outputClrCount, 0, motor.port); end function setBrake(motor, brake) @@ -963,7 +917,24 @@ classdef Motor < MaskedHandle & dynamicprops end end - methods (Access = private) % Private functions that directly interact with commLayer + methods (Access = private) % Private functions that directly interact with commLayer# + function varargout = execute(motor, commandHandle, varargin) + % Execute a CommunicationInterface-method given as a handle + % + % As those methods have different, fixed numbers of output arguments, this quantity + % has to be retrieved first. + + if motor.debug + fprintf('(DEBUG) Sending %s\n', func2str(commandHandle)); + end + + % Note: Arrg. MATLAB does not support nargout for class methods directly, so I have to + % do this ugly workaround using strings. See + % https://de.mathworks.com/matlabcentral/answers/96617-how-can-i-use-nargin-nargout-to-determine-the-number-of-input-output-arguments-of-an-object-method + nOut = nargout(strcat('CommunicationInterface>CommunicationInterface.', func2str(commandHandle))); + [varargout{1:nOut}] = commandHandle(motor.commInterface, varargin{:}); + end + function success = setPower(motor, power) % Sets given power value on the physical Brick. % @@ -983,15 +954,9 @@ classdef Motor < MaskedHandle & dynamicprops end; if motor.currentSpeedRegulation - if motor.debug - fprintf('\n(DEBUG) Motor::setPower: Calling ''outputSpeed''-command on Port %s...\n', port2str('Motor', motor.port)); - end - motor.commInterface.outputSpeed(0, motor.port, power); + motor.execute(@outputSpeed, 0, motor.port, power); else - if motor.debug - fprintf('\n(DEBUG) Motor::setPower: Calling ''outputPower''-command on Port %s...\n', port2str('Motor', motor.port)); - end - motor.commInterface.outputPower(0, motor.port, power); + motor.execute(@outputPower, 0, motor.port, power); end success = true; return; @@ -1002,12 +967,7 @@ classdef Motor < MaskedHandle & dynamicprops error('Motor::getTypeMode: Motor-Object not connected to comm handle.'); end - if motor.debug - fprintf('\n(DEBUG) Motor::getTypeMode: Calling ''inputDeviceGetTypeMode''-command on Port %s...\n',... - port2str('Motor', motor.port)); - end - - [typeNo,modeNo] = motor.commInterface.inputDeviceGetTypeMode(0, motor.portInput); + [typeNo, modeNo] = motor.execute(@inputDeviceGetTypeMode, 0, motor.portInput); type = DeviceType(typeNo); mode = DeviceMode(type,modeNo); end @@ -1017,11 +977,7 @@ classdef Motor < MaskedHandle & dynamicprops error('Motor::getStatus: Motor-Object not connected to comm handle.'); end - if motor.debug - fprintf('\n(DEBUG) Motor::getStatus: Calling ''inputDeviceGetConnection''-command on Port %s...\n',... - port2str('Motor', motor.port)); - end - statusNo = motor.commInterface.inputDeviceGetConnection(0, motor.portInput); + statusNo = motor.execute(@inputDeviceGetConnection, 0, motor.portInput); status = ConnectionType(statusNo); end @@ -1030,17 +986,11 @@ classdef Motor < MaskedHandle & dynamicprops error('Motor::getTachoCount: Motor-Object not connected to comm handle.'); end - if motor.debug - fprintf('\n(DEBUG) Motor::getTachoCount: Calling ''outputGetCount''-command on Port %s...\n', port2str('Motor', motor.port)); - end - cnt = motor.commInterface.outputGetCount(0, motor.portNo); + cnt = motor.execute(@outputGetCount, 0, motor.portNo); end function cnt = getInternalTachoCount(motor) - if motor.debug - fprintf('\n(DEBUG) Motor::getInternalTachoCount: Calling ''outputRead''-command on Port %s...\n', port2str('Motor', motor.port)); - end - [~, cnt] = motor.commInterface.outputRead(0, motor.portNo); + [~, cnt] = motor.execute(@outputRead, 0, motor.portNo); end function speed = getSpeed(motor) @@ -1048,10 +998,7 @@ classdef Motor < MaskedHandle & dynamicprops error('Motor::getSpeed: Motor-Object not connected to comm handle.'); end - if motor.debug - fprintf('\n(DEBUG) Motor::getSpeed: Calling ''inputReadSI''-command on Port %s...\n', port2str('Motor', motor.port)); - end - speed = motor.commInterface.inputReadSI(0, motor.portInput, DeviceMode.Motor.Speed); + speed = motor.execute(@inputReadSI, 0, motor.portInput, DeviceMode.Motor.Speed); end function busy = getBusyFlag(motor) @@ -1067,10 +1014,7 @@ classdef Motor < MaskedHandle & dynamicprops error('Motor::getBusyFlag: Motor-Object not connected to comm handle.'); end - if motor.debug - fprintf('\n(DEBUG) Motor::getBusyFlag: Calling ''outputTest''-command on Port %s...\n', port2str('Motor', motor.port)); - end - busy = motor.commInterface.outputTest(0, motor.port); + busy = motor.execute(@outputTest, 0, motor.port); end function applyBrake(motor) @@ -1083,18 +1027,13 @@ classdef Motor < MaskedHandle & dynamicprops error('Motor::applyBrake: Can''t apply brake because Motor is moving'); end - if motor.debug - fprintf(['\n(DEBUG) Motor::applyBrake: Calling ''outputPower'', ''outputStart'' and' ,... - '''outputStop'' on Port %s...\n'], port2str('Motor', motor.port)); - end - if motor.speedRegulation - motor.commInterface.outputPower(0, motor.port, 0); + motor.execute(@outputPower, 0, motor.port, 0); else - motor.commInterface.outputSpeed(0, motor.port, 0); + motor.execute(@outputSpeed, 0, motor.port, 0); end - motor.commInterface.outputStart(0, motor.port); - motor.commInterface.outputStop(0, motor.port, BrakeMode.Brake); + motor.execute(@outputStart, 0, motor.port); + motor.execute(@outputStop, 0, motor.port, BrakeMode.Brake); end function releaseBrake(motor) @@ -1107,17 +1046,13 @@ classdef Motor < MaskedHandle & dynamicprops error('Motor::releaseBrake: Can''t release brake because Motor is moving'); end - if motor.debug - fprintf(['\n(DEBUG) Motor::releaseBrake: Calling ''outputPower'', ''outputStart'' and' ,... - '''outputStop'' on Port %s...\n'], port2str('Motor', motor.port)); - end if motor.speedRegulation - motor.commInterface.outputPower(0, motor.port, 0); + motor.execute(@outputPower, 0, motor.port, 0); else - motor.commInterface.outputSpeed(0, motor.port, 0); + motor.execute(@outputSpeed, 0, motor.port, 0); end - motor.commInterface.outputStart(0, motor.port); - motor.commInterface.outputStop(0, motor.port, BrakeMode.Coast); + motor.execute(@outputStart, 0, motor.port); + motor.execute(@outputStop, 0, motor.port, BrakeMode.Coast); end end