C-MEX S-Function MATLAB SIMULINK SI Lab Presentation Presented by: Ashkan Jalili 2007/12/08.

Slides:



Advertisements
Similar presentations
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 1 Engineering Problem Solving.
Advertisements

Basic S-Functions and TLC Coding
SIMULINK EXAMPLE transmitter Receiver Channel
MATLAB and SimulinkLecture 71 To days Outline  Callbacks  MATLAB And Simulink  S-functions  Project suggestions.
F. Nagi Dep. Of Mech Eng. Universiti Tenaga Nasional
Write a program step by step. Step 1: Problem definition. Given the coordinate of two points in 2-D space, compute and print their straight distance.
Simulink Model of Human’s Elbow Neuromuscular System Tian Bo.
Module R2 CS450. Next Week R1 is due next Friday ▫Bring manuals in a binder - make sure to have a cover page with group number, module, and date. You.
Simulink Overview: open simulink to open simulink Library Browser.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
1 Engineering Problem Solving With C++ An Object Based Approach Fundamental Concepts Chapter 1 Engineering Problem Solving.
Computer Programming 1 Functions. Computer Programming 2 Objectives Take a first look at building functions Study how a function is called Investigate.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 1.
Macros. There are three basic phases for C programming. preprocessing, compiling, and linking. C input file is first passed to a preprocessing program.
Mex. Introduction to MEX MEX = Matlab EXecutable – Dynamically Linked Libraries – Used like a.m function – Written in C (or Fortran)
Chapter 06 (Part I) Functions and an Introduction to Recursion.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
Introduction to C & C++ Lecture 10 – library JJCAO.
Programming Fundamentals. Today’s Lecture Why do we need Object Oriented Language C++ and C Basics of a typical C++ Environment Basic Program Construction.
MotoHawk™ Components Scalable, Secure, Model-Based Design of Embedded Systems.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Some Fortran programming tips ATM 562 Fall 2015 Fovell (see also PDF file on class page) 1.
© 2003 The MathWorks, Inc. 1 Importing C code into Simulink for Signal Processing Applications © 2003 The MathWorks, Inc. Colin Warwick
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
Programming Fundamentals Enumerations and Functions.
A First Book of ANSI C Fourth Edition
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
Software Engineering Algorithms, Compilers, & Lifecycle.
Functions + Overloading + Scope
User-Written Functions
Chapter 7 User-Defined Methods.
Chapter 6: User-Defined Functions I
Chapter 7: User-Defined Functions II
Engineering Problem Solving With C An Object Based Approach
Computer Science 210 Computer Organization
Objectives You should be able to describe: Interactive Keyboard Input
Matlab Training Session 4: Control, Flow and Functions
Introduction to Exceptions in Java
User-Defined Functions
Chapter 14: Exception Handling
Computer Science 210 Computer Organization
Exceptions & exception handling
User Defined Functions
Exceptions & exception handling
Chapter 12 Exception Handling and Text IO
Visual Basic – Decision Statements
Introduction to Programming
Exception Handling Contents
CSE 1020:Software Development
Presentation transcript:

C-MEX S-Function MATLAB SIMULINK SI Lab Presentation Presented by: Ashkan Jalili 2007/12/08

2 S-Function  S-functions allow you to add your own blocks to Simulink models  Provide a powerful mechanism for extending the capabilities of Simulink  Can be written in MATLAB®, C, C++, Ada, or Fortran

3 Comparison MATLAB m-fileMEX-file  Functions are invoked via flags  Easier access to MATLAB and toolbox functions  Ease of development  Functions are invoked directly  Speed of simulation  Can access workspace directly  Much larger callback functions can be implemented

4 Callback methods : mdlInitializeSizes  Syntax: void mdlInitializeSizes(SimStruct *S) { }  This is the first of the S-function's callback methods that Simulink calls  Specifies the number of inputs, outputs, states, parameters, and other characteristics required for S- function

5 Callback methods : mdlInitializeSizes  ssSetNumSFcnParams ssSetNumSFcnParams Specify the number of parameters that this S- function supports  ssSetSFcnParamTunable(S,paramIdx, 0) Specifies whether a parameter can change during simulation or not

6 Callback methods : mdlInitializeSizes configuration of input and output ports  ssSetNumInput(Output)Ports ssSetNumInput(Output)Ports Specify the number of input and output ports that this S-function has  ssSetInput(Output)PortDimensionInfo ssSetInput(Output)PortDimensionInfo Specify the dimensions of the input and output ports  ssSetInputPortDirectFeedThrough ssSetInputPortDirectFeedThrough Specifies whether the input is used in mdlOutputs callback method for calculation of output or not

7 Sample Time  Block-based sample times: S-function specifies a set of operating rates for the block as a whole during the initialization phase of the simulation.  Port-based sample times: S-function specifies a sample time for each input and output port individually during initialization

8 Sample Time Block-Based VS Port-Based  With block-based sample times S-function processes all inputs and outputs each time a sample hit occurs for the block, while with port-based sample times, the block processes a particular port only  Example: C onsider two sample times, 0.5 and 0.25 seconds Block-based method, would direct the block to execute inputs and outputs at 0.25 second increments Port-based method, you could set the input port to 0.5 and the output port to 0.25, and the block would process inputs at 2 Hz and outputs at 4 Hz.

9 Block-Based Sample Times  In mdlInitializesize, ssSetNumSampleTimesssSetNumSampleTimes(S,numSampleTimes); numSampleTimes > 0  In mdlInitializeSampleTimes, ssSetSampleTimessSetSampleTime(S, PortIndex, sample_time) sample_time: CONTINUOUS_SAMPLE_TIME, discrete_sample_period, INHERITED_SAMPLE_TIME

10 Port-Based Sample Time  In mdlInitializesize: ssSetNumSampleTimesssSetNumSampleTimes(S, PORT_BASED_SAMPLE_TIMES) ssSetInputPortSampleTimessSetInputPortSampleTime(S, idx, period) ssSetOutputPortSampleTimessSetOutputPortSampleTime(S, idx, period)  Inherited Sample Time for a Port ssSetInputPortSampleTime(S, 0, -1);  Constant Sample Time for a Port: ssSetOptions(S, SS_OPTION_ALLOW_CONSTANT_PORT_SAMPLE_TIME); ssSetInputPortSampleTime(S, 0, mxGetInf());

11 Callback methods: mdlOutputs  Syntax void mdlOutputs(SimStruct *S, int_T tid)  Compute the signals that this block emits  tid is from: ssIsSampleHit(S, st_index, tid) Example: ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME); ssSetSampleTime(S, 1, 0.75); ssSetSampleTime(S, 2, 1.0); if (ssIsSampleHit(S, 1, tid)) { } The second argument, 1, corresponds to the second sample time, 0.75 second

12 Callback methods: mdlTerminate  Syntax void mdlTerminate(SimStruct *S)  Perform any actions required at termination of the simulation  option SS_OPTION_CALL_TERMINATE_ON_EXITSS_OPTION_CALL_TERMINATE_ON_EXIT  mdlStart Initialize the continuous and discrete states, if any Initialization activities such as allocating memory or setting up user data

13 S-Function Source File Requirements  Statements Required at the Top of S-Functions: #define S_FUNCTION_NAME your_sfunction_name_here #define S_FUNCTION_LEVEL 2 #include "simstruc.h“  Callback Methods That an S-Function Must Implement: mdlInitializeSizes mdlInitializeSampleTimes mdlOutputs mdlTerminate  Statements Required at the Bottom of S-Functions #ifdef MATLAB_MEX_FILE #include "simulink.c" #else #include "cg_sfun.h“ #endif

14 Exception Free Code  Refers to code that never long-jumps Ex: mexErrMsgTxt throws an exception when called. Issues error message and return to MATLAB prompt.  If code is exception free, in mdlInitializeSizes set: ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);  All mex* routines have the potential of long-jumping.  Run time routines: mdlGetTimeOfNextVarHit mdlOutputs mdlUpdate mdlDerivatives

15 Example  “timestwo” block double the input

16 Example Code #define S_FUNCTION_NAME timestwo #define S_FUNCTION_LEVEL 2 #include "simstruc.h" static void mdlInitializeSizes(SimStruct *S)mdlInitializeSizes { ssSetNumSFcnParams(S, 0); if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) { return;} if (!ssSetNumInputPorts(S, 1)) return; ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED); ssSetInputPortDirectFeedThrough(S, 0, 1); if (!ssSetNumOutputPorts(S,1)) return; ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED); ssSetNumSampleTimes(S, 1); ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE); }

17 Example Code static void mdlInitializeSampleTimes(SimStruct *S)dlInitializeSampleTimes { ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME); ssSetOffsetTime(S, 0, 0.0); } static void mdlOutputs(SimStruct *S, int_T tid)mdlOutputs { int_T i; InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0); real_T *y = ssGetOutputPortRealSignal(S,0); int_T width = ssGetOutputPortWidth(S,0); for (i=0; i<width; i++) { *y++ = 2.0 *(*uPtrs[i]); } }

18 Example Code static void mdlTerminate(SimStruct *S) {}mdlTerminate #ifdef MATLAB_MEX_FILE #include "simulink.c" #else #include "cg_sfun.h" #endif

19 Compile and Link the code  mex “s-fcname”.c  mex –setup  In S-Function Block: S-Function name (no extension and path name) Parameters

20

Thank You