您现在的位置是:首页 > 名人名句

【研发日记】Matlab/Simulink避坑指南(十二)——Initialize Function执行Bug

作者:欧阳逸时间:2024-05-12 08:27:15分类:名人名句

简介  文章浏览阅读909次,点赞56次,收藏43次。在一个嵌入式软件项目中,需要滚动输出一组0~7的序列,但是第一个周期输出1。在Simulink的Initialize Function模块使用中出现了Bug,对其排查和优化。

点击全文阅读

文章目录

前言

背景介绍

问题描述

分析排查

解决方案

总结归纳


前言

        见《研发日记,Matlab/Simulink避坑指南(七)——数据溢出钳位Bug》

        见《研发日记,Matlab/Simulink避坑指南(八)——else if分支结构Bug》

        见《研发日记,Matlab/Simulink避坑指南(九)——可变数组应用Bug》

        见《【研发日记】Matlab/Simulink避坑指南(十)——移位溢出Bug》

         见《【研发日记】Matlab/Simulink避坑指南(十一)——Delay周期Bug》

背景介绍

        最近在一个项目中,使用Simulink写一段嵌入式代码,功能是滚动输出一组0~7的序列,但是软件启动第一个周期输出的0,要做特殊处理改成输出1。乍一看感觉挺简单,想着直接用一个Initialize Function模块重写第一个周期的值就能搞定了,但是就这么一个小应用让我栽了跟头,在整个工程中排查了几个小时才找到问题。下面就来分享一下这一小段个人经历。

        一开始的模型搭建大概是下面这个样子:

        Counter Write设置的优先级最高,Read设置的优先级最低,确保第一个周期的Counter值能被Initialize Function模块重写。

问题描述

        按照上述示例,预想应该可以正常运行,但是实际运行时却跟预想的不一样,示例如下:

        希望看到的是第一个周期输出1,但是看到的运行结果仍是0。

分析排查

        使用前段时间解锁的Debug技能《【研发日记】Matlab/Simulink技能解锁(四)——在Simulink Debugger窗口调试》,来调试一下上述模型,示例如下:

        按Block步进执行,并输出Data Value,可以看到Initialize Function模块是在进入主循环之前就已经执行完了,所以才没有在第一个周期里重写Counter值。

        这时候我们把模型生成C代码,来进一步确认实际执行顺序是否如上面Debuger窗口所示,示例如下:

#include <stddef.h>#include <stdio.h>              /* This ert_main.c example uses printf/fflush */#include "InitializeFunctionBug.h"     /* Model's header file */#include "rtwtypes.h"/* * The example "main" function illustrates what is required by your * application code to initialize, execute, and terminate the generated code. * Attaching rt_OneStep to a real-time clock is target specific.  This example * illustrates how you do this relative to initializing the model. */int_T main(int_T argc, const char *argv[]){  /* Unused arguments */  (void)(argc);  (void)(argv);  /* Initialize model */  InitializeFunctionBug_initialize();//初始化模块  /* Attach rt_OneStep to a timer or interrupt service routine with   * period 0.01 seconds (the model's base sample time) here.  The   * call syntax for rt_OneStep is   *   *  rt_OneStep();   */  printf("Warning: The simulation will run forever. "         "Generated ERT main won't simulate model step behavior. "         "To change this behavior select the 'MAT-file logging' option.\n");  fflush((NULL));  while (rtmGetErrorStatus(InitializeFunctionBug_M) == (NULL)) {    /*  Perform other application tasks here *//* Step the model */InitializeFunctionBug_step();//主循环  }  /* Disable rt_OneStep() here */  /* Terminate model */  InitializeFunctionBug_terminate();  return 0;}
#include "InitializeFunctionBug.h"#include "InitializeFunctionBug_private.h"/* Block states (default storage) */DW_InitializeFunctionBug_T InitializeFunctionBug_DW;/* External outputs (root outports fed by signals with default storage) */ExtY_InitializeFunctionBug_T InitializeFunctionBug_Y;/* Real-time model */static RT_MODEL_InitializeFunctionBu_T InitializeFunctionBug_M_;RT_MODEL_InitializeFunctionBu_T *const InitializeFunctionBug_M =  &InitializeFunctionBug_M_;/* Model step function */void InitializeFunctionBug_step(void){  /* Outport: '<Root>/Out1' incorporates:   *  DataStoreWrite: '<Root>/Data Store Write'   *  UnitDelay: '<S1>/Output'   */  InitializeFunctionBug_Y.Out1 = InitializeFunctionBug_DW.Output_DSTATE;  /* Switch: '<S4>/FixPt Switch' incorporates:   *  Constant: '<S3>/FixPt Constant'   *  Constant: '<S4>/Constant'   *  Sum: '<S3>/FixPt Sum1'   *  UnitDelay: '<S1>/Output'   */  if ((uint8_T)(InitializeFunctionBug_DW.Output_DSTATE + 1U) > 7) {    InitializeFunctionBug_DW.Output_DSTATE = 0U;  } else {    InitializeFunctionBug_DW.Output_DSTATE++;  }  /* End of Switch: '<S4>/FixPt Switch' */}/* Model initialize function */void InitializeFunctionBug_initialize(void){  /* (no initialization code required) */  /*  DataStoreWrite: '<Root>/Data Store Write' */  InitializeFunctionBug_DW.Output_DSTATE = 1U;  }/* Model terminate function */void InitializeFunctionBug_terminate(void){  /* (no terminate code required) */}

         Counter值赋1的代码位于initialize(void)函数中,Counter值累加的代码和输出的代码都位于后面的while()循环体中。

        根据上面的排查和分析,Initialize Function模块是在第一个周期前执行的,所以不能重写第一个周期的Counter值,问题就出在这里。

解决方案

        根据上面的分析,调整第一个周期重写Counter值的方式,使用Stateflow做一个只在第一个周期运行的使能子系统,并调整优先级确保第一个周期的Counter值能被重写,示例如下:

        重新运行程序,问题不再出现,Bug修复完工,示例如下:

        至此,这个滚动输出一组0~7的序列,但是第一个周期输出1的应用就算是没问题了。

        生的代码示例如下:

