Simulink代码生成(十一)——建模所使用的枚举类型的管理与自动生成

Simulink代码生成(十一)——建模所使用的枚举类型的管理与自动生成

一、通过数据字典生成枚举

在数据字典中建立枚举,最后生成代码采用的是宏定义。

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

具体生成代码如下。

/*
 * File: Enum.h
 *
 * Code generated for Simulink model 'Enum'.
 *
 * Model version                  : 1.7
 * Simulink Coder version         : 9.8 (R2022b) 05-Oct-2022
 * C/C++ source code generated on : Fri Oct 21 05:18:52 2022
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#ifndef RTW_HEADER_Enum_h_
#define RTW_HEADER_Enum_h_
#ifndef Enum_COMMON_INCLUDES_
#define Enum_COMMON_INCLUDES_
#include "rtwtypes.h"
#endif                                 /* Enum_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_Enum_T RT_MODEL_Enum_T;

#ifndef DEFINED_TYPEDEF_FOR_Enum_Type_
#define DEFINED_TYPEDEF_FOR_Enum_Type_

typedef uint8_T Enum_Type;

/* enum Enum_Type */
#define Enum_Type_OFF                  ((Enum_Type)0U)           /* Default value */
#define Enum_Type_ON                   ((Enum_Type)1U)
#endif

/* Real-time Model Data Structure */
struct tag_RTM_Enum_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 Enum_Type out;                  /* '<Root>/Switch' */

/* Model entry point functions */
extern void Enum_initialize(void);
extern void Enum_step(void);
extern void Enum_terminate(void);

/* Real-time Model object */
extern RT_MODEL_Enum_T *const Enum_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>' : 'Enum'
 */
#endif                                 /* RTW_HEADER_Enum_h_ */

/*
 * File trailer for generated code.
 *
 * [EOF]
 */

/*
 * File: Enum.c
 *
 * Code generated for Simulink model 'Enum'.
 *
 * Model version                  : 1.7
 * Simulink Coder version         : 9.8 (R2022b) 05-Oct-2022
 * C/C++ source code generated on : Fri Oct 21 05:18:52 2022
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Enum.h"
#include "rtwtypes.h"

/* Exported block signals */
real_T in;                             /* '<Root>/In1' */
Enum_Type out;                         /* '<Root>/Switch' */

/* Real-time model */
static RT_MODEL_Enum_T Enum_M_;
RT_MODEL_Enum_T *const Enum_M = &Enum_M_;

/* Model step function */
void Enum_step(void)
{
  /* Switch: '<Root>/Switch' incorporates:
   *  Inport: '<Root>/In1'
   */
  if (in > 0.0) {
    /* Switch: '<Root>/Switch' incorporates:
     *  Constant: '<Root>/Constant'
     */
    out = Enum_Type_OFF;
  } else {
    /* Switch: '<Root>/Switch' incorporates:
     *  Constant: '<Root>/Constant1'
     */
    out = Enum_Type_ON;
  }

  /* End of Switch: '<Root>/Switch' */
}

/* Model initialize function */
void Enum_initialize(void)
{
  /* (no initialization code required) */
}

/* Model terminate function */
void Enum_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */

二、通过m文件生成枚举

通常我们希望生成的枚举是通过typedef的方式,此时需要通过m文件进行生成。
假设需要生成的枚举如下

typedef struct
{
	LOW   = (uint8_t)5,
	MIDDLE= (uint8_t)6,
	HIGH  = (uint8_t)7,
} Priority_t;

在m文件中需要用classdef进行定义,这样就可以直接在模型中使用了,注意m文件名需要和定义的枚举名一致。

classdef Priority_t < Simulink.IntEnumType
    enumeration
		LOW(5)
		MIDDLE(6)
		HIGH(7)
    end
end

生成的枚举定义是我们常见的,和C语言中定义的基本一致。
在这里插入图片描述

