The Simulink self-contained tools are used to generate C and C + + code, where C code can be used without modification for C engineering, and because the generated C + + code is a standard value code, and Qt is a C + + implementation, invoking Simulink-generated C + + programs can also work correctly.
Software version:
simulink:2016a
qt:5.7
1, build a simple mathematical operation model, this model has no input, only one output. As follows:
2, configuring the model
2.1 Set the model to a fixed step, run end time INF.
2.2 Set the output format to "Structure" or set it to another, such as an array
2.3 Select the code generation language C + +, set as follows, where the target file is GRT.TLC, which is generic real-time target.
2.4 Setting the Code interface in C + + class, there are some inexplicable errors that can be avoided in QT calls.
2.5 Check the settings, click the "Check Model" button in the 2.3 step, check the configuration is correct, if there is an error or warning, follow the recommended settings, the final result is as follows:
3, compile the model, CTRL+B or click the button below
4, after compiling will be in the current directory to generate the suffix Grt_rtw folder, which is the compiled program.
5, create a simple widght program in QT with the name Testmodel.
6, add the. h,.cpp file that you generated in step 4 to the QT project, and add all the dependent. h files (in the MATLAB installation file), and the resulting additions are as follows:
7, add the following code in the Mainwindow.cpp constructor (the associated variable declaration is not listed separately):
New Testmodelclass (); Pmodel, Initialize (); Pmodel, Step (); Qdebug ()<<"simulink output:"<<pModel->test_Y.Out2;
8, run the QT program output results as follows, and the expected consistency:
12
The above indicates that QT can invoke the C + + code of Simulink, and further the input of the model can be set in the QT program.
QT calls Simulink auto-generated C + + code