Alternative of sim for Simulink model that allows code generation
Ältere Kommentare anzeigen
I am trying to use the Unscented Kalman filter in Simulink. My state and measurement functions would ideally be SimScape models. I have tried calling those models from the following e.g. the measurement update function:
function y= UKFmeasurementModelonly(x,uk)
%Reads the Current states to workspace
dx=x(1);
vx=x(2);
Fx=x(3);
%The above states are simulink parameters
%The code below runs the SimscapeModel
out=sim('Model_only','SimMechanicsOpenEditorOnUpdate','off');
%The lines below store the measurement vector
XX=out.Xk_1.Data;
xk_1=XX(1,:)';
y(1)=xk_1(1);
y(2)=xk_1(4);
y(3)=xk_1(7);
end
I receive multiple errors the main being that the sim command is not supported for code generation. I assume that the Unscented Kalman Filter block requires that the time update and measurement functions must be converted to code. That would indeed be a useful feature as I would eventually want to deploy this code to hardware.
Is there an optimal alternative to sim for the above purposes. Would that be using the Simulink coder or the Simulink Compiler? If the Simulink Coder is used can the generated c code be called in the above function with an inline command, e.g. using a mex file or otherwise?
1 Kommentar
E_C
am 27 Jun. 2022
Antworten (1)
Kartik Saxena
am 8 Jan. 2024
Bearbeitet: Kartik Saxena
am 8 Jan. 2024
0 Stimmen
Hi,
I understand that you require an alternate for 'sim' command that allows code generation.
You can use Simulink Coder to generate C code from the Simscape models and then compile this C code into a MEX-function or standalone executable. You can then call this MEX-function or executable from within your state or measurement function. This approach allows you to bypass the 'sim' command while still using the Simscape models during code generation.
Refer to the following example from MathWorks documentation for the exact steps for generating code using Simulink Coder:
I hope this resolves your issue.
Kategorien
Mehr zu Run-Time Parameters finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!