© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
R. J. SmytheAdvanced Arduino Techniques in Sciencehttps://doi.org/10.1007/978-1-4842-6784-4_10

10. The Kalman Filter

Richard J. Smythe1  
(1)
Wainfleet, ON, Canada
 

Kalman filters reportedly came to prominence in the early years of the space program as a result of their success in definitively tracking space exploration vehicles. Kalman filters are mathematical processes first applied to extract vehicular courses or trajectories from noise-filled, measured radio-telemetry navigational data. Navigational data involves numerous degrees of freedom, such as roll, pitch, and yaw combined with forward, sideways, and vertical motions. Following a vehicle motion involves simultaneous application of the Kalman filter to each spatial dimension. Simultaneous, multiple filtering requires the application of advanced high-speed matrix algebra that goes beyond the introductory nature of this work. Kalman filtering is often taught by the application of mathematics to models of motion in a single dimension in which the uncertainty in position as a function of time is significantly reduced. The mathematical modeling of an error in a one-dimensional position as a function of time is equivalent to that of errors in a time-based sensor data stream.1 (See Prof. M. van Biezen presenting the first six lectures of “The Kalman Filter” for an excellent qualitative and quantitative demonstration of the filter theory and practical single-dimension application to the smoothing of a temperature sensor data stream.)

Kalman filtering can be used to significantly reduce or smooth the noise present in the output of a sensor data stream in which the errors in measurement form a Gaussian or normal distribution about the true value of the parameter being monitored.

The Single-Dimension Kalman Filter Process

As noted, in a Kalman filter smoothing of a single-dimension data stream, it is assumed that the errors in the data stream are of a Gaussian or normal distribution. (See Chapter 7, “Data Errors, Quality Control: Quality Assurance with Statistical Process Control.”) A Gaussian distribution is completely defined by its mean value and the variance; hence, the error distributions in the previous, present, and future data points of a data stream are well defined and amenable to processing with the Kalman filter.

In very simplified terms, a Kalman filter can be considered as an iterative mathematical process in which the past data is used to estimate a correction to be applied to the present data in order to predict a more accurate estimate of a future, true value in a data stream. A Kalman filter uses three functions to continuously refine the value of a data stream by determining three parameters called the Kalman Gain, then using the error in the estimate and the error in the measurement to converge on a better estimate of the true value of the streamed data. A Kalman filter converges very quickly on a final output variable value and only uses a few points of data from the recent past to achieve the convergence.

Figure 10-1 records the output voltage taken from the wiper lead of a 10 kΩ potentiometer biased between 5V and ground. Listing 10-1 (provided at the end of the chapter) applies a Kalman filter to the streamed wiper voltage value that is depicted in the spiking orange trace. The code for the Arduino library can be found at github.com/denyssene/SimpleKalmanFilter.
../images/503606_1_En_10_Chapter/503606_1_En_10_Fig1_HTML.jpg
Figure 10-1

The Kalman filter values from a wiper lead voltage from a 5V biased potentiometer

Close examination of the expanded-scale recorder tracing of the noise-filled voltage signal from the potentiometer wiper displays the raw signal in orange, the correction in red, and the estimate or calculated value in blue (Figure 10-2).
../images/503606_1_En_10_Chapter/503606_1_En_10_Fig2_HTML.jpg
Figure 10-2

An expanded-scale view of Figure 10-1

Application of the Kalman filter to the data stream produced by a TMP36 or LM35 ambient temperature sensor and the addition of a sixty-four-point smoothing allows the experimentalist to use the scale expansion of the DAQFactory plotting facility to vastly increase the sensitivity of temperature measurement with these simple, easy-to-use sensors. See Listing 10-2 at the end of the chapter.
../images/503606_1_En_10_Chapter/503606_1_En_10_Fig3_HTML.jpg
Figure 10-3

A 1oC graphic recording calibration

As can be seen in Figure 10-3, with an FSD of 21.25 to 22.50oC or 1.25oC, a nominal 1oC temperature jump in an insulated aluminum dry well, as measured by a mercury-in-glass thermometer, produced a 0.84oC recorder-tracing deflection from 10:50 to 11:00. (For dry-well configuration, see Chapter 3, Figure 3-3, High Heat and Temperature.) The cooling curve from 11:00 to 13:00 is marked by the temperature variations caused by the on/off cycling of the building’s central heating system. As with other time-based recorded data streams, the tendency for heat to flow and the limitations of insulation to retard that flow are graphically illustrated by the periodic temperature fluctuations in the aluminum mass.

