master class production code generation (pcg) · master class production code generation (pcg) ......

50
1 © 2014 The MathWorks, Inc. Master Class Production Code Generation (PCG) By Simon Eriksson

Upload: others

Post on 31-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

1© 2014 The MathWorks, Inc.

Master Class Production Code Generation (PCG)

By Simon Eriksson

Page 2: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

2

Case Study Introduction

We are design or software engineers who inherited a Simulink model

Our Tasks:

– Implement the algorithm in C code.

– Verify that the generated code produces the same output as the original model.

Page 3: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

3

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 4: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

4

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 5: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

5

Demonstration

I will…

1. Introduce the Simulink model

2. Introduce the test harness model

3. Setup the model for code generation

4. Generate code from the model

Page 6: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

6

What Files are Generated?

model.c Model algorithm code

model_data.c Parameter definitions

ert_main.c Example main function (optional)

model.h Public interface to model code

model_private.h Private macros, private data, etc.

model_types.h Data types, structures and macros

Page 7: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

7

What Functions are Generated?

model_step: Contains your algorithm code

void model_step(void)

model_initialize: Initializes the model variables and states

void model_initialize(void)

model_terminate (optional): Hardware shutdown

void model_terminate(void)

Page 8: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

8

Optimizing Generated Code based on Specified Objectives

Page 9: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

10

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 10: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

11

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 11: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

12

Subsystems

A subsystem is a block containing other groups of blocks

A subsystem can be either virtual or atomic

Virtual or atomic can influence the execution sequence of the blocks within it

Page 12: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

13

Making a virtual subsystem atomic

Page 13: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

14

Influence on the Structure of the Generated Code

Virtual subsystems does not influence the structure of the code of the

blocks

The code for blocks in an atomic subsystem will be contiguous in the

generated code

Atomic subsystems can be used in three ways:

– Inline

– Nonreusable function

– Reusable function

Page 14: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

15

Atomic Subsystems

Inline: Inlined contiguous code

/* Start of Subsystem1 */

code line 1

code line 2

...

/* End of Subsystem1 */

Page 15: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

16

Atomic Subsystems

Nonreusable function: Contiguous code inside a void-void function or a void function with arguments

/* Start of Subsystem1 */

void Subsystem1(void)

{

code line 1

code line 2

...

}

/* End of Subsystem1 */

Page 16: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

17

Atomic Subsystems

Nonreusable function: Contiguous code inside a void-void function or a void function with arguments

/* Start of Subsystem1 */

void Subsystem1(arg1, arg2, ...)

{

code line 1

code line 2

...

}

/* End of Subsystem1 */

Page 17: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

18

Atomic Subsystems

Reusable function: Contiguous code inside a void function with arguments (reused across model)

/* Start of Subsystem1 */

void Subsystem1(arg1, arg2, ...)

{

code line 1

code line 2

...

}

/* End of Subsystem1 */

Page 18: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

19

Demonstration

I will…

1. Make PI_ctrl a reusable function

Page 19: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

20

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 20: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

21

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 21: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

22

Default Function Interface

Algorithm code called at base sample rate

Initializes variables ones

Optional (hardware shut down)

Page 22: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

23

Function Prototype Control

Pass arguments by value or pointer

Control argument names and order

Simplifies code integration and testing

Reduces global RAM usage

Page 23: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

24

Demonstration

I will…

1. Show how to change the code interface

– Change the function prototype of model_step

– Change the name of model_initialize

Page 24: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

25

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 25: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

26

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 26: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

27

Data Characteristics

What information could be contained in a variable definition and

declaration?

– Name

– Data type

– Dimension

– Initial value

– Comment (description, units, etc.)

– Scope (global, local, static, extern)

– Type (const, pointer)

– Placement (definition file, header file)

Simulink Data Object

Page 27: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

28

Creating Data Dictionariesusing Simulink Data Objects

Excel sheet Data Object Wizard MATLAB script

Simulink Data Objects

Page 28: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

29

Simulink Data Objects

Page 29: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

30

Control Storage Class of Datausing Simulink Data Objects

Standard storage classes:

Global

Bit Field

