Kalman filter for single value : one-dimensional Kalman filter (with example problem - python3)

Kalman filter for single value

- Kalman filter is a an optimal estimation algorithm

-Kalman filtering is an algorithm that provides estimates of some unknown variables given the measurements observed over time

Kalman filter is one of the main topics in the field of robotic motion planning and control , can be used in trajectory optimization,prediction etc.

In situation like time delay between issuing motor commands and receiving sensory feedback - use of the Kalman filter supports a realistic model for making estimates of the current state of the motor system and issuing updated command.

          Let say we want to check the internal temperature of an engine/furnace which operates at very high temperature . yaah we cannot put sensor inside the furnace/engine directly to measure the temperature as it will burn the sensor 💥.

So the best way to get the internal temperature of the furnace/engine is to keep the sensor out side (in less temperature part) furnace/engine and calculate the estimated internal temperature of the furnace/engine from external temperature values✌.

Single measured value- Kalman filter - Flow chart 

one-dimensional Kalman filter block diagram

 Steps:

1.Calculate gain(kalman gain)

To calculate the gain we need to feed the Error in the estimate and error in the data.

Error in the estimate is the previous error(note the feedback line) or some times use original error and Error in data is from measured data/value

    Gain have relative importance with Error in data and Error in estimate  i.e  if the error in estimate is the less the it will give more important to it , similarly if the error in the data is the smallest then the gain calculation will give more importance to Error in data 

2.Calculate current estimate 

It uses calculated gain value ,previous estimate(note the feedback line) ,measured value(new data)

and the gain will decide the priority of previous estimate  and measured value in current estimate calculation

-This step gives filtered output   

3.Calculate new error in estimate and feed it back to kalman gain calculation

if we represent,

KALMAN GAIN = KG

ERROR IN ESTIMATE = Eest

ERROR IN MEASUREMENT = Emea

Then KG = Eest(Eest+Emea)

(KG= Error in estimate ➗ (Error in estimate + Error in measurement) )

The value of KG will be always in between 0 and 1

0<=KG=>1

 

CURRENT ESTIMATE =ESTt

PREVIOUS ESTIMATE =ESTt-1  

Then the estimate at time t will be ,

ESTt= ESTt-1 + KG [MEA ⁻ ESTt-1] 

Current estimate = previous estimate + kalman gain × (Difference between the new measurement and the previous estimate )


Role of kalman gain: If the kalman  gain value is high (close to 1) that means the measurements we are getting are fairly  accurate   and Estimates are unstable

KG = Eest/(Eest+Emea) - take look at the equation for better understanding

similarly if KG is less(near to 0 ) means measurements are inaccurate and Estimates are stable(only small error) 

Error in the estimate =[1-KG] × Previous estimate

or  

Error in the estimate =(Error in measurement × previous estimate ) ÷ (Error in measurement + previous estimate)

 

Example problem :

_________________________________________________________

Initial temperature =72

Initial estimate =68

Initial error in estimate =2

Initial measurement =75

Error in measurement =4


KG calculation:

KG= Error in estimate ➗ (Error in estimate + Error in measurement)

      =  2 ➗ (2+4) 

      =0.33

Estimate calculation:

Estimate at time t

ESTt=ESTt-1+KG[Measurement - ESTt-1] 

                        ( ESTt-1 --> previous estimate)

        =68+0.33[75-68]

        =70.33  ---> second iteration it will become the previous estimate

 

New error in the Estimate/Error in estimate :

Error in estimate  = [1-KG] × Error in previous estimate

                              = [1-0.33] ×2

                              = 1.33  

Updates these values for the next iteration  --> refer the block diagram above for better understanding

______________________________________________________

Example python script : by implementing  the equations :

#!/usr/bin/env python3
ERROR_IN_MEASUREMENT=4.0
ERROR_IN_PREV_ESTIMATE=2.0
#ERROR_IN_DATA=2

PREV_ESTIMATE=68.0
MEASURED_VALUE=None
KG=0.0


def calculate_gain():
    global PREV_ESTIMATE,ERROR_IN_MEASUREMENT
    global KG,MEASURED_VALUE,ERROR_IN_PREV_ESTIMATE
    #(KG= Error in estimate ➗ (Error in estimate + Error in measurement) ) 
    KG=ERROR_IN_PREV_ESTIMATE/(ERROR_IN_PREV_ESTIMATE+ERROR_IN_MEASUREMENT)
    print("KG:",KG)

def calculate_current_estimate():
    global PREV_ESTIMATE,KG,MEASURED_VALUE
    #Estimate at time t --> ESTt=ESTt-1+KG[Measurement - ESTt-1] 
    PREV_ESTIMATE=PREV_ESTIMATE+(KG*(MEASURED_VALUE-PREV_ESTIMATE))
    print("predicted value:",PREV_ESTIMATE)

def Calculate_new_error_in_estimate():
    global PREV_ESTIMATE,ERROR_IN_MEASUREMENT
    global KG,MEASURED_VALUE,ERROR_IN_PREV_ESTIMATE
    #Error in estimate  = [1-KG] × Previous estimate
    ERROR_IN_PREV_ESTIMATE= (1-KG)*ERROR_IN_PREV_ESTIMATE
    #print(ERROR_IN_PREV_ESTIMATE)


data=[60,61,64,66,20,65,67,69,0,1,0,69,1,68,69,69,68,69]
  
for MEASURED_VALUE in data:
    print("measured data:",MEASURED_VALUE)
    calculate_gain()
    calculate_current_estimate()
    Calculate_new_error_in_estimate() 
 
 

Read:

Locomotion in mobile robotics. 

What is mavros?.

Introduction to robotics. 

Different types of robots?

ROS : 5 Major advantages of using ROS.

Important branches of robotics?