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

news2024/10/5 16:25:12

文章目录

前言

背景介绍

问题描述

分析排查

解决方案

总结归纳


前言

        见《研发日记,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工程,可以到笔者的主页查找和下载。


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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1662391.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

uni-app(四):原生插件开发(Android)

原生插件开发 原生插件开发module1.创建模块2.解决报错3.修改依赖4.编写插件代码5.添加插件配置6.引入模块7.调用插件代码8.运行 component1.创建模块2.解决报错3.修改依赖4.编写插件代码5.添加插件配置6.引入模块7.调用插件代码8.运行 原生插件开发 主要分为两类扩展: Module:…

UE4 3D文字自动换行

效果&#xff1a;3D文字超过5位自动换行 1.随意输入一段字符串测试&#xff0c;创建string临时变量&#xff0c;用于迭代存储字符串 2.当字符串遍历至第“换行长度”&#xff08;我这里是5&#xff09;位时&#xff0c;附加一次空行

修改ollama模型文件下载位置

修改ollama模型文件下载位置。你如果不改这个东西&#xff0c;所有的模型文件都会下到c盘&#xff0c;土豪随意。 这里修改环境变量&#xff1a; OLLAMA_MODELS将这个环境变量设置为你想存放的路径。然后重启电脑&#xff01;

AI大模型探索之路-训练篇20:大语言模型预训练-常见微调技术对比

系列篇章&#x1f4a5; AI大模型探索之路-训练篇1&#xff1a;大语言模型微调基础认知 AI大模型探索之路-训练篇2&#xff1a;大语言模型预训练基础认知 AI大模型探索之路-训练篇3&#xff1a;大语言模型全景解读 AI大模型探索之路-训练篇4&#xff1a;大语言模型训练数据集概…

javaFor循环-打印九九乘法表

虽然所有循环结构都可以用while或者do...while表示&#xff0c;但java提供了另一种循环语句--for循环&#xff0c;使一些循环结构变得简单。for循环语句是支持迭代的一种通用结构&#xff0c;是最有效&#xff0c;最灵活的循环结构。 先写第一列&#xff1a; 运行结果&#xf…

uni-appH5Android混合开发三 || uni-app调用Android原生方法的三种方式

前言&#xff1a; 关于H5的调用Android原生方法的方式有很多&#xff0c;在该片文章中我主要简单介绍三种与Android原生方法交互的方式。 uni-app跨平台框架介绍和快速入门 uni-app跨平台框架介绍和快速入门 一、H5方法调用android原生方法 H5 Android开发规范官方文档&#…

stm32开发三、GPIO

部分引脚可容忍5V&#xff0c;容忍5V的意思是:可以在这个端口输入5V的电压&#xff0c;也认为是高电平 但是对于输出而言&#xff0c;最大就只能输出3.3V&#xff0c;因为供电就只有3.3V 具体哪些端口能容忍5V&#xff0c;可以参考一下STM32的引脚定义 不带FT的&#xff0c;就只…

Java通过百度地图API获取定位-普通IP定位

项目中有一个登录邮箱提醒的功能&#xff0c;需要根据IP地址获取定位信息&#xff0c;从而更好地提示用户账号登录的所在地。为此&#xff0c;花费了一些时间来实现这个功能。 在CSDN搜索了一下&#xff0c;发现关于获取定位的文章说明都不够详细&#xff0c;于是决定自己创作一…

CAP与BASE分布式理论

一、分布式理论 1.CAP理论 CAP理论是说对于分布式数据存储&#xff0c;最多只能同时满足一致性&#xff08;C&#xff0c;Consistency&#xff09;、可用性&#xff08;A&#xff0c; Availability&#xff09;、分区容忍性&#xff08;P&#xff0c;Partition Tolerance&…

编程式导航

目录 一、问题引入 二、基本跳转 1.path路径跳转&#xff08;简易方便&#xff09; 2.name命名路由跳转&#xff08;适合path路径长的场景&#xff09; 三、路由传参 1.path路径跳转传参 &#xff08;1&#xff09;query传参 &#xff08;2&#xff09;动态路由传参 2.…

