0% found this document useful (0 votes)
27 views7 pages

CAE - RT - Linux - PI - HIL - Practical Experiences With Python and Linux RT at The SUPSI Laboratory

This paper presents a Computer Aided Control System Design (CACSD) envi- ronment completely based on the programming language Python. Students can perform all the control design tasks (modeling, identification, controller design, simulation) in this environment, and at the end, they can automatically generate RT code for targets like a PC or a Raspberry PI with a Linux RT OS. A Python package for control purposes have been developed at the Caltech by Richard Murray, with the aim of integrating th

Uploaded by

Peter Müller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views7 pages

CAE - RT - Linux - PI - HIL - Practical Experiences With Python and Linux RT at The SUPSI Laboratory

This paper presents a Computer Aided Control System Design (CACSD) envi- ronment completely based on the programming language Python. Students can perform all the control design tasks (modeling, identification, controller design, simulation) in this environment, and at the end, they can automatically generate RT code for targets like a PC or a Raspberry PI with a Linux RT OS. A Python package for control purposes have been developed at the Caltech by Richard Murray, with the aim of integrating th

Uploaded by

Peter Müller
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/335781799

Practical experiences with Python and Linux RT at the SUPSI Laboratory

Article in IFAC-PapersOnLine · January 2019


DOI: 10.1016/j.ifacol.2019.08.137

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 user has requested enhancement of the downloaded file.


