usable simulink embedded coder target for linux -...
TRANSCRIPT
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
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
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
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
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
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
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
Why people use it?
M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 4 / 22
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
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
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
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
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
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
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
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
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
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
Is there better code generationtarget?
Yes
ERT_LINUX
M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 9 / 22
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
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
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
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
Applications/demos
M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 12 / 22
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
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
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
M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 16 / 22
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
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
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
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
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
Demo: Raspberry Pi & CAN
M. Sojka, P. Píša Usable Simulink Embedded Coder Target for Linux RTLWS16 21 / 22
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
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