Silicon ChipReal-Time System Modelling - January 2017 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Publisher's Letter: Pumped hydro storage is no panacea for renewables & Airbags could kill your daughter
  4. Subscriptions
  5. Feature: Pumped Storage Hydroelectricity by Dr David Maddison
  6. PartShop
  7. Feature: Viewing Radio Waves In Colour by Ross Tester
  8. Project: New SC200 Audio Amplifier by Nicholas Vinen & Leo Simpson
  9. Project: High Power DC Motor Speed Control by John Clarke
  10. Serviceman's Log: When spare parts aren't around by Dave Thompson
  11. Project: Programming the ATtiny85 With An Arduino by Lawrence Billson
  12. Product Showcase
  13. Project: El Cheapo Modules From Asia - Part 3 by Jim Rowe
  14. Project: Giving the Ultrasonic Theremin A Volume Control by Bao Smith
  15. Feature: Real-Time System Modelling by Karthik Srinivasan
  16. Feature: Set-Top Boxes Make Great (Cheap!) PVRs by Jim Rowe
  17. Vintage Radio: Pye 1951 5-Valve Model APJ-Modified by Associate Professor Graham Parslow
  18. PartShop
  19. Market Centre
  20. Advertising Index
  21. Outer Back Cover

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":
  • Ultra-LD Mk3/Mk4 Amplifier Power Supply PCB [01109111] (AUD $15.00)
  • New SC200 Audio Amplifier PCB [01108161] (AUD $10.00)
  • Hard-to-get parts for the SC200 Audio Amplifier Module (Component, AUD $50.00)
  • New SC200 Audio Amplifier PCB pattern (PDF download) [01108161] (Free)
Articles in this series:
  • New SC200 Audio Amplifier (January 2017)
  • New SC200 Audio Amplifier (January 2017)
  • New SC200 Audio Amplifier – Part 2 (February 2017)
  • New SC200 Audio Amplifier – Part 2 (February 2017)
  • New SC200 Audio Amplifier - Part 3 (March 2017)
  • New SC200 Audio Amplifier - Part 3 (March 2017)
Items relevant to "High Power DC Motor Speed Control":
  • High Power DC Motor Speed Control main PCB [11112161] (AUD $10.00)
  • High Power DC Motor Speed Control Mosfet PCB [11112162] (AUD $12.50)
  • PIC16F88-I/P programmed for the High Power DC Motor Speed Controller [1111216A.HEX] (Programmed Microcontroller, AUD $15.00)
  • Hard-to-get parts for the High Power DC Motor Speed Controller (Component, AUD $50.00)
  • Firmware (ASM and HEX) files for the High Power DC Motor Speed Control [1111216A.HEX] (Software, Free)
  • High Power DC Motor Speed Control PCB patterns (PDF download) [11112161/2] (Free)
Articles in this series:
  • High Power DC Motor Speed Control (January 2017)
  • High Power DC Motor Speed Control (January 2017)
  • High Power DC Motor Speed Control – Part 2 (February 2017)
  • High Power DC Motor Speed Control – Part 2 (February 2017)
Items relevant to "Programming the ATtiny85 With An Arduino":
  • Firmware (.ino) files for the Programming the ATtiny85 With An Arduino (Software, Free)
Items relevant to "El Cheapo Modules From Asia - Part 3":
  • CP2102-based USB/TTL serial converter with microUSB socket and 6-pin right-angle header (Component, AUD $5.00)
  • CP2102-based USB/TTL serial converter with microUSB socket and 6-pin right-angle header (clone version) (Component, AUD $3.00)
