Simulink代码生成(十)——数据别名控制及代码生成中的控制
一、模型结构:
如果需要按自定义的数据类型生成代码,则需要设置下面这里。
设置完之后,需要添加自定义的数据类型,注意DataScope只能选择Imported,选择Exported在生成代码的时候会报错,添加的User.h最后会被添加到rtwtypes.h中。
注意结合自己的代码时,需要包含User.h
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";
User_double = Simulink.AliasType;
User_double.DataScope = 'Imported';
User_double.HeaderFile = 'User.h';
生成的代码如下。
/*
* File: untitled.h
*
* Code generated for Simulink model 'untitled'.
*
* Model version : 1.9
* Simulink Coder version : 9.8 (R2022b) 05-Oct-2022
* C/C++ source code generated on : Thu Oct 20 02:23:53 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 User_double in; /* '<Root>/In1' */
extern User_double 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 User_double k1; /* Variable: k1
* Referenced by: '<Root>/Gain'
*/
extern User_double k2; /* Variable: k2
* Referenced by: '<Root>/Gain1'
*/
extern User_double 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.9
* Simulink Coder version : 9.8 (R2022b) 05-Oct-2022
* C/C++ source code generated on : Thu Oct 20 02:23:53 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 */
User_double in; /* '<Root>/In1' */
User_double out; /* '<Root>/Switch' */
/* Exported block parameters */
User_double k1 = 1.5; /* Variable: k1
* Referenced by: '<Root>/Gain'
*/
User_double k2 = 2.0; /* Variable: k2
* Referenced by: '<Root>/Gain1'
*/
User_double 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]
*/
/*
* File: rtwtypes.h
*
* Code generated for Simulink model 'untitled'.
*
* Model version : 1.9
* Simulink Coder version : 9.8 (R2022b) 05-Oct-2022
* C/C++ source code generated on : Thu Oct 20 02:23:53 2022
*
* Target selection: ert.tlc
* Embedded hardware selection: NXP->Cortex-M3
* Code generation objectives: Unspecified
* Validation result: Not run
*/
#ifndef RTWTYPES_H
#define RTWTYPES_H
/* Logical type definitions */
#if (!defined(__cplusplus))
#ifndef false
#define false (0U)
#endif
#ifndef true
#define true (1U)
#endif
#endif
/*=======================================================================*
* Target hardware information
* Device type: NXP->Cortex-M3
* Number of bits: char: 8 short: 16 int: 32
* long: 32
* native word size: 32
* Byte ordering: LittleEndian
* Signed integer division rounds to: Zero
* Shift right on a signed integer as arithmetic shift: on
*=======================================================================*/
/*=======================================================================*
* Fixed width word size data types: *
* int8_T, int16_T, int32_T - signed 8, 16, or 32 bit integers *
* uint8_T, uint16_T, uint32_T - unsigned 8, 16, or 32 bit integers *
* real32_T, real64_T - 32 and 64 bit floating point numbers *
*=======================================================================*/
typedef signed char int8_T;
typedef unsigned char uint8_T;
typedef short int16_T;
typedef unsigned short uint16_T;
typedef int int32_T;
typedef unsigned int uint32_T;
typedef float real32_T;
typedef double real64_T;
/*===========================================================================*
* Generic type definitions: boolean_T, char_T, byte_T, int_T, uint_T, *
* real_T, time_T, ulong_T. *
*===========================================================================*/
typedef double real_T;
typedef double time_T;
typedef unsigned char boolean_T;
typedef int int_T;
typedef unsigned int uint_T;
typedef unsigned long ulong_T;
typedef char char_T;
typedef unsigned char uchar_T;
typedef char_T byte_T;
/*===========================================================================*
* Complex number type definitions *
*===========================================================================*/
#define CREAL_T
typedef struct {
real32_T re;
real32_T im;
} creal32_T;
typedef struct {
real64_T re;
real64_T im;
} creal64_T;
typedef struct {
real_T re;
real_T im;
} creal_T;
#define CINT8_T
typedef struct {
int8_T re;
int8_T im;
} cint8_T;
#define CUINT8_T
typedef struct {
uint8_T re;
uint8_T im;
} cuint8_T;
#define CINT16_T
typedef struct {
int16_T re;
int16_T im;
} cint16_T;
#define CUINT16_T
typedef struct {
uint16_T re;
uint16_T im;
} cuint16_T;
#define CINT32_T
typedef struct {
int32_T re;
int32_T im;
} cint32_T;
#define CUINT32_T
typedef struct {
uint32_T re;
uint32_T im;
} cuint32_T;
/*=======================================================================*
* Min and Max: *
* int8_T, int16_T, int32_T - signed 8, 16, or 32 bit integers *
* uint8_T, uint16_T, uint32_T - unsigned 8, 16, or 32 bit integers *
*=======================================================================*/
#define MAX_int8_T ((int8_T)(127))
#define MIN_int8_T ((int8_T)(-128))
#define MAX_uint8_T ((uint8_T)(255U))
#define MAX_int16_T ((int16_T)(32767))
#define MIN_int16_T ((int16_T)(-32768))
#define MAX_uint16_T ((uint16_T)(65535U))
#define MAX_int32_T ((int32_T)(2147483647))
#define MIN_int32_T ((int32_T)(-2147483647-1))
#define MAX_uint32_T ((uint32_T)(0xFFFFFFFFU))
/* Block D-Work pointer type */
typedef void * pointer_T;
/* Define Simulink Coder replacement data types. */
#include "User.h" /* User defined replacement datatype for real_T */
#endif /* RTWTYPES_H */
/*
* File trailer for generated code.
*
* [EOF]
*/