Automatic code generation of c++ code from matlab functions

2 ビュー (過去 30 日間)
Michael Knörzer
Michael Knörzer 2021 年 1 月 7 日
コメント済み: Michael Knörzer 2021 年 1 月 17 日
Hi everyone,
I would like to generate c++ code from a automatically generated matlab function.
The matlab function header looks like this:
function fd = e7bd_nonlinsys_f(in1,in2,Ts)
with
fd and in1 being single 6x1 vectors, in2 being a single 2x1 vector and Ts being a single scalar.
The code for the code generator looks like this:
% Set configuration for Code Generation
cfg = coder.config('lib');
cfg.GenCodeOnly = true; % Code generator produces C++ source code, but does not invoke make
% command or build object
%cfg.GenerateExampleMain = 'DoNotGenerate'; % Code Generator does not generate an example main function
cfg.TargetLang = 'C++';
cfg.CastingMode = 'Standards'; % Data type casting conforms to MISRA standards
%cfg.GenerateReport = true;
%cfg.GenerateCodeMetricsReport = true; % Generate Metrics report for static code analyzation
cfg.CppInterfaceStyle = 'Functions'; % = 'Methods';
%cfg.CppInterfaceClassName = 'e7bd_linsys_class';
cfg.HighlightPotentialDataTypeIssues = true; % The code generation report highlights MATLAB code that
% results in single-precision or double-precision operations
% in the generated C/C++ code. If you have Fixed-Point Designer™,
% the report also highlights expressions in the MATLAB code that
% result in expensive fixed-point operations in the generated code.
cfg.PreserveArrayDimensions = true; % Generate code that uses N-dimensional indexing
%cfg.PurelyIntegerCode = true; % The Code generator does not allow floating point data or operation.
cfg.SaturateOnIntegerOverflow = true; % Code generator produces code to handle integer overflow
% Generate C++-Code of function e7bd_nonlinsys_f
codegen e7bd_nonlinsys_f -config cfg -v...
-args { single(zeros(6,1)), single(zeros(2,1)), single(0.02) }
As a result, I get several c++/h - header files, implementing checks for initialization of the vectors, checking data types and values of the input parameter and also a termination function.
The core c++ function itself, looks like this:
// Function Definitions
//
// E7BD_NONLINSYS_F
// FD = E7BD_NONLINSYS_F(IN1,IN2,TS)
// Arguments : const float in1[6]
// const float in2[2]
// float Ts
// float fd[6]
// Return Type : void
//
void e7bd_nonlinsys_f(const float in1[6], const float in2[2], float Ts, float
fd[6])
I have several questions regarding this generated code:
  1. The resulting c++ function has the output vector fd as input parameter and void as return type. However, I would like to generate a function returning the vector fd just like the matlab function does. How can I change that?
  2. Ideally, I would like to get a header file only, containing both the declaration and the definition of the function e7bd_nonlinsys_f. How can I omit the generation of the other helper files and compromise the e7bd_nonlinsys_f.cpp and e7bd_nonlinsys_f.h - file in one .h - file only?
Regarding question 2 - The ideal generated code (c++-header file only) should look somewhat like this:
e7bd_nonlinsys_f.h :
float[6] e7bd_nonlinsys_f(const float in1[6], const float in2[2], float Ts)
{
< calculation>
return fd;
}
I´m using Matlab R2020a, MATLAB Coder, Embedded Coder and the Symbolic math toolbox.
I furthermore also tried the ccode command which only generated the assignment of the mathematical term to the matrix without declaring/defining the function or any other part of the header file (e.g. including necessary libraries and so on).
Thanks for your help :)
Michael

回答 (1 件)

Ryan Livingston
Ryan Livingston 2021 年 1 月 10 日
  1. There is not an option to return arrays as a return value from the generated functions. The documentation describes the expected generated interfaces. In C, returning an array as a return parameter doesn't really make much sense either.
  2. There is not a way to generate inline implementations in a header file. MATLAB Coder will always put the definitions in a C or CPP file. The FilePartitionMethod config settting will allow you package the code into fewer files.
  3 件のコメント
Ryan Livingston
Ryan Livingston 2021 年 1 月 11 日
編集済み: Ryan Livingston 2021 年 1 月 11 日
This answer:
has a good discussion of returning arrays in C. Returning an array by value isn't supported in C. The common idioms are laid out there: allocate memory in the callee and return that or allocate memory in the caller and pass that to the callee. The generated code uses the second idiom.
Michael Knörzer
Michael Knörzer 2021 年 1 月 17 日
Ok, thanks for sharing!

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

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by