Matlab + Simulink Crash while running CarSim S Function in loop
Show older comments
Hello,
I am using CarSim S-Function in Simulink and calling it using Matlab command as written below:
warning('off')
K = lqr(A,B(:,2),Q,R);
beep off;
simout = sim('model', 'SrcWorkspace','current');
This is in a loop which runs about 2000 - 5000 times (it is an optimization loop) to calculate fitness values from real-time simulation.
The program runs properly but randomly crashes after 100 - 200 iterations. I observed the crash and it occurs randomly during which Simulink and Matlab crash with no crash log or continue message. It just instantly closes along with simulink.
My Simulink .mdl model has no errors/warnings or algebraic loop warnings.
I have attached an image of my simulink block. Below is the complete code that is called to run in a loop
function z = pfot(x, A, B, C, D)
Q = [ x(1) 0 0 0;
0 x(2) 0 0;
0 0 x(3) 0;
0 0 0 x(4)];
R = 1;
%% Ensuring system is stable
E = eig(Q);
if all(E > 0)
try
warning('off')
K = lqr(A,B(:,2),Q,R);
beep off;
simout = sim('model', 'SrcWorkspace','current');
truckAcc = simout.Ay1.Data;
traiAcc = simout.Ay2.Data;
%Getting peak lateral acc
peakAccTruck = max(abs(truckAcc));
peackAccTrail = max(abs(traiAcc));
%rwa = info3.Max/info2.Max;
rwa = peackAccTrail/peakAccTruck;
errorRWA = abs(1 - rwa);
%PFOT Using Target
targetX = simout.xTarget.Data;
targetY = simout.yTarget.Data;
trailerX = simout.Xo2.Data;
trailerY = simout.Yo2.Data;
PFOTX = abs(sum(targetX - trailerX));
PFOTY = abs(sum(targetY - trailerY));
PFOT = (PFOTX+PFOTY)/2;
if (errorRWA > 2)% || peackAccTrail > 0.65 || peakAccTruck > 0.65 )
z = [inf inf]';
else
z = [errorRWA PFOT]';
end
catch
z = [inf inf]';
end
else
z = [inf inf]';
end
end

2 Comments
Vineeth Nair
on 15 Apr 2019
In the block diagram atatched, a Gain block with K*u value can be seen. Its marked in red, which possibly denotes an error. Can you open it and check what the error message is
Khizar Qureshi
on 16 Apr 2019
Answers (0)
Categories
Find more on Configure C/C++ S-Function Features in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!