Simulink代码生成(十三)——Lookuptable生成代码
一、Lookuptable模型和设置
建立如下查表模型,一个简单的线性查表。
in = Simulink.Signal;
in.CoderInfo.StorageClass = "ExportedGlobal";
in.DataType = "single";
out = Simulink.Signal;
out.CoderInfo.StorageClass = "ExportedGlobal";
out.DataType = "single";
二、生成代码
如果将代码生成方式设为紧凑型,数据会放在.c文件中,为了方便管理,一般将查表的数据单独进行管理,所以采用独立的数据文件,生成代码如下。新版本的matlab对查表算法有优化,2018b查表会有两个函数。
/*
* File: LookUpTable.h
*
* Code generated for Simulink model 'LookUpTable'.
*
* Model version : 1.12
* Simulink Coder version : 9.8 (R2022b) 05-Oct-2022
* C/C++ source code generated on : Mon Oct 24 02:38:51 2022
*
* Target selection: ert.tlc
* Embedded hardware selection: NXP->Cortex-M0/M0+
* Code generation objectives: Unspecified
* Validation result: Not run
*/
#ifndef RTW_HEADER_LookUpTable_h_
#define RTW_HEADER_LookUpTable_h_
#ifndef LookUpTable_COMMON_INCLUDES_
#define LookUpTable_COMMON_INCLUDES_
#include "rtwtypes.h"
#endif /* LookUpTable_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_LookUpTable_T RT_MODEL_LookUpTable_T;
/* Constant parameters (default storage) */
typedef struct {
/* Computed Parameter: uDLookupTable_tableData
* Referenced by: '<Root>/1-D Lookup Table'
*/
real32_T uDLookupTable_tableData[11];
/* Computed Parameter: uDLookupTable_bp01Data
* Referenced by: '<Root>/1-D Lookup Table'
*/
real32_T uDLookupTable_bp01Data[11];
} ConstP_LookUpTable_T;
/* Real-time Model Data Structure */
struct tag_RTM_LookUpTable_T {
const char_T * volatile errorStatus;
};
/* Constant parameters (default storage) */
extern const ConstP_LookUpTable_T LookUpTable_ConstP;
/*
* 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 real32_T in; /* '<Root>/In1' */
extern real32_T out; /* '<Root>/1-D Lookup Table' */
/* Model entry point functions */
extern void LookUpTable_initialize(void);
extern void LookUpTable_step(void);
extern void LookUpTable_terminate(void);
/* Real-time Model object */
extern RT_MODEL_LookUpTable_T *const LookUpTable_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>' : 'LookUpTable'
*/
#endif /* RTW_HEADER_LookUpTable_h_ */
/*
* File trailer for generated code.
*
* [EOF]
*/
/*
* File: LookUpTable.c
*
* Code generated for Simulink model 'LookUpTable'.
*
* Model version : 1.12
* Simulink Coder version : 9.8 (R2022b) 05-Oct-2022
* C/C++ source code generated on : Mon Oct 24 02:38:51 2022
*
* Target selection: ert.tlc
* Embedded hardware selection: NXP->Cortex-M0/M0+
* Code generation objectives: Unspecified
* Validation result: Not run
*/
#include "LookUpTable.h"
#include "rtwtypes.h"
/* Exported block signals */
real32_T in; /* '<Root>/In1' */
real32_T out; /* '<Root>/1-D Lookup Table' */
/* Real-time model */
static RT_MODEL_LookUpTable_T LookUpTable_M_;
RT_MODEL_LookUpTable_T *const LookUpTable_M = &LookUpTable_M_;
static real32_T look1_iflf_binlgpw(real32_T u0, const real32_T bp0[], const
real32_T table[], uint32_T maxIndex);
static real32_T look1_iflf_binlgpw(real32_T u0, const real32_T bp0[], const
real32_T table[], uint32_T maxIndex)
{
uint32_T bpIdx;
uint32_T iLeft;
uint32_T iRght;
/* Column-major Lookup 1-D
Search method: 'binary'
Use previous index: 'off'
Interpolation method: 'Linear point-slope'
Use last breakpoint for index at or above upper limit: 'off'
Remove protection against out-of-range input in generated code: 'on'
*/
/* Prelookup - Index and Fraction
Index Search method: 'binary'
Use previous index: 'off'
Use last breakpoint for index at or above upper limit: 'off'
Remove protection against out-of-range input in generated code: 'on'
*/
/* Binary Search */
bpIdx = maxIndex >> 1U;
iLeft = 0U;
iRght = maxIndex;
while (iRght - iLeft > 1U) {
if (u0 < bp0[bpIdx]) {
iRght = bpIdx;
} else {
iLeft = bpIdx;
}
bpIdx = (iRght + iLeft) >> 1U;
}
real32_T yL_0d0;
/* Column-major Interpolation 1-D
Interpolation method: 'Linear point-slope'
Use last breakpoint for index at or above upper limit: 'off'
Overflow mode: 'portable wrapping'
*/
yL_0d0 = table[iLeft];
return (u0 - bp0[iLeft]) / (bp0[iLeft + 1U] - bp0[iLeft]) * (table[iLeft + 1U]
- yL_0d0) + yL_0d0;
}
/* Model step function */
void LookUpTable_step(void)
{
/* Lookup_n-D: '<Root>/1-D Lookup Table' incorporates:
* Inport: '<Root>/In1'
*/
out = look1_iflf_binlgpw(in, LookUpTable_ConstP.uDLookupTable_bp01Data,
LookUpTable_ConstP.uDLookupTable_tableData, 10U);
}
/* Model initialize function */
void LookUpTable_initialize(void)
{
/* (no initialization code required) */
}
/* Model terminate function */
void LookUpTable_terminate(void)
{
/* (no terminate code required) */
}
/*
* File trailer for generated code.
*
* [EOF]
*/
/*
* File: LookUpTable_data.c
*
* Code generated for Simulink model 'LookUpTable'.
*
* Model version : 1.12
* Simulink Coder version : 9.8 (R2022b) 05-Oct-2022
* C/C++ source code generated on : Mon Oct 24 02:38:51 2022
*
* Target selection: ert.tlc
* Embedded hardware selection: NXP->Cortex-M0/M0+
* Code generation objectives: Unspecified
* Validation result: Not run
*/
#include "LookUpTable.h"
/* Constant parameters (default storage) */
const ConstP_LookUpTable_T LookUpTable_ConstP = {
/* Computed Parameter: uDLookupTable_tableData
* Referenced by: '<Root>/1-D Lookup Table'
*/
{ -10.0F, -8.0F, -6.0F, -4.0F, -2.0F, 0.0F, 2.0F, 4.0F, 6.0F, 8.0F, 10.0F },
/* Computed Parameter: uDLookupTable_bp01Data
* Referenced by: '<Root>/1-D Lookup Table'
*/
{ -5.0F, -4.0F, -3.0F, -2.0F, -1.0F, 0.0F, 1.0F, 2.0F, 3.0F, 4.0F, 5.0F }
};
/*
* File trailer for generated code.
*
* [EOF]
*/