F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

Slides:



Advertisements
Similar presentations
Distributor meeting October 2007
Advertisements

1 1 Mechanical Design and Production Dept, Faculty of Engineering, Zagazig University, Egypt. Mechanical Design and Production Dept, Faculty of Engineering,
Basic S-Functions and TLC Coding
Introduction to HT-IDE3000 Micro-C development System Department.
SIMULINK EXAMPLE transmitter Receiver Channel
MATLAB and SimulinkLecture 71 To days Outline  Callbacks  MATLAB And Simulink  S-functions  Project suggestions.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
The Web Warrior Guide to Web Design Technologies
© by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Visual Basic 2010 How to Program Reference: Instructor: Maysoon Bin Duwais slides Visual Basic 2010 how to program by Deitel © by Pearson Education,
A Guide to Oracle9i1 Creating an Integrated Database Application Chapter 8.
SIMULINK Dr. Samir Al-Amer. SIMULINK SIMULINK is a power simulation program that comes with MATLAB Used to simulate wide range of dynamical systems To.
HossamTalaat - MATLAB Course - KSU - 21/1/24 1 IEEE Student Branch - College of Engineering - KSU Getting started with Simulink By Prof. Hossam Talaat.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Simulink ® From Simulink® 7 Getting Started Guide.
Digital Image Processing Lecture3: Introduction to MATLAB.
Infinity-project.org Engineering education for today’s classroom The Infinity Project SM LabVIEW for The Infinity Project.
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
Visual Basic Chapter 1 Mr. Wangler.
© 2004 The MathWorks, Inc. 1 MATLAB for C/C++ Programmers Support your C/C++ development using MATLAB’s prebuilt graphics functions and trusted numerics.
Programming with Microsoft Visual Basic 2012 Chapter 12: Web Applications.
Simulink ® Interface Course 13 Active-HDL Interfaces.
UNIVERSITI TENAGA NASIONAL “Generates Professionals” CHAPTER 4 : Part 2 INTRODUCTION TO SOFTWARE DEVELOPMENT: PROGRAMMING & LANGUAGES.
Computing IV Visual C Introduction with OpenCV Example Xinwen Fu.
Each VI has 2 Windows Front Panel User Interface (UI) –Controls = Inputs –Indicators = Outputs Block Diagram Graphical Code –Data travels on wires from.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
IE 411/511: Visual Programming for Industrial Applications
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
C-MEX S-Function MATLAB SIMULINK SI Lab Presentation Presented by: Ashkan Jalili 2007/12/08.
Simulink ® Interface Course 13 Active-HDL Interfaces.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
ICONICS ActiveX ToolWorX V 6.1.
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Renesas Technology America Inc. 1 SKP8CMINI Tutorial 2 Creating A New Project Using HEW.
POS 406 Java Technology And Beginning Java Code
Visual C++ Programming: Concepts and Projects
Active-HDL Interfaces Debugging C Code Course 10.
Interfaces to External EDA Tools Debussy Denali SWIFT™ Course 12.
ECEN/MAE 3723 – Systems I MATLAB Lecture 2. Lecture Overview What is Simulink? How to use Simulink  Getting Start with Simulink  Building a model Example.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
Chapter 2 – Introduction to the Visual Studio .NET IDE
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Bit-DSP-MicrocontrollerTMS320F2812 Texas Instruments Incorporated European Customer Training Center University of Applied Sciences Zwickau (FH)
3 Copyright © 2004, Oracle. All rights reserved. Working in the Forms Developer Environment.
© 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick
SIMULINK-Tutorial 1 Class ECES-304 Presented by : Shubham Bhat.
FORTRAN History. FORTRAN - Interesting Facts n FORTRAN is the oldest Language actively in use today. n FORTRAN is still used for new software development.
INTRODUCTION TO SIMULINK by Yasmin Hanum Md Thayoob & Aidil Azwin Zainul Abidin.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Renesas Technology America Inc. 1 SKP8CMINI Tutorial 2 Creating A New Project Using HEW.
Simulink  ? 1 Simulink  ( Simu lation and Link ) is an extension of Matlab Offers modeling, simulation, and analysis of dynamical systems; i.e., a system.
Visual Basic Integrated Development Environment (IDE) 56:150 Information System Design.
Dive Into® Visual Basic 2010 Express
VAB™ for INFINITY Tutorial
Integration of OCP and SIMULINK
Chapter 2 – Introduction to the Visual Studio .NET IDE
ECEN/MAE 3723 – Systems I MATLAB Lecture 2.
1. Introduction to Visual Basic
WORKSHOP 3 GSE IMPORT.
Digital Image Processing
Electronics II Physics 3620 / 6620
ECE 3567 Microcontroller Lab
Tonga Institute of Higher Education
Debugging Visual Basic Programs
ENERGY 211 / CME 211 Lecture 28 December 1, 2008.
Presentation transcript:

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional What is S-Function? S-Functions is computer language description of Simulink block If written in MATLAB programming environment it is called M-file s-function If written in ‘C/C++’ programming environment it is called C Mex s-function What S-Function Do? Allows you to add your own code to simulink model or environment Code can be written for continuous, discrete or hybrid system Can be use with Real Time Workshop Real Time workshop generates code for Simulink/s-function to run in real time ………..faster F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional How S-function Works Mathematically simulink block consist of a set of inputs u, a set of states x, and set of outputs y Simulation Stages Initialization – port widths, data types, sample time, memories allocation, execution order, evaluates block parameters Simulation loop – Execute block determined during initialization, computes/updates blocks states, derivatives and outputs during a sample time F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional M-file S-function M-files s-function is a matlab function [sys,x0,str,ts] = f(t,x,u,flag,p1,p2,………) Inputs f s-function name t is the current time x is the state vector flag indicates the task to be performed p1, p2 are block parameter to be changed externally Outputs sys Output of s-function x0 [ initial value vector of states] str not used [ ] ts [sample time offset time] M-file s-function template Examples: <matlabroot>/toolbox/simulink/blocks/msfuntmpl.m Sfundemos: >>sfundemos >> is matlab workspace prompt <matlabroot>/toolbox/simulink/simdemos User Manaul: Advantage: Easy to write and compatibility with matlab toolbox functions http://www.mathworks.com/access/helpdesk/help/pdf_doc/simulink/sfunction.pdf F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

