# Using Online Estimation for Real-Time Fault Detection in a DC Motor

By Karthik Srinivasan, MathWorks

Control algorithms are typically developed based on a nominal plant model. Design criteria such as gain and phase margins can ensure acceptable performance when there are slight changes in plant dynamics. However, if plant dynamics change significantly due to a component or sensor failure, the result can be suboptimal or even catastrophic.

To ensure that such failures do not produce such a result, it is important to detect failures as they happen. An online model lets you compare plant measurements with predictions from the model and detect failures when the difference exceeds a certain threshold.

This article shows how to develop an online plant model for a DC motor and how to use this model to detect changes in motor dynamics in real time.

## Problem Setup

The setup for our example is a hobby DC motor connected to an Arduino® Uno board (Figure 1).

Figure 1. DC motor connected to an Arduino Uno board.

We command the DC motor to track a square wave reference angle. After 20 seconds we introduce a disturbance to make the motor stop tracking the reference angle (Figure 2).

Figure 2. Reference angle tracking fault.

We want to be able to detect this change in motor behavior while the DC motor is operating. To do this, we will program an Arduino Uno board to perform online estimation.

## Online Fault Detection

To program the Arduino Uno we build a simple two-part model in Simulink® using blocks from System Identification Toolbox™ (Figure 3).

Figure 3. Simulink model for DC motor control and online parameter identification. Top: The control algorithm. Bottom: Online parameter identification and fault detection algorithm.

The control algorithm part of the model uses motor angle measurement and a simple PID controller to calculate a voltage request to the motor to track the angle position reference.

The lower part of the model implements online parameter identification and fault detection. It is built using the Recursive Polynomial Estimator block in System Identification Toolbox.

We provide the voltage commanded to the DC motor and the measured motor angle as inputs to this block. Since an ARMAX model structure lets us model noise and dynamics independently, we configure this block to estimate an ARMAX polynomial model of the DC motor.

Selecting the right number of parameters is part science, part trial and error. Since DC motors are typically modeled as second-order systems we choose 2 for the number of poles (A(q) terms), and for B(q) and C(q) we choose 2 and 1, respectively. We choose the input delay by observing how long it takes the DC motor to respond to a step input and dividing this number by the estimator sample time. The result is a value of 2 for the Input Delay (nk) (Figure 4).

Figure 4. Estimated model parameters.

The Recursive Polynomial Estimator block gives us the option to enable or disable parameter estimation (Figure 5).

Figure 5. Recursive Polynomial Estimator block with parameter estimation enabled.

We will use the Enable port to perform online parameter estimation for the first 10 seconds of the experiment—long enough for the estimated parameters to converge to their steady-state values. After 10 seconds we will disable the estimation. With the estimation disabled, the block does not update the DC motor parameters; instead, it outputs their previously estimated values. We can use these estimated parameter values to predict the motor angle for a given commanded voltage under normal operating conditions of the motor, and compare this prediction with the measured motor angle.

To compare the predicted and measured motor angle we enable the Error port, which outputs the one-step-ahead prediction error (the difference between the measured motor angle and the angle estimated from the block).

We pass the low pass filtered version of the one-step-ahead prediction error to the fault detection algorithm, implemented as a two-state Stateflow® chart (Figure 6). The Stateflow chart sets the fault flag to 1 when the filtered error is greater than the threshold and resets the fault flag to 0 when 10 seconds have passed and the error is less than 1.

Figure 6. Fault detection algorithm implemented in Stateflow.

We deploy this Simulink model onto the Arduino Uno board using the Run on Target Hardware feature of Simulink.

## Results

We are now all set to detect changes in DC motor dynamics while the motor is running (Figure 7).

Figure 7. Real-time fault detection.

We let the online parameter estimation algorithm estimate the nominal DC motor dynamics for the first 10 seconds. About 20 seconds into the simulation, we introduce a disturbance into the DC motor. As soon as the disturbance occurs, the prediction error shoots up and the algorithm sets the fault flag to 1. When the disturbance is removed, the algorithm resets the fault flag to zero.

## Extending This Approach

In this example we used online estimation to detect faults in a physical system as they occurred. Common applications of online estimation also include adaptive control, where it is used to estimate a plant model to modify a controller based on changes in the plant model, and soft sensing where you generate a "measurement" based on the estimated plant model for feedback control or fault detection.

After validating the online estimation in simulation, you can generate code for your model for deployment to the target hardware.

Published 2015 - 92302v00