From ace6ba21eb43814d668e3f85bcdea3c7d0b64efe Mon Sep 17 00:00:00 2001 From: Tim Stadtmann <tim.stadtmann@rwth-aachen.de> Date: Sun, 14 Aug 2016 20:07:32 +0200 Subject: [PATCH] Add, update and clean-up utility functions --- examples/exampleCommLayer.m | 81 ------------- {examples => old_examples}/testConnSpeed.m | 0 {examples => old_examples}/testSyncMotor.m | 0 {examples => old_examples}/testToolbox.m | 0 source/+DeviceMode/{Error.m => Default.m} | 4 +- source/bitfield2input.m | 10 ++ source/bitfield2port.m | 9 +- source/bool2str.m | 4 + source/input2bitfield.m | 8 ++ source/input2port.m | 4 +- source/isBool.m | 12 +- source/isDeviceValid.m | 13 +++ source/isModeValid.m | 30 ++--- source/isMotorValid.m | 9 -- source/isPortEnumValid.m | 27 +++++ source/isPortStrValid.m | 19 +++ source/isPortValid.m | 36 ------ source/isSensorValid.m | 9 -- source/isSyncedBitfield.m | 21 ++++ source/port2bitfield.m | 6 +- source/port2input.m | 4 +- source/port2str.m | 130 +++++++++++---------- source/str2PortParam.m | 128 ++++++++++---------- source/str2bool.m | 11 +- 24 files changed, 275 insertions(+), 300 deletions(-) delete mode 100644 examples/exampleCommLayer.m rename {examples => old_examples}/testConnSpeed.m (100%) rename {examples => old_examples}/testSyncMotor.m (100%) rename {examples => old_examples}/testToolbox.m (100%) rename source/+DeviceMode/{Error.m => Default.m} (52%) create mode 100644 source/bitfield2input.m create mode 100644 source/input2bitfield.m create mode 100644 source/isDeviceValid.m delete mode 100644 source/isMotorValid.m create mode 100644 source/isPortEnumValid.m create mode 100644 source/isPortStrValid.m delete mode 100644 source/isPortValid.m delete mode 100644 source/isSensorValid.m create mode 100644 source/isSyncedBitfield.m diff --git a/examples/exampleCommLayer.m b/examples/exampleCommLayer.m deleted file mode 100644 index 48f27d2..0000000 --- a/examples/exampleCommLayer.m +++ /dev/null @@ -1,81 +0,0 @@ -% init the connection -disp('Connecting ... ') -% brick usb init -b = Brick('ioType','usb'); -% beep to indicate connection -b.beep(); -% print information -disp('===================================') -disp('EV3 example code') -disp('===================================') -disp('u - increase motor speed') -disp('d - decrease motor speed') -disp('t - start the motor') -disp('s - stop the motor') -disp('b - beep') -disp('o - output the tachometer') -disp('c - clear the tachometer') -disp('v - output the brick voltage') -disp('f - quit the program'); -disp('===================================') -% user intput -userIn = ''; -% motor power -motorPower = 10; -% set motor power and start -b.outputPower(0,Device.MotorA,motorPower) -b.outputStart(0,Device.MotorA) -while(~strcmp(userIn,'f')) - % get input - userIn = input('> Input(u,d,t,s,b,o,c,v,f): ','s'); - % increase speed - if (userIn == 'u') - motorPower = motorPower+10; - if motorPower >= 100 - motorPower = 100; - end - b.outputPower(0,Device.MotorA,motorPower) - disp(['> Motor Power: ' num2str(motorPower)]); - end - % decrease speed - if (userIn == 'd') - motorPower = motorPower-10; - if motorPower <= -100 - motorPower = -100; - end - b.outputPower(0,Device.MotorA,motorPower) - disp(['> Motor Power: ' num2str(motorPower)]); - end - % start the motor - if (userIn == 't') - b.outputStart(0,Device.MotorA) - disp('> Motor Started'); - end - % stop the motor - if (userIn == 's') - b.outputStop(0,Device.MotorA,0) - disp('> Motor Stopped'); - end - % beep test - if (userIn == 'b') - b.beep(); - disp('> Beep'); - end - % output the tachometer - if (userIn == 'o') - tacho = b.outputGetCount(0,Device.MotorA); - disp(['> Tachometer: ' num2str(tacho)]); - end - % clear the tachometer - if (userIn == 'c') - b.outputClrCount(0,Device.MotorA) - disp('> Cleared tacho'); - end - % voltage output - if (userIn == 'v') - v = b.uiReadVbatt; - disp(['> Brick voltage: ' num2str(v) 'V']); - end -end -% delete the brick object -delete(b) \ No newline at end of file diff --git a/examples/testConnSpeed.m b/old_examples/testConnSpeed.m similarity index 100% rename from examples/testConnSpeed.m rename to old_examples/testConnSpeed.m diff --git a/examples/testSyncMotor.m b/old_examples/testSyncMotor.m similarity index 100% rename from examples/testSyncMotor.m rename to old_examples/testSyncMotor.m diff --git a/examples/testToolbox.m b/old_examples/testToolbox.m similarity index 100% rename from examples/testToolbox.m rename to old_examples/testToolbox.m diff --git a/source/+DeviceMode/Error.m b/source/+DeviceMode/Default.m similarity index 52% rename from source/+DeviceMode/Error.m rename to source/+DeviceMode/Default.m index 244ea15..1efa576 100644 --- a/source/+DeviceMode/Error.m +++ b/source/+DeviceMode/Default.m @@ -1,6 +1,4 @@ -classdef Error < uint8 - %Error Test - +classdef Default < uint8 enumeration Undefined (0) end diff --git a/source/bitfield2input.m b/source/bitfield2input.m new file mode 100644 index 0000000..2f59bb0 --- /dev/null +++ b/source/bitfield2input.m @@ -0,0 +1,10 @@ +function inputPort = bitfield2input(bitfield) +% Converts a motor-bitfield to motor-inputport-number(s). + [isSynced, ~, ~] = isSyncedBitfield(bitfield); + if isSynced + ports = bitfield2port(bitfield); + inputPort = [port2input(ports(1)), port2input(ports(2))]; + else + inputPort = port2input(bitfield2port(bitfield)); + end +end \ No newline at end of file diff --git a/source/bitfield2port.m b/source/bitfield2port.m index 1b5ab92..85f52b0 100644 --- a/source/bitfield2port.m +++ b/source/bitfield2port.m @@ -1,4 +1,9 @@ function port = bitfield2port(bitfield) -% Converts motorBitfield-enum to motorPort-enum. - port = MotorPort(log2(double(bitfield))); +% Converts motor-bitfield to motor-port-number(s). + [isSynced, b1, b2] = isSyncedBitfield(bitfield); + if isSynced + port = [b1-1, b2-1]; + else + port = uint8(log2(double(bitfield))); + end end \ No newline at end of file diff --git a/source/bool2str.m b/source/bool2str.m index e820015..f397f84 100644 --- a/source/bool2str.m +++ b/source/bool2str.m @@ -10,6 +10,10 @@ function bool = bool2str(inp) elseif inp~=0 error('bool2str: Not a valid boolean.'); end + elseif islogical(inp) + if inp==true + bool = 'on'; + end end end diff --git a/source/input2bitfield.m b/source/input2bitfield.m new file mode 100644 index 0000000..cbc3d9e --- /dev/null +++ b/source/input2bitfield.m @@ -0,0 +1,8 @@ +function bitfield = input2bitfield(varargin) +% Converts motor-inputport-number(s) to motor-bitfield. + if nargin==1 + bitfield = port2bitfield(input2port(varargin{1})); + elseif nargin==2 + bitfield = port2bitfield(input2port(varargin{1}), input2port(varargin{2})); + end +end diff --git a/source/input2port.m b/source/input2port.m index 98bc1b2..4765c3a 100644 --- a/source/input2port.m +++ b/source/input2port.m @@ -1,4 +1,4 @@ function port = input2port(input) -% Converts motorInput-enum to motorPort-enum. - port = MotorPort(input-16); +% Converts a motor-inputport-number to motor-port-number. + port = uint8(input)-16; end \ No newline at end of file diff --git a/source/isBool.m b/source/isBool.m index b7cbb71..c1711a9 100644 --- a/source/isBool.m +++ b/source/isBool.m @@ -1,15 +1,11 @@ function isValid = isBool(inp) % Returns whether given boolean is valid or not. - isValid = 0; if ischar(inp) - if strcmpi(inp, 'on') || strcmpi(inp, 'true') ... - || strcmpi(inp, 'off') || strcmpi(inp, 'false') - isValid = 1; - end + isValid = strcmpi(inp, 'on') || strcmpi(inp, 'off'); elseif isnumeric(inp) - if inp==1 || inp==0 - isValid = 1; - end + isValid = inp==1 || inp==0; + else + isValid = islogical(inp); end end diff --git a/source/isDeviceValid.m b/source/isDeviceValid.m new file mode 100644 index 0000000..a0bcc93 --- /dev/null +++ b/source/isDeviceValid.m @@ -0,0 +1,13 @@ +function isValid = isDeviceValid(deviceType, device) +% Returns whether given device object is valid or not. + isValid = 0; + try + if ~isempty(device) + if isa(device, deviceType) && device.isvalid + isValid = 1; + end + end + catch ME + % Ignore + end +end diff --git a/source/isModeValid.m b/source/isModeValid.m index ef88e57..ac5b575 100644 --- a/source/isModeValid.m +++ b/source/isModeValid.m @@ -1,6 +1,6 @@ function isValid = isModeValid(mode, type) % Returns whether given mode is a valid mode in given type. - isValid = 1; + isValid = true; if strcmp(class(mode), 'DeviceMode.Error') return; @@ -9,56 +9,58 @@ function isValid = isModeValid(mode, type) switch type case DeviceType.NXTTouch if ~strcmp(class(mode), 'DeviceMode.NXTTouch') - isValid = 0; + isValid = false; end case DeviceType.NXTLight if ~strcmp(class(mode), 'DeviceMode.NXTLight') - isValid = 0; + isValid = false; end case DeviceType.NXTSound if ~strcmp(class(mode), 'DeviceMode.NXTSound') - isValid = 0; + isValid = false; end case DeviceType.NXTColor if ~strcmp(class(mode), 'DeviceMode.NXTColor') - isValid = 0; + isValid = false; end case DeviceType.NXTUltraSonic if ~strcmp(class(mode), 'DeviceMode.NXTUltraSonic') - isValid = 0; + isValid = false; end case DeviceType.NXTTemperature if ~strcmp(class(mode), 'DeviceMode.NXTTemperature') - isValid = 0; + isValid = false; end case DeviceType.LargeMotor if ~strcmp(class(mode), 'DeviceMode.Motor') - isValid = 0; + isValid = false; end case DeviceType.MediumMotor if ~strcmp(class(mode), 'DeviceMode.Motor') - isValid = 0; + isValid = false; end case DeviceType.Touch if ~strcmp(class(mode), 'DeviceMode.Touch') - isValid = 0; + isValid = false; end case DeviceType.Color if ~strcmp(class(mode), 'DeviceMode.Color') - isValid = 0; + isValid = false; end case DeviceType.UltraSonic if ~strcmp(class(mode), 'DeviceMode.UltraSonic') - isValid = 0; + isValid = false; end case DeviceType.Gyro if ~strcmp(class(mode), 'DeviceMode.Gyro') - isValid = 0; + isValid = false; end case DeviceType.InfraRed if ~strcmp(class(mode), 'DeviceMode.InfraRed') - isValid = 0; + isValid = false; end + otherwise + isValid = false; end end diff --git a/source/isMotorValid.m b/source/isMotorValid.m deleted file mode 100644 index a645107..0000000 --- a/source/isMotorValid.m +++ /dev/null @@ -1,9 +0,0 @@ -function isValid = isMotorValid(motor) -% Returns whether given motor object is valid or not. - isValid = 0; - if ~isempty(motor) - if isa(motor, 'Motor') && motor.isvalid - isValid = 1; - end - end -end diff --git a/source/isPortEnumValid.m b/source/isPortEnumValid.m new file mode 100644 index 0000000..009c995 --- /dev/null +++ b/source/isPortEnumValid.m @@ -0,0 +1,27 @@ +function isValid = isPortEnumValid(device, port) +% Returns whether given port-number is valid for given device + isValid = false; + + if ~ischar(device) + error(['isPortValid: First argument has to be a string (''Sensor'', ',... + '''Motor'' or ''SyncMotor'')']); + end + + try + if strcmpi(device, 'Motor') + if ~isa(port, 'MotorBitfield') + MotorBitfield(port); + end + isValid = true; % Otherwise, an error would have already been thrown + elseif strcmpi(device, 'Sensor') + if ~isa(port, 'SensorPort') + SensorPort(port); + end + isValid = true; % Otherwise, an error would have already been thrown + elseif strcmpi(device, 'SyncMotor') + isValid = isSyncedBitfield(port); + end + catch ME + % Ignore + end +end diff --git a/source/isPortStrValid.m b/source/isPortStrValid.m new file mode 100644 index 0000000..4b2398b --- /dev/null +++ b/source/isPortStrValid.m @@ -0,0 +1,19 @@ +function isValid = isPortStrValid(device, port) +% Returns whether given port-number is valid for given device + if strcmpi(device, 'Motor') + validPorts = {'A', 'B', 'C', 'D'}; + elseif strcmpi(device, 'Sensor') + validPorts = {'1', '2', '3', '4'}; + elseif strcmpi(device, 'SyncMotor') + validPorts = {'AB', 'AC', 'AD', 'BA', 'BC', 'BD', ... + 'CA', 'CB', 'CD', 'DA', 'DB', 'DC'}; + else + error(['isPortValid: First argument has to be either ''Sensor'', ',... + '''Motor'' or ''SyncMotor''']); + end + + if ischar(port) + isValid = ismember(port, validPorts); + end +end + diff --git a/source/isPortValid.m b/source/isPortValid.m deleted file mode 100644 index b2050fe..0000000 --- a/source/isPortValid.m +++ /dev/null @@ -1,36 +0,0 @@ -function isValid = isPortValid(device, port) -% Returns whether given port-string is valid or not. - isValid = 0; - - if ~ischar(device) - error(['isPortValid: First argument has to be a string (''Sensor'', ',... - '''Motor'' or ''SyncMotor'')']); - end - - if strcmpi(device, 'Motor') - validPorts = {'A', 'B', 'C', 'D'}; - elseif strcmpi(device, 'Sensor') - validPorts = {'1', '2', '3', '4'}; - elseif strcmpi(device, 'SyncMotor') - validPorts = {'AB', 'AC', 'AD', 'BA', 'BC', 'BD', ... - 'CA', 'CB', 'CD', 'DA', 'DB', 'DC'}; - else - error(['isPortValid: First argument has to be either ''Sensor'', ',... - '''Motor'' or ''SyncMotor''']); - end - - % Note: Both port2str() and validatestring() generate errors if the input - % argument is 'wrong'. This method here isn't supposed to do this, - % it's only supposed to return whether given port is valid or not. - try - if ~ischar(port) - port = port2str(device, port); - end - - if any(validatestring(port, validPorts)) - isValid = 1; - end - catch - return; - end -end diff --git a/source/isSensorValid.m b/source/isSensorValid.m deleted file mode 100644 index fb01ec7..0000000 --- a/source/isSensorValid.m +++ /dev/null @@ -1,9 +0,0 @@ -function isValid = isSensorValid(sensor) -% Returns whether given sensor object is valid or not. - isValid = 0; - if ~isempty(sensor) - if isa(sensor, 'Sensor') && sensor.isvalid - isValid = 1; - end - end -end diff --git a/source/isSyncedBitfield.m b/source/isSyncedBitfield.m new file mode 100644 index 0000000..08ae85c --- /dev/null +++ b/source/isSyncedBitfield.m @@ -0,0 +1,21 @@ +function [isSynced, bit1, bit2] = isSyncedBitfield(bitfield) +% A SyncMotor-port is the sum of the two ports to sync +% -> As motor ports are represented by bitfields, check how many bits are set (2 +% bits = 2 ports). +% -> Just checking 'port' against allowed values would have been too easy :) + bit1 = 0; bit2 = 0; + + setBits = []; + for i=1:4 + if bitget(bitfield, i) == 1 + setBits = [setBits, i]; + end + end + + isSynced = (bitfield<=15)&&(length(setBits)==2); + if isSynced + bit1 = setBits(1); + bit2 = setBits(2); + end +end + diff --git a/source/port2bitfield.m b/source/port2bitfield.m index b4e5de4..9f8d2c7 100644 --- a/source/port2bitfield.m +++ b/source/port2bitfield.m @@ -1,8 +1,8 @@ function bitfield = port2bitfield(varargin) -% Converts a motorPort-enum to motorBitfield-enum. +% Converts motor-port-number(s) to motor-bitfield. if nargin==1 - bitfield = MotorBitfield(2 ^ varargin{1}); + bitfield = uint8(2 ^ varargin{1}); elseif nargin==2 - bitfield = MotorBitfield(2 ^ (varargin{1}+varargin{2})); + bitfield = uint8(2 ^varargin{1}+2^varargin{2}); end end diff --git a/source/port2input.m b/source/port2input.m index cd3bbad..58c3076 100644 --- a/source/port2input.m +++ b/source/port2input.m @@ -1,4 +1,4 @@ function input = port2input(port) -% Converts a motorPort-enum to motorInput-enum. - input = MotorInput(port+16); +% Converts a motor-port-number to motor-inputport-number. + input = uint8(port)+16; end \ No newline at end of file diff --git a/source/port2str.m b/source/port2str.m index 696b990..048613d 100644 --- a/source/port2str.m +++ b/source/port2str.m @@ -1,72 +1,82 @@ -function port = port2str(device, inp) +function portStr = port2str(varargin) % Converts a port-enum to corresponding-string. - if ~ischar(device) - error(['port2str: First argument has to be a string (''Sensor'', ',... - '''Motor'' or ''SyncMotor'')']); + p = inputParser(); + + validDevices = {'Motor', 'Sensor', 'SyncMotor'}; + validPortTypes = {'Bitfield', 'PortNo', 'InputPortNo'}; + checkDevice = @(x) ismember(x, validDevices); + checkPortType = @(x) ismember(x, validPortTypes); + + p.addRequired('device', checkDevice); + p.addRequired('port'); + p.addOptional('portType', 'Bitfield', checkPortType); + + p.parse(varargin{:}); + + device = p.Results.device; + port = p.Results.port; + portType = p.Results.portType; + + if ~isPortEnumValid(device, port) + error('port2str: Given parameter is not a valid port-enum'); end if strcmpi(device, 'Motor') - if ~ischar(inp) - if inp == MotorBitfield.MotorA || inp == MotorPort.MotorA || ... - inp == MotorInput.MotorA - port = 'A'; - elseif inp == MotorBitfield.MotorB || inp == MotorPort.MotorB || ... - inp == MotorInput.MotorB - port = 'B'; - elseif inp == MotorBitfield.MotorC || inp == MotorPort.MotorC || ... - inp == MotorInput.MotorC - port = 'C'; - elseif inp == MotorBitfield.MotorD || inp == MotorPort.MotorD || ... - inp == MotorInput.MotorD - port = 'D'; - else - error('port2str: Given parameter is not a valid motor port.' ); + if strcmpi(portType, 'Bitfield') + if port == MotorBitfield.MotorA + portStr = 'A'; + elseif port == MotorBitfield.MotorB + portStr = 'B'; + elseif port == MotorBitfield.MotorC + portStr = 'C'; + elseif port == MotorBitfield.MotorD + portStr = 'D'; + end + elseif strcmpi(portType, 'PortNo') + if port == MotorPort.MotorA + portStr = 'A'; + elseif port == MotorPort.MotorB + portStr = 'B'; + elseif port == MotorPort.MotorC + portStr = 'C'; + elseif port == MotorPort.MotorD + portStr = 'D'; + end + elseif strcmpi(portType, 'InputPortNo') + if port == MotorInput.MotorA + portStr = 'A'; + elseif port == MotorInput.MotorB + portStr = 'B'; + elseif port == MotorInput.MotorC + portStr = 'C'; + elseif port == MotorInput.MotorD + portStr = 'D'; end - else - warning('port2str: Given parameter already is a string.'); - port = inp; end elseif strcmpi(device, 'Sensor') - if ~ischar(inp) - if inp == SensorPort.Sensor1 - port = '1'; - elseif inp == SensorPort.Sensor2 - port = '2'; - elseif inp == SensorPort.Sensor3 - port = '3'; - elseif inp == SensorPort.Sensor4 - port = '4'; - else - error('port2str: Given parameter is not a valid sensor port.' ); - end - else - warning('port2str: Given parameter already is a string.'); - port = inp; + if port == SensorPort.Sensor1 + portStr = '1'; + elseif port == SensorPort.Sensor2 + portStr = '2'; + elseif port == SensorPort.Sensor3 + portStr = '3'; + elseif port == SensorPort.Sensor4 + portStr = '4'; end elseif strcmpi(device, 'SyncMotor') - if ~ischar(inp) - switch inp - case MotorBitfield.MotorA+MotorBitfield.MotorB - port = 'AB'; - case MotorBitfield.MotorA+MotorBitfield.MotorC - port = 'AC'; - case MotorBitfield.MotorA+MotorBitfield.MotorD - port = 'AD'; - case MotorBitfield.MotorB+MotorBitfield.MotorC - port = 'BC'; - case MotorBitfield.MotorB+MotorBitfield.MotorD - port = 'BD'; - case MotorBitfield.MotorC+MotorBitfield.MotorD - port = 'CD'; - otherwise - error('port2str: Given parameter is not a valid motor port.' ); - end - else - warning('port2str: Given parameter already is a string.'); - port = inp; + switch port + case MotorBitfield.MotorA+MotorBitfield.MotorB + portStr = 'AB'; + case MotorBitfield.MotorA+MotorBitfield.MotorC + portStr = 'AC'; + case MotorBitfield.MotorA+MotorBitfield.MotorD + portStr = 'AD'; + case MotorBitfield.MotorB+MotorBitfield.MotorC + portStr = 'BC'; + case MotorBitfield.MotorB+MotorBitfield.MotorD + portStr = 'BD'; + case MotorBitfield.MotorC+MotorBitfield.MotorD + portStr = 'CD'; end - else - error(['port2str: First argument has to be either ''Sensor'', ',... - '''Motor'' or ''SyncMotor''']); end end \ No newline at end of file diff --git a/source/str2PortParam.m b/source/str2PortParam.m index 75fdb6d..0f2a139 100644 --- a/source/str2PortParam.m +++ b/source/str2PortParam.m @@ -34,109 +34,109 @@ function varargout = str2PortParam(device, port) if strcmpi(device, 'Motor') if strcmpi(port, 'A') - varargout{1} = uint8(MotorBitfield.MotorA); - varargout{2} = uint8(MotorPort.MotorA); - varargout{3} = uint8(MotorInput.MotorA); + varargout{1} = MotorBitfield.MotorA; + varargout{2} = MotorPort.MotorA; + varargout{3} = MotorInput.MotorA; elseif strcmpi(port, 'B') - varargout{1} = uint8(MotorBitfield.MotorB); - varargout{2} = uint8(MotorPort.MotorB); - varargout{3} = uint8(MotorInput.MotorB); + varargout{1} = MotorBitfield.MotorB; + varargout{2} = MotorPort.MotorB; + varargout{3} = MotorInput.MotorB; elseif strcmpi(port, 'C') - varargout{1} = uint8(MotorBitfield.MotorC); - varargout{2} = uint8(MotorPort.MotorC); - varargout{3} = uint8(MotorInput.MotorC); + varargout{1} = MotorBitfield.MotorC; + varargout{2} = MotorPort.MotorC; + varargout{3} = MotorInput.MotorC; elseif strcmpi(port, 'D') - varargout{1} = uint8(MotorBitfield.MotorD); - varargout{2} = uint8(MotorPort.MotorD); - varargout{3} = uint8(MotorInput.MotorD); + varargout{1} = MotorBitfield.MotorD; + varargout{2} = MotorPort.MotorD; + varargout{3} = MotorInput.MotorD; else error('str2PortParam: Given port is not a valid motor port.'); end elseif strcmpi(device, 'Sensor') if strcmpi(port, '1') - varargout{1} = uint8(SensorPort.Sensor1); + varargout{1} = SensorPort.Sensor1; elseif strcmpi(port, '2') - varargout{1} = uint8(SensorPort.Sensor2); + varargout{1} = SensorPort.Sensor2; elseif strcmpi(port, '3') - varargout{1} = uint8(SensorPort.Sensor3); + varargout{1} = SensorPort.Sensor3; elseif strcmpi(port, '4') - varargout{1} = uint8(SensorPort.Sensor4); + varargout{1} = SensorPort.Sensor4; else error('str2PortParam: Given parameter not a valid sensor port.'); end elseif strcmpi(device, 'SyncMotor') if strcmpi(port,'AB') varargout{1} = MotorBitfield.MotorA+MotorBitfield.MotorB; - varargout{2} = uint8(MotorPort.MotorA); - varargout{3} = uint8(MotorInput.MotorA); - varargout{4} = uint8(MotorPort.MotorB); - varargout{5} = uint8(MotorInput.MotorB); + varargout{2} = MotorPort.MotorA; + varargout{3} = MotorInput.MotorA; + varargout{4} = MotorPort.MotorB; + varargout{5} = MotorInput.MotorB; elseif strcmpi(port,'AC') varargout{1} = MotorBitfield.MotorA+MotorBitfield.MotorC; - varargout{2} = uint8(MotorPort.MotorA); - varargout{3} = uint8(MotorInput.MotorA); - varargout{4} = uint8(MotorPort.MotorC); - varargout{5} = uint8(MotorInput.MotorC); + varargout{2} = MotorPort.MotorA; + varargout{3} = MotorInput.MotorA; + varargout{4} = MotorPort.MotorC; + varargout{5} = MotorInput.MotorC; elseif strcmpi(port,'AD') varargout{1} = MotorBitfield.MotorA+MotorBitfield.MotorD; - varargout{2} = uint8(MotorPort.MotorA); - varargout{3} = uint8(MotorInput.MotorA); - varargout{4} = uint8(MotorPort.MotorD); - varargout{5} = uint8(MotorInput.MotorD); + varargout{2} = MotorPort.MotorA; + varargout{3} = MotorInput.MotorA; + varargout{4} = MotorPort.MotorD; + varargout{5} = MotorInput.MotorD; elseif strcmpi(port,'BA') varargout{1} = MotorBitfield.MotorB+MotorBitfield.MotorA; - varargout{2} = uint8(MotorPort.MotorB); - varargout{3} = uint8(MotorInput.MotorB); - varargout{4} = uint8(MotorPort.MotorA); - varargout{5} = uint8(MotorInput.MotorA); + varargout{2} = MotorPort.MotorB; + varargout{3} = MotorInput.MotorB; + varargout{4} = MotorPort.MotorA; + varargout{5} = MotorInput.MotorA; elseif strcmpi(port,'BC') varargout{1} = MotorBitfield.MotorB+MotorBitfield.MotorC; - varargout{2} = uint8(MotorPort.MotorB); - varargout{3} = uint8(MotorInput.MotorB); - varargout{4} = uint8(MotorPort.MotorC); - varargout{5} = uint8(MotorInput.MotorC); + varargout{2} = MotorPort.MotorB; + varargout{3} = MotorInput.MotorB; + varargout{4} = MotorPort.MotorC; + varargout{5} = MotorInput.MotorC; elseif strcmpi(port,'BD') varargout{1} = MotorBitfield.MotorB+MotorBitfield.MotorD; - varargout{2} = uint8(MotorPort.MotorB); - varargout{3} = uint8(MotorInput.MotorB); - varargout{4} = uint8(MotorPort.MotorD); - varargout{5} = uint8(MotorInput.MotorD); + varargout{2} = MotorPort.MotorB; + varargout{3} = MotorInput.MotorB; + varargout{4} = MotorPort.MotorD; + varargout{5} = MotorInput.MotorD; elseif strcmpi(port,'CA') varargout{1} = MotorBitfield.MotorC+MotorBitfield.MotorA; - varargout{2} = uint8(MotorPort.MotorC); - varargout{3} = uint8(MotorInput.MotorC); - varargout{4} = uint8(MotorPort.MotorA); - varargout{5} = uint8(MotorInput.MotorA); + varargout{2} = MotorPort.MotorC; + varargout{3} = MotorInput.MotorC; + varargout{4} = MotorPort.MotorA; + varargout{5} = MotorInput.MotorA; elseif strcmpi(port,'CB') varargout{1} = MotorBitfield.MotorC+MotorBitfield.MotorB; - varargout{2} = uint8(MotorPort.MotorC); - varargout{3} = uint8(MotorInput.MotorC); - varargout{4} = uint8(MotorPort.MotorB); - varargout{5} = uint8(MotorInput.MotorB); + varargout{2} = MotorPort.MotorC; + varargout{3} = MotorInput.MotorC; + varargout{4} = MotorPort.MotorB; + varargout{5} = MotorInput.MotorB; elseif strcmpi(port,'CD') varargout{1} = MotorBitfield.MotorC+MotorBitfield.MotorD; - varargout{2} = uint8(MotorPort.MotorC); - varargout{3} = uint8(MotorInput.MotorC); - varargout{4} = uint8(MotorPort.MotorD); - varargout{5} = uint8(MotorInput.MotorD); + varargout{2} = MotorPort.MotorC; + varargout{3} = MotorInput.MotorC; + varargout{4} = MotorPort.MotorD; + varargout{5} = MotorInput.MotorD; elseif strcmpi(port,'DA') varargout{1} = MotorBitfield.MotorD+MotorBitfield.MotorA; - varargout{2} = uint8(MotorPort.MotorD); - varargout{3} = uint8(MotorInput.MotorD); - varargout{4} = uint8(MotorPort.MotorA); - varargout{5} = uint8(MotorInput.MotorA); + varargout{2} = MotorPort.MotorD; + varargout{3} = MotorInput.MotorD; + varargout{4} = MotorPort.MotorA; + varargout{5} = MotorInput.MotorA; elseif strcmpi(port,'DB') varargout{1} = MotorBitfield.MotorD+MotorBitfield.MotorB; - varargout{2} = uint8(MotorPort.MotorD); - varargout{3} = uint8(MotorInput.MotorD); - varargout{4} = uint8(MotorPort.MotorB); - varargout{5} = uint8(MotorInput.MotorB); + varargout{2} = MotorPort.MotorD; + varargout{3} = MotorInput.MotorD; + varargout{4} = MotorPort.MotorB; + varargout{5} = MotorInput.MotorB; elseif strcmpi(port,'DC') varargout{1} = MotorBitfield.MotorD+MotorBitfield.MotorC; - varargout{2} = uint8(MotorPort.MotorD); - varargout{3} = uint8(MotorInput.MotorD); - varargout{4} = uint8(MotorPort.MotorC); - varargout{5} = uint8(MotorInput.MotorC); + varargout{2} = MotorPort.MotorD; + varargout{3} = MotorInput.MotorD; + varargout{4} = MotorPort.MotorC; + varargout{5} = MotorInput.MotorC; else error('str2PortParam: Given port is not a valid sync motor port.'); end diff --git a/source/str2bool.m b/source/str2bool.m index 0b88252..cc65622 100644 --- a/source/str2bool.m +++ b/source/str2bool.m @@ -3,15 +3,12 @@ function bool = str2bool(inp) bool = 0; if ischar(inp) - if strcmpi(inp, 'on') || strcmpi(inp, 'true') + if strcmpi(inp, 'on') bool = 1; - elseif ~strcmpi(inp, 'off') && ~strcmpi(inp, 'false') + elseif ~strcmpi(inp, 'off') error('str2bool: Given parameter is not a valid string.'); end - elseif isnumeric(inp) - % warning('str2bool: Given parameter already is a numeric. Returning...'); - if bool==0 || bool==1 - bool = inp; - end + elseif isnumeric(inp) || islogical(inp) + bool = inp; end end -- GitLab