Skip to content

Commit

Permalink
simout2timetable does not work for multiple output ports
Browse files Browse the repository at this point in the history
  • Loading branch information
FannoFlow committed Sep 12, 2020
1 parent c1019a0 commit 29126a6
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 12 deletions.
16 changes: 16 additions & 0 deletions boost_matlab/private/constructTimeTable.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function tt= constructTimeTable(time,data)
%CONSTRUCTTIMETABLE internal method for dispatching timetable construction

arguments
time {mustBeNumeric}
data
end

if isa(data, "Simulink.SimulationData.Dataset")
tt = constructTimeTableFromDataset(data);
else
tt = constructTimeTableFromNumericTimeVector(time, data);
end

end

23 changes: 23 additions & 0 deletions boost_matlab/private/constructTimeTableFromDataset.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function tt = constructTimeTableFromDataset(data)
%CONSTRUCTTIMETABLEFROMDATASET internal method for constructing a timetable
%from a Simulink dataset

arguments
data Simulink.SimulationData.Dataset
end

tt = arrayfun(@(n) sig2tt(data{n}),1:data.numElements, "UniformOutput",false);

end

function tt = sig2tt(sig)
tt = constructTimeTableFromNumericTimeVector(sig.Values.Time,sig.Values.Data);
if sig.Name == "" % unammed signal
% extract the name from the signal port and index
tt.Properties.VariableNames = {sig.PortType + "_" + sig.PortIndex};
else
tt.Properties.VariableNames = {sig.Name};
end
end


26 changes: 14 additions & 12 deletions boost_matlab/simout2timetable.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,21 @@
end
end

% post-arguments validation
boost.validation.mustHaveToutWithYout(simout, NameValueArgs.TimeSaveName, NameValueArgs.OutputSaveName);

% initialization
simvars = simout.who;
timetables = {};

% post-arguments validation
boost.validation.mustHaveToutWithYout(simout, NameValueArgs.TimeSaveName, NameValueArgs.OutputSaveName);

if any(simvars == NameValueArgs.OutputSaveName)
timetables = [timetables, {constructTimeTableFromNumericTimeVector(simout.(NameValueArgs.TimeSaveName), simout.(NameValueArgs.OutputSaveName))}];
outputTT = constructTimeTable(simout.(NameValueArgs.TimeSaveName), simout.(NameValueArgs.OutputSaveName));
timetables = appendTimeTable(timetables, outputTT);
end

if any(simvars == NameValueArgs.SignalLoggingName)
sigsTT = arrayfun(@(n) sig2tt(simout.sigsOut{n}),1:simout.sigsOut.numElements, "UniformOutput",false);
timetables = [timetables, sigsTT{:}];
sigsTT = constructTimeTableFromDataset(simout.(NameValueArgs.SignalLoggingName));
timetables = appendTimeTable(timetables, sigsTT);
end

% final step, synchronize all of the timetables
Expand All @@ -56,12 +58,12 @@
end
end

function tt = sig2tt(sig)
tt = constructTimeTableFromNumericTimeVector(sig.Values.Time,sig.Values.Data);
if sig.Name == "" % unammed signal
% extract the name from the signal port and index
tt.Properties.VariableNames = sig.PortType + "_" + sig.PortIndex;
function timetables = appendTimeTable(timetables, newtables)
if iscell(newtables)
timetables = [timetables, newtables];
else
tt.Properties.VariableNames = sig.Name;
timetables = [timetables, {newtables}];
end
end


Binary file added test/FullRegression.mlx
Binary file not shown.
54 changes: 54 additions & 0 deletions test/simout2timetable/basic_ilc/tBasic_ilc.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
classdef tBasic_ilc < matlab.unittest.TestCase

properties
Model = "mBasic_ilc"
end

methods (TestMethodSetup)
function loadmodel(testCase)
load_system(testCase.Model)
end
end

methods
function closemodel(testCase)
evalin("base", "clear sys_dt T_end Ts")
close_system(testCase.Model, 0)
end
end

methods (Test)
function verifyErrorFreeConstruction(testCase)
%% System
m = 5; %kg
Ts = 0.1e-3; % 0.1 ms
s = tf('s');
sys_ct = 1/(m*s^2);
sys_dt = c2d(sys_ct,Ts);
assignin("base", "sys_dt", sys_dt)

%% Controller
%z = tf('z',Ts);
Kp = 571;
Kd = 51.1;
%C = Kp+Kd*(z-1)/Ts;

set_param(testCase.Model + "/" + "Gain", "Gain", string(m))
set_param(testCase.Model + "/" + "PID Controller", "P", string(Kp))
set_param(testCase.Model + "/" + "PID Controller", "D", string(Kd))

%% Simulation
T_end = 1;
assignin("base", "T_end", T_end)
assignin("base", "Ts", Ts)

t = 0:Ts:T_end;
[ref, vel, acc, ~] = quinticpolytraj([0 0.1 0.1 0],[0 0.25*T_end 0.75*T_end T_end], t);
% plot(t,ref,t,vel,t,acc)
in = Simulink.SimulationInput(testCase.Model);
in = in.setExternalInput([t',ref',vel',acc']);
simout = sim(in);
testCase.verifyWarningFree(@() simout2timetable(simout));
end
end
end

0 comments on commit 29126a6

Please sign in to comment.