Alternative of sim for Simulink model that allows code generation
이전 댓글 표시
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개)
Kartik Saxena
2024년 1월 8일
편집: Kartik Saxena
2024년 1월 8일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Run-Time Parameters에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!