12th IFAC Symposium on Advances in Control Education
12th IFAC Symposium on Advances in Control Education
July
12th 7-9,
IFAC2019. Philadelphia,
Symposium PA, USAin
on Advances
Advances
12th
July IFAC
7-9, Symposium
2019. on
Philadelphia, PA, USAinAvailable
Control Education
Control Education
online at www.sciencedirect.com
12th
July IFAC Symposium on Advances
July 7-9, 2019. Philadelphia, PA,
7-9, 2019. Philadelphia, USAin Control Education
PA, USA
July 7-9, 2019. Philadelphia, PA, USA
ScienceDirect
IFAC PapersOnLine 52-9 (2019) 133–138
Practical
Practical experiences
experiences with
with Python
Python and
and
Practical
Linux RT
Practical experiences
at the with
SUPSI
experiences Python
Python and
withLaboratory and
Linux RT at the SUPSI Laboratory
Linux RT at the SUPSI Laboratory
Linux RT atRoberto
the SUPSI
Bucher ∗∗
Laboratory
Roberto Bucher ∗∗∗
Roberto Bucher
Roberto Bucher ∗
∗ Roberto Bucher
∗ University
∗ of Applied Sciences of Southern Switzerland (SUPSI),
∗ University of Applied Sciences of Southern Switzerland (SUPSI),
∗ University
Institute
∗ University
Institute for
for of
SystemsApplied
of Applied
Systems and
and Sciences
Applied
Sciences
Applied of
of Southern
Electronics
Southern(DTI
Electronics Switzerland
-- ISEA),
Switzerland
(DTI ISEA), (SUPSI),
CH-6928
(SUPSI),
CH-6928
University
Institute
Institute for for of
SystemsApplied
Lugano-Manno, and
Systems and Applied Sciences
Applied
(e-mail: of Southern
Electronics Switzerland
(DTI
(DTI - ISEA), CH-6928
[email protected]).
Electronics - ISEA), (SUPSI),
CH-6928
Institute for Lugano-Manno,
Systems and Applied (e-mail: [email protected]).
Lugano-Manno,
Lugano-Manno, (e-mail: Electronics
(e-mail: (DTI - ISEA), CH-6928
[email protected]).
[email protected]).
Lugano-Manno, (e-mail: [email protected]).
Abstract: This
Abstract: This paperpaper presents
presents a a Computer
Computer Aided Aided Control Control System
System Design Design (CACSD)
(CACSD) envi- envi-
ronment completely
Abstract:
Abstract: This paper
This paper
based presents
on the a
presents aprogramming
Computer Aided
Computer Aided
language Control
Python.
Control System
SystemStudentsDesign
Design (CACSD)
can(CACSD)
perform allenvi-envi-
the
ronment
Abstract:
ronment completely
This
completely based
paper
based on
presentsthe
on the
the programming
aprogramming
programming language
Computercontroller
Aided
language Python.
Control
Python. SystemStudents
StudentsDesign can
can perform
(CACSD)
perform all the
allenvi-
the
control design
ronment tasks
completely (modeling,
based on identification, language design,
Python. simulation)
Students in
canthis environment,
perform all the
control
ronment
control design tasks (modeling,
completely
design based on theidentification,
programming controller
language design,
Python. simulation)
Students in
canthis environment,
perform all the
and
and at
control
controlat the
design
the
designend,tasks
end, they(modeling,
they
tasks
tasks
can
(modeling,
(modeling,
identification,
can automatically generate
identification,
automatically generate
identification,
controller
RT
RT code
controller
controller codedesign,
for
design,
design,
simulation)
for targets
simulation)
targets
simulation) like aain
like PC
in
PC
in
this
orenvironment,
or
this
this
aa Raspberry
environment,
Raspberry
environment,
and
PI
and at
with
PI with the
a
at the end,
Linux
end, they
a Linux they
RT can
OS.
RT OS. automatically generate RT code for targets
can automatically generate RT code for targets like a PC or a Raspberry like a PC or a Raspberry
and
PI at the
with
A Python
PI with a end, they
a Linux
Linux
package can
RTforOS.
RT OS. automatically
control purposes have generate
beenRTdevelopedcode forattargets
the Caltech like a byPCRichard
or a Raspberry
Murray,
A
PI
A Python
with
Python a package
Linux
package RTfor
forOS.control
control purposes
purposes have
have been
been developed
developed at
at the
the Caltech
Caltech by
by Richard
Richard Murray,
Murray,
with
A the aim
Python of integrating
package for control thepurposes
most used havefunctions
been and methods
developed at the available
Caltech in
by other
Richard commercial
Murray,
with
A the
Python aim of
package integrating
for software.
control the most used functions and methods at theavailable
Caltech in other commercial
with
and
and non
with the
the aim
non aim of
of integrating
commercial
commercial integrating thepurposes
the
software. most
Usingused
Using
most have
this
used been developed
thisfunctions
package and
package
functions it ismethods
it is
and possibleavailable
possible
methods to designin
to design
available bydifferent
in Richard
other
other
different
Murray,
commercial
kinds
kinds of
commercial of
with
and the
non
controllers
and aim forof
commercial
non commercial integrating
laboratory the
software.
plants
software. most
Using
(PID,
Using used
this functions
package
state-feedback,
this and
it
package it isLQR, ismethods
possible
LQR, available
including
possible to design
to designfullin other
different
and commercial
reduced
different kinds
order
kinds of
of
controllers
and
controllers for
non commercial laboratory
for laboratory
laboratory plants
software.
plantsUsing(PID,
(PID,this state-feedback,
package it isLQR,
state-feedback, including
possible
LQR, includingto designfull and reduced
different
full and
and reduced order
kinds
orderof
observers).
controllers for plants (PID, state-feedback, including full reduced order
observers).
controllers
observers). for laboratory plants (PID, state-feedback, LQR, including full and reduced order
An additional package developed at SUPSI allows representing the full system in a graphical
observers).
An additional
observers).
An additional package developed at SUPSI allows representing the full system in aa graphical
block
An
block
An diagram,package
diagram,
additional
additional
similar
similar to
package
package
developed
xCos
xCos or
developed
to
developed or at
at SUPSI
Simulink,
SUPSI and
Simulink,
at SUPSI
allows
allows
and representing
automatically
automatically
allows representing
the
the full
representinggenerating full system
generating
the full
code
code for
system
system
in
in RT
for
in
a graphical
RT targets.
graphical
targets.
a graphical
block
block diagram, similar
This application
diagram, similar
offers toto xCos
thexCos
mostororused
Simulink,
blocksand
Simulink, and automatically
required generating
for the design
automatically generating code for
of a control
code forsystem.
RT
RT targets.
New
targets.
This
block application
diagram, offers
similar the
to most
thexCos used
orused blocks
Simulink, required for the design of a control system. New
This application
blocksapplication
This can be easily offers
integrated
offers the mostinto
most used this SW.and
blocks
blocks automatically
required
required for the
for generating
the design
design of code forsystem.
of aa control
control RT targets.
system. New
New
blocks
This can be easily integrated into this SW. required for the design of a control system. New
This application
blocks
blocks can
can be be easily
environment offers
easily has the most
integrated
been
integrated used
into
validated
into this
thisblocks
atSW.
the SUPSI laboratory on different electromechanical
SW.
This
blocks
This environment
can be
environment easily has
has been
integrated
been validated
into
validated thisat
at the
SW.
the SUPSI
SUPSI laboratory
laboratory on
on different electromechanical
plants:
This
plants: the classical
environment
the classicalhas Inverted Pendulum,
been validated
Inverted Pendulum, the
the Disks
at the SUPSIand
Disks Spring
laboratory
and on different
Spring system,
system, the
different electromechanical
the Ball on
on Plate
Plate and
electromechanical
Ball and
This
plants:
the environment
the
Ballthe
plants: classical
on classical has
Wheel plants. been
Inverted validated
Inverted Pendulum,
Pendulum, the at the SUPSI
the Disks
Disks and laboratory
and Spring on
Spring system,
system,different electromechanical
the Ball
the Ball on Plate
on Plate andand
the
the Ball
plants:
Ballthe on Wheel
on classical plants.
Wheel plants. Inverted Pendulum, the Disks and Spring system, the Ball on Plate and
plants.
the Ball on Wheel
© 2019,
the BallIFACon Wheel(International
plants. Federation of Automatic Control) Hosting by Elsevier Ltd. All rights reserved.
Keywords: Computer
Keywords: Computer aided aided control
control system
system design,
design, Laboratory
Laboratory techniques,techniques, Laboratory
Laboratory
Keywords: Education.
education,
Keywords: Computer aided
Computer aided control
control system
system design,
design, Laboratory
Laboratory techniques,techniques, Laboratory
Laboratory
education,
education, Education.
Keywords: Computer aided control system design, Laboratory techniques, Laboratory
Education.
education, Education.
education, Education.
1. INTRODUCTION
1. INTRODUCTION ticular
ticular by by little
little companies,
companies, due due to to the the expensive
expensive licence
licence
1. INTRODUCTION
1. INTRODUCTION ticular
costs.
ticular
costs. by
by little
little companies,
companies, due
due to
to the
the expensive licence
expensive licence
1. INTRODUCTION ticular
costs. by little companies, due to the expensive licence
costs.
Nowadays
Nowadays other
costs. other companies
companies offer offer similar
similar CACSDCACSD environ-
environ-
Teaching controller design is not possible only with the- Nowadays
ments
Nowadays
ments as
as other
commercial
other
commercial companies
or
companies
or open
open offer similar
source
offer similar
source CACSD
solutions.
CACSD
solutions. This
Thisenviron-
situa-
environ-
situa-
Teaching controller design is not possible only with the- Nowadays ments
tion as
leads other
commercial
to some companies
or
ethical open offer
issues similar
source
that CACSD
solutions.
should be Thisenviron-
situa-
considered
Teaching
oretical
Teaching
oretical controller
lectures
controller
lectures design
without is
designpractical
without not
practical possible only
experiences.
is not possible only with
experiences. with the-
Students
the- tion
Students ments as
leads commercial
to some or
ethical open
issuessource
that solutions.
should be This situa-
considered
Teaching
oretical
need to
oretical controller
lectures
apply
lecturestheir designpractical
without
knowledge
without is not
practical
to possible only
experiences.
real plants
experiences. in with the- ments
Students
education
Students tion
by
tion
by the
the
as
leads commercial
to
to some
universities
leads some
universities
or
ethical
in the
ethical
in the
open
issues
choice
issues
choice
source
that
of
that
of aa solutions.
should
should be
proprietary
be
proprietary
This situa-
considered
software
considered
software
need to apply
oretical lecturestheir knowledge
without to real
practical plants in education
experiences. Students tion
by
for leads
the
the to some
universities
control systemethical
in the issues
choice
design that
of
courses. a should be
proprietary
In a considered
previous software
paper
need to
need to apply
applyAs
laboratories.
laboratories. their
As
their knowledge
future engineers,
knowledge
future engineers, to real
to real plants
they plants
they have to
have toin education by
inexperience
experience
education for the
the universities
control system in the choice
design of a proprietary
courses. In a previous software
paper
need to design
applyAs
laboratories. their
As knowledge
future engineers, tohave
real
they plants
have toin education
experience by
for the
(Meza
for the et
the universities
control system
al. (2009))
control system inwe the choice
design
have
design of a proprietary
courses.
already
courses. In aa previous
In previous
formulate software
thesepaper
two
paper
the full
laboratories. process
the full designAsprocessfuture and
and they
engineers,
they have they to work
have
tohave to with
worktowith similar
experience
similar for (Meza et al. (2009)) we have already formulate these two
laboratories.
the full design future
process engineers,
and they they
have to work experience
with similar (Meza the et
questions:
(Meza control
et al.
al. systemwe
(2009))
(2009)) wedesign
have
have courses.
already
already In a previous
formulate
formulate these
thesepaper
two
two
computational
the
computational tools
full design process
tools to
to the
and
the ones
they
ones that
have
that they
to
theyworkcan
can find
with
find in the
similar
in the questions:
the full design
computational process
tools to and
the they
ones have
that to
theyworkcan with
find similar
in the (Meza
questions:
questions: et al. (2009)) we have already formulate these two
industry
computationalor, as researchers,
tools to the in
onesthe academia.
that they can find in the •• Which
industry or, as tools
computational
industry or,
industry or, as
researchers,
to the ones
as researchers,
researchers,
in thethat
in the
in
academia.
they can find in the questions:
the academia.
academia. Which proprietary
proprietary software software should should the the academia
academia
Rapid
industry controller prototyping
or, as researchers, in the(RCP) methods
academia. play an •
• Which
teach?
Which proprietary
proprietary software
software should
should the academia
the academia
Rapid controller prototyping (RCP) methods play an teach?
•• Which
teach? proprietary softwarefavor should the academia
Rapid
important
Rapid controller
role
controller in prototyping
these
prototypingpractical (RCP)
(RCP) methods
aspects,
methodsactually play
play an
they
an Why
teach?
Why should
should the
the academia
academia favor one
one software
software com-
com-
important
Rapid role in prototyping
controller these practical (RCP)aspects,
methodsactually play they
an
• teach?
Why
pany should
among the
others? academia favor one software com-
important
allow to
important role
increase
role
allow to increase in
in these
the
these
the practical
effectiveness
practical
effectiveness aspects,
of the
aspects, actually
laboratory
actually
of the laboratory they
(see
they
(see

• Why
pany should
among the
others? academia favor one software com-
important
allow to role
increase in these
the practical
effectiveness aspects,
of the actually
laboratory they
(see • Why pany
pany should others?
among
among the academia favor one software com-
others?
Bucher
allow
Bucher and
toand Balemi
increase
Balemi the(2006)).
effectiveness of the laboratory (see As lecturers
(2006)). we may also ask ourselves one more question:
allow
Buchertoand increase
and Balemi the effectiveness of the laboratory (see As lecturers
(2006)). pany among we may others?
also ask ourselves one more question:
Bucher
RCP methods Balemi
require (2006)).
a Computer Aided Control System As lecturers
As lecturers we we maymay alsoalso ask
ask ourselves
ourselves one one more
more question:
question:
Bucher and Balemi (2006)).
RCP methods require a Computer Aided Control System As••lecturers Do we educate
we maystudentsalso ask in methods
ourselves or
one do
more we directly
question:
RCP
Design
RCP methods
(CACSD) require a
environmentComputer for Aided
the Control
design, the System
analysis, Do we educate students in methods or do we directly
Designmethods
RCP (CACSD)
methods
require a Computer
environment
require a Computer for the Aided Control
design,
Aided
System
the analysis,
Control System Do
•• transmit
Do we
we educate
transmit tools?
educate
tools? students
students in in methods
methods or or do
do we
we directly
directly
Design
the
Design (CACSD)
simulation
(CACSD)and
the simulation environment
and the
environment for the
implementation design,
for the design,
the implementation on the
onthe theanalysis,
target
theanalysis,
target • Do we educate
transmit tools?
tools? students in methods or do we directly
Design (CACSD) environment for the design, the analysis, The transmit
the
of
the
of thesimulation
the controller
simulation
controllerandand
and the
algorithms:
the
algorithms: implementation
in
implementationparticular,
in particular, on
on the target
universities
the target
universities The author is still convicted that Free Open Sources
author
transmit is still
tools? convicted that Free Open Sources
the simulation the implementation on universities
the target The author is
is still convicted
convictedaa that
of
of the
invest
the
invest a
a controller
lot of
controller
lot of algorithms:
resources
algorithms:
resources in
in in
teaching
in
teaching particular,
methods
particular,
methods universities
based
based on
on
(FOSS)
The
(FOSS)
The
alternatives
author
alternatives
author is
still represent
still represent
convicted more Free
more
that
that
Free Open
convenient
convenient
Free Open
Sources
Open solution
Sources
solution
Sources
of the
invest
Mathworks
invest a controller
a lot�

