S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling...

22
1 © 2015 The MathWorks, Inc. Automatic Code Generation for Embedded Control Systems Tiffany Liang Application Engineer MathWorks

Transcript of S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling...

Page 1: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

1© 2015 The MathWorks, Inc.

Automatic Code Generation for Embedded Control Systems

Tiffany LiangApplication EngineerMathWorks

Page 2: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

2

Embedded Control Systems

Automotive Aero/Def

Power Electronics

Robot

Construction Machine

Industrial Equipment

Page 3: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

3

Model-Based Design “From Concept to Code”

Modeling &Simulation

Control Design Physical Modeling

Real-Time Test Code Generation Verification & Validation

Page 4: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

4

Quick MBD OverviewBrushless DC Motor Control

TI F28069ISO + DRV8312-C2-KIT used

if (g_f4_iq_ref > MTR_LIMIT_IQ) {g_f4_iq_ref = MTR_LIMIT_IQ;

} else {if (g_f4_iq_ref < -MTR_LIMIT_IQ) {g_f4_iq_ref = -MTR_LIMIT_IQ;

}}

ModelCode

Generation

Code

Page 5: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

5

Page 6: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

6

Example on YouTube MATLAB Channel

https://www.youtube.com/watch?v=wxYTLbYfBP0

Page 7: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

7

How to Embed Your Model?Coder Solution!

Simulink Coder®

RCP/HILS

C

Embedded Coder®

HDL Coder™

FPGA/ASIC

HDL

SimulinkPLC Coder™

PLC

ST

MATLAB Coder®

MATLAB

MEX

C

MCU/DSP

C/C++

Page 8: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

8

C/C++ Code Development with Embedded Coder

if (reset) {y = 0;} else {y += k * u;}

MATLAB ProgramSimulink/Stateflow Model

Code Generation

Used with existing code Used for implementation Standalone executable/dll

Page 9: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

9

What is Your Merit?

Graphical modeling– Readable, easy understanding

Early verification & validation– Concept test without hardware

Modeling & Simulation

Automatic Code Generation Coding time reduction Error reduction from model to

code conversion Automatic synchronization

between model and code

if (reset) {y = 0;} else {y += k * u;}

Page 10: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

10

User Story

Nearly 100% of the software for many of Volt’s modules was generated automatically.

“We have a single source for how a particular function should behave. Automatic code generation using The MathWorks’ Real-Time Workshop Embedded Coder was vital to meeting Volt’s aggressive program timing.”

Greg HubbardSenior Manager

Credit: General Motors LLC 2011

Page 11: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

11

Embedded Coder Quick DemoModeling

Configuration

Data Attributes

boolean_T reset;const volatile int8_T k = 2;

void rst_cntr_step(void){if (reset) {y = 0;

} else {y += (int16_T)(k * u);

}}

Equivalent Code as Model High Readability & Efficiency

Page 12: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

12

Data Attributes You can define data

attributes in your model– Signal line– States (Unit Delay etc.)– Block parameter– Stateflow data

boolean_T reset;const volatile int8_T k = 2;

void rst_cntr_step(void){

if (reset) {y = 0;

} else {y += (int16_T)(k * u);

}}

void rst_cntr(boolean_T reset,int16_T u,int16_T *y)

Global VariableFunction Argument

Page 13: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

13

Code Generation Report

Code Metrics(lines, memory size, etc.)

Generated Code

Simulink Report Generator required to generate model view

Traceability between Model and Code

html report

Page 14: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

14

How about Efficiency of Generated Code?

Page 15: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

15

How about the Standard Compliance?

Most rules satisfied You can download MISRA compliance document

http://www.mathworks.com/matlabcentral/answers/102532-misra-c

Tool Qualified by TÜV SÜD(ISO26262 ASIL A-D)

You can download AUTOSAR Support Package via Simulink menu

MISRA C

ISO 26262 / IEC 61508

AUTOSAR

Page 16: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

16

How to Design Fixed-Point Controller?Fixed-Point Designer

Fixed-Point Data Type Settings

Conversion between Float and Fixed-Point Fixed-Point Simulation

Fixed-Point Auto Scaling Fixed-Point Code Generation

Useful for Quantization Error Test

Efficient Fixed-Point Scaling Efficient Code Generation

int16_T k = 13U;

y = y + k * u >> 3;Scaling Adjustment CalculationReduce Quantization Error

Fixed-Point Value

Page 17: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

17

Vout = 2VControlled by

TI C2000 MCU

One More Example:Digital DC/DC Converter Control System

TI C2000 DPS Workshop Kit (CPU: F28035)

Vin = 9V

Page 18: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

18

Simulation ModelUse for circuit/controller design, parameter tunings

Making an analog circuit with Simscape components Fixed-point PI controller + Rate Limiter (Soft Start)

Page 19: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

19

Code Generation Model to F28035Turnkey to download the controller model into MCU

Using EC TI C2000 support package to implement I/O and ISR

1 kHz periodic task

100Hz periodic task

PWM ISR200 kHz task

Page 20: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

20

CLR/IQmath Blocks and PIL ProfilingC2000 Support Package provides code optimization opportunity and execution time estimationModel Generated Code PIL ExecutionANSI-C (No Optim)

rtb_Gain * Pgain6.15 us(cannot runat 200 kHz)

TI C28x CLR Onlyc28x_mul_s32_s32_s32_sr(Pgain, rtb_Gain, 24L)

3.23 us

TI C28x CLR + IQmath_IQ24mpy(rtb_Gain, Pgain) 1.8 us

Page 21: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

21

Page 22: S3 Automatic Code Generation for Embedded Control Systems · What is Your Merit? Graphical modeling – Readable, easy understanding Early verification & validation – Concept test

22

Summary

ConceptSpecification

ModelDesktop Simulation

MCU/DSPExperiment

Reduce CodingCoder Products

Focus on ImprovementSimulink/Stateflow

Code

You can achieve highly efficient development cycle