#include <stddef.h>#include <stdio.h>              /* This ert_main.c example uses printf/fflush */#include "InitializeFunctionBug.h"     /* Model's header file */#include "rtwtypes.h"/* * The example "main" function illustrates what is required by your * application code to initialize, execute, and terminate the generated code. * Attaching rt_OneStep to a real-time clock is target specific.  This example * illustrates how you do this relative to initializing the model. */int_T main(int_T argc, const char *argv[]){  /* Unused arguments */  (void)(argc);  (void)(argv);  /* Initialize model */  InitializeFunctionBug_initialize();  /* Attach rt_OneStep to a timer or interrupt service routine with   * period 0.01 seconds (the model's base sample time) here.  The   * call syntax for rt_OneStep is   *   *  rt_OneStep();   */  printf("Warning: The simulation will run forever. "         "Generated ERT main won't simulate model step behavior. "         "To change this behavior select the 'MAT-file logging' option.\n");  fflush((NULL));  while (rtmGetErrorStatus(InitializeFunctionBug_M) == (NULL)) {    /*  Perform other application tasks here *//* Step the model */InitializeFunctionBug_step();//主循环  }  /* Disable rt_OneStep() here */  /* Terminate model */  InitializeFunctionBug_terminate();  return 0;}
#include "InitializeFunctionBug.h"#include "InitializeFunctionBug_private.h"/* Named constants for Chart: '<Root>/Chart' */#define InitializeFunctionBug_IN_s1    ((uint8_T)1U)#define InitializeFunctionBug_IN_s2    ((uint8_T)2U)/* Block states (default storage) */DW_InitializeFunctionBug_T InitializeFunctionBug_DW;/* External outputs (root outports fed by signals with default storage) */ExtY_InitializeFunctionBug_T InitializeFunctionBug_Y;/* Real-time model */static RT_MODEL_InitializeFunctionBu_T InitializeFunctionBug_M_;RT_MODEL_InitializeFunctionBu_T *const InitializeFunctionBug_M =  &InitializeFunctionBug_M_;/* Model step function */void InitializeFunctionBug_step(void){  /* Outport: '<Root>/Out1' incorporates:   *  DataStoreWrite: '<Root>/Data Store Write'   *  UnitDelay: '<S2>/Output'   */  InitializeFunctionBug_Y.Out1 = InitializeFunctionBug_DW.Output_DSTATE;  /* Chart: '<Root>/Chart' */  if (InitializeFunctionBug_DW.is_active_c3_InitializeFunction == 0U) {    InitializeFunctionBug_DW.is_active_c3_InitializeFunction = 1U;    InitializeFunctionBug_DW.is_c3_InitializeFunctionBug =      InitializeFunctionBug_IN_s1;    /* Outputs for Enabled SubSystem: '<Root>/Subsystem' incorporates:     *  EnablePort: '<S3>/Enable'     */    /* Outport: '<Root>/Out1' incorporates:     *  Constant: '<S3>/Constant1'     *  DataStoreWrite: '<S3>/Data Store Write1'     */    InitializeFunctionBug_Y.Out1 = 1U;    /* End of Outputs for SubSystem: '<Root>/Subsystem' */  } else if (InitializeFunctionBug_DW.is_c3_InitializeFunctionBug ==             InitializeFunctionBug_IN_s1) {    InitializeFunctionBug_DW.is_c3_InitializeFunctionBug =      InitializeFunctionBug_IN_s2;  } else {    /* case IN_s2: */  }  /* End of Chart: '<Root>/Chart' */  /* Switch: '<S5>/FixPt Switch' incorporates:   *  Constant: '<S4>/FixPt Constant'   *  Constant: '<S5>/Constant'   *  Sum: '<S4>/FixPt Sum1'   *  UnitDelay: '<S2>/Output'   */  if ((uint8_T)(InitializeFunctionBug_DW.Output_DSTATE + 1U) > 7) {    InitializeFunctionBug_DW.Output_DSTATE = 0U;  } else {    InitializeFunctionBug_DW.Output_DSTATE++;  }  /* End of Switch: '<S5>/FixPt Switch' */}/* Model initialize function */void InitializeFunctionBug_initialize(void){  /* (no initialization code required) */}/* Model terminate function */void InitializeFunctionBug_terminate(void){  /* (no terminate code required) */}

总结归纳

        那么这个代码调试过程,发现的问题可以积累下来这么几条小经验以供自己将来使用,也供广大网友参考:

        1、Initialize Function模块并不是我们预想的在主循环的第一个周期执行,而是在主循环前面执行。

        2、Initialize Function模块的应用,局现性特别强。就是软件初始化工作,例如读取固件信息,配置基础的运行参数这一类,并不适合参与主程序的运算。

        3、与Initialize Function类似的还有Reitialize、Rest、Terminnate,其使用方法和注意事项都是类似的。

        4、Stateflow配合使能子系统,也能事项初始化相关的功能,而且用法更加灵活,适用范围更广。

        5、设置模块的优先级,只能调整同一个周期中,同一级子系统内各个模块之间执行的先后顺序,不能跨越周期。

        以上就是本人在解决Initialize Function模块执行Bug时,一些个人理解和分析的总结,首先介绍了基本的项目背景,然后描述了问题的想象,最后分析排查了Bug原因,并给出了问题解决方案。

        后续还会分享其他的,使用Matlab/Simulink进行研发时遇到的Bug,欢迎评论区留言、点赞、收藏和关注,这些鼓励和支持都将成文本人持续分享的动力。

        上述例程使用的Demo工程,可以到笔者的主页查找和下载。


        版权声明:原创文章,转载请注明出处与链接,违者必究!

点击全文阅读

郑重声明:

本站所有活动均为互联网所得,如有侵权请联系本站删除处理

我来说两句