lotR
of
R of
algorithms:
resources
products:
resourcesMatlab in
Matlab in�
teaching
R
in teaching

particular,
for methods
the
methods universities
design based
of
based on
the (FOSS)
to
(FOSS) all alternatives
these issues.
alternatives The represent
reasons
represent a
that
a more
more
on to all these issues. The reasons that support this statement convenient
support this
convenient solution
statement
solution
Mathworks
invest a

lot

R
ofproducts:
resources in�

R
R
teaching
� for the
methods design of
based the
on (FOSS)
to all alternatives
all these
these issues.
issues. The represent
The reasons
reasons thata more
that support convenient
support this solution
this statement
statement
Mathworks
control
Mathworks �R products:
R
algorithm, Simulink
products: Matlab
R for
R
Matlab

R
Rthe
� for the
the design
forhybrid simulation
design of
of the of to
the are:
control
Mathworks algorithm,
�R Simulink
products:
�R
Matlab
� for�Rthe hybrid simulation of
forhybrid
the design of the are:
to all these issues. The reasons that support this statement
control
control algorithm,
the designed
algorithm, Simulink
controller
Simulinkand �R
R for
Embedded
for the
the hybrid
Coder �
simulation
R as tool to
R
simulation

�R of
of are:
are:
the designed
control controller
algorithm, Simulinkand �REmbedded
forforthethe Coder
hybrid R as tool to
simulation
� of are: •• FOSS
FOSS are are accessible
accessible on on the
the Web
Web to to small
small companies
companies
the
the designed
automatically
designed controller
generate
controller and
the code
and Embedded
Embedded Coder
control
Coder �R as
as tool
tool to
target. to FOSS
and are
developingaccessible on
countries the
too.Web to small companies
companies
automatically
the designed
automatically generate
controller
generate the
and
the code for
Embedded
code for the
the control
Coder
control
�R target.
as tool
target. to •
• FOSS
and are
developingaccessible on
countries the
too.Web to small
automatically generate the code for are
Matlab/Simulink/EmbeddedCoder the decontrol
facto target.
the tools •• FOSS
and
The are accessible
and developing
code
developing of FOSS on
countries
is
countries the
too.
open:
too.Web to small
curious companies
students have
automatically generate
Matlab/Simulink/EmbeddedCoder the code for the
are control
de facto target.
the tools • The
and code
developing of FOSS is
countries open:
too. curious students have
Matlab/Simulink/EmbeddedCoder
for control design. Despite of its popularity
Matlab/Simulink/EmbeddedCoder are
are de
de facto
facto the
the tools
in universities,
tools The
the
• The
• the code
possibilityof
code of FOSS
possibility FOSS
to
to see
seeis open:
how
is how a curious
open:a curiousparticular
particularstudents
method
students
method have
haveis
is
for
for control
control design.
design. Despite
Matlab/Simulink/EmbeddedCoder
Despite of
of its
its popularity
are
popularity de in
facto
in universities,
the tools
universities, • The
the code of and
possibility
implemented FOSS
to seeis how
increase open:
how a
their curious
particularstudents
knowledge. method haveis
these
for products
control
these design.
products are
are not
Despite
not widely used
of itsused
widely in
popularity
in industries, in
in universities,
industries, in par-
par- the possibility
implemented and to see
increase a
their particular
knowledge. method is
for control
these design.
products areDespite
not widely of itsused
widely popularity
used in universities,
in industries,
industries, in par-
par- the possibilityand
implemented
implemented and toincrease
see howtheir
increase a particular
their knowledge.method is
knowledge.
these products are not in in
these products
2405-8963 © 2019, are
IFACnot widely used
(International in industries,
Federation of Automatic in Control)
par- Hosting implemented and increase their knowledge.
by Elsevier Ltd. All rights reserved.
Copyright © 2019 IFAC 138
Copyright
Peer review© under
2019 IFAC 138 Control.
responsibility of International Federation of Automatic
Copyright
Copyright © 2019 IFAC
© 2019 IFAC 138
138
10.1016/j.ifacol.2019.08.137
Copyright © 2019 IFAC 138
2019 IFAC ACE
134 1-3, 2016. Bratislava, Slovakia
June Roberto Bucher / IFAC PapersOnLine 52-9 (2019) 133–138