Mex s-function are written in C or C++, Fortran, Adao C MEX s-function Mex s-function are written in C or C++, Fortran, Adao C Mex files can modify data structure of Simulink – SimStruct which is used for housekeeping Can handle multiple data types and matrix signals Used with Real Time Workshop , wrapper s-function How to build C Mex s-function Simulink specific naming convention and housekeeping (SimStruct) standards should be followed by C Mex files Template and examples in <matlabroot> / simulink /src sfuntmpl_basic.c >> mex – setup should be run a priori to setup the compiler C Mex files are compiled in MATLAB with mex command Matlab Executable (mex) creates xxx.dll, dynamically loadable file excutable for simulink S- function Builder F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional S-Function Builder Easiest way to embed your C code in Simulink model S-function builder is a Simulink block Act as calling program (wrapper) to C source code from Simulink – providing necessary interfacing options S-function builder generates its own: builder-name.c Have standard code representation similar to sfuntmpl_basic.c builder-name_wrapper.c Customized interfacing options entered in builder builder-name.tlc Permit code generated to run in accelerated and real time Workshop builder-name.dll Simulink executable dll file F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional Importing C code into Simulink for Signal Processing Applications S-function Builder C-code S-function C Simulink M-code S-function Caution: interpreted, not compiled MATLAB >> sim(‘mymodel’) MEX ActiveX/COM & DDE Engine or COM [x,y,t] = sim(‘mymodel’,u0) MATLAB Engine MATLAB Compiler & runtime How? Call… From… F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

