c code generation with embedded matlab (conference 2010)
TRANSCRIPT
1
MATLAB to C Code Generation with
Embedded MATLAB
SahngHoh Kwon
2
Agenda
Introduction– Challenges of the MATLAB-to-C workflow
What is Embedded MATLAB?– Benefits of the Embedded MATLAB workflow
How to use Embedded MATLAB?– Demonstration
– Tracking a moving object with a Kalman filter
Summary
3
Traditional Concept to Implementation
Re-implementas you go down the level of abstraction
MATLAB code C/C++ codeAssembly code
HDL
This workflow requires you to:
maintain multiple copies of the same algorithm
fix and test across implementation boundaries
Workflow challenges:
time-consuming and error-prone
not scalable to the system level
4
A better workflow would be…
One language
– No multiple copies of source code
– No redundant debugging and testing
One integrated design environment
– Integrated real-world design constraints in MATLAB
– Integrated visualization, analysis, and debugging
– Ease of porting individual algorithm IPs in MATLAB
Automatic code generation
– Path to embedded software ( MATLAB to C)
5
Embedded
Embedded MATLAB is the subset of MATLAB for embedded algorithm
development
What Is Embedded MATLAB?
cellarrays
variable-sized data
objects
java
visualization
analysisarrays
struct
numeric
fixed-point
functions
nested functions
complex
sparse
6
Features of Embedded MATLAB
Extensive function library– 300 MATLAB operators and functions
– 110 Fixed-Point Toolbox functions
– 40 Signal Processing Toolbox functions
Automatic C code generation – emlmex – Compiled C code from MATLAB code
– emlc – C source code from MATAB code
Reference functions on the MATLAB path– Enables partitioning of large Embedded MATLAB programs
Ability to call custom C code directly from your MATLAB function
Integration with Simulink and Stateflow
7
Generating C Code Automatically
with Embedded MATLAB Subset
1. Examine your original MATLAB algorithm.
2. If necessary, modify it to conform with the Embedded MATLAB subset.3. Generate C code using the emlc command.
4. If necessary, iterate to optimize the MATLAB code for memory and
speed.
5. Verify easily within MATLAB.
Original
MATLAB
Code
Compliant
MATLAB
Code
Generate
C Code C Code
Optimize the MATLAB
Code
MEX
exe
lib
8
Demo: Tracking a Moving Object with a
Kalman Filter
Using Embedded MATLAB
to generate C code directly
from MATLAB code
9
Kalman Demonstration Workflow
1. Implement MATLAB algorithm with floating-point data types.
2. Convert MATLAB to MEX using emlmex function for fast
simulation and large test-set verification.
3. Convert MATLAB to C using emlc function for embedded
implementation.
4. Modify algorithm to handle packets (frames) of variable-sized
data.
10
Variable-Size Data Supported in
MATLAB-to-C Code Generation
Define variable-size inputs, outputs, and local variables, with known
upper bounds.
Expands the scope of automatic C code generation for typical MATLAB
algorithm
Ensures static memory allocation in generated C code for variables that
change in size during execution
11
Fixed-Point Acceleration: Introducing emlmex Function
Automatically generates C-MEX
function from MATLAB function
(MATLAB algorithm must
conform to Embedded MATLAB
language subset.)
Runs at compiled C-code
execution speed (may remove
speed bottlenecks)
Available with Fixed-Point
Toolbox
12
C Code Generation from MATLAB: Introducing emlc Function
Automatically generates C source
code from your Embedded
MATLAB code, from the MATLAB
command line
Supports floating-point and fixed-
point variables with up to 128-bit
word-lengths
Uses only native C floating-point
and integer data types
Available with Real-Time Workshop
13
Product Licensing Requirements
Capability Required Product
Generate embeddable C code using emlccommand
MATLAB, Simulink, and
Real-Time Workshop
Generate a compiled MATLAB executable (MEX) function using emlmex command
Fixed-Point Toolbox or
Simulink
14
MATLAB®Simulink®
Embedded MATLAB™
codeEmbedded MATLAB™
functions
• Algorithm development
• Data analysis
• Advanced visualization
• Graphical models
• Time-based simulation
• Multi domain modeling
• System verification
HDL code
MathWorks Code Generation Products
Embedded C code
Domains
15
Power of MATLAB in Simulink
Math-intensive algorithm
Easy conversion from MATLAB
Can build standalone simulation
applications
Call custom C code
C code or executable
Embedded MATLAB
runtime library
Simulink
Embedded MATLAB
Function
Embedded MATLAB™ functions
in Simulink®
16
Demo: Inverse Kinematics of Robot
Manipulator
17
pythagoras_lct.c
Calling External C Code from an
Embedded MATLAB™ Function
18
Summary(1)
Embedded MATLAB is the subset of the MATLAB
language for embedded implementation
– 300 MATLAB operators and functions
– 110 Fixed-Point Toolbox functions
– 40 Signal Processing Toolbox functions
It narrows the concept-to-implementation design gap
– Automatic MATLAB-to-C translation
It makes “conceptual” MATLAB algorithm
“implementation-ready”
– Complying with Embedded MATLAB subset
19
Summary(2)
emlc command – Automatically generate C source code from your compliant
MATLAB code directly from the MATLAB desktop
Embedded MATLAB Function block– Seamlessly integrate your Embedded MATLAB compliant
algorithm into Simulink
– Simulate the effect of your algorithm in the context of the
whole system
– Automatically generate C code using Real-Time Workshop
20
For More Information
Embedded MATLAB Web page
– Description, demos, customer testimonials, technical info
– Links to related MathWorks products
– www.mathworks.com/products/featured/embeddedmatlab/
Webinars featuring Embedded MATLAB
– Generate C Code from MATLAB Functions Using the Embedded
MATLAB Subset www.mathworks.com/company/events/webinars/wbnr40173.html
MATLAB Central: User-contributed examples
– Look for “Embedded MATLAB”
– www.mathworks.com/matlabcentral