simulink 中 s-function builder的坑

如何使用s-function builder

1. 双击页面空白处,搜索找到S-Function Builder

在这里插入图片描述
2. 双击模块键入编辑界面
在这里插入图片描述

注意:(1)模块的数据处理部分(即要实现的功能)都在下面这个函数里写。

void system_Outputs_wrapper(const real_T *u0,
                            real_T *y0,
                            const real_T *p0, const int_T p_width0)
{
/* Output_BEGIN */
要编写的代码
/* Output_END */
}

(2)real_T 即 c语言中的double类型,输入u0、输出y0和参数p0的声明加了指针*,且观察端口和参数栏他们的维度,可知输入、参数的后面要加[0],输出的格式应该为:

//此示例将输出设置为输入*参数
*y0 = u0[0] * p0[0]

如果中间定义了什么变量,可以直接用以下定义,不用加指针*。

int_T a;
real_T b;

  1. 因为s-function builder用的是C mex环境,代码可以直接在前面提到的函数
    void system_Outputs_wrapper()中写,然后按编译按钮;会生成6个文件,分别是rtwmakecfg.m文件(没什么用)、FUN.mexw64文件、.mat文件(没什么用)、tlc文件、FUN.c文件、FUN_wrapper.c文件。
    也可以把wrapper.c文件用visual studio打开,这样写代码比较方便,然后在matlab的命令行 打出 mex FUN.c FUN_wrapper.c进行编译。
  2. s-function builder 对精度要求比较高,如果将一大串式子赋给A,A再赋给B,会丢失好多值,建议直接将一大串式子直接赋给B,减少中间变量的过程。(如果画的图不理想很大可能是这个原因)
    事实证明,并不是这个原因哈哈哈哈。因为输出的定义为指针类型,我最开始是按照编辑器自带的示例y0[0] = u0[0]写的,但可能是野指针跑飞的原因,换成了*y0 = u0[0],结果就好了
### 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、付费专栏及课程。

余额充值