37
RealTime systems for measurement and control Robert Grepl Institute of Solid Mechanics, Mechatronics and Biomechanics Faculty of Mechanical Engineering, Brno University of Technology 2010-2015

RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

  • Upload
    others

  • View
    10

  • Download
    0

Embed Size (px)

Citation preview

Page 1: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

RealTime systems for measurement and control

Robert GreplInstitute of Solid Mechanics, Mechatronics and Biomechanics

Faculty of Mechanical Engineering, Brno University of Technology

2010-2015

Page 2: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Agenda

Applications in MechLab

What is Real Time System?

Multitasking in Simulink

Applications: RCP and HIL

HW platforms

SW tools

Fixed Point computations

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

2

Page 3: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

What is real time system?

Def #1:A real-time system is one that must process information and produce a response within a specified time, else risk severe consequences, including failure. That is, in a system with a real-time constraint it is no good to have the correct action or the correct answer after a certain deadline: it is either by the deadline or it is useless!

Def #2:Any information processing activity or system which has to respond to externally generated input stimuli within a finite and specified period.[Schlindwein]

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

3

Page 4: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

What is real time system?

specification of the result logical – c = sin(a+b)

time – we need to know this result in 0,1 sec. = deadline

soft & hard RT Hard – Missing a deadline is a total system failure.

Firm - Infrequent deadline misses are tolerable, but may degrade the system's quality of service. The usefulness of a result is zero after its deadline.

Soft - The usefulness of a result degrades after its deadline, thereby degrading the system's quality of service. [wikipedia]

Examples:

ABS, aircraft control, ticket reservation system at airport, over-temperature monitor in nuclear power station, mobile phone, oven temp. controller, ECG/arrhythmia monitor.

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

4

Page 5: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

What is real time system?

Typical sequence of RT system: get input (measure)

compute

set output.

Complex system: several tasks

with different priorities.

How to simulate them? Use multi rate framework.

[Goddard]

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

5

Page 6: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Multi-rate systems in Simulink

Consider position control of some actuator.

Control loop runs at 1kHz (hard RT).

Required position is send via serial line (10Hz – soft RT).

-> we have two rates in the system.

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

6

Page 7: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Preemptive multitasking

multi-taskingexecution

single-taskingexecution

[SimulinkCoder]

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

7

Page 8: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Application of RT systems

A typical mechatronic system picks up signals from the environment, processes them to generate output signals, transforming them for example into forces, motions and actions.“ [Schweitzer]

Embedded systems = a computer system with a dedicated function within a larger mechanical or electrical system, often with real-time computing constraints. [wikipedia]

Design and test of embedded system: RCP = Rapid Control Prototyping

HIL - Hardware In the Loop

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

8

Page 9: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

mechatronics laboratory :: ismmb :: fme :: brno university of technology

Rapid Control Prototyping & Hardware In the Loop

Page 10: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

www.mechlab.czFaculty of Mechanical Engineering, Brno University of Technology

10

Page 11: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Rapid Control Prototyping & Hardware In the Loop

For the realization of RCP and HIL we need:

HW with appropriate computational power

I/Os

SW

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

11

Page 12: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

General structure of RT system

A) Host PC

B) Target RT CPU

C) HW for fast I/Os

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

12

Host PC

CPU DSP/FPGA

Page 13: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Overview of HW for fast I/Os

DSP – digital signal processor+ programming in C (assembler,...)

- speed

ASIC - application-specific integrated circuit+/- good price for high volumes

CPLD - complex programmable logic device+ Non-volatile design

+ faster than FPGA

- lower power/simpler algorithms compare to FPGA

FPGA - field-programmable gate array

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

13

Page 14: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

FPGA = field-programmable gate array

FPGA is an integrated circuit designed to be configured by a customer or a designer after manufacturing—hence "field-programmable".

Configuration is specified using a hardware description language (HDL).

Large resources of logic gates and RAM blocks to implement complex digital computations.

[wikipedia]

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

14

Page 15: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Examples of HW platforms: dsPIC/PIC32 microcontroller

C code is automatically generatedfrom Simulink

tools: Simulink Coder

Embedded Coder Kerhuel blockset- support of the subset of the Microchip 16,32-bit devices[http://www.kerhuel.eu/]

MPLAB compiler and bootloader.

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

15

Page 16: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Examples of HW platforms: NI cRIO

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

16

Page 17: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

mechatronics laboratory :: ismmb :: fme :: brno university of technology

Examples of HW platforms: dSPACE Single-board

DS1104 PPC Controller Board

Page 18: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

mechatronics laboratory :: ismmb :: fme :: brno university of technology

Examples of HW platforms: dSPACE Single-board

DS1103 PPC Controller Board

Page 19: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

mechatronics laboratory :: ismmb :: fme :: brno university of technology

Examples of HW platforms: MF 624

I/O 8x 14bit A/D (+-10V, 1.6(1 ch.)-3.7(8ch.)us

conversion time)

8x 14bit D/A (+-10V, 10mA max., 31us settling time)

8x Dout, 8x Din (TTL compatible)

4x counter/timer (32-bit, použití PWM)

4x encoder

voltage supply on board current limit:

o +5V (100mA / 250mA max.)

o +12V (nejištěno / 150mA max.)

connectors Canon (2 x DB-37 F)

Page 20: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

mechatronics laboratory :: ismmb :: fme :: brno university of technology

Examples of HW platforms: NXT Mindstorms

Code Generation from Simulink(no licence for Simulink Coder and Embedded Coder required)

Data logging and parametersettings via Bluetooth and External mode

Page 21: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

www.mechlab.czFaculty of Mechanical Engineering, Brno University of Technology

Examples of HW platforms: NXT Mindstorms

32-bit Atmel AT91SAM7S256 main microcontroller (256 KB flash memory, 64 KB RAM)

8-bit Atmel ATmega48 microcontroller @ 4 MHz (4 KB flash memory, 512 Bytes RAM)

100x64 pixel LCD Screen

four input ports (ports 1-4), three output ports (ports A-C)

USB Port, Bluetooth Class II V2.0

Loudspeaker - 8 kHz sound quality, 8-bit resolution, 2-16 kHz sample rate

Four Push ButtonsOrange button: On/Enter Light grey arrows: moving left and right in the NXT menu Dark

grey button: Clear/Go back

Powered by six AA batteries or the NXT Rechargeable DC Battery21

Page 22: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

RT software tools in Simulink

Concept #1 = just slow down the Simulink to real-time RT Toolbox

Concept #2 = generate C code for specific target (processor) RT Windows Target

xPC target

Kerhuel blockset

dSPACE RTI

...

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

22

Page 23: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

mechatronics laboratory :: ismmb :: fme :: brno university of technology

SW tools: Real-Time Toolbox

RT scheduler in Kernel mode of Windows

just slow down the normal execution of Simulink (User mode)

without Code Generation

thus: all features of Simulink available– variable step solver

– Fcn block

– Interpret Matlab Fcn

support for > 300 I/O boards

rate approx. 1kHz (with MF624)

soft RT - „missed ticks“ e.g. if you “move with mouse over Scope”

Page 24: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW tools: Code Generation from Simulink

Programming of target XY:– algorithm

– I/O drivers

Simulink Coder – generates C code from the model

Embedded Coder – generates “nice” code (readable, compact, and fast code for use on embedded processors, on-target rapid prototyping boards, and microprocessors used in mass production) [help Matlab]

Target specific package (drivers)

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

24

Page 25: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW tools - Targets: RT Windows Target

HOST = PC

TARGET = the same PC, but Kernel mode

Scheduler and also simulation runs in Kernel mode.

Thus generation of C and compilation.

Fixed step solver only.

Fcn, Matlab Fcn are not supported.

Rate approx. 10kHz (with MF624)

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

25

Page 26: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

mechatronics laboratory :: ismmb :: fme :: brno university of technology

SW tools - Targets: RT Windows Target

rtwintgt –setup

set rtwin.tlc as system target file

Tools/External Mode Control Panel

Page 27: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW tools - Targets: xPC

HOST = PC

TARGET = different PC with lightweight RTOSand with some I/O board

Code generated and compiled on HOST.

Upload using LAN.

Rate up to 50kHz (with MF624)

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

27

Page 28: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW tools - Targets: others

dSPACE

microcontrollers ...

LEGO NXT Mindstorm

BeagleBoard

Raspberry Pi

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

28

Page 29: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW Tools: Fixed Point

TASK:

The temperature in the room is in interval (–20; 50) deg. C

How we can represents this temperature with the maximal precision

and with the 8 bits ?

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

29

Page 30: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW Tools: Fixed Point

Floating Point vs. Fixed Point

Example: consider Unsigned 8bit number. Which number can this represents? 0 – 255 (binary point = 0)

0 – 15.9375 (binary p. = 4)

0 - 0.99609375 (b.p. = 8)

If floating arithmetic is used, the binary point can vary.

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

30

Page 31: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW Tools: Fixed Point

Solution #1 fixdt(1,8,1)

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

31

Page 32: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW Tools: Fixed Point

Solution #2 Slope and bias

fixdt(1,8,0.27450980392156865,15.137254901960787)

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

32

Page 33: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW Tools: Fixed Point

Fixed Point Toolboxa = fi(21, 1,8,3)

a = fi(number, signed,length,binaryPoint)

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

33

Page 34: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

SW Tools: Fixed Point

FPU = floating point unit is a part of a computer system specially designed to carry out operations on floating point numbers. Typical operations are addition, subtraction, multiplication, division, and square root. In most modern general purpose computer architectures, one or more FPUs are integrated with the CPU; however many embedded processors do not have hardware support for floating-point operations.

Benefits of Using Fixed-Point Hardware Size and Power Consumption

Memory Usage and Speed

Cost

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

34

Page 35: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

Conclusions

Model Based Design of Mechatronic product includes:o Plant modelling

o Controller design

o Simulating the plant and controller (offline)

o Real-Time simulations

o Deployment For RCP and HIL we need

– HW

– SW

– knowledge how to use them

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

35

Page 36: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

www.mechlab.czISMMB, Faculty of Mechanical Engineering, Brno University of Technology

36

References

[Schlindwein] http://www.le.ac.uk/eg/fss1/real%20time.htm

[wikipedia] http://en.wikipedia.org/wiki/Real-time_computinghttp://en.wikipedia.org/wiki/Embedded_system

http://en.wikipedia.org/wiki/Field-programmable_gate_array

[SimulinkCoder] help of Simulink Coder

[Pelánek] Radek Pelánek: Real Time Systemshttp://www.fi.muni.cz/~xpelanek/IA158/

[Goddard] Steve Goddard: Real-Time Systemshttp://www.cse.unl.edu/~goddard/Courses/RealTimeSystems/Lectures/Lectures.html

[Schweitzer] http://www.mcgs.ch/mechatronics_definition.html

[NI RIO] NI Single-Board RIO Embedded Control and Acquisitionhttp://www.ni.com/white-paper/7441/en

Page 37: RealTime systems for measurement and control...Examples of HW platforms: dsPIC/PIC32 microcontroller C code is automatically generated from Simulink tools: Simulink Coder Embedded

www.mechlab.czInstitute of Solid Mechanics, Mechatronics and Biomechanics

Faculty of Mechanical EngineeringBrno University of Technology

Contakt: assoc. prof. Robert Grepl, [email protected], tel.: +420 5114 288