CAE - RT - Linux - PI - HIL - Practical Experiences With Python and Linux RT at The SUPSI Laboratory
CAE - RT - Linux - PI - HIL - Practical Experiences With Python and Linux RT at The SUPSI Laboratory
net/publication/335781799
CITATIONS READS
4 89
1 author:
Roberto Bucher
University of Applied Sciences and Arts of Southern Switzerland
22 PUBLICATIONS 232 CITATIONS
SEE PROFILE
All content following this page was uploaded by Roberto Bucher on 04 May 2022.
• The methods learned with FOSS can be easily ported mechanical plant using the Lagrange’s or the Kane’s
to commercial softwares. method.
This paper present a new FOSS alternative based on the For example, the following code implements the model-
programming language Python: this language is already ing of the classical inverted pendulum using the Kane’s
widely used in many universities and consequently can be method.
better accepted as alternative to commercial software.
# Car
Python offers almost all the functions required to imple- Nf = R e f e r e n c e F r a m e ( ’ Nf ’ ) ; C = P o i n t ( ’C ’ )
ment control system algorithms, as shown in numerous C . s e t v e l ( Nf , v∗ Nf . x ) ; Car = P a r t i c l e ( ’ Car ’ ,C,M)
applications in our mechatronics laboratory at SUPSI.
# Pendulum
In Section 2 we introduce Python as control system design A = Nf . o r i e n t n e w ( ’A ’ , ’ A x i s ’ , [ th , Nf . z ] )
tool. In section 3 and 4 we present the open source A . s e t a n g v e l ( Nf , w∗ Nf . z )
P = C . l o c a t e n e w ( ’P ’ , r ∗A . x )
application pysimCoder, used at SUPSI to design control P . v 2 p t t h e o r y (C, Nf , A)
block diagrams for hybrid simulation and code generation. Pa = P a r t i c l e ( ’ Pa ’ , P , m)
I = o u t e r ( Nf . z , Nf . z ) ; I n e r t i a t u p l e = ( I c ∗ I , P)
Finally we present a laboratory example, in order to Bp = RigidBody ( ’ Bp ’ ,P , A,m, I n e r t i a t u p l e )
demonstrate the full design chain.
# Forces and t o r q u e s
f o r c e s = [ ( C, F∗ Nf . x−d∗v∗ Nf . x ) , ( P,−m∗ g ∗ Nf . y ) ]
2. PYTHON FOR CONTROL f r a m e s = [ Nf ,A ] ; p o i n t s = [ C, P ]
Since 2017 Python has become one of the most used # Model
programming languages in industry (see IEEE (2018)). KM = KanesMethod ( Nf , q i n d = [ x , t h ] , u i n d = [ v , w ] ,
kd eqs = k i n d i f f s )
The Python Control Systems Library (Murray (2009)), is f r , f r s t a r = KM. k a n e s e q u a t i o n s ( p a r t i c l e s , f o r c e s )
a package developed since 2009 by Richard Murray at
Caltech. This toolbox contains a set of Python classes The modeling is completely performed in symbolic form,
and functions that implement common operations for the and the two methods deliver the modeled system in state
analysis and design of feedback control systems. space form. At the end of this first phase, it is possible to
The toolbox is still in development but at the moment it linearize the system at a given equilibrium point and to
is already possible to: find the numerical matrices A, B, C and D of the state
space representation.
• implement systems in state space form and as transfer
function, in both continuous and discrete time More details about the Kane’s method are available
• work with SISO and MIMO systems in Kane and Levinson (1985).
• design controllers using graphical methods like Bode,
Nyquist, root locus etc. 2.4 Design controllers with Python
• develop state feedback controllers using poles place-
ment, LQR etc.
The toolbox provides methods to design controllers in
• simulate dynamic systems (controlled or not) with
different ways. In particular it is possible to find state feed-
step response, initial value analysis, etc.
back controllers in continuous and discrete time domain.
On the other hand, with this package it is not possible to The poles placement function implements the Tits and
simulate hybrid systems. This lack has been covered by Yang algorithm (Tits and Yang (1996)), a robust method
the pysimCoder toolbox (Bucher (2018)), which will be that is able to find well conditioned solutions by MISO
presented later in this paper. and MIMO cases.
1.0 1.0
0.8 0.8
for the analysis of dynamic systems. The following oper- 0.4 0.4
ations are available among others: step response, initial 0.2 0.2
0
0.6
10
0.4
Figure 1 shows some diagrams implemented in the toolbox. 20
30 0.2
40
-1 0 1
10 10 10 0.0
− 0.2
− 0.4
− 0.6
− 0.8
− 1.0 − 0.5 0.0 0.5 1.0
139
2019 IFAC ACE
June 1-3, 2016. Bratislava, Slovakia Roberto Bucher / IFAC PapersOnLine 52-9 (2019) 133–138 135
c l p 1 = [ 1 , 2 ∗ x i 1 ∗wn , wn ∗ ∗ 2 ]
c l p 2 = [ 1 , 2 ∗ x i 2 ∗wn , wn ∗ ∗ 2 ]
c l p 3 = [ 1 , wn ]
c l p o l y 1 = sp . p o l y m u l ( c l p 1 , c l p 2 )
c l p o l y = sp . p o l y m u l ( c l p o l y 1 , c l p 3 )
# De sir e d c o n t i n u o u s c l o s e d l o o p p o l e s
c l p o l e s = sp . r o o t s ( c l p o l y )
Fig. 3. Simulation of a hybrid system - Block diagram
# De sir e d d i s c r e t e c l o s e d l o o p p o l e s
c l p o l e s d = sp . exp ( c l p o l e s ∗ t s )
k = place ( Phi f , G f , c l p o le s d )
140
2019 IFAC ACE
136 1-3, 2016. Bratislava, Slovakia
June Roberto Bucher / IFAC PapersOnLine 52-9 (2019) 133–138
put nodes, number of continous and discrete states, etc. 4.3 Translating the block diagram into C-code
The netlist contains objects of this class.
During the RT execution, the operations described in 1
For the step input block, this function is
and 2 are executed in reverse order. First, the output
values of the blocks (y = g(x, u, t) or yk = g(xk , uk , k))
def s t e p B l k ( pout , i n i t T i m e , Val ) : are computed and then the states are updated.
[...]
b l k = RCPblk ( ’ s t e p ’ , [ ] , pout , [ 0 , 0 ] , 0 , Consequently, the first step in code generation consists in
[ i n i t T i m e , Val ] , [ ] )
finding the right sequence of block executions. There are
return b l k
blocks where the outputs are not directly depending on
the inputs (for example an integration block) and other
where pout is a matrix with the output node of the blocks where the outputs can be calculated only when the
block. In addition there are information about the number inputs are known (for example a gain block).
of continuous and discrete states (matrix [0, 0]), a value
indicating if the output depend on the input (here 0: no An algorithm in pysimCoder transforms the generated
feed forward) and the block parameters. netlist into another ordered list, where the blocks are
inserted in the right order of execution.. After this step,
The diagram of Figure 3 will be translated into this python the new netlist is translated into C-code, where the RT
code, where all the xxxBlk functions return an object of task can run in a high priority thread to get low latency.
the RCPblk class.
4.4 The main file under Linux RT
CONTROLLER FBK = d s s B l k ( [ 3 ] , [ 1 ] , gss out , 0)
Plot = plotBlk ( [ 6 , 2 ] , ’ Plot ’ ) A main program handles all the phases of the RT exe-
PLANT = c s s B l k ( [ 3 ] , [ 2 ] , sysc , 0)
Saturation = saturBlk ( [ 4 ] , [ 3 ] , 3000 , −3000)
cution. First of all it instantiates a high priority thread,
SUM = sumBlk ( [ 5 , 1 ] , [ 4 ] , [1 ,1]) scheduled in real-time FIFO. This thread first calls the
CONTROLLER IN = d s s B l k ( [ 6 , 2 ] , [ 5 ] , gss in , 0) generated function NAME(MODEL) init(), where all the
REF = s t e p B l k ( [ 6 ] , 1, 1) block methods are called with the INIT flag.
b l k s = [CONTROLLER FBK, P l o t ,PLANT, S a t u r a t i o n , NAME(MODEL)... is a macro and the right names of the
SUM, CONTROLLER IN, REF , ]
functions are inserted only during the compilation.
In the previous example, the block “REF” is a step signal After the initialization phase, the RT thread enters in a
with stepT ime = 1.0, F inalvalue = 1.0 and the output is loop to perform the following tasks:
connected to the node 6 of the diagram. The corresponding • Wait for the timer interrupt
Python RCPclass object contains the following fields: • Call the generated isr function (NAME(MODEL) isr).
Here, all the block methods are first called with the
I n [ 5 ] p r i n t (REF) OUT flag and, in case of blocks having internal states,
Function : step # l ib r a r y function the related methods are called with the OUT and
Input p o r t s : [] STUPD flag.
Output p o r t s : [6]
Nr . o f s t a t e s : [0 0] # [ c o nt . , d i s c . ] • Stop until the next time interrupt
R e l a t i o n u−>y : 0 # feedforward
Real parameters : [1.0 1.0]
The RT-Preempt patch (RT-Preempt (2016)) gives the
I n t e ge r parameters : [] real-time features to the Linux OS, in particular low
S t r i n g Parameter : latency and determinism.
The call of the function clock nanosleep is responsible to
The implementation function contains the C-code related correctly handle the sampling time of the RT task.
to the behaviour of a block. The system theory explains
that all the block functions can be written in state-space
v o i d ∗ r t t a s k ( v o i d ∗p )
form as (1) for continuous-time systems or (2) for discrete- {
time systems param . s c h e d p r i o r i t y = p r i o ;
i f ( s c h e d s e t s c h e d u l e r ( 0 , SCHED FIFO , ¶m)==−1)
ẋ = f (x, u, t) exit (1);
(1)
y = g(x, u, t) ...
d o u b l e Tsamp = NAME(MODEL, g e t t s a m p ) ( ) ;
xk+1 = f (xk , uk , k) ...
(2) NAME(MODEL, i n i t ) ( ) ;
yk = g(xk , uk , k)
while ( ! end ) {
/∗ w a i t u n t i l l next s h o t ∗/
where function f represents the dynamic part and g
c l o c k n a n o s l e e p (CLOCK MONOTONIC,
represents the static part of the system. TIMER ABSTIME , &t , NULL ) ;
...
For each block these functions have been implemented /∗ p e r i o d i c t a s k ∗/
in C into a library, and can be accessed through the 4 NAME(MODEL, i s r ) (T ) ;
flags INIT (initialization), OUT (output → g), STUPD ...
(states update → f ) and END (termination function). The }
NAME(MODEL, e n d ) ( ) ;
automatically generated code simply calls these functions }
in the right sequence.
141
2019 IFAC ACE
June 1-3, 2016. Bratislava, Slovakia Roberto Bucher / IFAC PapersOnLine 52-9 (2019) 133–138 137
#B a l l Center o f mass
CM2 = O. l o c a t e n e w ( ’CM2 ’ , ( R w+R b ) ∗ N b . z )
CM2. v 2 p t t h e o r y (O, N, N b )
# Inertia
I y = o u t e r (N . y , N . y ) ;
In1T = ( J w ∗ Iy , O) # Wheel
Fig. 5. Simple block diagram In2T = ( J b ∗ Iy , CM2) # B a l l
B w = RigidBody ( ’ B w ’ , O, N w , M w , In1T )
B r = RigidBody ( ’ B r ’ , CM2, N r o l l , M b , In2T )
5. A LABORATORY EXAMPLE
f o r c e s = [ ( CM2,−M b∗ g ∗N . z ) , ( N w , T∗N . y ) ]
k i n d i f f s = [ p h i w . d i f f ( t )−w w , p h i b . d i f f ( t )−w b ]
5.1 The Ball on Wheel plant
KM = KanesMethod (N, q i n d =[ p h i b , p h i w ] ,
u i n d =[w b , w w ] , k d e q s= k i n d i f f s )
The balancing of a ball on a bike wheel (see Figure 6) f r , f r s t a r = KM. k a n e s e q u a t i o n s ( [ B r , B w ] , f o r c e s )
is a challenging control system problem. The system has
been described in many papers (for example Hauser et al.
(1992), Ho and Lin (2006), Matsuda and Ohse (2006) and From the matrices f r and f rstar it is possible to linearize
Fernandes and van Niekerk (2013)) with different control the system and find the A and B matrices of the state
approaches. space form at the equilibrium point.
ϕb The ball and wheel inertia have been identified in an
experimental form.
ϕroll
5.3 Controller
Q = np . d i a g ( [ 1 0 , 1 , 2 0 , 1 ] ) ;
ϕw R = [4];
K S , E = d l q r (Ad , Bd , Q, R)
The plant has been modeled using the Kane’s method with 5.5 The real system
two degrees of freedom.
Figure 9 shows the block diagram realized with pysim-
The rolling condition (3) eliminates ϕroll from the model. Coder in order to generate the application for the Linux
Rw · ϕb = Rw · ϕw + Rb · ϕroll (3) RT platform.
142
2019 IFAC ACE
138 1-3, 2016. Bratislava, Slovakia
June Roberto Bucher / IFAC PapersOnLine 52-9 (2019) 133–138
REFERENCES
Bucher, R. (2018). URL
https://github.com/robertobucher/pysimCoder.
Bucher, R. and Balemi, S. (2006). Rapid controller
prototyping with matlab/simulink and linux. Con-
trol Engineering Practice, 14(2), 185 – 192. doi:
https://doi.org/10.1016/j.conengprac.2004.09.009. Spe-
cial Section on Advances in Control Education.
Fernandes, J.M. and van Niekerk, T. (2013). Im-
plementation of an industrial controller for a ball-
on-wheel system. In 2013 6th Robotics and
Fig. 7. Ball on wheel - Diagram for simulation Mechatronics Conference (RobMech), 129–134. doi:
10.1109/RoboMech.2013.6685504.
Hauser, J., Sastry, S., and Kokotovic, P. (1992). Nonlinear
control via approximate input-output linearization: The
ball and beam example. In IEEE Transactions on
Automatic Control, 37, 392–398.
Ho, M. and Lin, H. (2006). Balance control of ball and
wheel systems via feedback linearization. In Proceedings
of the 45th IEEE Conference on Decision and Control,
3926–3931. doi:10.1109/CDC.2006.376708.
IEEE (2018). The 2018 Top Pro-
gramming Languages. URL
https://spectrum.ieee.org/at-work/innovation.
Accessed: 2018-07-31.
Kane, T.R. and Levinson, D.A. (1985). Dynamics, Theory
Fig. 8. Ball on wheel - Simulation results and Applications. McGraw Hill.
Matsuda, Y. and Ohse, N. (2006). Synthesis of dynamic
controllers for a class of nonlinear systems: An ap-
plication to a ball-on-wheel system. In 2006 IEEE
Conference on Computer Aided Control System Design,
2006 IEEE International Conference on Control Appli-
cations, 2006 IEEE International Symposium on Intel-
ligent Control, 1061–1066. doi:10.1109/CACSD-CCA-
ISIC.2006.4776791.
Meza, C., Andrade-Romero, J.A., Bucher, R., and Balemi,
S. (2009). Free open source software in control engineer-
ing education: A case study in the analysis and control
design of a rotary inverted pendulum. In Proceedings of
the 14th IEEE International Conference on Emerging
Fig. 9. Ball on wheel - Diagram of the real system Technologies & Factory Automation, ETFA’09, 1403–
1410. IEEE Press, Piscataway, NJ, USA.
FH 5XXX INIT TQ initializes the Faulhaber motor. The Murray, R. (2009). URL
blocks of the real sensors are on the left part of the https://python-control.readthedocs.io.
diagram. The AD block, reads the ball position from a laser RT-Preempt (2016). URL
triangulation sensor with analog output and transforms https://rt.wiki.kernel.org/index.php/Main Page.
it into the angle ϕb . The motor encoder reads the wheel Tits, A. and Yang, Y. (1996). Globally convergent al-
position ϕw . gorithms for robust pole assignment by state feedback.
In IEEE Transactions on Automatic Control, Vol. 41,
The output signal of the controller is sent to the motor as 1432–1452.
torque value.
6. CONCLUSION
143
View publication stats