Matlab·Simulink的使用—【S函数的创建与应用】
文章目錄
- (〇) 前言
- (一) S函數(shù)的結(jié)構(gòu)及編輯
- (二)S函數(shù)的應(yīng)用:
- ??(1)定義S函數(shù)
- ????????①初始化主函數(shù)
- ????????②初始化子函數(shù)
- ??(2)Simulink模型中使用S函數(shù)
- (三)特別注意*——解決初學(xué)者第一次應(yīng)用S函數(shù)常碰到的錯(cuò)誤
- (四)常見錯(cuò)誤
(〇) 前言
S函數(shù)是什么:
????S函數(shù)是系統(tǒng)函數(shù)的英文縮寫,指采用一種設(shè)計(jì)語言(非圖形方式)去描述的一個(gè)功能模塊
如何編寫S函數(shù):
????可以用Matlab的語言,也可以用其它編程語言入C,C++等
????我只介紹下常用的MATLAB自帶“語言”和S函數(shù)模板去編寫S函數(shù)
后文的源代碼
https://download.csdn.net/download/Nirvana_Tai/12321280
https://download.csdn.net/download/Nirvana_Tai/12321288
(一) S函數(shù)的結(jié)構(gòu)及編輯
①在命令行窗口輸入命令: >>edit sfuntmpl.m
????即可打開模板文件
②模板文件sfuntmpl.m包含了一個(gè)主函數(shù)和六個(gè)子函數(shù)
【1】我們先看主函數(shù):
??主函數(shù)首句——引導(dǎo)語句為:
function [sys,x0,str,ts,simStateCompliance] = sfuntmpl(t,x,u,flag)
?? ??其中,fname是S函數(shù)的函數(shù)名;
?? ??輸入形參t,x,y,flag分別為仿真時(shí)間,狀態(tài)向量,輸入向量和調(diào)用標(biāo)志
?? ??輸出形參sys表示返回參數(shù);x0是初始狀態(tài)值;str被設(shè)置為空陣;ts是一個(gè)兩列矩陣,一列為各狀態(tài)變量的采樣周期,另一列是相應(yīng)采樣時(shí)間的偏移量。
?? ??【M文件中S函數(shù)是這么設(shè)置的】
??子函數(shù)的前綴為mdl,由flag來控制調(diào)用情況
常用的四種情況:
???Flag=0:調(diào)用初始化子函數(shù) mdlInitializeSizes;
?? Flag=1:調(diào)用連續(xù)狀態(tài)更新子函數(shù) mdlDerivatives(t,x,u);
?? Flag=2: 調(diào)用離散狀態(tài)更新子函數(shù) mdlUpdate(t,x,u);
?? Flag=3: 調(diào)用輸出子函數(shù) mdlOutputs(t,x,u);
(二)S函數(shù)的應(yīng)用:
??舉個(gè)最簡單的栗子:y=kx+b
??(1)定義S函數(shù)
????????①初始化主函數(shù)
function [sys,x0,str,ts,simStateCompliance] = move(t,x,u,flag) switch flag,case 0,[sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes;case 1,sys=mdlDerivatives(t,x,u);case 2,sys=mdlUpdate(t,x,u);case 3,sys=mdlOutputs(t,x,u);case 4,sys=mdlGetTimeOfNextVarHit(t,x,u);case 9,sys=mdlTerminate(t,x,u);otherwiseDAStudio.error('Simulink:blocks:unhandledFlag', num2str(flag)); end????????②初始化子函數(shù)
function [sys,x0,str,ts,simStateCompliance]=mdlInitializeSizes sizes = simsizes; sizes.NumContStates = 1; sizes.NumDiscStates = 0; sizes.NumOutputs = 1; sizes.NumInputs = 1; sizes.DirFeedthrough = 0; sizes.NumSampleTimes = 1; % at least one sample time is needed sys = simsizes(sizes); x0 = [0]; str = []; ts = [0 0]; simStateCompliance = 'UnknownSimState'; function sys=mdlDerivatives(t,x,u) sys = u; function sys=mdlUpdate(t,x,u) sys=[]; function sys=mdlOutputs(t,x,u) sys=x; function sys=mdlGetTimeOfNextVarHit(t,x,u) sampleTime = 1; % Example, set the next hit to be one second later. sys = t + sampleTime; function sys=mdlTerminate(t,x,u) sys = [];??(2)Simulink模型中使用S函數(shù)
????首先我們需要用模塊搭建出這個(gè)模型
????新手可能找不到器件,這里我簡單說一下:simulink標(biāo)準(zhǔn)模塊庫中,Sinks下有Scope,Sources下有Sine Wave,User-Defined Functions下有S-Function.連起來就可以了
????這里再說一下如何修改Scope的輸入端子數(shù):雙擊打開Scope界面,點(diǎn)擊左上方設(shè)置,修改Input參數(shù)就可以了,記得修改完點(diǎn)Apply。
建立完模型如下:
得到結(jié)果如下:
????采樣時(shí)間:對于Simulink模型來說,解算器中的一個(gè)步長決定了整個(gè)模型最小的采樣時(shí)間間隔。
(三)特別注意*——解決初學(xué)者第一次應(yīng)用S函數(shù)常碰到的錯(cuò)誤
????第一次應(yīng)用S函數(shù)時(shí)肯能會(huì)碰到很多問題,我就幾個(gè)常見問題總結(jié)接個(gè)需要注意的地方,希望大家能一次寫對。
????①注意此處 S函數(shù)名一定要和編寫的.m文件,否則會(huì)報(bào)錯(cuò)
????②注意,一定要把S函數(shù)的.m文件和模型的.slx文件放在一個(gè)目錄下,確保點(diǎn)擊Edit時(shí),可以找到并打開對應(yīng)S函數(shù)M文件
????③注意,要把matlab的當(dāng)前工作目錄修改為你這兩個(gè)文件保存的目錄下
(四)常見錯(cuò)誤
①步長錯(cuò)誤
Starting build procedure for model: test Build procedure for model:
‘test’ aborted due to an error. The specified code generation target
for model ‘test’ cannot be used with a variable-step solver.
Suggested Actions You may configure the solver options for a
fixed-step solver with an appropriate integration algorithm Select a
target that supports a variable-step solver, such as rsim.tlc You can
use ‘Embedded Coder Quick Start’ to help you generate code
遇到這種情況,在模型編輯窗口點(diǎn)擊設(shè)置,修改步長設(shè)置即可。
②類似于此,請檢查模型的搭建是否符合S函數(shù)的邏輯,檢查輸入輸出端。看S函數(shù)源代碼是否正確
再查看目錄是否一致,工作區(qū)是否在當(dāng)前目錄
Generating code into build folder: D:\Study Playground\sample_grt_rtw
Unconnected input line found on ‘sample/Scope’ (input port: 1)
Component: Simulink | Category: Block warning Unconnected output line
found on ‘sample/Sine Wave’ (output port: 1) Component: Simulink |
Category: Block warning Invoking Target Language Compiler on
sample.rtw Using System Target File: D:\Baidu network disk
download\Matlab\rtw\c\grt\grt.tlc Loading TLC function libraries
Initial pass through model to cache user defined code Caching model
source code Writing header file sample.h Writing header file
sample_types.h Writing header file rtwtypes.h Writing header file
builtin_typeid_types.h Writing header file multiword_types.h Writing
source file sample.c Writing header file sample_private.h Writing
header file rtmodel.h Writing source file sample_data.c Writing header
file rt_nonfinite.h Writing source file rt_nonfinite.c Writing header
file rtGetInf.h Writing source file rtGetInf.c Writing header file
rtGetNaN.h Writing source file rtGetNaN.c TLC code generation
complete. Using toolchain: LCC-win64 v2.4.1 | gmake (64-bit Windows)
‘D:\Study Playground\sample_grt_rtw\sample.mk’ is up to date Building
‘sample’: D:\Baidu network disk download\Matlab\bin\win64\gmake -f
sample.mk all D:\Study Playground\sample_grt_rtw>set MATLAB=D:\Baidu
network disk download\Matlab D:\Study Playground\sample_grt_rtw>cd .
D:\Study Playground\sample_grt_rtw>if “” == “” (D:\Baidu network disk
download\Matlab\bin\win64\gmake -f sample.mk all ) else (D:\Baidu
network disk download\Matlab\bin\win64\gmake -f sample.mk )
‘D:\Baidu’ 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序 或批處理文件。 D:\Study
Playground\sample_grt_rtw>echo The make command returned an error of
9009 The make command returned an error of 9009 D:\Study
Playground\sample_grt_rtw>An_error_occurred_during_the_call_to_make
‘An_error_occurred_during_the_call_to_make’ 不是內(nèi)部或外部命令,也不是可運(yùn)行的程序
或批處理文件。 Build procedure for model: ‘sample’ aborted due to an error.
Error(s) encountered while building “sample”: Failed to generate all
binary outputs.
????這樣基本上就可以保證運(yùn)行,如果有其他問題,請自行查詢或者評論錯(cuò)誤截圖。
總結(jié)
以上是生活随笔為你收集整理的Matlab·Simulink的使用—【S函数的创建与应用】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java弹框形式输入_java中点击一个
- 下一篇: 添加dubbo.xsd的方法