This is only a preview of the January 2017 issue of Silicon Chip. You can view 40 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "New SC200 Audio Amplifier":
Items relevant to "High Power DC Motor Speed Control":
Items relevant to "Programming the ATtiny85 With An Arduino":
Items relevant to "El Cheapo Modules From Asia - Part 3":
Items relevant to "Giving the Ultrasonic Theremin A Volume Control":
Articles in this series:
Purchase a printed copy of this issue for $10.00. |
While many readers will
be familiar with electronic
circuit simulation by
programs like Spice or
system design software
such as LabVIEW, there are
also simulation programs
to test mechanical systems
like motors. Simulink is
a graphical programming
language developed by
MathWorks and it can be
used to test Arduino control
systems.
By Karthik Srinivasan*
Real-time system modelling
with Arduino and Simulink
T
his practical demonstration
involves using Simulink to analyse the operation of a standard radio
control servo (Hitec HS-422) under Arduino control. Simulink can simulate
faults and it allows the user to make
alterations to the system to correct any
faults in real time.
Note, that the rest of this article assumes some familiarity with control
theory, linear regression and related
concepts.
When designing any system, criteria such as gain and phase margins can
ensure acceptable performance when
there are slight changes in system dynamics. However, if system dynamics
were to change significantly because
of a component or sensor failure, the
result can be less than optimal or even
catastrophic.
To ensure that these types of failures
do not produce an unexpected result,
it is important to detect these events
as they happen. A real-time model lets
you compare actual system measurements with predictions from the model
and detect failures when the difference
exceeds a certain threshold.
This article details how to build the
base of the model, and also how to ac82 Silicon Chip
quire data from an Arduino device.
You can find it at www.siliconchip.
com.au/l/aaak and it includes a download of the source code.
A video version of this article can
be found here: www.siliconchip.com.
au/l/aaal and another related video
here: www.siliconchip.com.au/l/aaam
For this demonstration we use a
Hitec HS-422 servo (widely used in
radio-controlled model cars, boats
and planes) mounted on a motor driver which is connected to an Arduino
Duemilanove board (an Arduino Uno
board can be substituted).
In Fig.1, we have the servo track a
square wave reference angle and after
20 seconds we introduce a disturbance
Fig.1: the predicted (yellow) and measured (mauve) motor angle. The measured
motor angle does not go back to a rest position once the fault has been
introduced into the system.
siliconchip.com.au
Fig.2: the Zero-Order Holds in the control algorithm provide a specified time-delay for the inputs provided, here we leave
it at the default value of 1. The polynomial estimator performs the comparison between the measured and predicted motor
angle, it then calculates the absolute error and original parameters. The data then needs to be sent to a host computer so it
can be plotted.
to the system, stopping the servo motor from tracking the reference angle.
Since we want to be able to detect
this change in behaviour while the
servo motor is running, we program
the Arduino Duemilanove board to
perform this calculation.
Fault detection
To program the Arduino Duemilanove we build a two-part model in Simulink using blocks from the System
Identification Toolbox (Fig.2).
The first part of the model is the
control algorithm and it uses motor angle measurements and a PID (proportional-integral-derivative) controller
to send a voltage request to the servo
motor which then tracks the reference angle position.
The PID controller looks at
the set-point value provided and
compares it to the actual value
of the process variable; in this
case the reference angle of the
motor. It then returns low if both values are the same, or high if they differ too much.
The second model handles parameter identification and fault detection
and this is where the maths gets heavy
siliconchip.com.au
(After all, it is from MathWorks!).
It uses a “recursive polynomial estimator” block located in the System
Identification Toolbox which has inputs for the input motor voltage and
measured angle.
This is configured as an ARMAX
(autoregressive-moving-average exogenous) model that is used to estimate
an ARMAX polynomial with the form:
A(q) * y(t) = B(q) * u(t – nk) + C(q) * e(t)
This lets us model noise and dynamics independently.
Choosing the right parameters for
our ARMAX model is part science,
part trial and error. Since any DC motor can be modelled as a second-order
differential equation, we choose two
The inner workings of the model DC
motor and what blocks are required
to take data from an Arduino device
over a serial port.
January 2017 83
Figures 3 & 4: an ARMAX polynomial is used for comparison between the measured and calculated motor reference angle
and the value of each parameter should be set as follows in the menus above. The “Output estimation error” selection is
needed so that we can plot the difference between our results.
for the number of poles [A(q)], two for
B(q) and one for C(q) as shown in Fig.3.
For the input delay (nk), we record
how long the servo takes to respond to
a step input and divide this number by
the estimator sample time, providing
us with a value of two (milliseconds)
for our model.
The input delay can be treated as
phase shift or a time delay applied to
the polynomial.
In Fig.4, the recursive polynomial
estimator block gives us the option to
enable or disable parameter estimation. We use “Add enable port” to perform ‘online’ parameter estimation for
the first 10 seconds of runtime – this
provides time for the parameters to
converge to their steady state values.
Once this period has passed, the parameter estimation block stops updating the servo motor with new values;
instead it uses the previously estimated values to calculate the next step.
These estimated values are used to
predict the motor angle for a given motor input voltage (under normal operating conditions) and are then compared
to the measured motor angle. This is
how the error for the motor system is
calculated.
Simulink provides a way to calculate a steady state error of the system.
To do this we enable the error port
which outputs the one-step-ahead prediction error (the difference between
the measured motor angle and the predicted angle).
We use a low-pass filtered version
(ie, another pole) of the fault detection
84 Silicon Chip
algorithm, which is implemented as a
two-state StateFlow chart; which is a
finite-state machine (Fig.5).
The StateFlow chart sets the fault
flag high when the filtered error is
greater than the threshold value which
is currently 1 and sets the flag low
when 10 seconds have passed and the
error is less than 1.
This Simulink model is now loaded
onto the Arduino Duemilanove board
by using the “Run on Target” feature
of Simulink.
results. We have the parameter estimation algorithm calculate the approximate servo motor dynamics during
the first 10 seconds of runtime before
it reaches its steady state.
After around 20 seconds have passed
we introduce our disturbance into the
servo motor, this causes the error value
to shoot up and the fault detection algorithm sets its fault flag high.
Once the disturbance is removed,
the system returns to normal and the
fault flag is once again set low.
Result
Building on this approach
Everything is set to detect changes in the servo’s dynamics while it is
running and in Fig.6 we can see our
For this project, we use real-time
estimation to detect faults as they occur in our system.
Fig.5: the StateFlow
diagram is pictured below
and to the right is what the
inside of the diagram looks
like.
siliconchip.com.au
Extra Reading
Fig.6: the top yellow trace is the predicted (or control) motor angle while the
mauve trace is the measured angle. The plot below shows the fault prediction
error (the difference between the predicted and measured values). Below that is
the fault flag value; it steps high when the error rises above a set constant.
Common applications of this are
in adaptive control, where this technique is used to modify some controller based on changes to the system
helping to maintain a required level
of performance.
For example, some radio control
(RC) servo motors use adaptive control to correct the performance of its
mechanism. An odd example would
be a swing driven by a motor with an
adaptive control system to help sustain periodic movement.
After making sure the prototype
model is correct, Simulink provides
an easy way to generate code for your
model, letting you deploy it to your
target hardware, similar to how we
loaded the software onto an Arduino
Duemilanove board using the “Run on
Target” feature.
Contact and pricing
A free 30 day licence for the MathWorks suite which includes Matlab,
Simulink, StateFlow and more can be
applied for on the MathWorks website
(https://au.mathworks.com/) by clicking the Trial Software link at the bottom of the page.
Pricing details for each type of licence can also be found on their website and more example projects can
be found at http://makerzone.mathworks.com/
SC
* MathWorks
https://au.mathworks.com/help/
instrument/direct-interface-communication-in-simulink.html – using
serial communications in Simulink
http://au.mathworks.com/help/
supportpkg/arduino/ug/run-modelon-arduino-hardware.html – running
Simulink programs on Arduino
https://au.mathworks.com/help/
ident/ref/recursivepolynomialmodelestimator.html – recursive polynomial estimator block
http://au.mathworks.com/help/ident/
ref/armax.html – ARMAX function
https://en.wikipedia.org/wiki/Autoregressive-moving-average_model
– ARMA details
http://au.mathworks.com/help/ident/
examples/comparison-of-variousmodel-identification-methods.html
– model comparisons
www.facstaff.bucknell.edu/mastascu/eControlHTML/Design/Perf1SSE.
htm – steady state error details
https://au.mathworks.com/help/
stateflow/gs/anatomy-of-a-stateflow-chart.html – StateFlow chart
example
www.landau-adaptivecontrol.org/
Slides%20Ch1.pdf – slides on adaptive control compared to conventional
and robust methods
w w w. g o o g l e . c o m . a u / p a t e n t s /
US5833545 – adaptive control swing
patent
http://au.mathworks.com/matlabcentral/fileexchange/44416-simpleadaptive-control-example – example
program using adaptive control
Glossary
Exogenous variable: variables independent of the process being measured; an example could be a shift in consumer confidence
leading to lower sales, or a natural disaster affecting energy production.
Online: the model or plant being run during the runtime of the system being tested, ergo in real-time.
Pole: poles and zeros are terms applied to mathematical transfer functions. In electronic circuitry, every filter network has a
time constant, a rolloff frequency and associated slope and a phase characteristic. Each filter network, which may be as
simple as an RC low-pass network, is referred to as a “pole”. The filter network which is the main determinant of a circuit’s
frequency response is referred to as the “dominant” pole.
Plant: a term from control theory referring to the combination of an input and output signal with some component that is
responsible for controlling a mechanism or system.
Polynomial: a polynomial is an expression that can be expressed in the form: anxn + an-1xn-1 + … + a1x + a0 where
a0, …, an are constants, xn are unknown variables and n > 0.
Servo motor: a servo motor contains a DC motor, gear reduction unit, a position-sensing device (normally a potentiometer)
and a control circuit. A servo receives some control signal that represents the desired output motor angle of the shaft and
applies power to the motor until that angle is reached; the position-sensing device is used to determine which way the motor
should move.
Transfer Function: a mathematical function relating the output or response of a system to its input, eg, a filter circuit.
siliconchip.com.au
January 2017 85
|