尝试过的方法:
1. coder.extrinsic('quadprog'); 只能适用于能运行matlab的平台(调用matlab引擎),无法生成embed code
2. MEX 生成mex文件,要求生成mex时quadprog传进去确定的参数H,f,A,b,Aeq,beq,opt。 同样不适用于embed code,只在matlab2019b以后版本兼容。命令:
codegen -config:mex test_quadp
3. 将quadprog函数生成dll文件,直升使用用友操作系统的平台,不适用于embed code 。
4. 先用codegen 生成C/C++代码,在matlabfunction中用coder.ceval.电泳c函数????
经过老夫多次试验,终于呼之欲出:
- 关于quadprog生成embed C代码的办法如下:
使用条件:quadprog(H,f,A,b,Aeq,beq,x0)中的H,f,A,b,Aeq,beq,x0矩阵(或向量)的维度必须固定下来,知识维度固定,值可以实时改变。
方法:s-function builder + mex + matlab function + embedded Coder
1. matlbb function:负责H,f,A,b,Aeq,beq,x0的每个采样时刻的赋值工作。
2. embedded coder负责将H,f,A,b,Aeq,beq,x0矩阵(或向量)维度固定的quadprog函数(matlab系统函数)转换成C代码。
3. mex负责将第2步中生成的C代码mex成 s-function builder 可以调用的代码。
4. s-function builder : 负责调用第三步生成的代码。
slx图:
matlab fuction 如下:
function [H,f,A,b] = fcn()
H = [2 -1; -1 2];
f = [-5; -2];
A = [1 1; -1 2; 2 1];
b = [2; 2; 3];
s-function builder 模块:
diagnostic Viewer 对生成embed code的报告:
### Starting build procedure for: myquadprog
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: E:\matlabworkspace\matlab2020a\quadprog1\myquadprog_ert_rtw
### Invoking Target Language Compiler on myquadprog.rtw
### Using System Target File: E:\Program Files\MATLAB\R2020a\rtw\c\ert\ert.tlc
### Loading TLC function libraries
### Generating TLC interface API for custom data
### Initial pass through model to cache user defined code
### Caching model source code
### Writing header file myquadprog_types.h
### Writing source file myquadprog.c
### Writing header file myquadprog_private.h
### Writing header file myquadprog.h
.
### Writing header file rtwtypes.h
### Writing source file ert_main.c
### TLC code generation complete.
### Using toolchain: Microsoft Visual C++ 2015 v14.0 | nmake (64-bit Windows)
### Creating 'E:\matlabworkspace\matlab2020a\quadprog1\myquadprog_ert_rtw\myquadprog.mk' ...
### Building 'myquadprog': nmake -f myquadprog.mk all
E:\matlabworkspace\matlab2020a\embedcode\2\codegen\lib\quadprog1\myquadprog1_wrapper.c(38): warning C4100: “y1”: 未引用的形参
E:\matlabworkspace\matlab2020a\embedcode\2\codegen\lib\quadprog1\myquadprog1_wrapper.c(37): warning C4100: “y0”: 未引用的形参
E:\matlabworkspace\matlab2020a\embedcode\2\codegen\lib\quadprog1\myquadprog1_wrapper.c(36): warning C4100: “b1”: 未引用的形参
E:\matlabworkspace\matlab2020a\embedcode\2\codegen\lib\quadprog1\myquadprog1_wrapper.c(35): warning C4100: “A1”: 未引用的形参
E:\matlabworkspace\matlab2020a\embedcode\2\codegen\lib\quadprog1\myquadprog1_wrapper.c(34): warning C4100: “f1”: 未引用的形参
E:\matlabworkspace\matlab2020a\embedcode\2\codegen\lib\quadprog1\myquadprog1_wrapper.c(33): warning C4100: “H1”: 未引用的形参
cl -c -nologo -GS -W4 -DWIN32 -D_MT -MT -D_CRT_SECURE_NO_WARNINGS /Od /Oy- -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTERMFCN=1 -DONESTEPFCN=1 -DMAT_FILE=0 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0 -DTID01EQ=0 -DMODEL=myquadprog -DNUMST=1 -DNCSTATES=0 -DHAVESTDIO -DMODEL_HAS_DYNAMICALLY_LOADED_SFCNS=0 @myquadprog_comp.rsp -Fo"ert_main.obj" E:\matlabworkspace\matlab2020a\quadprog1\myquadprog_ert_rtw\ert_main.c
ert_main.c
### Creating standalone executable "..\myquadprog.exe" ...
link /RELEASE /INCREMENTAL:NO /NOLOGO kernel32.lib ws2_32.lib mswsock.lib advapi32.lib -out:..\myquadprog.exe @myquadprog.rsp
### Created: ..\myquadprog.exe
### Successfully generated all binary outputs.
### Successful completion of build procedure for: myquadprog
### Simulink cache artifacts for 'myquadprog' were created in 'E:\matlabworkspace\matlab2020a\quadprog1\myquadprog.slxc'.
Build process completed successfully
设置:
注意问题:
s-funtion builder 的最后一步mex时,需要将所有C文件包含进去,不然会报错:“error LNK2019: 无法解析的外部符号”
如:
mex -output mpc_sfunc *.c 其中 mpc_sfunc是sfunction builder中的文件名
或老老实实:
mex myquadprog1.c myquadprog1_wrapper.c quadprog1.c addBoundToActiveSetMatrix_.c checkStoppingAndUpdateFval.c compute_deltax.c compute_lambda.c computeFirstOrderOpt.c computeFval.c computeFval_ReuseHx.c computeGrad_StoreHx.c computeQ_.c countsort.c deleteColMoveEnd.c driver.c factor.c factorQR.c factorQRE.c factoryConstruct.c feasibleratiotest.c feasibleX0ForWorkingSet.c fullColLDL2_.c iterate.c linearForm_.c main.c maxConstraintViolation.c myquadprog1.c myquadprog1_wrapper.c partialColLDL3_.c phaseone.c PresolveWorkingSet.c quadprog1.c quadprog1_data.c quadprog1_initialize.c quadprog1_terminate.c RemoveDependentIneq_.c rt_nonfinite.c rtGetInf.c rtGetNaN.c setProblemType.c solve.c squareQ_appendCol.c xgemm.c xgeqp3.c xnrm2.c xrotg.c xzgeqp3.c xzlarf.c xzlarfg.c
结果展示:
当参数为
H = [2 -1; -1 2];
f = [-1; -2];
A = [1 1; -1 2; 2 1];
b = [2; 2; 3];
结果为: 1.167,0.833
参考文献:
matlab转C语言的操作步骤(1)——基本操作_野生猿-群号1025127672-CSDN博客_matlab转c语言
一文教你快速学会在matlab的simulink中调用C语言进行仿真_GREYWALL-CSDN博客_simulink调用c语言函数