In Figure 10-4, the limitations of scale expansion are seen in the trace portions marked with the arrows.
../images/503606_1_En_10_Chapter/503606_1_En_10_Fig4_HTML.jpg
Figure 10-4

An Arduino limitation on scale expansion

Although the Kalman filter and signal averaging can greatly reduce the baseline noise in a data stream, there are electronic limitations imposed by the analog to digital converter (ADC) on the amount of scale expansion that can be used to increase signal sensitivity.

In Figure 10-4, certain areas of the trace containing “square steps” are marked with black arrows. The square portions of the trace are in reality the limit of resolution possible with a 10-bit ADC on the Arduino. The stream of data coming from the Arduino is the series of numerical values that have been created when the analog voltage from the sensor passes through the analog-to-digital converter on the microcontroller. At high sensitivities, low temperature differentials are being read, and as the sensitivity is increased to display smaller and smaller temperature differentials eventually the unit-value steps of the ADC are resolved.

The Kalman filter and signal-smoothing statistical packages from the DAQFactory plotting software are able to increase the resolution of the temperature differentials to the point at which the individual digital transitions become visible and limit any further scale expansion.

Sensitivity can be increased in many data-acquisition applications by inserting an analog front end between the sensor and the data-acquisition system in the form of an ASIC, such as the LMP9100, 9150, or 91200 AFE chips from Texas Instruments, or as a circuit built from common components as described in Chapter 9.

Code Listings

Listing 10-1. Arduino Code for a Single-Dimension Kalman Filter of Temperature Data
#include <SimpleKalmanFilter.h>
/*
 This sample code demonstrates how to use the SimpleKalmanFilter object.
 A TMP36 temperature sensor is used to generate a temperature-value data point for the reference real value.
 Random noise will be in the signal data stream, and the estimated value obtained from
 the SimpleKalmanFilter should match the real reference value.
 SimpleKalmanFilter(e_mea, e_est, q);
 e_mea: Measurement Uncertainty
 e_est: Estimation of Uncertainty
 q: Process Noise
 */
 //
SimpleKalmanFilter simpleKalmanFilter(2, 2, 0.01);
// Serial output refresh time
const long SERIAL_REFRESH_TIME = 100;
long refresh_time;
void setup() {
  Serial.begin(9600);
  analogReference(EXTERNAL); // the low noise 3.3 volt reference is used not the noisy 5 volt
}
void loop() {
  // read a reference value from A0 and calculate temperature in real_value variable
  float real_value = analogRead(A0);
  real_value = ((float) real_value / (float) 1024) * 3.32;  // convert to voltage
  real_value = (real_value * 100) - 50;                     // calculate IC off-set
  // add noise to the reference value if required and use as the measured value variable
 float measured_value = real_value; // for artificial noise ->  + random(-100,100)/100.0;
  // calculate the estimated value with Kalman Filter
  float estimated_value = simpleKalmanFilter.updateEstimate(measured_value);
  // send to Serial output every 100ms
  // use the Serial Plotter for graphic visualization
  if (millis() > refresh_time) {
    //Serial.println(real_value,4);      // single value for DAQFtry plotting
    //Serial.print(",");                 // IDE serial plotter
    //Serial.print(measured_value,4);    // IDE serial plotter
    //Serial.print(",");                 // IDE serial plotter
    Serial.println(estimated_value,4);   // IDE/DAQFtry serial plotter
    //Serial.println();                  // IDE serial plotter
    refresh_time = millis() + SERIAL_REFRESH_TIME;
  }
}
// This sequence auto polls Com3 for streamed Arduino data
// ensure the null protocol has been selected in the protocol window
// and that the correct data is streaming into the DAQFactory serial
// port
// clear the buffer
device.Com3.Purge()
while(1)
   try
      private string datain = device.Com3.ReadUntil(13)
      private data = strToDouble(datain)
      ArduinoStream_ref.addValue(data)
   catch()
      delay(0.1)
   endcatch
endwhile
Listing 10-2

DAQFactory Sequence Code for Plotting a Data Stream on Com3

Summary

  • Kalman filters originally implemented in three dimensions to track and position space vehicles from noisy telemetry data have found use in reducing the noise in single-dimension data streams from experiments.

  • A Kalman filter iteratively uses three parameters called the Kalman gain, the error in the estimate, and the error in the measurement to converge on a more accurate estimate of the true value of the streamed data.

An Arduino microprocessor using a Kalman filter library provides a voltage-based data stream for microprocessor plotting and a temperature-based data stream for a DAQFactory plotting of single-dimension sensor data streams.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
44.200.237.112