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

19 Ansichten (letzte 30 Tage)
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.

Antworten (1)

Emmanouil Tzorakoleftherakis
Emmanouil Tzorakoleftherakis am 21 Mär. 2023
Bearbeitet: Emmanouil Tzorakoleftherakis am 24 Mär. 2023
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 Kommentare
Carson Murray
Carson Murray am 23 Mär. 2023
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.

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by