Simulink代码生成(七)——提升代码生成可读性的两种方法
一、通过ExportedGlobal:
默认代码的名字是源于模块的名字,当给信号线命名后,生成代码的名称会显示我们定义的信号线的名字。
新版的matlab点击信号线右击->属性已经没有code generation的选项,现在推荐用的是信号线关联的方式
二、通过信号关联:
采用信号线关联的方式,可以将模型和数据分离开来,便于管理
通过建立信号和参数的对象,对数据进行管理
然后信号线右击->属性,勾选关联信号对象,出现蓝色的叉子表示关联成功
in = Simulink.Signal;
in.CoderInfo.StorageClass = "ExportedGlobal";
out = Simulink.Signal;
out.CoderInfo.StorageClass = "ExportedGlobal";
k1 = Simulink.Parameter;
k1.Value = 1.5;
k1.CoderInfo.StorageClass = "ExportedGlobal";
k2 = Simulink.Parameter;
k2.Value = 2;
k2.CoderInfo.StorageClass = "ExportedGlobal";
k3 = Simulink.Parameter;
k3.Value = 0;
k3.CoderInfo.StorageClass = "ExportedGlobal";
生成的代码和手写类似
/*
* File: untitled.h
*
* Code generated for Simulink model 'untitled'.
*
* Model version : 1.5
* Simulink Coder version : 9.8 (R2022b) 05-Oct-2022
* C/C++ source code generated on : Wed Oct 19 03:27:33 2022
*
* Target selection: ert.tlc
* Embedded hardware selection: NXP->Cortex-M3
* Code generation objectives: Unspecified
* Validation result: Not run
*/
#ifndef RTW_HEADER_untitled_h_
#define RTW_HEADER_untitled_h_
#ifndef untitled_COMMON_INCLUDES_
#define untitled_COMMON_INCLUDES_
#include "rtwtypes.h"
#endif /* untitled_COMMON_INCLUDES_ */
/* Macros for accessing real-time model data structure */
#ifndef rtmGetErrorStatus
#define rtmGetErrorStatus(rtm) ((rtm)->errorStatus)
#endif
#ifndef rtmSetErrorStatus
#define rtmSetErrorStatus(rtm, val) ((rtm)->errorStatus = (val))
#endif
/* Forward declaration for rtModel */
typedef struct tag_RTM_untitled_T RT_MODEL_untitled_T;
/* Real-time Model Data Structure */
struct tag_RTM_untitled_T {
const char_T * volatile errorStatus;
};
/*
* Exported Global Signals
*
* Note: Exported global signals are block signals with an exported global
* storage class designation. Code generation will declare the memory for
* these signals and export their symbols.
*
*/
extern real_T in; /* '<Root>/In1' */
extern real_T out; /* '<Root>/Switch' */
/*
* Exported Global Parameters
*
* Note: Exported global parameters are tunable parameters with an exported
* global storage class designation. Code generation will declare the memory for
* these parameters and exports their symbols.
*
*/
extern real_T k1; /* Variable: k1
* Referenced by: '<Root>/Gain'
*/
extern real_T k2; /* Variable: k2
* Referenced by: '<Root>/Gain1'
*/
extern real_T k3; /* Variable: k3
* Referenced by: '<Root>/Switch'
*/
/* Model entry point functions */
extern void untitled_initialize(void);
extern void untitled_step(void);
extern void untitled_terminate(void);
/* Real-time Model object */
extern RT_MODEL_untitled_T *const untitled_M;
/*-
* The generated code includes comments that allow you to trace directly
* back to the appropriate location in the model. The basic format
* is <system>/block_name, where system is the system number (uniquely
* assigned by Simulink) and block_name is the name of the block.
*
* Use the MATLAB hilite_system command to trace the generated code back
* to the model. For example,
*
* hilite_system('<S3>') - opens system 3
* hilite_system('<S3>/Kp') - opens and selects block Kp which resides in S3
*
* Here is the system hierarchy for this model
*
* '<Root>' : 'untitled'
*/
#endif /* RTW_HEADER_untitled_h_ */
/*
* File trailer for generated code.
*
* [EOF]
*/
/*
* File: untitled.c
*
* Code generated for Simulink model 'untitled'.
*
* Model version : 1.5
* Simulink Coder version : 9.8 (R2022b) 05-Oct-2022
* C/C++ source code generated on : Wed Oct 19 03:27:33 2022
*
* Target selection: ert.tlc
* Embedded hardware selection: NXP->Cortex-M3
* Code generation objectives: Unspecified
* Validation result: Not run
*/
#include "untitled.h"
#include "rtwtypes.h"
/* Exported block signals */
real_T in; /* '<Root>/In1' */
real_T out; /* '<Root>/Switch' */
/* Exported block parameters */
real_T k1 = 1.5; /* Variable: k1
* Referenced by: '<Root>/Gain'
*/
real_T k2 = 2.0; /* Variable: k2
* Referenced by: '<Root>/Gain1'
*/
real_T k3 = 0.0; /* Variable: k3
* Referenced by: '<Root>/Switch'
*/
/* Real-time model */
static RT_MODEL_untitled_T untitled_M_;
RT_MODEL_untitled_T *const untitled_M = &untitled_M_;
/* Model step function */
void untitled_step(void)
{
/* Switch: '<Root>/Switch' incorporates:
* Inport: '<Root>/In1'
*/
if (in > k3) {
/* Switch: '<Root>/Switch' incorporates:
* Gain: '<Root>/Gain'
*/
out = k1 * in;
} else {
/* Switch: '<Root>/Switch' incorporates:
* Gain: '<Root>/Gain1'
*/
out = k2 * in;
}
/* End of Switch: '<Root>/Switch' */
}
/* Model initialize function */
void untitled_initialize(void)
{
/* (no initialization code required) */
}
/* Model terminate function */
void untitled_terminate(void)
{
/* (no terminate code required) */
}
/*
* File trailer for generated code.
*
* [EOF]
*/
选择不同的代码生成方式,生成的代码形式有所不同
添加标志符后,原参数或者信号名会被新添加的标识符替代。
- Auto:以结构体的形式呈现
- Model default:以结构体的方式呈现
- ImportedExtern:以全局变量的形式呈现,
- ImportedExternPointer:以指针的形式呈现,
- ExportedGlobal:以全局变量的形式呈现,直接在源文件中定义
- BitField:以带BitField的结构体呈现
- Volatile:以带Volatile修饰符的形式呈现
- Const: 以带Const修饰符的形式呈现
- Const Volatile: 以带Const Volatile修饰符的形式呈现
- ExportToFile:生成新的头文件和源文件,并在文件中定义
- Define:将变量进行宏定义
- ImportedDefine:需要添加一个头文件,生成代码中会显示如果没有定义该变量,会报错
- Localizable:全局变量
- FileScope:信号选择这个编译会报错,参数选择这个会变为static变量
- Struct:生成 rt_Simulink_Struct的结构体
- CompileFlag:和ImportedDefine类似
- Reusable:全局变量,输入和输出设置为同名
- GetSet:要结合data store memory和data store read, data store write模块实现,类似matlab的getparam和set param,获取外部的信号,如GPIO_Read, 写到GPIO_Write.