Simulink自动代码生成4——集成生成代码和外部代码(integrating generated code with external code)

本文详细介绍了如何在ETC_Controller模型中处理输入输出接口,手动调用入口函数,以及如何将多个模型代码合并到一个工程,并涉及了代码打包、存放位置管理和配置设置。通过实际操作演示了代码管理和集成的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有些时候在生成代码后,可能需要和外部代码(如手写代码或其他工具生成的代码)进行集成,此时我们可做以下准备工作:

  1. 确保模型输入输出接口正确
  2. 确保入口函数可被调用
  3. 多个模型生成的代码集成到一个工程里
  4. 代码文件存储管理:代码打包、代码存放位置管理

下面以官方提供的模型ETC_Controller为例说明
在这里插入图片描述
在这里插入图片描述

模型输入输出接口

如果模型中的input是从workspace中读取数据的,embedded coder是不会把这部分生成到代码中的,如下图,打开model properties查看模型的callback。
在这里插入图片描述
从下图可知,模型打开前(PreLoadFun)前会加载Kp、Ki、Ts和padel、angle等数据:
在这里插入图片描述
并且configset中的data import/export设置为:
在这里插入图片描述
这种从workspace中读取的import数据(pedal,angle)并不会生成到代码中:
在这里插入图片描述

手动调用入口函数
之前介绍过,主函数中有3个入口函数(entry point function),

extern void piCtrl_integ_initialize(void);	// initialization entry point
extern void piCtrl_integ_step(void);	// routine entry point
extern void piCtrl_integ_terminate(void);	// termination entry point

如果想用手写代码调用这3个函数,需要在手写代码中#include “model.h”。
另外在ert_main.c中只调用piCtrl_integ_initialize()和piCtrl_integ_terminate(),并不会用周期性中断调用piCtrl_integ_step()。
在这里插入图片描述
在这里插入图片描述
我们可以做一些改动,手写一些代码进行调用,比如在原代码ert_main.c的基础上进行如下修改:
在这里插入图片描述
修改好后在work folder中用cmd命令打开控制台,输入:

cd model_ert_rtw	// 更改文件路径
model.bat	// 编译新的exe文件(把修改后的ert_main.c编译进去)
cd ..	// 更改文件路径
model.exe	// 执行exe

则会出现:模拟周期中断调用后的结果输出。要说明一点,这里是借用Visual Studio或者MinGW进行编译链接的。
在这里插入图片描述
这里有个补充,当在configset中做如下设置后,每次生成代码时ert_main.c会都被覆盖。
在这里插入图片描述

多个模型生成的代码集成到一个工程里
当多个模型生成代码需要放到一个工程中时(并行开发时常出现这种情况),有些文件是最需包含一次的如rtwtype.h。此时可以通过如下设置把共享文件分离出来以便管理。该选项会把共享文件放在\slprj\ert_sharedutils里。
在这里插入图片描述
代码打包
该选项可把需要IDE进行集成(编译链接)的代码文件打包好,以便管理。
在这里插入图片描述
在这里插入图片描述
如果代码已经生成好了,想进行打包,可在command窗口敲入以下命令进行打包压缩

// buildInfo.mat在model_ert_rtw文件夹中,每次生成代码都出现
load('buildInfo.mat')	
packNGo('buildInfo.mat')	// packNGo('buildInfo.mat',‘packType’,‘hierarchical’)

代码存放位置管理
代码生成后一般存放在model_ert_rtw文件夹中,但也可以修改其存放位置,在command窗口敲入以下命令:

Simulink.fileGenControl('set','CodeGenFolder','newFolder')
Simulink.fileGenControl('set','CodeGenFolder','newFolder','CreateDir','ture')

如果想更改编译、仿真或代码生成过程中缓存文件的存放位置(一般默认存放在\slprj文件夹中),可在command窗口敲入以下命令:

Simulink.fileGenControl('set','Cachefile','newFolder','CreateDir','ture')

如果想要查看当前设置的文件存放路径,,可在command窗口敲入以下命令:

Simulink.fileGenControl('getConfig')
### S-Function Builder in MATLAB Simulink #### Introduction to S-Functions An S-function (system function) provides a powerful mechanism for extending the capabilities of the Simulink environment. An S-function is a computer language description of a Simulink block written using one of several implementation methods such as C, C++, or MATLAB[^1]. The S-Function Builder tool simplifies this process by providing an interface that allows users to create custom blocks without extensive knowledge of low-level programming. #### Using S-Function Builder Tool The S-Function Builder tool facilitates creating and integrating user-defined algorithms into Simulink models through these steps: - **Creating Custom Blocks**: Users can define inputs, outputs, parameters, and discrete/continuous states within the graphical interface. - **Generating Code Automatically**: After defining the algorithmic behavior via dialog boxes, the builder automatically generates necessary source code files along with makefiles required for compilation. - **Compiling Generated Files**: Once generated, compile the created `.c` file alongside any additional libraries needed directly from inside MATLAB/Simulink. Below shows how to use the `sfun_counter_sd.c` template provided when opening the S-Function Builder app: ```matlab % Open S-Function Builder App slbuild('sfuntmpl_sfun') ``` This command opens up the S-Function Builder application where customization takes place before generating the final output suitable for inclusion back into your model. #### Example Usage Scenario Consider implementing a simple counter which increments its value every time step based on input signal characteristics. This example demonstrates setting up initial conditions, handling state variables, updating them over simulation iterations while also managing parameter tuning externally during runtime operations. To implement this functionality, follow these guidelines after launching the S-Function Builder GUI: - Define Input Ports: Specify number of ports depending upon whether you want external reset capability etc. - Configure Output Port(s): Set dimensions according to desired outcome e.g., scalar integer representing count status post incrementation operation. - Initialize States & Parameters: Assign default values like starting point (`Initial Count`) plus other configurable options available under mask settings tab once integrated back into main diagram workspace area. - Implement Discrete-Time Algorithm Logic Inside Callback Functions Section Provided Within Editor Pane Of Interface Window Displayed Upon Launching Utility Program From Command Line Prompt Or Through Library Browser Menu Option Under User Defined Components Category Listing Available In Main Application Software Package Installation Directory Path On Local Machine File System Hierarchy Structure Organized By Vendor Specifications And Conventions Established For Third Party Developers To Adhere When Contributing Add-On Modules And Extensions That Enhance Core Productivity Tools Offerings Targeted Towards Engineering Professionals Engaged In Various Domains Spanning Across Multiple Industries Including But Not Limited To Automotive Design Automation Aerospace Defense Electronics Telecommunications Energy Management Systems Process Control Applications Etcetera. --related questions-- 1. How does one integrate third-party library functions into an S-Function? 2. What are common pitfalls encountered while developing complex systems utilizing multiple interconnected S-functions? 3. Can S-Function Builder support multi-threaded execution environments effectively? If so, what considerations should be taken into account? 4. Is there documentation detailing best practices regarding performance optimization techniques applicable specifically towards S-functions development workflow processes? 5. Are there alternatives to S-Function Builder for crafting highly specialized components tailored uniquely per project requirements outside standard offerings found within typical installation packages distributed officially by MathWorks Inc.?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值