/*
 * File: Enum.h
 *
 * Code generated for Simulink model 'Enum'.
 *
 * Model version                  : 1.9
 * Simulink Coder version         : 9.8 (R2022b) 05-Oct-2022
 * C/C++ source code generated on : Fri Oct 21 05:34:15 2022
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#ifndef RTW_HEADER_Enum_h_
#define RTW_HEADER_Enum_h_
#ifndef Enum_COMMON_INCLUDES_
#define Enum_COMMON_INCLUDES_
#include "rtwtypes.h"
#endif                                 /* Enum_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_Enum_T RT_MODEL_Enum_T;

#ifndef DEFINED_TYPEDEF_FOR_Priority_t_
#define DEFINED_TYPEDEF_FOR_Priority_t_

typedef enum {
  LOW = 5,                             /* Default value */
  MIDDLE,
  HIGH
} Priority_t;

#endif

/* Real-time Model Data Structure */
struct tag_RTM_Enum_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 Priority_t out;                 /* '<Root>/Switch' */

/* Model entry point functions */
extern void Enum_initialize(void);
extern void Enum_step(void);
extern void Enum_terminate(void);

/* Real-time Model object */
extern RT_MODEL_Enum_T *const Enum_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>' : 'Enum'
 */
#endif                                 /* RTW_HEADER_Enum_h_ */

/*
 * File trailer for generated code.
 *
 * [EOF]
 */

/*
 * File: Enum.c
 *
 * Code generated for Simulink model 'Enum'.
 *
 * Model version                  : 1.9
 * Simulink Coder version         : 9.8 (R2022b) 05-Oct-2022
 * C/C++ source code generated on : Fri Oct 21 05:34:15 2022
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: Intel->x86-64 (Windows64)
 * Code generation objectives: Unspecified
 * Validation result: Not run
 */

#include "Enum.h"
#include "rtwtypes.h"

/* Exported block signals */
real_T in;                             /* '<Root>/In1' */
Priority_t out;                        /* '<Root>/Switch' */

/* Real-time model */
static RT_MODEL_Enum_T Enum_M_;
RT_MODEL_Enum_T *const Enum_M = &Enum_M_;

/* Model step function */
void Enum_step(void)
{
  /* Switch: '<Root>/Switch' incorporates:
   *  Inport: '<Root>/In1'
   */
  if (in > 0.0) {
    /* Switch: '<Root>/Switch' incorporates:
     *  Constant: '<Root>/Constant'
     */
    out = LOW;
  } else {
    /* Switch: '<Root>/Switch' incorporates:
     *  Constant: '<Root>/Constant1'
     */
    out = HIGH;
  }

  /* End of Switch: '<Root>/Switch' */
}

/* Model initialize function */
void Enum_initialize(void)
{
  /* Registration code */

  /* block I/O */

  /* exported global signals */
  out = LOW;
}

/* Model terminate function */
void Enum_terminate(void)
{
  /* (no terminate code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */

三、通过m文件向数据字典中添加枚举

clear all;clc;
myColors = Simulink.data.dictionary.EnumTypeDefinition;
appendEnumeral(myColors,'Orange',0,'')
appendEnumeral(myColors,'Black',1,'')
appendEnumeral(myColors,'Cyan',2,'')
removeEnumeral(myColors,1)

myColors.Description = 'These are my favorite colors.';
myColors.DefaultValue = 'Cyan';
myColors.HeaderFile = 'colorsType.h';

mybool = Simulink.data.dictionary.EnumTypeDefinition;
appendEnumeral(mybool,'False',0,'')
appendEnumeral(mybool,'True',1,'')
removeEnumeral(mybool,1)


mybool.Description = 'These are my bool types.';
mybool.DefaultValue = 'False';
mybool.HeaderFile = 'boolType.h';

if exist('myNewDictionary2.sldd')
    myDictionaryObj = Simulink.data.dictionary.open('myNewDictionary2.sldd');
else  
    dd = Simulink.data.dictionary.create('myNewDictionary2.sldd');
    myDictionaryObj = Simulink.data.dictionary.open('myNewDictionary2.sldd');
end
importFromBaseWorkspace(myDictionaryObj,'varList',{'myColors','mybool'});
% importEnumTypes(myDictionaryObj,{'mybool'})

在这里插入图片描述

  • 4
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值