• 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 ]

2.1 The Python Control module k i n d i f f s = [ x . d i f f ( t )−v , t h . d i f f ( t ) − w ]


p a r t i c l e s = [ Car , Bp ]

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.

2.2 System analysis 1.2 1.2

1.0 1.0

0.8 0.8

The python-control module contains a set of functions 0.6 0.6


y

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

conditions response, impulse response, response to generic 0.0


0 2 4 6 8
t
10 12 14 16
0.0
0 2 4 6 8
t
10 12 14 16

inputs, Bode, Nyquist, Nichols, root locus and poles-zeros


analysis. 10 0.8
Magnit ude (dB)

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

2.3 Modeling with Python


Phase (deg)

− 0.4

− 0.6

− 0.8
− 1.0 − 0.5 0.0 0.5 1.0

The sympy module (symbolic python) contains a full set


of operations to manage physical systems. In particular,
it is possible to find the linearized model of a complex Fig. 1. Examples of available plots

139
2019 IFAC ACE
June 1-3, 2016. Bratislava, Slovakia Roberto Bucher / IFAC PapersOnLine 52-9 (2019) 133–138 135

# C o nt r o l d e s i g n f o r t h e Dis k s And s p r i n g system


# State feedback with i n t e g r a l part
wn = 1 0 ; x i 1 = np . s q r t ( 2 ) / 2 ; x i 2 = np . s q r t ( 3 ) / 2

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 )

