Skip to content
Snippets Groups Projects
Commit 19030409 authored by Marco Luigi Gaibotti's avatar Marco Luigi Gaibotti
Browse files

[refactoring-ode][classes] Improved object constructor

parent 4a2a9bab
No related branches found
No related tags found
1 merge request!3Refactoring ODE functions
...@@ -28,48 +28,12 @@ classdef Component < Config ...@@ -28,48 +28,12 @@ classdef Component < Config
varName = strtok(fileName, 'C'); varName = strtok(fileName, 'C');
end end
[m, n] = size(varsIn.(varName)); [varsIn.(varName).mission] = deal(mission);
objMatrix(m,n) = obj; obj = varsIn.(varName);
for i = 1:m
for j = 1:n
varIn.(varName) = varsIn.(varName)(i,j);
objMatrix(i,j).loadConfig(varIn);
end
end
obj = objMatrix;
end end
end end
methods(Access = protected) methods(Access = protected)
function loadConfig(obj, varargin)
% This method loads desired configs into calling component.
% The component can chose between:
% - Only specifying the configName (name of the config file):
% The loader will look for a variable named like the
% config file.
% - Specifying both the configName and the variableName:
% Useful for when you want to group multiple small
% objects into a larger config file
%
% Syntax:
% obj.loadConfig() - To load from a config file
% obj.loadConfig(vars) - To load from given variables
fileName = obj.configName;
varName = obj.variableName;
if isempty(varName), varName = strtok(fileName, 'C'); end
if nargin == 2, configObj = varargin{end}.(varName);
else, configObj = obj.getConfig(); end
fields = obj.getProperties('writable');
for j = 1:size(fields, 2)
if isempty(configObj.(fields{j})), continue; end
obj.(fields{j}) = configObj.(fields{j});
end
end
function outputVariables = getConfig(obj) function outputVariables = getConfig(obj)
fileName = obj.configName; fileName = obj.configName;
filePath = obj.mission.configPath; filePath = obj.mission.configPath;
......
...@@ -229,12 +229,13 @@ classdef Rocket < Component ...@@ -229,12 +229,13 @@ classdef Rocket < Component
options.checkGeometry logical = true options.checkGeometry logical = true
end end
obj@Component(mission, varIn); obj@Component(mission, varIn);
obj.mission = mission;
%% Loading data %% Loading data
if nargin == 0 if nargin == 0
return; return;
end end
obj.mission = mission;
vars = obj.getConfig(); % Load config once and apply to other bays vars = obj.getConfig(); % Load config once and apply to other bays
obj.payload = Payload(mission, vars); obj.payload = Payload(mission, vars);
obj.recovery = Recovery(mission, vars); obj.recovery = Recovery(mission, vars);
......
...@@ -30,7 +30,7 @@ classdef Motor < Bay ...@@ -30,7 +30,7 @@ classdef Motor < Bay
ae double % [Pa] Eflux Area ae double % [Pa] Eflux Area
end end
properties(Dependent) properties
mass % [kg] Total Motor mass mass % [kg] Total Motor mass
fuselageXCg double % [m] xcg of the engine fuselage only from tank tip fuselageXCg double % [m] xcg of the engine fuselage only from tank tip
end end
...@@ -53,20 +53,19 @@ classdef Motor < Bay ...@@ -53,20 +53,19 @@ classdef Motor < Bay
end end
obj@Bay(mission, varIn); obj@Bay(mission, varIn);
obj.loadData(); obj.loadData();
obj.updateAll();
end end
function set.name(obj, name) function set.name(obj, name)
obj.name = name; obj.name = name;
obj.loadData(); obj.loadData();
obj.updateAll();
end end
function mass = get.mass(obj) function updateAll(obj)
mass = obj.propellantMass + obj.structureMass; obj.fuselageXCg = (obj.length - ...
end
function fuselageXCg = get.fuselageXCg(obj)
fuselageXCg = (obj.length - ...
obj.tankLength)/2 + obj.tankLength; obj.tankLength)/2 + obj.tankLength;
obj.mass = obj.propellantMass + obj.structureMass;
end end
end end
......
...@@ -56,15 +56,19 @@ classdef Environment < Component ...@@ -56,15 +56,19 @@ classdef Environment < Component
obj.omega = deg2rad(obj.omega); obj.omega = deg2rad(obj.omega);
obj.phi = deg2rad(obj.phi); obj.phi = deg2rad(obj.phi);
obj.updateAll();
end
end
% Updaters
methods
function updateAll(obj)
obj.updateG0; obj.updateG0;
obj.updatePinDistance; obj.updatePinDistance;
obj.updateRamp; obj.updateRamp;
obj.updateLocal; obj.updateLocal;
end end
end
% Updaters
methods
function obj = updateG0(obj) function obj = updateG0(obj)
obj.g0 = gravitywgs84(obj.z0, obj.lat0); obj.g0 = gravitywgs84(obj.z0, obj.lat0);
end end
......
...@@ -38,7 +38,7 @@ classdef WindCustom < Component ...@@ -38,7 +38,7 @@ classdef WindCustom < Component
varIn = [] varIn = []
end end
obj@Component(mission, varIn); obj@Component(mission, varIn);
obj.setData(); obj.updateAll();
end end
function [uw, vw, ww] = getVels(obj, z) function [uw, vw, ww] = getVels(obj, z)
...@@ -57,7 +57,7 @@ classdef WindCustom < Component ...@@ -57,7 +57,7 @@ classdef WindCustom < Component
end end
end end
function obj = setData(obj) function updateAll(obj)
s = length(obj.altitudes); s = length(obj.altitudes);
magVector = nan(1,s); magVector = nan(1,s);
azVector = nan(1,s); azVector = nan(1,s);
......
function [value, isterminal, direction] = eventLanding(~, Y, ~, ~, ~, ~) function [value, isterminal, direction] = eventLanding(~, Y, varargin)
%{ %{
eventLanding - Event function to stop simulation at landing checking when a eventLanding - Event function to stop simulation at landing checking when a
value tends to zero; the value taken is to account is the value tends to zero; the value taken is to account is the
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment