Afghanistan
Albania
Algeria
Andorra
Anguilla
Antigua & Barbuda
Argentina
Armenia
Aruba
Australia
Austria
Azerbaijan
Bahamas
Bahrain
Bangladesh
Barbados
Belarus
Belgium
Belize
Benin
Bermuda
Bhutan
Bolivia
Bonaire
Bosnia & Herzegovina
Botswana
Brazil
British Indian Ocean Ter
Brunei
Bulgaria
Burkina Faso
Burundi
Cambodia
Cameroon
Canada
Canary Islands
Cape Verde
Cayman Islands
Central African Republic
Chad
Channel Islands
Chile
China
Christmas Island
Cocos Island
Colombia
Comoros
Congo
Cook Islands
Costa Rica
Cote D Ivoire
Croatia
Cuba
Curacao
Cyprus
Czech Republic
Denmark
Djibouti
Dominica
Dominican Republic
East Timor
Ecuador
Egypt
El Salvador
Equatorial Guinea
Eritrea
Estonia
Ethiopia
Falkland Islands
Faroe Islands
Fiji
Finland
France
French Guiana
French Polynesia
French Southern Ter
Gabon
Gambia
Georgia
Germany
Ghana
Gibraltar
Greece
Greenland
Grenada
Guadeloupe
Guam
Guatemala
Guinea
Guyana
Haiti
Hawaii
Honduras
Hong Kong
Hungary
Iceland
India
Indonesia
Iran
Iraq
Ireland
Isle of Man
Israel
Italy
Jamaica
Japan
Jordan
Kazakhstan
Kenya
Kiribati
Korea North
Korea South
Kuwait
Kyrgyzstan
Laos
Latvia
Lebanon
Lesotho
Liberia
Libya
Liechtenstein
Lithuania
Luxembourg
Macau
Macedonia
Madagascar
Malawi
Malaysia
Maldives
Mali
Malta
Marshall Islands
Martinique
Mauritania
Mauritius
Mexico
Midway Islands
Moldova
Monaco
Mongolia
Montserrat
Morocco
Mozambique
Myanmar
Nambia
Nauru
Nepal
Netherland Antilles
Netherlands (Holland, Europe)
Nevis
New Caledonia
New Zealand
Nicaragua
Niger
Nigeria
Niue
Norfolk Island
Norway
Oman
Pakistan
Palau Island
Palestine
Panama
Papua New Guinea
Paraguay
Peru
Philippines
Pitcairn Island
Poland
Portugal
Puerto Rico
Qatar
Republic of Montenegro
Reunion
Romania
Russia
Rwanda
Saipan
Samoa
Samoa American
San Marino
Sao Tome & Principe
Saudi Arabia
Senegal
Serbia
Seychelles
Sierra Leone
Singapore
Slovakia
Slovenia
Solomon Islands
Somalia
South Africa
Spain
Sri Lanka
St Barthelemy
St Eustatius
St Helena
St Kitts-Nevis
St Lucia
St Maarten
St Pierre & Miquelon
St Vincent & Grenadines
Sudan
Suriname
Swaziland
Sweden
Switzerland
Syria
Tahiti
Taiwan
Tajikistan
Tanzania
Thailand
Togo
Tokelau
Tonga
Trinidad & Tobago
Tunisia
Turkey
Turkmenistan
Turks & Caicos Is
Tuvalu
Uganda
Ukraine
United Arab Emirates
United Kingdom
United States of America
Uruguay
Uzbekistan
Vanuatu
Vatican City State
Venezuela
Vietnam
Virgin Islands (Brit)
Virgin Islands (USA)
Wake Island
Wallis & Futana Is
Yemen
Zaire
Zambia
Zimbabwe
Why integrate legacy code?
Simulink
C++
C
Matlab
Fortran
Simulink
Outline
Calling MATLAB Code from Simulink
Calling External Routines
Writing Wrapper S-Functions
Code Generation Considerations
Review
Code integration methods
Conclusions
Caveats
Hodge-podge of methods for integration
No unified development
2012: Matlab Coder
C++ and Fortran deprecated
No other languages supported
Main point: reconcile MATLAB (interpreted) vs. C (Compiled)
System Function S-Function
Have to fit simulink operational structure
Primary method for making code (in C) available to Simulink
Code integration methods
Overview of code integration methods
State how a Sumulink model interacts with user-defined blocks.
Identify the fundamental techniques for integrating external code into a Sumulink model.
Introduction to the S-Function
HOw S-Functions work
Initialize simulation
Order components
Calculate the time of next calculation
Calculate output
Update discrete states
Calculate derivatives
Calculate outputs
Calculate derivatives
Calling MATLAB code from Simulink
Writing a MATLAB Function BLock
Connecting a MATLAB function to a MATLAB FUnction block
MATLAB Function block coding standards
Implement an algorithm in Sumulinkn using a MATLAB Function Block
Given a MATLAB function, integrate the function into Simulink using MATLAB Function block
MATLAB Code Methods
Matlab Code Methods
MATLAB Function BLoc
MATLAB S-Function
Can accept multiple input signals and produce multiple output signals
Can contain local functions
Can call many MATLAB and user-defined functions
Is compatible with Simulink Coder for CERTAIN situations
Can't support all MATLAB into C.
MATLAB Function Block
Can't write toys like displays and plots for C code.
Can call all MATLAB functions in simulation
Opens to a MATLAB editor
Found in User-Defined functions
Can't convert Java or visualization
Can build stand-alone simulation applications
Check specific documentation for usage notes
Can have mulitpule inputs/outputs
Can switch Input terms between Inputs and parameter.
Simulink -> edit data allows tweaking of hard data types
Parameters will be found in MATLAB workspace
Calling external functions
Will suck in secondary .m file during runtime.
Have to add "coder.extrinsic" in all files involved
Rules for MATLAB function block
Using matrix indexing operations
Declaring and initializing persistent variables
Declaring uninitialized variables
Declare variables by assignment
Working with characters
Working with complex numbers
z(2,2) = -1; % Error
coder.screener
%#codegen indicates code generation intent.
>>coder.screener('blah.m')
Command-line only
Will pre-screen your code
Persistence
"persistent" keyword
Exercise
Vectorize for loops
Guard against zero values in parameters
F-3
Make a bunch of stuff persistent
All initial conditions need to be persistent.
Calling External Routines
Calling an external C routine in a MATLAB Function
Choose tool situationally
Given a C function integrate the function in to Simulink model using:
Legacy Code Tool
A MATLAB function block
Calling an external C routine with the Legacy Code Tool
C
Stateflow Custom code interface
Coder.ceval in MATLAB Function Block
C MEX S-function
C Code Methods
S-Function Builder
Legacy Code Tool
Manual C code wrapper
C++
Manual C++ code wrapper
Stateflow custom code interface
Integration Methods
Fortran
Level 2 Fortran S-Function
Level 1 Fortran S-Function
Legacy Code Tool
S-Function generation can be scripted
Automatic S-Function block creations
Cannot automatically add code around external routine calls
Limited code formatting control options
Quick method of bringing C code ito Simulink
Delcare extra files as part of the input structure
Generates a block.
Can run as a script. Does not need to be a function
Two main components
legacy_code function
used to specify properties of your C code interface
Legacy Code Tool data structure
verb to do the specification
Results
All inputs are U
All outputs are Y
Parameters are P's.
See table for syntax
Will write wrapper Level 2 interface function for .mex call
All data types are converted
coder
Runs from within block
Can operate on multiple systems simultaneously.
Pointers: coder.rref() coder.wref()
Map function prototype 1:1 to coder call
Different syntax
Files added for coder under Configuration-> Simulation Target -> files
To attach files need to do it in configuration parameters for the MODEL.
Persistent variables
Move to a work vector in the model (pass references)
Move to a delay function in the Simulnk model
clear mex during initialization
Results
All port numbers and types are per the function call
Simulink block that calls external code
Exercise
F-5
Start with a template: edit pythagorias_lct
Use legacy code tool to generate a block
bi_quad.c&&h
Replace filter subsystem with a function call
Hard part is getting the function call correct in the tool script.
biquad_lct
S-Function Templates
Additional macros
Inheriting I/O port dimensions
Work vectors
Calling external code from a C MEX S-function
Writing a C MEX S-function
Given a C program integrate that program into Simulink using a C S-function wrapper.
Use a template, stupid.
Good luck.
Edit sfuntemp1_basic.c
Try to follow Simulink workflow
Can call external C code from within the wrapper
Initialization
Initialize sample times
initialize signals
Simulation Loop
mdlOutputs
SimStruct Functions
doc for details on everything.
Bazillions of accessor and set functions to private data.
Passing SimStruct S everywhere.
Work vectors
Allows for separate static variables
dwork (General purpose)
pwork (pointers)
iwork (integers)
rwork ("real"/floating-point)
Allocated to each instance of an S-Function
Model Termination
Functions
ssSetDWorkWidth Sets number of elements in each DWork vector
ssGetDWork/ssSetDWork
ssSetNumDWork set the number of dwork vectors in the S-Function
Inheriting ports
When specifying use DYNAMIC_DIMENSION
Exercises
F-11
Fill out template add_nums.c|h
Start with sfun_tmpl.c
Code Generation
State code generation limitations for each code integration method
State the procedure for generating code from each code integration method
From a C MEX S-Function (Legacy Code Tool)
From a MATLAB Function block
Matlab Function block
Make Solver Options Fixed Step
Make parameters Tunable under Code Generation -> Option
Use ert.tlc for System Target File
Extrinsic functions are not for code generation. Simulation only.
From C-MEX S-Functions
Can request configuration file from legacy_code tool
Or write a wrapper (another layer)
Write a .tlc file
Fully inline functions
Hardware
Can generate stright from block diagrams
Matlab coder
Simulink coder
Hardware Support Packages
Has the communication tools for the hardware
Gives extra options to "download to board"
Extra TLC files
Arduino
Has the compiler available
In-the-loop