# Add d i s c r e t e int e g r a t o r for steady s t a t e zero error


Phi f = np . v s t a c k ( ( g z . A,− g z . C∗ t s ) )
Phi f = np . h s t a c k ( ( P h i f , [ [ 0 ] , [ 0 ] , [ 0 ] , [ 0 ] , [ 1 ] ] ) )
G f = np . v s t a c k ( ( g z . B , z e r o s ( ( 1 , 1 ) ) ) )

k = place ( Phi f , G f , c l p o le s d )

3. HYBRID SIMULATION WITH PYSIMCODER

An important step in control design is the possibility


to simulate the discrete controller with the continuous
plant. This task is not possible in the original Python
Control Library. Nowadays no tool like Simulink or xCos Fig. 4. Simulation of a hybrid system - Plot
is available in Python.
information and the topology of the connections. This list
After having tested numerous schematics editors, we de- is similar to a PSPICE netlist.
cide to develop our own graphical editor, using the Python
language and the Qt libraries. This tool, called pysim- 4.2 Definition of a block
Coder, contains the most used blocks required for the
realization of dynamic controlled systems. A small set of
these blocks is represented in Figure 2. All the blocks in pysimCoder are defined by three ele-
ments:
The interface file has the extension .xblk and describes
the default characteristics of a block (number of inputs,
number of outputs, icon file, parameters, etc.)
The class function translates the block into a Python
class for simulation and code generation: this object is
the basic element of the netlist.
The implementation function contains the code re-
lated to the block, it is written in C-language and
compiled into a library.
The interface file in particular contains a description of
Fig. 2. Some blocks available in the pysimCoder tool the default characteristics of the block. When pysimCoder
is launched, all the .xblk files are read and copied into a
Using pysimCoder it is possible to design the dynamic graphical library. Using drag-and-drop methods, the user
system into a block diagram, mixing continuous and dis- can get these blocks and put them into the diagram.
crete time blocks, to perform a simulation and display the
results. The following code, for example, describes the step.xblk
file associated to the block step input.
Figure 3 represents the block diagram of a controlled
dc servo motor. The plant is a continuous LTI system. {” l i b ” : ” input ” ,
The controller is discrete and implements an anti windup ”name” : ” S t e p ” ,
mechanism. ” ip” : 0 ,
” op ” : 1 ,
Figure 4 shows the result of the simulation. ” st ” : 0 ,
” i c o n ” : ”STEP” ,
4. CODE GENERATION ” params ” : ” s t e p B l k | S t e p Time : 1 . 0 | S t e p Value : 1 . 0 ” }

