Está en la página 1de 20

MATLAB to C Code Generation with

Embedded MATLAB

SahngHoh Kwon

1
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

2
Traditional Concept to Implementation

Assembly code
MATLAB code C/C++ code
HDL

Re-implement
as you go down the level of abstraction
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

3
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)

4
What Is Embedded MATLAB?
Embedded MATLAB is the subset of MATLAB for embedded algorithm
development

objects
java
variable-sized data sparse
struct visualization
functions

Embedded
numeric arrays analysis
complex cellarrays
fixed-point
nested functions

5
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

6
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.

MEX

Original Compliant Generate


C Code exe
MATLAB MATLAB C Code
Code Code
lib

Optimize the MATLAB


Code
7
Demo: Tracking a Moving Object with a
Kalman Filter

Using Embedded MATLAB


to generate C code directly
from MATLAB code

8
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.

9
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

10
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

11
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

12
Product Licensing Requirements
Capability Required Product
Generate embeddable C code using emlc MATLAB, Simulink, and
command Real-Time Workshop

Generate a compiled MATLAB executable Fixed-Point Toolbox or


(MEX) function using emlmex command Simulink

13
Domains

MATLAB® Simulink®
• Algorithm development • Graphical models
Embedded MATLAB™ Embedded MATLAB™ • Time-based simulation
• Data analysis
• Advanced visualization
code functions • Multi domain modeling
• System verification

MathWorks Code Generation Products

Embedded C code HDL code

14
Embedded MATLAB™ functions
in Simulink®
Power of MATLAB in Simulink Embedded MATLAB
runtime library

Math-intensive algorithm

Easy conversion from MATLAB Simulink


C code or executable

Can build standalone simulation


applications
Embedded MATLAB
Function
Call custom C code

15
Demo: Inverse Kinematics of Robot
Manipulator

16
Calling External C Code from an
Embedded MATLAB™ Function

pythagoras_lct.c

17
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

18
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

19
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

20

También podría gustarte