Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in...

48
1 © 2014 The MathWorks, Inc. Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI

Transcript of Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in...

Page 1: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

1© 2014 The MathWorks, Inc.

Modeling HDL components for

FPGAs in control applications

Mark Corless, Principal Application Engineer, Novi MI

Page 2: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

2

High resolution

voltage

modulation

Low latency

control loops

Critical

diagnostics

Position

sensing

System cost

reduction

Why would I use an

FPGA for a controls application?

FPGA = Field Programmable Gate Array

Page 3: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

3

How do I get an idea

out of my head

and into an

FPGA?

Page 4: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

4

How do I get an idea

out of my head

and into an

FPGA?

How do I get an idea

out of my head

and into a microprocessor?

Page 5: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

5

desktop simulation

rapid prototyping

production code generation

Page 6: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

6

That sounds nice, but…

Show me!

Page 7: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

7

Begin with existing C component models

Position

Velocity

Units

Open

Loop

Disabled

Encoder

CalibrationVoltage

Units

Current

Units

Mode

Scheduler

Field

Oriented

Control

Velocity

Control

Page 8: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

8

Create new HDL component models

Position

Velocity

Units

Open

Loop

Disabled

Encoder

CalibrationVoltage

Units

PWM

Peripheral

Current

Units

Encoder

Peripheral

Mode

Scheduler

Field

Oriented

Control

Velocity

Control

C HDL

HDL = Hardware Description Language

Page 9: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

9

Migrate some component models from C to HDL

Position

Velocity

Units

Open

Loop

Disabled

Encoder

CalibrationVoltage

Units

PWM

Peripheral

Current

Units

Encoder

Peripheral

Mode

Scheduler

Field

Oriented

Control

Velocity

Control

C HDL

Page 10: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

10

How did models help us design C

components?

Page 11: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

11

Baseline three-phase motor control hardware setup

FPGA

with vendor

provided

peripherals

Simulink Real-TimeTM

scheduler and peripheral interface

Controller model for C

implementationWe will use this for

HDL implementation

Page 12: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

12

Rapid prototyping model with Speedgoat bitstream

Controller model for C

implementation

Field-oriented controller

is the fastest loop and

runs at 25 kHz

Page 13: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

13

Rapid prototyping model with Speedgoat bitstream

Interface block to

encoder peripheral

on FPGA

Interface block to

PWM peripheral on

FPGA

Controller model for C

implementation

Page 14: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

14

Rapid prototyping model with Speedgoat bitstream

This is the end

result, but it’s

not where we

started…

Page 15: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

15

Simulate design on the desktop

Controller model for C

implementation

We started

designing the

controller at our

desktops…

Mathematical models

of motor, load, and

peripherals

Page 16: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

16

Simulate design on the desktop

“Lumped parameter”

model of encoder sensor

and peripheral

Page 17: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

17

Compare desktop simulation and hardware data

Correlation of simulation and

hardware results provided

confidence in the workflow

Page 18: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

18

How do I create a new HDL

component model?

Page 19: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

19

Begin with existing C component models

Position

Velocity

Units

Open

Loop

Disabled

Encoder

CalibrationVoltage

Units

Current

Units

Mode

Scheduler

Field

Oriented

Control

Velocity

Control

Page 20: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

20

Create new HDL component models

Position

Velocity

Units

Open

Loop

Disabled

Encoder

CalibrationVoltage

Units

PWM

Peripheral

Current

Units

Encoder

Peripheral

Mode

Scheduler

Field

Oriented

Control

Velocity

Control

C HDL

Page 21: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

21

Example workflow to design an HDL component

1. Design the component with a unit-level testbench

2. Integrate the component with the system-level testbench

3. Partition the design for code generation

4. Generate a bitstream for the FPGA

5. Prototype the design on real-time hardware

Page 22: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

22

1. Design the component with a unit-level testbench

Peripheral will

run at

33 MHz on the

FPGA

Lo-fidelity model to capture

25 kHz dynamics of combined

sensor and peripheral

Page 23: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

23

1. Design the component with a unit-level testbench

Page 24: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

24

1. Design the component with a unit-level testbench

Page 25: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

25

1. Design the component with a unit-level testbench

Simulation results provide

confidence in lo-fidelity and

implementation fidelity

peripheral models

Page 26: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

26

2. Integrate the component with the system-level testbench

Implementation fidelity peripheral models require a

1/33 MHz simulation step size so simulation time will be