Structure of code to be imported: `main() and slicer.c´ test_slicer.exe //slicer.c slicer() { return; } //test_slicer.c main() { for(){ slicer(); } t

Structure of code after import: `wrapped slicer.c´ Call your `slicer.c´ code from a wrapper to interface with Simulink Compile both and link  .dll Simulink calls the DLL using several functions (“methods”) wrap_slicer.dll //slicer.c //definition // of //slicer() //wrap_slicer.c mdlOutputs(S) { slicer(); } Simulink t UNIX is a registered trademark of The Open Group F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

mdlInitializeSizes() mdlInitializeSampleTimes() The “S-function” DLL contains several methods mdlInitializeSizes() mdlInitializeSampleTimes() mdlOutputs() yt = f(t, ut) y = outputs, t = time u = inputs n Done? y mdlTerminate() F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

What about leaf functions with states? int always_one(void) { int y = 0; y = y + 1; return y; } int up(void) static int y = 0; int counter(int *z) *z = *z + 1; return *z; States preserved across time steps Independent set of states per instance FIR, IIR, coders, decoders Also known as... Computer science: “Re-entrant persistent storage” Object-oriented: “property” C++: “member variable” Different from… static variables in C are persistent but shared, not re-entrant If a leaf contains static data it can only be instantiated once F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

Two main types of state available in Simulink Discrete states, xd  Read-only (const) in the mdlOutput() method  Available in S-Function Builder block and full S-function Work vectors, w  Read-write in mdlOutput()  Presently only accessible from full S-function API states xd, w output y=f(t,xd,w,u) input u F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

Approach depends on code structure 1) “Output-update” structure – use S-function Builder block Leaf with separate output and update functions  Use “discrete states”, update them in mdlUpdate() 2) General structure or advanced needs – use full S-function Leaf with output and update functions intermixed  Use “work vectors” , update them in mdlOutputs() yt = f(t, xt, ut), xt+ t = g(t, xt, ut) y = outputs, t = time, x = states, u = inputs [xt+ t , yt]= f(t, xt, ut) F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

mdlInitializeSizes() mdlInitializeSampleTimes() 1) “Output-update” function.c structure mdlInitializeSizes() mdlInitializeSampleTimes() yt = f(t, xt, ut) y = outputs, t = time x = states, u = inputs mdlOutputs() “Division of labor is preferred” mdlUpdate() xt+t = g(t, xt, ut) n Done? y mdlTerminate() F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

mdlInitializeSizes() mdlInitializeSampleTimes() 2) General leaf structure mdlInitializeSizes() mdlInitializeSampleTimes() mdlOutputs() yt = f(t, wt, ut) wt+t = g(t, wt, ut) y = outputs t = time w = work vec u = inputs n Done? y mdlTerminate() F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional Demonstrations Examples of S-function builder S-function Builder block features Slicer: a memoryless block IIR Filter: a block with memory Output-update structure S-function Builder A Discrete state Output in mdlOutputs()and update in mdlUpdate() General structure Full S-function 1-element Work vector Output and update in mdlOutputs() Using the sample code as a resource Tour of the S-function reference material F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

Debugging s-function.dll in MSVC Open sfunc_name.dll file built by S-builder for e.g. simfunc_name.mdl in MSVC Setup ‘breakpoint’ in sfunction.c using Right Mouse button Click Right mouse button on sfunc_name.dll – open ‘setting’ dialouge box, select ‘debug’ tab, under executable field enter ‘C:\MATLAB6p5\bin\win32\matlab.exe’, OK! load sfunction.c in MSVC on the desired line – enabled Choose ‘build’ option from main menu and select ‘ start debug’ and Go! MATLAB will start under debugging mode Open simfunc_name.mdl for degguer ‘MATLAB’ Run the file Watch the stop sign in MSVC sfunction.c file, the program should stop with arrow Select view from main menu and select ‘debug Windows – memory, variable, registers use F11 or steps increment icons from the MSVC toolbar to step to next line in sfunction.c F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional S-function compilation issue* Microsoft Visual C/C++ MATLAB Real Time Workshop Windows Ver 7.1 Recommended Version 7 Ver 2.5.0 XP or 2000 Version 6.0 Version 6 Ver 2.2.0 98 or 2000 *MATLAB TECH NOTE 1601 Always set MATLAB Command Window directory to the directory where source files are stored >> mex –setup % At matlab startup % >> rtwintgt –install % At startup % >> mex –g sfun_name.c % s-function debugging% F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

s-function use with Real Time Workshop For Real Time Window Target Real time Workshop (RTW) generates portable , customize standalone C/C++ code of simulink model which operates in Real time Target Language Compiler (TLC) transform Simulink Block into C/C++ Simulink external mode enables communication between simulink and external process Real Time Window Target (RTWT) is a component of RTW RTWT helps to connect simulink model with external actuators and sensor for control and monitoring of Physical process F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional

s-function examples using s-function builder and MSVC 6.0 Debugging slicer.c and iir_general.c Webinar http://www.mathworks.com/cmspro/req4858.html <%matlabroot>/extern/examples/mex/ <%matlabroot>/toolbox/simulink/blocks/ i) fnpointer - use pointers to pass simulink matrix data to and from external source in C. MATLAB7, RTWT compilation ii) myadd - add matrix internally using array indexes, MATLAB7 iii) disc_stsp7 – Discrete state updates internally, MATLAB7 iv) fnmat – use pointers and array index for internal and external codes, MATLAB7 ludlub – Linear system equation solver, using C source code from ‘Numerical Recipes in C’ ludcmp.c & lubksb.c, MATLAB 6.5 mylms – Least mean square , MATLAB7, RTWT F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional