usable simulink embedded coder target for linux -...

36
Usable Simulink Embedded Coder Target for Linux Michal Sojka, Pavel Píša Czech Technical University in Prague Faculty of Electrical Engineering Department of Control Engineering 16 th Real-Time Linux Workshop October 12, 2014 Düsseldorf, Germany M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 1 / 22

Upload: trantu

Post on 07-Feb-2018

290 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Usable Simulink Embedded Coder Target for Linux

Michal Sojka, Pavel Píša

Czech Technical University in PragueFaculty of Electrical Engineering

Department of Control Engineering

16th Real-Time Linux WorkshopOctober 12, 2014

Düsseldorf, Germany

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 1 / 22

Page 2: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Motivation

I Not everybody is a programmerI Control engineers can design

control algorithms but cannotprogram them in C

I They use tools like Simulink to simulate their algorithmsI They still want to run their algorithms on real hardware, not

just simulationsI Solution: Matlab/Simulink + automatic code generationI Control applications = real-time applications

I while (1) { read input; calculate control; write output }

I The generated code needs to run on RTOS, e.g. GNU/Linux

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 2 / 22

Page 3: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Motivation

I Not everybody is a programmerI Control engineers can design

control algorithms but cannotprogram them in C

I They use tools like Simulink to simulate their algorithmsI They still want to run their algorithms on real hardware, not

just simulations

I Solution: Matlab/Simulink + automatic code generationI Control applications = real-time applications

I while (1) { read input; calculate control; write output }

I The generated code needs to run on RTOS, e.g. GNU/Linux

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 2 / 22

Page 4: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Motivation

I Not everybody is a programmerI Control engineers can design

control algorithms but cannotprogram them in C

I They use tools like Simulink to simulate their algorithmsI They still want to run their algorithms on real hardware, not

just simulationsI Solution: Matlab/Simulink + automatic code generation

I Control applications = real-time applicationsI while (1) { read input; calculate control; write output }

I The generated code needs to run on RTOS, e.g. GNU/Linux

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 2 / 22

Page 5: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Motivation

I Not everybody is a programmerI Control engineers can design

control algorithms but cannotprogram them in C

I They use tools like Simulink to simulate their algorithmsI They still want to run their algorithms on real hardware, not

just simulationsI Solution: Matlab/Simulink + automatic code generationI Control applications = real-time applications

I while (1) { read input; calculate control; write output }

I The generated code needs to run on RTOS, e.g. GNU/Linux

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 2 / 22

Page 6: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Motivation

I Not everybody is a programmerI Control engineers can design

control algorithms but cannotprogram them in C

I They use tools like Simulink to simulate their algorithmsI They still want to run their algorithms on real hardware, not

just simulationsI Solution: Matlab/Simulink + automatic code generationI Control applications = real-time applications

I while (1) { read input; calculate control; write output }

I The generated code needs to run on RTOS, e.g. GNU/Linux

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 2 / 22

Page 7: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

What is Simulink?

I Widely used tool for design and simulation of dynamic systemsI Commercial, non-free program – a part of MatlabI Developed by MathWorksI In its core is a solver of differential equationsI Algorithm is drawn as a data-flow graph (= Simulink model)I Simulink can simulate itI Simulink can generate C code of the model

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 3 / 22

Page 8: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Why people use it?

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 4 / 22

Page 9: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Code generation in Simulink

I Example:Sine Wave

Product

3

Constant

Pin 1

ErrFlag

Analog Input

Pin 1 ErrFlag

Analog Output

double in1, out1;in1 = adc_read(1);out1 = sin(t) + 3 * in1;dac_write(1, out1);

I Simulink Coder & Embedded CoderI Code generation target

I Template for “main.c”I Template for MakefileI I/O blocks

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 5 / 22

Page 10: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Code generation in Simulink

I Example:Sine Wave

Product

3

Constant

Pin 1

ErrFlag

Analog Input

Pin 1 ErrFlag

Analog Output

double in1, out1;in1 = adc_read(1);out1 = sin(t) + 3 * in1;dac_write(1, out1);

I Simulink Coder & Embedded CoderI Code generation target

I Template for “main.c”I Template for MakefileI I/O blocks

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 5 / 22

Page 11: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Can Simulink generate code that runs on Linux?

Yes, but. . .

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 6 / 22

Page 12: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Can Simulink generate code that runs on Linux?

Yes, but. . .

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 6 / 22

Page 13: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Problems of Simulink code generation targets for Linux

I Very good support for popular embedded boards(Raspberry Pi, BeagleBone, . . . )

I Can only be installed on Windows hosts /I Other Linux targets are fortunately supported via “IDE Link”

I Uses Eclipse IDE to compile and run the code /I Slow! /

I Generated code uses POSIX timersI POSIX timers do not guarantee real-time properties (latencies)

even under preempt_rt /

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 7 / 22

Page 14: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Problems of Simulink code generation targets for Linux

I Very good support for popular embedded boards(Raspberry Pi, BeagleBone, . . . )

I Can only be installed on Windows hosts /

I Other Linux targets are fortunately supported via “IDE Link”I Uses Eclipse IDE to compile and run the code /I Slow! /

I Generated code uses POSIX timersI POSIX timers do not guarantee real-time properties (latencies)

even under preempt_rt /

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 7 / 22

Page 15: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Problems of Simulink code generation targets for Linux

I Very good support for popular embedded boards(Raspberry Pi, BeagleBone, . . . )

I Can only be installed on Windows hosts /I Other Linux targets are fortunately supported via “IDE Link”

I Uses Eclipse IDE to compile and run the code /I Slow! /

I Generated code uses POSIX timersI POSIX timers do not guarantee real-time properties (latencies)

even under preempt_rt /

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 7 / 22

Page 16: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Problems of Simulink code generation targets for Linux

I Very good support for popular embedded boards(Raspberry Pi, BeagleBone, . . . )

I Can only be installed on Windows hosts /I Other Linux targets are fortunately supported via “IDE Link”

I Uses Eclipse IDE to compile and run the code /I Slow! /

I Generated code uses POSIX timersI POSIX timers do not guarantee real-time properties (latencies)

even under preempt_rt /

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 7 / 22

Page 17: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Problems of Simulink code generation targets for Linux

I Very good support for popular embedded boards(Raspberry Pi, BeagleBone, . . . )

I Can only be installed on Windows hosts /I Other Linux targets are fortunately supported via “IDE Link”

I Uses Eclipse IDE to compile and run the code /I Slow! /

I Generated code uses POSIX timersI POSIX timers do not guarantee real-time properties (latencies)

even under preempt_rt /

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 7 / 22

Page 18: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

POSIX timers under preempt_rt Linux

I POSIX timers use signals to notify user space about theirexpiration

I Signal delivery path uses sleeping locks under preempt_rtI Deferred to softirqI Softirqs are:

I executed in the context of arbitrary task or ksoftirqdI executed with random (e.g. non-real-time) priority

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 8 / 22

Page 19: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Is there better code generationtarget?

Yes

ERT_LINUX

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 9 / 22

Page 20: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

ERT_LINUX code generation target

I Goal: Minimalist target which reuses as much Simulink codeas possible (not that easy)

I No Eclipse needed ,I Uses clock_nanosleep() for timing ,I External modeI Works both natively and with cross-compiling

http://lintarget.sf.net

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 10 / 22

Page 21: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

ERT_LINUX I/O support

I MF624: PCI data acquisition cardI Digital IO, Analog IO, PWM outI Counters (can be used for PWM input!)I Incremental encoder inputI Based on UIO driver ⇒ no syscall overhead

I CAN bus transmit/receiveI Some support for Raspberry PiI No Comedi yet

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 11 / 22

Page 22: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

ERT_LINUX I/O support

I MF624: PCI data acquisition cardI Digital IO, Analog IO, PWM outI Counters (can be used for PWM input!)I Incremental encoder inputI Based on UIO driver ⇒ no syscall overhead

I CAN bus transmit/receiveI Some support for Raspberry Pi

I No Comedi yet

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 11 / 22

Page 23: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

ERT_LINUX I/O support

I MF624: PCI data acquisition cardI Digital IO, Analog IO, PWM outI Counters (can be used for PWM input!)I Incremental encoder inputI Based on UIO driver ⇒ no syscall overhead

I CAN bus transmit/receiveI Some support for Raspberry PiI No Comedi yet

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 11 / 22

Page 24: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Applications/demos

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 12 / 22

Page 25: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

PMSM motor simulationHardware-in-the-loop (HIL) simulation

Infineon TriBoard running a motor control algorithmPC

Motor & fault simulation

MF

624

I/O

car

d

PCLinux, CANCAN attacks

Linux, preempt_rt

PCInfoneon DeviceAccess Server (DAS)

CAN bus

Inte

rfac

e bo

ard

USB(serial line,JTAG, ...)

I Analysis of how does security influences safetyI PMSM motor simulation @ 20 kHzI Hand-written motor control algorithm runs on real hardwareI Message authentication on CANbus + brute force attacks

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 13 / 22

Page 26: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Robot with parallel kinematic structure

I 4 DC motors, 4 incremental encoders, other I/OsI Presented at Embedded world 2014I Sampling period 1ms but complex computationsI More reliable that previously used Windows target

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 14 / 22

Page 27: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Motor control on Raspberry Pi

I Final task for students of our real-time course:I Create software motor controller on an embedded PowerPC

board with VxWorks

I Can the same be implemented on Raspberry Pi with Linux?

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 15 / 22

Page 28: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 16 / 22

Page 29: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Hardware

PWM

GPIOsRasp

berr

y P

i –

P1

CHB

CHA

IRC

HI DRV

LO DRV

IN

HI DRV

LO DRV

IN

DCMOTOR

Motor Power Supply

DIR

I As simple as possibleI Four NOR gates (SN74HCT02)I H-bridge (L6203)

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 17 / 22

Page 30: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Incremental encoder (IRC) processing

I The most demanding partI IRC signal: 0 – 20 kHzI Every edge generates an interrupt (up to 80 k interrupts/sec)I Kernel driver to calculate the position

I Sustainable IRQ frequency up to ≈ 32 kHzI Further improvement: Use of ARM FIQ

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 18 / 22

Page 31: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Incremental encoder (IRC) processing

I The most demanding partI IRC signal: 0 – 20 kHzI Every edge generates an interrupt (up to 80 k interrupts/sec)I Kernel driver to calculate the positionI Sustainable IRQ frequency up to ≈ 32 kHzI Further improvement: Use of ARM FIQ

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 18 / 22

Page 32: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

IRC processing details

GPIOs

Rasp

berr

y P

i

CHB

CHA

IRC

Hard IRQ

IRQ threadedhandler

I Position calculation works better if derived from the order ofIRQs than from the signal values read in the handler.

I FIFO run queue preserves order!

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 19 / 22

Page 33: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Demo: Raspberry Pi motor controller

sfIRCInput

IRC0

int32 0

IRC-display

Convert

IRC int32 to Real

double

IRC-scope

-0.01

ManualPWM

double

sfPWMwDirOutput

PWMwDirManualPWM

Control

double Opt. PSD Controller

w RSTsrred I

u

I P

Subsys PSD

double

0

PositionRequest

double

0

RESET

double

ActiveOutputRange

double-K-

AntiWindup

double

Position

double

Convert

To int

uint8

0.05976

PWM-display

Pos

Trajectory

double

PWM

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 20 / 22

Page 34: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Demo: Raspberry Pi & CAN

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 21 / 22

Page 35: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Conclusion

I ERT_LINUX target worksI Used in several applicationsI Does not suffer from serious problems as original MathWorks

code generation targets.I Limited support for I/Os – Blocks for Comedi are on our todo

listI It might be possible to use our target with other I/O blocks

(e.g. from MathWorks)

Thank you!

M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 22 / 22

Page 36: Usable Simulink Embedded Coder Target for Linux - cvut.czrtime.felk.cvut.cz/publications/public/ert_linux-rtlws2014.pdf · Usable Simulink Embedded Coder Target for Linux MichalSojka,PavelPíša

Conclusion

I ERT_LINUX target worksI Used in several applicationsI Does not suffer from serious problems as original MathWorks

code generation targets.I Limited support for I/Os – Blocks for Comedi are on our todo

listI It might be possible to use our target with other I/O blocks

(e.g. from MathWorks)

Thank you!M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 22 / 22