Simulink代码生成(十三)——Lookuptable生成代码

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]
 */

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值