Skip to content

Commit

Permalink
readability, simulink fixes
Browse files Browse the repository at this point in the history
input argument parsing now removed from magicformula function which improves readability; simulink files were not up to date
  • Loading branch information
teasit committed Jan 21, 2024
1 parent 323473a commit 4ed5616
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 113 deletions.
Binary file modified doc/examples/Example_Simulink_MagicFormulaTyre.slx
Binary file not shown.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="InputParser.m" type="File" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info />
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version='1.0' encoding='UTF-8'?>
<Info location="kamm.png" type="File" />
26 changes: 0 additions & 26 deletions src/+magicformula/InputArgumentParser.m

This file was deleted.

76 changes: 76 additions & 0 deletions src/+magicformula/InputParser.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
classdef (Hidden) InputParser
%MAGICFORMULA.INPUTPARSER Parses magic formula input arguments.
methods (Static)
function [p,SX,SA,FZ,IP,IA,VX,side,ver] = parse(p,SX,SA,varargin)
versions = enumeration('MagicFormulaVersion');
isValidVersion = @(x) mustBeMember(x, versions);
isValidNumericVector = @(x) isnumeric(x) && isvector(x);
isValidTyreSide = @(x) mustBeMember(x, [0, 1]);

getNonempty = @magicformula.InputParser.getNonempty;
p = magicformula.InputParser.parseParams(p);
FZ = getNonempty(p.FNOMIN, 1E3);
IP = getNonempty(p.INFLPRES, p.NOMPRES, 80E3);
IA = 0;
VX = getNonempty(p.LONGVL, p.VXLOW, 10);
side = getNonempty(p.TYRESIDE, 'LEFT');
ver = MagicFormulaVersion.fromFITTYP(p.FITTYP);
ver = getNonempty(ver,versions(1));

parser = inputParser();
parser.addRequired('SX',isValidNumericVector);
parser.addRequired('SA',isValidNumericVector);
parser.addOptional('FZ',FZ,isValidNumericVector);
parser.addOptional('IP',IP,isValidNumericVector);
parser.addOptional('IA',IA,isValidNumericVector);
parser.addOptional('VX',VX,isValidNumericVector);
parser.addOptional('side',side,isValidTyreSide);
parser.addOptional('version',ver,isValidVersion);
parser.parse(SX, SA, varargin{:});

results = parser.Results;
SX = results.SX;
SA = results.SA;
FZ = results.FZ;
IP = results.IP;
IA = results.IA;
VX = results.VX;
side = results.side;
ver = results.version;
end
end
methods (Static, Hidden)
function arg = getNonempty(varargin)
for i = 1:nargin()
arg = varargin{i};
if ~isempty(arg)
return
end
end
end
function p = parseParams(p)
isValidParameters = @(x) isstruct(x) ...
|| isa(x, 'MagicFormulaTyre') ...
|| isa(x, 'magicformula.Parameters') ...
|| isfile(x);
parser = inputParser();
parser.addRequired('params',isValidParameters);
parser.parse(p);
results = parser.Results;
p = results.params;
switch class(p)
case 'MagicFormulaTyre'
tyre = p;
p = tyre.Parameters;
p = struct(p);
case 'char'
file = p;
tyre = MagicFormulaTyre(file);
p = tyre.Parameters;
p = struct(p);
otherwise
p = struct(p);
end
end
end
end
8 changes: 4 additions & 4 deletions src/enum/MagicFormulaVersionSimulink.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
end
switch fittyp
case 5
obj = MagicFormulaVersion.v51;
obj = MagicFormulaVersionSimulink.v51;
case {6, 21}
obj = MagicFormulaVersion.v52;
obj = MagicFormulaVersionSimulink.v52;
case 60
obj = MagicFormulaVersion.v60;
obj = MagicFormulaVersionSimulink.v60;
case 61
obj = MagicFormulaVersion.v61;
obj = MagicFormulaVersionSimulink.v61;
otherwise
error('Invalid FITTYP value ''%d''!', fittyp)
end
Expand Down
93 changes: 12 additions & 81 deletions src/magicformula.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,94 +50,25 @@
% See also MagicFormulaTyre, magicformula.v61.Parameters.
%
nargoutchk(0,5)
[params, SX, SA, FZ, IP, IA, VX, side, version] = ...
parseInputs(params, SX, SA, varargin{:});
if coder.target('MATLAB')
[params,SX,SA,FZ,IP,IA,VX,side,version] = ...
magicformula.InputParser.parse(params,SX,SA,varargin{:});
else
[FZ,IP,IA,VX,side,version] = deal(varargin{:});
end
switch version
case MagicFormulaVersion.v61
f = @magicformula.v61.eval;
if nargout() <= 2
[FX,FY] = magicformula.v61.eval(params, SX, SA, FZ, IP, IA, VX, side);
[FX,FY] = f(params,SX,SA,FZ,IP,IA,VX,side);
elseif nargout() == 3
[FX,FY,MZ] = magicformula.v61.eval(...
params, SX, SA, FZ, IP, IA, VX, side);
[FX,FY,MZ] = f(params,SX,SA,FZ,IP,IA,VX,side);
elseif nargout() == 4
[FX,FY,MZ,MY] = magicformula.v61.eval(...
params, SX, SA, FZ, IP, IA, VX, side);
[FX,FY,MZ,MY] = f(params,SX,SA,FZ,IP,IA,VX,side);
elseif nargout() == 5
[FX,FY,MZ,MY,MX] = magicformula.v61.eval(...
params, SX, SA, FZ, IP, IA, VX, side);
else

[FX,FY,MZ,MY,MX] = f(params,SX,SA,FZ,IP,IA,VX,side);
end
otherwise
error('Version ''%s'' not supported yet.', char(version))
end
end

function [params,SX,SA,FZ,IP,IA,VX,side,version] = parseInputs(...
params,SX,SA,varargin)
if coder.target('MATLAB')
parser = magicformula.InputArgumentParser;
parser.parse(params,SX,SA,varargin{:})

results = parser.Results;
params = results.params;
SX = results.SX;
SA = results.SA;
FZ = results.FZ;
IP = results.IP;
IA = results.IA;
VX = results.VX;
side = results.side;
version = results.version;

if isstruct(params)
elseif isa(params, 'MagicFormulaTyre')
tyre = params;
params = tyre.Parameters;
params = struct(params);
elseif isa(params, 'magicformula.Parameters')
params = struct(params);
elseif isfile(params)
file = params;
tyre = MagicFormulaTyre(file);
params = tyre.Parameters;
params = struct(params);
end

if isempty(FZ)
FZ = params.FNOMIN;
end

if isempty(IP)
IP = params.INFLPRES;
if isempty(IP)
IP = params.NOMPRES;
end
end

if isempty(VX)
VX = params.LONGVL;
if isempty(VX)
VX = params.VXLOW;
if isempty(VX)
VX = 10;
end
end
end

if isempty(side)
side = params.TYRESIDE;
end

if isempty(version)
FITTYP = params.FITTYP;
if isempty(FITTYP)
version = MagicFormulaVersion.v61;
else
version = MagicFormulaVersion.fromFITTYP(FITTYP);
end
end
else
[FZ,IP,IA,VX,side,version] = deal(varargin{:});
error('Version ''%d'' not supported yet.', version)
end
end
Binary file modified src/simulink/MagicFormulaTyreLibrary.slx
Binary file not shown.

0 comments on commit 4ed5616

Please sign in to comment.