Cannot generate C code from MPC object

9 ビュー (過去 30 日間)
Andy Zelenak
Andy Zelenak 2024 年 1 月 29 日
コメント済み: Andy Zelenak 2024 年 2 月 1 日
I have a Model Predictive Controller designed in MATLAB. I can generate MEX for it like so:
cfg = coder.config('mex');
cfg.EnableDynamicMemoryAllocation = 'off';
[coredata, statedata, onlinedata] = getCodeGenerationData(nav_mpc);
mexfun = buildMEX(nav_mpc, 'mpcmoveMEX', coredata, statedata, onlinedata, cfg);
However, I can't generate a C or C++ library, like this:
cfg = coder.config('lib');
The error is:
Error using buildMEX
Expected "mexconfig" to be one of these types:
coder.MexCodeConfig
Instead its type was coder.CodeConfig.
Error in mpc/buildMEX (line 105)
validateattributes(cfg,{'coder.MexCodeConfig'},{'scalar'},'buildMEX','"mexconfig"');
Error in run_mpc (line 45)
mexfun = buildMEX(nav_mpc, 'mpcmoveMEX', coredata, statedata, onlinedata, cfg);
According to the documentation here, I should be able to do this:
"You can change the code generation target to C/C++ static library, dynamic library, executable, etc. by using a different set of coder.config settings."
  2 件のコメント
Ji Lee
Ji Lee 2024 年 1 月 31 日
The cited documentation passage can be misleading. I have passed this observation to the team responsible for the Model Predictive Control Toolbox.
What the documentation intends to say is that the codegen function offered by MATLAB Coder can be used to handle the full range of code generation targets while the buildMEX method of mpc can handle MEX generation. In other words, one would use codegen to generate libraries or executables and buildMex to generate MEX functions. The example code in the documentation passage utilizes buildMex to demonstrate the MEX-specific workflow.
Andy Zelenak
Andy Zelenak 2024 年 1 月 31 日
OK. So I have the Mex function now; can I get a C/C++ library out of it?
I'm aware of MexCallMATLAB() but it seems hard to use and (I think) it requires MATLAB to be installed on the realtime system where I want to run MPC.
Bottom line: how do I get this MPC controller out of MATLAB onto a PC that runs C with no MATLAB?

サインインしてコメントする。

採用された回答

Emmanouil Tzorakoleftherakis
Emmanouil Tzorakoleftherakis 2024 年 1 月 31 日
Please take a look at the example here that uses 'codegen' command.
  3 件のコメント
Emmanouil Tzorakoleftherakis
Emmanouil Tzorakoleftherakis 2024 年 2 月 1 日
The code you posted in the question was using 'buildMex' instead of 'codegen'. 'buildMex' is a wrapper around codegen to make it easier to generate mex files without going into the (harder) syntax of codegen. For generating c libraries, etc you need to use 'codegen'. Now, the example I shared above (the code snippet you shared) is still generating a mex file because that what the following line tells it to do.
Cfg = coder.config('mex');
Take a look at the pages here and here to see what options you have instead of 'mex' and for more examples on how to use 'codegen'.
Hope that helps
Andy Zelenak
Andy Zelenak 2024 年 2 月 1 日
ok, that works. Thanks!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCode Generation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by