揭秘VSCode魔法工具箱:HTML5 CSS3超强插件集合 + Css Reset与Normalize.css的终极对决

个人主页&#xff1a;学习前端的小z 个人专栏&#xff1a;HTML5和CSS3悦读 本专栏旨在分享记录每日学习的前端知识和学习笔记的归纳总结&#xff0c;欢迎大家在评论区交流讨论&#xff01; 文章目录 ✍HTML5、CSS3常用的vscode插件&#x1f34e;1 HTML 标签同步重命名– Auto…

每日一题1:从表中创建DataFrame

本文通过一道题来对创建DataFrame知识点进行拓展&#xff0c;方便以后直接调用。下面先对知识点进行介绍&#xff1a; 在Python中&#xff0c;使用pandas库创建DataFrame有多种方法&#xff0c;这里列举几种常见的创建方式&#xff1a; 一、常见创建方式 1. 从二维列表创建 …

Android之给Button上添加按压效果

一、配置stateListAnimator参数实现按压效果 1、按钮控件 <Buttonandroid:id"id/mBtnLogin"android:layout_width"match_parent"android:layout_height"48dp"android:background"drawable/shape_jfrb_login_button"android:state…

融知财经:期货和现货的区别是什么?哪个风险大?

期货和现货在交易对象等方面存在明显的区别。期货交易是一种衍生金融工具&#xff0c;主要用于价格发现、风险管理和投机&#xff0c;而现货交易则是商品和服务的实际买卖。在选择进行期货交易还是现货交易时&#xff0c;投资者需要根据自己的需求和市场情况来决定。 期货和现货…

如何访问远程MySQL数据库服务器?

访问远程MySQL数据库服务器是一项常见的任务&#xff0c;它允许我们在不同的地点通过网络连接到MySQL服务器&#xff0c;并进行数据库管理和数据处理操作。我们将重点介绍一种名为【天联】的组网技术&#xff0c;该技术提供了一系列优势&#xff0c;使远程访问MySQL数据库服务器…

javac编译web项目中的src

对于单个文件的且不引用其他类文件的java源码用javac编译大家都很熟悉即 javac hello.java, 服务器未安装idea&#xff0c;现在在服务器里面直接编译src目录 1 idea项目结构如下 2 web目录为最终部署的代码 WEB-INF下面没有 classes 目录 3 使用javac 编译src javac -encod…

Nachi那智不二越机器人维修技术合集

一、Nachi机械手维护基础知识 1. 定期检查&#xff1a;定期检查机器人的各个部件&#xff0c;如机械手伺服电机、机器人减速器、机械臂传感器等&#xff0c;确保其运行正常。 2. 清洁与润滑&#xff1a;定期清洁Nachi工业机器人表面和内部&#xff0c;并使用合适的润滑油进行润…

在线旅游网站,基于 SpringBoot+Vue+MySQL 开发的前后端分离的在线旅游网站设计实现

目录 一. 前言 二. 功能模块 2.1. 登录界面 2.2. 管理员功能模块 2.3. 用户功能模块 三. 部分代码实现 四. 源码下载 一. 前言 随着科学技术的飞速发展&#xff0c;各行各业都在努力与现代先进技术接轨&#xff0c;通过科技手段提高自身的优势&#xff0c;旅游网站当然…

OpenHarmony usb打开报错“usb fail error code = -3, error msg = LIBUSB_ERROR_ACCESS”

一、前言&#xff1a;最近公司项目需求&#xff0c;定位要求使用国产系统&#xff0c;国产系统无非就是 统信os &#xff0c;麒麟OS, 还有这两年比较热的 OpenHarmony。于是&#xff0c;老板要求公司产品适配OpenHarmony , 跟上时代步伐。 二、在开发中使用 usb 通讯时&#x…

党务政务服务热线|基于SSM的党务政务服务热线平台(源码+数据库+文档)

目录 基于SprinBootvue的党务政务服务热线平台 一、前言 二、系统设计 三、系统功能设计 1系统功能模块 2后台功能模块 5.2.1管理员功能模块 5.2.2部门功能模块 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; …