Alternative of sim for Simulink model that allows code generation

7 vues (au cours des 30 derniers jours)
E_C
E_C le 21 Juin 2022
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 commentaire
E_C
E_C le 27 Juin 2022
By looking further into this my current goal is to do the following:
-1) Use the Simulink Coder to convert the Simulink Model to C code (or a series of C related files).
(While the executable generated by the Simulink Coder works, the Unscented Kalman Filter does not allow calls to system.)
-2) Call the generated C Code from Simulink Coder using the coder.ceval command
I imagine that there is a way to run the result of the Simulink Coder, e.g., the example main script, usually called ert_main.c, from a Matlab function using the coder.ceval command. Is there a way to do so or such an example?
the coder.ceval('ert_main.c') does not work, as it likely requires filies to be included first (perhaps with coder.cinclude?)

Connectez-vous pour commenter.

Réponses (1)

Kartik Saxena
Kartik Saxena le 8 Jan 2024
Modifié(e) : Kartik Saxena le 8 Jan 2024
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.

Catégories

En savoir plus sur Troubleshooting dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by