Articles in this series:
  • El Cheapo Modules From Asia - Part 1 (October 2016)
  • El Cheapo Modules From Asia - Part 1 (October 2016)
  • El Cheapo Modules From Asia - Part 2 (December 2016)
  • El Cheapo Modules From Asia - Part 2 (December 2016)
  • El Cheapo Modules From Asia - Part 3 (January 2017)
  • El Cheapo Modules From Asia - Part 3 (January 2017)
  • El Cheapo Modules from Asia - Part 4 (February 2017)
  • El Cheapo Modules from Asia - Part 4 (February 2017)
  • El Cheapo Modules, Part 5: LCD module with I²C (March 2017)
  • El Cheapo Modules, Part 5: LCD module with I²C (March 2017)
  • El Cheapo Modules, Part 6: Direct Digital Synthesiser (April 2017)
  • El Cheapo Modules, Part 6: Direct Digital Synthesiser (April 2017)
  • El Cheapo Modules, Part 7: LED Matrix displays (June 2017)
  • El Cheapo Modules, Part 7: LED Matrix displays (June 2017)
  • El Cheapo Modules: Li-ion & LiPo Chargers (August 2017)
  • El Cheapo Modules: Li-ion & LiPo Chargers (August 2017)
  • El Cheapo modules Part 9: AD9850 DDS module (September 2017)
  • El Cheapo modules Part 9: AD9850 DDS module (September 2017)
  • El Cheapo Modules Part 10: GPS receivers (October 2017)
  • El Cheapo Modules Part 10: GPS receivers (October 2017)
  • El Cheapo Modules 11: Pressure/Temperature Sensors (December 2017)
  • El Cheapo Modules 11: Pressure/Temperature Sensors (December 2017)
  • El Cheapo Modules 12: 2.4GHz Wireless Data Modules (January 2018)
  • El Cheapo Modules 12: 2.4GHz Wireless Data Modules (January 2018)
  • El Cheapo Modules 13: sensing motion and moisture (February 2018)
  • El Cheapo Modules 13: sensing motion and moisture (February 2018)
  • El Cheapo Modules 14: Logarithmic RF Detector (March 2018)
  • El Cheapo Modules 14: Logarithmic RF Detector (March 2018)
  • El Cheapo Modules 16: 35-4400MHz frequency generator (May 2018)
  • El Cheapo Modules 16: 35-4400MHz frequency generator (May 2018)
  • El Cheapo Modules 17: 4GHz digital attenuator (June 2018)
  • El Cheapo Modules 17: 4GHz digital attenuator (June 2018)
  • El Cheapo: 500MHz frequency counter and preamp (July 2018)
  • El Cheapo: 500MHz frequency counter and preamp (July 2018)
  • El Cheapo modules Part 19 – Arduino NFC Shield (September 2018)
  • El Cheapo modules Part 19 – Arduino NFC Shield (September 2018)
  • El cheapo modules, part 20: two tiny compass modules (November 2018)
  • El cheapo modules, part 20: two tiny compass modules (November 2018)
  • El cheapo modules, part 21: stamp-sized audio player (December 2018)
  • El cheapo modules, part 21: stamp-sized audio player (December 2018)
  • El Cheapo Modules 22: Stepper Motor Drivers (February 2019)
  • El Cheapo Modules 22: Stepper Motor Drivers (February 2019)
  • El Cheapo Modules 23: Galvanic Skin Response (March 2019)
  • El Cheapo Modules 23: Galvanic Skin Response (March 2019)
  • El Cheapo Modules: Class D amplifier modules (May 2019)
  • El Cheapo Modules: Class D amplifier modules (May 2019)
  • El Cheapo Modules: Long Range (LoRa) Transceivers (June 2019)
  • El Cheapo Modules: Long Range (LoRa) Transceivers (June 2019)
  • El Cheapo Modules: AD584 Precision Voltage References (July 2019)
  • El Cheapo Modules: AD584 Precision Voltage References (July 2019)
  • Three I-O Expanders to give you more control! (November 2019)
  • Three I-O Expanders to give you more control! (November 2019)
  • El Cheapo modules: “Intelligent” 8x8 RGB LED Matrix (January 2020)
  • El Cheapo modules: “Intelligent” 8x8 RGB LED Matrix (January 2020)
  • El Cheapo modules: 8-channel USB Logic Analyser (February 2020)
  • El Cheapo modules: 8-channel USB Logic Analyser (February 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules (May 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules (May 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules, Part 2 (June 2020)
  • New w-i-d-e-b-a-n-d RTL-SDR modules, Part 2 (June 2020)
  • El Cheapo Modules: Mini Digital Volt/Amp Panel Meters (December 2020)
  • El Cheapo Modules: Mini Digital Volt/Amp Panel Meters (December 2020)
  • El Cheapo Modules: Mini Digital AC Panel Meters (January 2021)
  • El Cheapo Modules: Mini Digital AC Panel Meters (January 2021)
  • El Cheapo Modules: LCR-T4 Digital Multi-Tester (February 2021)
  • El Cheapo Modules: LCR-T4 Digital Multi-Tester (February 2021)
  • El Cheapo Modules: USB-PD chargers (July 2021)
  • El Cheapo Modules: USB-PD chargers (July 2021)
  • El Cheapo Modules: USB-PD Triggers (August 2021)
  • El Cheapo Modules: USB-PD Triggers (August 2021)
  • El Cheapo Modules: 3.8GHz Digital Attenuator (October 2021)
  • El Cheapo Modules: 3.8GHz Digital Attenuator (October 2021)
  • El Cheapo Modules: 6GHz Digital Attenuator (November 2021)
  • El Cheapo Modules: 6GHz Digital Attenuator (November 2021)
  • El Cheapo Modules: 35MHz-4.4GHz Signal Generator (December 2021)
  • El Cheapo Modules: 35MHz-4.4GHz Signal Generator (December 2021)
  • El Cheapo Modules: LTDZ Spectrum Analyser (January 2022)
  • El Cheapo Modules: LTDZ Spectrum Analyser (January 2022)
  • Low-noise HF-UHF Amplifiers (February 2022)
  • Low-noise HF-UHF Amplifiers (February 2022)
  • A Gesture Recognition Module (March 2022)
  • A Gesture Recognition Module (March 2022)
  • Air Quality Sensors (May 2022)
  • Air Quality Sensors (May 2022)
  • MOS Air Quality Sensors (June 2022)
  • MOS Air Quality Sensors (June 2022)
  • PAS CO2 Air Quality Sensor (July 2022)
  • PAS CO2 Air Quality Sensor (July 2022)
  • Particulate Matter (PM) Sensors (November 2022)
  • Particulate Matter (PM) Sensors (November 2022)
  • Heart Rate Sensor Module (February 2023)
  • Heart Rate Sensor Module (February 2023)
  • UVM-30A UV Light Sensor (May 2023)
  • UVM-30A UV Light Sensor (May 2023)
  • VL6180X Rangefinding Module (July 2023)
  • VL6180X Rangefinding Module (July 2023)
  • pH Meter Module (September 2023)
  • pH Meter Module (September 2023)
  • 1.3in Monochrome OLED Display (October 2023)
  • 1.3in Monochrome OLED Display (October 2023)
  • 16-bit precision 4-input ADC (November 2023)
  • 16-bit precision 4-input ADC (November 2023)
  • 1-24V USB Power Supply (October 2024)
  • 1-24V USB Power Supply (October 2024)
  • 14-segment, 4-digit LED Display Modules (November 2024)
  • 0.91-inch OLED Screen (November 2024)
  • 0.91-inch OLED Screen (November 2024)
  • 14-segment, 4-digit LED Display Modules (November 2024)
  • The Quason VL6180X laser rangefinder module (January 2025)
  • TCS230 Colour Sensor (January 2025)
  • The Quason VL6180X laser rangefinder module (January 2025)
  • TCS230 Colour Sensor (January 2025)
  • Using Electronic Modules: 1-24V Adjustable USB Power Supply (February 2025)
  • Using Electronic Modules: 1-24V Adjustable USB Power Supply (February 2025)
Items relevant to "Giving the Ultrasonic Theremin A Volume Control":
  • Firmware (.ino and .c) files for Giving the Ultrasonic Theremin a Volume Control (Software, Free)
Articles in this series:
  • Arduino-Based Digital Theremin (December 2016)
  • Arduino-Based Digital Theremin (December 2016)
  • Giving the Ultrasonic Theremin A Volume Control (January 2017)
  • Giving the Ultrasonic Theremin A Volume Control (January 2017)

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