首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何将结构传递给Matlab中重新集成的自动生成的C++代码

如何将结构传递给Matlab中重新集成的自动生成的C++代码
EN

Stack Overflow用户
提问于 2021-03-08 01:05:16
回答 2查看 126关注 0票数 2

我正在尝试测试一些从Matlab生成到C++中的代码。

为此,我尝试将生成的C++代码集成并运行回Matlab。

这是我尝试使用的函数(在Matlab中,预自动生成)

代码语言:javascript
运行
复制
function out = getRotationMatrix(aStruct)

out = aStruct.bank_rad*aStruct.heading_rad+aStruct.pitch_rad;

end

我用这个自动生成它。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
load([pwd,'\codegen\lib\getRotationMatrix\buildInfo.mat'])

packNGo(buildInfo,'packType', 'hierarchical','fileName','portzingbit')

现在,当我尝试重新集成它时,我使用的是clib

代码语言:javascript
运行
复制
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中的参数定义为1

当我尝试使用这个函数时,我不能传递一个没有错误的结构

代码语言:javascript
运行
复制
A = clib.FrameTransformation.FrameTransformation()
thisStruct.bank_rad = 2.1;
thisStruct.heading_rad = 3.1;
thisStruct.pitch_rad = 3.1;
>> A.getRotationMatrix(thisStruct)

我得到以下错误

代码语言:javascript
运行
复制
Unrecognized method, property, or field 'getRotationMatrix' for class 'clib.FrameTransformation.FrameTransformation'.

如果重写并重新生成函数以接受非结构化输入,例如double,问题就会消失。

通过使用汇总函数,我可以看到对getRotationMatrix的调用应该是clib.FrameTransformation.struct0_T类型

但是,我似乎不能创建这种类型的变量。

代码语言:javascript
运行
复制
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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-30 22:25:53

啊,你离解决方案太近了:)

这里有一个小陷阱。

当您使用clibgen.generateLibraryDefinition()时,您必须确保所有类型定义都包含在头文件中。在您的例子中,struct0_T的定义是在getRotationMatrix_types.h文件中预先设置的。

因此,您必须使用以下命令包括这两个标头:

代码语言:javascript
运行
复制
clibgen.generateLibraryDefinition(["FrameTransformation.h","getRotationMatrix_types.h"],"PackageName","mylib",'Libraries',{'getRotationMatrix.lib'})

现在,您必须在定义文件中填充所需的其他详细信息。

完成该步骤后,可以使用以下命令成功使用该接口

代码语言:javascript
运行
复制
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中得到下面的输出

代码语言:javascript
运行
复制
    >> 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

票数 3
EN

Stack Overflow用户

发布于 2021-07-12 15:34:35

如果您有嵌入式编码器( cfg.ReplacementTypes.uint32 = 'uint32_t'建议您这样做),那么只需几行代码,您就可以使用SIL来完成您提出的任务:

代码语言:javascript
运行
复制
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.runTestcodegen -test选项在SIL中重用它们。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66519118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档