4.1 Basics The class function is a python function that translates


a block into an object of the RCPblk class. This object
For automatic code generation, the block diagram must contains all necessary information to transform the block
be translated into a list of objects that contains the block diagram into C-code: block parameters, input nodes, out-

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 , &param)==−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

The advantage of this approach is that the generated


functions implement only a minimal code, required to # Mechanical system
N = R e f e r e n c e F r a m e ( ’N ’ )
handle the execution process. The ISR function associated O = P o i n t ( ’O ’ ) ; O. s e t v e l (N, 0 )
to the block diagram shown in Figure 5 contains this code:
# Roll conditions
p h i r o l l = −( p h i w ∗R w−p h i b ∗R w ) / R b
int t e s t i s r ( double t )
w roll = phi roll . diff ( t )
{
s t e p (OUT, &b l o c k t e s t [ 0 ] ) ;
#R o t a t i n g s ys t e m s
d s s (OUT, &b l o c k t e s t [ 1 ] ) ;
N b = N . o r i e n t n e w ( ’ N b ’ , ’ A x i s ’ , [ p h i b ,N . y ] )
p l o t (OUT, &b l o c k t e s t [ 2 ] ) ;
N w = N . o r i e n t n e w ( ’ N w ’ , ’ A x i s ’ , [ phi w ,N . y ] )
N r o l l = N. o r i e n t n e w ( ’ N r o l l ’ , ’ A x i s ’ , [ p h i r o l l , N. y ] )
d s s (STUPD, &b l o c k t e s t [ 1 ] ) ;
N w . s e t a n g v e l (N, w w∗N . y )
}
N r o l l . s e t a n g v e l (N, w r o l l ∗N . y )
N b . s e t a n g v e l (N, w b∗N. y )

#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

The controller has been designed using a LQR approach.

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)

A reduced order observer delivers the two non measurable


states (ωb and ωw ), and finally the controller and the
observer have been reimplemented into a compact form
Fig. 6. The Ball on Wheel system with 3 inputs ( zero reference, ϕb and ϕw ) and 1 output
(torque signal to the motor).
In the laboratory at SUPSI, the system has been modeled,
identified, controlled, simulated and realized completely 5.4 Simulation
using Python. The final controller runs on a Raspberry PI
microcontroller with a Linux operating system modifed The block diagram for the simulation has been created
with the RT-Preempt patch. The connection from the using the pysimCoder tool. The plant is a continuous
microcontroller to the real plant is realized through the system and the controller is discrete (Figure 7).
CAN bus.
Figure 8 shows the plot of the simulation. The position
of the Ball is at the equilibrium point and the wheel is
5.2 Model of the plant rotating.

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

A method for rapid control prototyping completely based


on Python has been presented. All the tasks are executed
in the Python environment: modeling (sympy), control
design (Python Control System Library), graphical imple-
mentation, simulation and code generation (pysimCoder).
Both the Python Control System Library and pysimCoder
are still in development, but they have already demon-
strated their potential in solving real control problems.

143
View publication stats

You might also like