Volatile or Const Volatile

Export to File

Import From File

Structure

GetSet

Simulink Global

External Global

Imported External Pointer

Imported External

Etc.

Page 30: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

31

Demonstration (1)

I will…

1. Use Data Object Wizard to:

a. Create parameter objects for all parameters

b. Create signal objects for selected signals

2. Change code generation properties for:

a. Signal objects

b. Parameter objects

Page 31: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

32

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 32: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

33

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 33: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

34

Including Legacy Code

Generated code

void model_step(void)

{

uint32 u1;

uint32 y1;

u1 = DevDrvFcn();

y1 = UtilFcn(u1);

ExtSetFcn(y1);

}

Device driver function

uint32 DevDrvFcn(void)

{

uint32 out;

...

return out;

}

Utility function

uint32 UtilFcn(uint32 u)

{

uint32 out;

...

return out;

}

External set function

void ExtSetFcn(uint32 y)

{

...

}

Page 34: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

35

Integrating Code with Simulink

How could I integrate code with Simulink?

– S-Function

Legacy Code Tool

S-Function Builder

Hand-Crafted S-function

– Custom Code

– Stateflow

– Target Function Library

Page 35: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

36

Legacy Code Tool

specs = legacy_code('initialize’)

Page 36: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

37

Demonstration (1)

I will…

1. Present external utility function (look-up table)

– SimpleTable.c

– SimpleTable.h

2. Run Legacy Code Tool script that:

– Creates S-function block (interface to SimpleTable)

3. Replace model look-up tables with S-function blocks

– Verify that model behavior is correct

– Generate and inspect code

Page 37: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

38

Data Type Replacement

Using your own defined data types in the generated code

Built-in types

Fixed-point types

Page 38: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

39

Creating Reconfigurable Data Types

Simulink data type classes:

– Simulink.AliasType

Supports only built-in types

– Simulink.NumericType

Expanded support to fixed-point types

Page 39: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

40

Creating Reconfigurable Data Types

With Simulink.AliasType you can :

– DBL double

– FLT single

– INT int32

– UINT uint32

Page 40: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

41

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 41: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

42

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 42: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

43

Why Perform Testing on the Generated Code?

Often, behaviour in simulation and on-target is identical

Some small differences may be acceptable

– Non-IEEE floating-point on the target

– Implementation of libraries, e.g. for trigonometric functions

– Differences due to hardware-specific optimised code

Page 43: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

44

Why Perform Testing on the Generated Code?

Other differences may indicate a problem

– Unintended side effect of compiler settings or optimizations

– Unintended side effect of code generator settings and optimizations

– Defects in the hardware, the compiler, the linker, or the code generator

The later problems are identified, the more costly they are to correct

Page 44: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

45

Software-in-the-Loop (SIL) Testing

Compiled C Code

(S-Function)

Code

Generation

Execution:

• Host/host

• Non-real-time

Page 45: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

46

Demonstration

I will…

1. Show how to create a SIL S-function block

2. Use the SIL block to perform SIL testing

Page 46: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

47

Processor-in-the-Loop (PIL) Testing

Production Processor

Code

Generation

Execution

• Host/target

• Non-real-time

Exercise the same object

code that will be used in

production software

Page 47: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

48

Verify behavior and execution time of compiled code with

processor-in-the-loop testing

Processor-in-the-loop (PIL) testing generates, cross-compiles, and executes production

object code on a target processor or simulator

Simulink

Embedded

Processor

Seria

l / TC

PIP

Serial / T

CP

IP

PIL Implementation

Code

Generation

Test

SignalsVerifications

Controller

Model

Controller

C Code

Page 48: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

49

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 49: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

50

Questions to be Answered Today

How do I…

1. Setup my model for production code generation?

2. Partition the code into sub-functions?

3. Change the code interface?

4. Affect variable naming and properties?

5. Include my own legacy code?

6. Verify the generated code?

Page 50: Master Class Production Code Generation (PCG) · Master Class Production Code Generation (PCG) ... Excel sheet Data Object Wizard MATLAB script Simulink Data Objects. 29 ... (PIL)

51

Thank you!