如何将结构传递到 Matlab 中重新集成的自动生成的 C++ 代码
How to pass structures into re-integrated autogenerated C++ code in Matlab
我正在尝试测试一些从 Matlab 生成到 C++ 中的代码。
为此,我尝试将生成的 C++ 代码集成 运行 回 Matlab。
这是我尝试使用的函数(在 Matlab pre-autogen 中)
function out = getRotationMatrix(aStruct)
out = aStruct.bank_rad*aStruct.heading_rad+aStruct.pitch_rad;
end
我使用这个自动生成它...
function varargout = AutoGenCode(varargin)
% Here we are attempting to generate a C++ class from a matlab function via a
% hosting function
% initialise
varargout = {};
% create instance of config object
cfg = coder.config('LIB');
% config options
cfg.MaxIdLength = int32(64);
cfg.TargetLang = 'C++';
cfg.CppPreserveClasses = 1;
cfg.EnableCustomReplacementTypes = true;
cfg.ReplacementTypes.uint32 = 'uint32_t';
cfg.CppInterfaceStyle = 'Methods';
cfg.CppInterfaceClassName = 'FrameTransformation';
cfg.FilePartitionMethod = 'MapMFileToCFile'; % or 'MapMFileToCFile' SingleFile
cfg.EnableVariableSizing = false;
%cfg.PreserveVariableNames = 'UserNames'; % Worse
%cfg.IncludeInitializeFcn = true;
cfg.PassStructByReference = true;
thisStruct.bank_rad = 2.1;
thisStruct.heading_rad = 3.1;
thisStruct.pitch_rad = 3.1;
codegen -config cfg getRotationMatrix -args {thisStruct} -report -preservearraydims -std:c++11
end
移动到另一个区域(\解压缩)我使用 packNGo
load([pwd,'\codegen\lib\getRotationMatrix\buildInfo.mat'])
packNGo(buildInfo,'packType', 'hierarchical','fileName','portzingbit')
现在,当我尝试重新集成它时,我正在使用 clib
cd 'D:\thoros\Components\Frame Transformation Service\FT_V2\unzippedCode'
clibgen.generateLibraryDefinition('FrameTransformation.h','Libraries',{'getRotationMatrix.lib'})
build(defineFrameTransformation)
addpath('D:\thoros\Components\Frame Transformation Service\FT_V2\unzippedCode\FrameTransformation')
我必须手动将 defineFrameTransformation.mlx 中的 < SHAPE > 参数定义为 1
当我尝试使用该函数时,我无法在没有错误的情况下传递结构
A = clib.FrameTransformation.FrameTransformation()
thisStruct.bank_rad = 2.1;
thisStruct.heading_rad = 3.1;
thisStruct.pitch_rad = 3.1;
>> A.getRotationMatrix(thisStruct)
我收到以下错误
Unrecognized method, property, or field 'getRotationMatrix' for class 'clib.FrameTransformation.FrameTransformation'.
如果重写并重新生成接受非结构输入的函数,问题就会消失。双.
通过使用汇总函数,我可以看到对 getRotationMatrix 的调用应该是 clib.FrameTransformation.struct0_T
类型
但是我似乎无法创建这种类型的变量。
summary(defineFrameTransformation)
MATLAB Interface to FrameTransformation Library
Class clib.FrameTransformation.struct0_T
No Constructors defined
No Methods defined
No Properties defined
Class clib.FrameTransformation.FrameTransformation
Constructors:
clib.FrameTransformation.FrameTransformation()
clib.FrameTransformation.FrameTransformation(clib.FrameTransformation.FrameTransformation)
Methods:
double getRotationMatrix(clib.FrameTransformation.struct0_T)
No Properties defined
那么应该如何将结构传递给 Matlab 中的 C++ 库???我做错了什么。
注意 Matlab 2020B,windows,Visual studio 2019
啊,你离解决方案太近了:)
那里有一个小问题。
当您使用 clibgen.generateLibraryDefinition()
时,您必须确保所有类型定义都包含在 header 文件中。在您的情况下,struct0_T
的定义是 getRotationMatrix_types.h
文件中的 presnet。
因此您必须使用以下命令同时包含 headers :
clibgen.generateLibraryDefinition(["FrameTransformation.h","getRotationMatrix_types.h"],"PackageName","mylib",'Libraries',{'getRotationMatrix.lib'})
现在您必须填写定义文件中所需的其他详细信息。
完成该步骤后,您可以使用以下命令成功使用界面
build(definemylib)
addpath('\mathworks\home\dramakan\MLAnswers\CLIB_With_Codegen\ToPort\portzingbit\mylib')
A = clib.mylib.FrameTransformation
s = clib.mylib.struct0_T
s.bank_rad =2.1;
s.heading_rad =3.1;
s.pitch_rad=3.1;
A.getRotationMatrix(s)
我可以在 R2021b 中获得低于输出的结果
>> A.getRotationMatrix(s)
ans =
9.6100
另外检查你是否可以使用 MATLAB Coder MEX 而不是做所有这些马戏团。您可以使用 MEX 在 MATLAB 中直接调用生成的代码。您可以参考以下文档:
https://www.mathworks.com/help/coder/gs/generating-mex-functions-from-matlab-code-at-the-command-line.html
如果您有 Embedded Coder(cfg.ReplacementTypes.uint32 = 'uint32_t'
行建议您这样做),那么您只需几行代码就可以使用 SIL 来完成您提议的工作:
cfg.VerificationMode = 'SIL';
thisStruct.bank_rad = 2.1;
thisStruct.heading_rad = 3.1;
thisStruct.pitch_rad = 3.1;
codegen ...
% Now test the generated code. This calls your actual generated code
% and automatically does all of the necessary data conversion. The code
% is run in a separate process and outputs are returned back
% to MATLAB
getRotationMatrix_sil(thisStruct)
更多信息在 doc 中。如果您的测试/脚本已经在运行您的 MATLAB 代码,您可以通过检查 coder.runTest
和 codegen -test
选项将它们与 SIL 一起重用。
我正在尝试测试一些从 Matlab 生成到 C++ 中的代码。
为此,我尝试将生成的 C++ 代码集成 运行 回 Matlab。
这是我尝试使用的函数(在 Matlab pre-autogen 中)
function out = getRotationMatrix(aStruct)
out = aStruct.bank_rad*aStruct.heading_rad+aStruct.pitch_rad;
end
我使用这个自动生成它...
function varargout = AutoGenCode(varargin)
% Here we are attempting to generate a C++ class from a matlab function via a
% hosting function
% initialise
varargout = {};
% create instance of config object
cfg = coder.config('LIB');
% config options
cfg.MaxIdLength = int32(64);
cfg.TargetLang = 'C++';
cfg.CppPreserveClasses = 1;
cfg.EnableCustomReplacementTypes = true;
cfg.ReplacementTypes.uint32 = 'uint32_t';
cfg.CppInterfaceStyle = 'Methods';
cfg.CppInterfaceClassName = 'FrameTransformation';
cfg.FilePartitionMethod = 'MapMFileToCFile'; % or 'MapMFileToCFile' SingleFile
cfg.EnableVariableSizing = false;
%cfg.PreserveVariableNames = 'UserNames'; % Worse
%cfg.IncludeInitializeFcn = true;
cfg.PassStructByReference = true;
thisStruct.bank_rad = 2.1;
thisStruct.heading_rad = 3.1;
thisStruct.pitch_rad = 3.1;
codegen -config cfg getRotationMatrix -args {thisStruct} -report -preservearraydims -std:c++11
end
移动到另一个区域(\解压缩)我使用 packNGo
load([pwd,'\codegen\lib\getRotationMatrix\buildInfo.mat'])
packNGo(buildInfo,'packType', 'hierarchical','fileName','portzingbit')
现在,当我尝试重新集成它时,我正在使用 clib
cd 'D:\thoros\Components\Frame Transformation Service\FT_V2\unzippedCode'
clibgen.generateLibraryDefinition('FrameTransformation.h','Libraries',{'getRotationMatrix.lib'})
build(defineFrameTransformation)
addpath('D:\thoros\Components\Frame Transformation Service\FT_V2\unzippedCode\FrameTransformation')
我必须手动将 defineFrameTransformation.mlx 中的 < SHAPE > 参数定义为 1
当我尝试使用该函数时,我无法在没有错误的情况下传递结构
A = clib.FrameTransformation.FrameTransformation()
thisStruct.bank_rad = 2.1;
thisStruct.heading_rad = 3.1;
thisStruct.pitch_rad = 3.1;
>> A.getRotationMatrix(thisStruct)
我收到以下错误
Unrecognized method, property, or field 'getRotationMatrix' for class 'clib.FrameTransformation.FrameTransformation'.
如果重写并重新生成接受非结构输入的函数,问题就会消失。双.
通过使用汇总函数,我可以看到对 getRotationMatrix 的调用应该是 clib.FrameTransformation.struct0_T
类型但是我似乎无法创建这种类型的变量。
summary(defineFrameTransformation)
MATLAB Interface to FrameTransformation Library
Class clib.FrameTransformation.struct0_T
No Constructors defined
No Methods defined
No Properties defined
Class clib.FrameTransformation.FrameTransformation
Constructors:
clib.FrameTransformation.FrameTransformation()
clib.FrameTransformation.FrameTransformation(clib.FrameTransformation.FrameTransformation)
Methods:
double getRotationMatrix(clib.FrameTransformation.struct0_T)
No Properties defined
那么应该如何将结构传递给 Matlab 中的 C++ 库???我做错了什么。
注意 Matlab 2020B,windows,Visual studio 2019
啊,你离解决方案太近了:)
那里有一个小问题。
当您使用 clibgen.generateLibraryDefinition()
时,您必须确保所有类型定义都包含在 header 文件中。在您的情况下,struct0_T
的定义是 getRotationMatrix_types.h
文件中的 presnet。
因此您必须使用以下命令同时包含 headers :
clibgen.generateLibraryDefinition(["FrameTransformation.h","getRotationMatrix_types.h"],"PackageName","mylib",'Libraries',{'getRotationMatrix.lib'})
现在您必须填写定义文件中所需的其他详细信息。
完成该步骤后,您可以使用以下命令成功使用界面
build(definemylib)
addpath('\mathworks\home\dramakan\MLAnswers\CLIB_With_Codegen\ToPort\portzingbit\mylib')
A = clib.mylib.FrameTransformation
s = clib.mylib.struct0_T
s.bank_rad =2.1;
s.heading_rad =3.1;
s.pitch_rad=3.1;
A.getRotationMatrix(s)
我可以在 R2021b 中获得低于输出的结果
>> A.getRotationMatrix(s)
ans =
9.6100
另外检查你是否可以使用 MATLAB Coder MEX 而不是做所有这些马戏团。您可以使用 MEX 在 MATLAB 中直接调用生成的代码。您可以参考以下文档: https://www.mathworks.com/help/coder/gs/generating-mex-functions-from-matlab-code-at-the-command-line.html
如果您有 Embedded Coder(cfg.ReplacementTypes.uint32 = 'uint32_t'
行建议您这样做),那么您只需几行代码就可以使用 SIL 来完成您提议的工作:
cfg.VerificationMode = 'SIL';
thisStruct.bank_rad = 2.1;
thisStruct.heading_rad = 3.1;
thisStruct.pitch_rad = 3.1;
codegen ...
% Now test the generated code. This calls your actual generated code
% and automatically does all of the necessary data conversion. The code
% is run in a separate process and outputs are returned back
% to MATLAB
getRotationMatrix_sil(thisStruct)
更多信息在 doc 中。如果您的测试/脚本已经在运行您的 MATLAB 代码,您可以通过检查 coder.runTest
和 codegen -test
选项将它们与 SIL 一起重用。