Problem with Using codegen commands to generate C++ code on NLMPC Code Generation Tutorial

조회 수: 5 (최근 30일)
I am trying to use the codegen funciton in MATLAB to chagne the code provided in the NLMPC Code Generation Turtorial so that it generates code in C++ instead of MEX files. When I run the tutorial with the original codegen functions it generates the MEX file completely fine but after changing the codegen arguments to generate C++ I continue to get an error. Below is the code I am running (note that my problem is at the very bottom):
nlobj = nlmpc(4,2,1);
Ts = 0.1;
nlobj.Ts = Ts;
nlobj.PredictionHorizon = 10;
nlobj.ControlHorizon = 5;
nlobj.Model.StateFcn = "pendulumDT0";
nlobj.Model.IsContinuousTime = false;
nlobj.Model.NumberOfParameters = 1;
params = {Ts};
nlobj.Model.OutputFcn = "pendulumOutputFcn";
nlobj.Weights.OutputVariables = [3 3];
nlobj.Weights.ManipulatedVariablesRate = 0.1;
nlobj.OV(1).Min = -10;
nlobj.OV(1).Max = 10;
nlobj.MV.Min = -100;
nlobj.MV.Max = 100;
x0 = [0.1;0.2;-pi/2;0.3];
u0 = 0.4;
validateFcns(nlobj,x0,u0,[],params);
EKF = extendedKalmanFilter(@pendulumStateFcn,@pendulumMeasurementFcn);
x0 = [0;0;-pi;0];
y0 = [x0(1);x0(3)];
EKF.State = x0;
mv0 = 0;
[coreData,onlineData] = getCodeGenerationData(nlobj,x0,mv0,params);
onlineData.ref = [0 0];
mv = mv0;
y = y0;
x = x0;
Duration = 10;
xHistory = x0;
for ct = 1:(Duration/Ts)
% Correct previous prediction
xk = correct(EKF,y);
% Compute optimal control move
[mv,onlineData] = nlmpcmoveCodeGeneration(coreData,xk,mv,onlineData);
% Predict prediction model states for the next iteration
predict(EKF,[mv; Ts]);
% Implement first optimal control move
x = pendulumDT0(x,mv,Ts);
% Generate sensor data
y = x([1 3]) + randn(2,1)*0.01;
% Save plant states
xHistory = [xHistory x];
end
%%%%%%This is where I am having problems:%%%%%%
func = 'nlmpcmoveCodeGeneration';
funcOutput = 'testingC';
codegen -config:lib -lang:c++ func;
cfg = coder.config('dll');
cfg.TargetLang = 'C++';
codegen -config cfg func;
Please let me know if there is anyway to format/utilize codegen differnetly to generate this C++ code.

답변 (1개)

Emmanouil Tzorakoleftherakis
Emmanouil Tzorakoleftherakis 2023년 3월 21일
편집: Emmanouil Tzorakoleftherakis 2023년 3월 24일
You did not specify what kind of error you were seeing? In my case, doing the following worked:
func = 'nlmpcmoveCodeGeneration';
funcOutput = 'nlmpcmoveMEX';
Cfg = coder.config('lib');
Cfg.TargetLang = 'C++';
Cfg.DynamicMemoryAllocation = 'off';
codegen('-config',Cfg,func,'-o',funcOutput,'-args',...
{coder.Constant(coreData),xk,mv,onlineData});
  댓글 수: 2
Carson Murray
Carson Murray 2023년 3월 23일
Hey Emmanouil,
The code you provided did work successfully when I ran it but is there anyway you coould modify it to generate C++ code instead of standard C? That would be very helpful.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB Coder에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by