longer than the testbench with lo-fidelity peripherals

Page 27: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

27

2. Integrate the component with the system-level testbench

Use lo-fidelity peripheral

models for interactive

control design

Use hi-fidelity peripheral implementation

models to confirm integrated behavior

Page 28: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

28

3. Partition the design for code generation

Group C components Group HDL components

Optionally generate

algorithmic

HDL code

Page 29: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

29

4. Generate a bitstream for the FPGA

Use HDL Workflow Advisor

to specify additional

information required to

create a bitstream

Page 30: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

30

4. Generate a bitstream for the FPGA

Specify card and FPGA

toolchain

Page 31: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

31

4. Generate a bitstream for the FPGA

Associate ports in model

with pins on FPGA or PCI

interface to processor

HDL Workflow Advisor will

automate interacting with the

Xilinx toolchain to create the

bitstream for the FPGA

Page 32: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

32

4. Generate a bitstream for the FPGA

HDL Workflow Advisor also

generates an interface block

which can be used with

Simulink Real-Time

Page 33: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

33

4. Generate a bitstream for the FPGA

We use a MATLAB® script

to only expose ports of

interest and make a prettier

mask

Page 34: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

34

5. Prototype the design on real-time hardware

Integrate bitstream interface

block into Simulink Real-

Time model

Page 35: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

35

5. Prototype the design on real-time hardware

Correlation of simulation and

hardware results provides

confidence in the workflow

Page 36: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

36

How do I migrate a component model

from C to HDL?

Page 37: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

37

Migrate some component models from C to HDL

Position

Velocity

Units

Open

Loop

Disabled

Encoder

CalibrationVoltage

Units

PWM

Peripheral

Current

Units

Encoder

Peripheral

Mode

Scheduler

Field

Oriented

Control

Velocity

Control

C HDL

Page 38: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

38

Migrate some component models from C to HDL

Position

Velocity

Units

Open

Loop

Disabled

Encoder

CalibrationVoltage

Units

PWM

Peripheral

Current

Units

Encoder

Peripheral

Mode

Scheduler

Field

Oriented

Control

Velocity

Control

C HDL

Page 39: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

39

Example workflow to migrate a component to HDL

1. Design the component with a unit-level testbench

2. Integrate the component with the system-level testbench

3. Partition the design for code generation

4. Generate a bitstream for the FPGA

5. Prototype the design on real-time hardware

Modify component to

account for differences

between C and HDL

Model to simplify timing

constraints for disparate rates

Add timing constraint file to

FPGA project

Page 40: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

40

Modify to account for differences between C and HDL

States can be reset through

enable or trigger portsStates are explicitly reset

using signals

Page 41: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

41

Modify to account for differences between C and HDL

“Tunable” parameters are

explicitly routed to ports

Parameters are “tunable”

directly from blocks or

data objects

Page 42: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

42

Modify to account for differences between C and HDL

Typically floating point for rapid

prototyping

Typically fixed-point for rapid

prototyping

Page 43: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

43

Model to simplify timing constraints for disparate rates

Integrate 25 kHz control loop

with 33 MHz peripheral

components

Page 44: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

44

Model to simplify timing constraints for disparate rates

Insert slow delay at fast to

slow rate transitions, this

makes it simple to author

timing constraint file

Example constraint file

Page 45: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

45

Compare simulation and hardware results

Correlation of simulation and

hardware results provides

confidence in the workflow

Page 46: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

46

What did we learn?

Page 47: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

47

High resolution

voltage

modulation

Low latency

control loops

Critical

diagnostics

Position

sensing

System cost

reduction

Models can help you design FPGA components for

control applications

Page 48: Modeling HDL components for FPGAs in control applications · Modeling HDL components for FPGAs in control applications Mark Corless, Principal Application Engineer, Novi MI. 2 High

48

Through this exercise we learned that…

C and HDL components often run at disparate rates

– Design components with similar rates of interest

– Confirm system level behavior by integrating components

– Consider low fidelity peripheral models for control design tasks

There are some differences when modeling for HDL than C

– Explicitly reset states and route tunable parameters for HDL

– Typically model HDL algorithms in fixed-point

– Consider adding delays to simplify timing constraint specification

Overall Model-Based Design is similar for C and HDL

– Simulate components to reduce dependency on hardware

– Rapid prototype to verify behavior on hardware

– Generate algorithm code for integration into production environment