🤖
Introduction to ROS2 and Robotics
  • Introduction
  • ROS2
    • Index
    • IDE and CMake Setup
      • How to add additional include search path
    • ROS2 Building Blocks
      • ROS Workspace and Package Layout
      • Launch File
      • tf2
      • Quality of Service
      • Configurations
        • Rviz Configuration
      • Built-in Types
        • Built-in Message Type
    • ROS Architecture
      • Intra-process Communication
    • Navigation and Planning
      • Navigation Stack and Concepts
      • Navigation2 Implementation Overview
        • 🏗️Cost Map
        • Obstacle Avoidance and DWB Controller
      • DWB Controller
      • Page 5
    • How to launch the Nav2 stack
    • ROS2 Control
      • Online Resources
      • Overview of Codebase
    • 🍳Cookbook
      • Useful Commands
      • How to specify parameters
      • How to build the workspace
      • 🏗️How to publish message to a topic from command line?
      • How to inspect service and make a service call
      • How to properly terminate ROS and Gazebo
      • How to add and remove models in Gazebo simulation dynamically
      • 🚧How to spin nodes
    • 🛒Tutorials
      • Services and Communication between ROS2 and Gazebo
      • Subscription and Message Filters Demo
      • Executor and Spin Explained
      • Lifecycle Node Demo
      • Robotic Arm Demo
      • ⚒️Multiple Robotic Arms Simulation Demo
      • 🚧Introduction to xacro
    • Page
    • 🍺Tech Blog
      • Difference between URDF and SDF and how to convert
  • Gazebo
    • Index
    • Terminology
    • GUI
    • World Frame and Axis
    • Cookbook
    • Page 1
  • Programming in Robotics
    • C++
      • CMake
    • Python
    • Rust
  • Mathematics in Robotics
    • Linear Algebra
    • Matrix Properties
    • Probability
      • Expectation-Maximization Algorithm
    • Multivariable Function and Derivatives
  • Physics in Robotics
  • Control of Dynamic Systems
    • Dynamic Response and Transfer Function
    • Block Diagram
    • PID Controller
  • Robot Modeling and Control
    • Rotation and Homogeneous Transformation
  • Probabilistic Robotics
    • Bayes Filter
    • Kalman Filter
    • Particle Filter
    • Discrete Bayes Filter
    • Motion Model
    • Perception Model
    • Localization
    • SLAM
  • Miscellany
  • Concept Index
    • Quaternions
Powered by GitBook
On this page
  • Kalman Filter
  • Extended Kalman Filter
  1. Probabilistic Robotics

Kalman Filter

PreviousBayes FilterNextParticle Filter

Last updated 1 year ago

Recall that the key steps in implementing a model are:

  • represent the probability distribution (analytically or using sampling)

  • model the motion (or state transition): p(xt∣ut,xt−1)p(x_t|u_t, x_{t-1})p(xt​∣ut​,xt−1​)

  • model the measurement: p(zt∣xt)p(z_t|x_t)p(zt​∣xt​)

Kalman Filter

In the standard Kalman Filter algorithm, the state transition is modeled as:

xt=Atxt−1+Btut+ϵtx_t = A_tx_{t-1} + B_t u_t + \epsilon_txt​=At​xt−1​+Bt​ut​+ϵt​

where ϵt∼N(0,Rt)\epsilon_t \sim \mathcal{N}(0, R_t)ϵt​∼N(0,Rt​). The covariance matrix RtR_tRt​ represents the uncertainty or noise. To obtain the analytic form of p(xt∣ut,xt−1)p(x_t|u_t,x_{t-1})p(xt​∣ut​,xt−1​), we just need to observer xt−Atxt−1−Btutx_t - A_tx_{t-1} - B_tu_{t}xt​−At​xt−1​−Bt​ut​ follows the normal distribution.

Similarly, the measurement in the standard Kalman filter is modeled as:

zt=Ctxt+δtz_t = C_tx_t + \delta_tzt​=Ct​xt​+δt​

where δt∼N(0,Qt)\delta_t \sim \mathcal{N}(0, Q_t)δt​∼N(0,Qt​) describing the measurement noise.

It can be shown that in the standard Kalman filter, the states xtx_txt​follows normal distribution. i.e.:

xt∼N(μt,Σt)x_t \sim \mathcal{N}(\mu_t, \Sigma_t)xt​∼N(μt​,Σt​)

and the algorithm is given as

\begin{algorithm}
    \renewcommand{\thealgorithm}{}
    \begin{algorithmic}[1]
    \Function{Kalman\_Filter}{$\mu_{t-1}, \Sigma_{t-1}, u_t, z_t$}
    \State $\bar{\mu}_t = A_t \mu_{t-1} + B_t u_t$
    \State $\bar{\Sigma}_t = A_t\Sigma_{t-1}A_t^T + R_t$
    
    \State $K_t = \bar{\Sigma}_t C_t^T (C_t \bar{\Sigma}_t  C_t^T + Q_t)^{-1}$  \Comment{This is called Kalman gain}
    
    \State $\mu_t = \bar{\mu}_t + K_t(z_t - C_t \bar{\mu}_t)$
    
    \State $\Sigma_t = (I - K_tC_t) \bar{\Sigma}_t$
    
    \State $\textbf{return} \;\;\; \mu_t, \Sigma_t$
    \EndFunction
    
\end{algorithmic}
\end{algorithm}

Extended Kalman Filter

In EKF, the state transition and measurement model take more general form:

xt=g(ut,xt−1)+ϵtzt=h(xt)+δt\begin{align*} x_t & = g(u_t, x_{t-1}) + \epsilon_t \\ z_t & = h(x_t) + \delta_t \end{align*}xt​zt​​=g(ut​,xt−1​)+ϵt​=h(xt​)+δt​​

The problem is that for arbitrary functions, it may not be possible to obtain an analytic form of the distribution of state variable xtx_txt​. One way to get around this problem is linearization using Taylor expansion.

Before we look into the detail of the Taylor expansion, let's take one step back and review what we already have. One of the important things is not to confuse parameters with known values.

Recall that the ultimate goal is to calculate p(xt∣ut,xt−1)p(x_t|u_t, x_{t-1})p(xt​∣ut​,xt−1​)and p(zt∣xt)p(z_t|x_t)p(zt​∣xt​). Although they are conditional probability and (ut,xt−1)(u_t, x_{t-1})(ut​,xt−1​) and xtx_txt​ are conditions in the two expressions respectively, all of them are parameters (or function arguments). If we forget about the probability context for a moment, it's quite obvious p(xt∣ut,xt−1)p(x_t|u_t, x_{t-1})p(xt​∣ut​,xt−1​) is a mapping from (xt,ut,xt−1)(x_t, u_t, x_{t-1})(xt​,ut​,xt−1​)to a value.

We also recall that in the standard Kalman filter, the distribution of the states is tracked by N(μt,Σt)\mathcal{N}(\mu_t, \Sigma_t)N(μt​,Σt​) so at time ttt, μ1,μ2,...,μt−1\mu_1, \mu_2, ..., \mu_{t-1}μ1​,μ2​,...,μt−1​ are known values.

Now, we can get back to the Taylor expansion. For the motion model, we perform the linearization around the μt−1\mu_{t-1}μt−1​ because this is our estimate of the state at t−1t-1t−1 and it should be close to xt−1x_{t-1}xt−1​. Therefore, we have

g(ut,xt−1)≈g(ut,μt−1)+∂g∂xt−1(ut,μt−1)(xt−1−μt−1)=g(ut,μt−1)+Gt(xt−1−μt−1) \begin{align*} g(u_t, x_{t-1}) & \approx g(u_t, \mu_{t-1}) + \frac{\partial g}{\partial x_{t-1}}(u_t, \mu_{t-1})(x_{t-1} - \mu_{t-1}) \\ & = g(u_t, \mu_{t-1}) + G_t(x_{t-1} - \mu_{t-1}) \end{align*} g(ut​,xt−1​)​≈g(ut​,μt−1​)+∂xt−1​∂g​(ut​,μt−1​)(xt−1​−μt−1​)=g(ut​,μt−1​)+Gt​(xt−1​−μt−1​)​

where ∂g∂xt−1(ut,μt−1)\frac{\partial g}{\partial x_{t-1}}(u_t,\mu_{t-1})∂xt−1​∂g​(ut​,μt−1​) means the partial derivative with respect to the second variable evaluated at (ut,μt−1)(u_t, \mu_{t-1})(ut​,μt−1​).

Similarly, we can write

h(xt)≈h(μˉt)+Ht(xt−μˉt)h(x_t) \approx h(\bar{\mu}_t) + H_t(x_t - \bar{\mu}_t)h(xt​)≈h(μˉ​t​)+Ht​(xt​−μˉ​t​)

where μˉt=g(ut,μt−1)\bar{\mu}_t = g(u_t, \mu_{t-1})μˉ​t​=g(ut​,μt−1​).

The extended Kalman filter algorithm is given as:

\begin{algorithm}
    \renewcommand{\thealgorithm}{}
    \begin{algorithmic}[1]
        \Function{EKF}{$\mu_{t-1}, \Sigma_{t-1}, u_t, z_t$}
        \State $\bar{\mu}_t = g(u_t, \mu_{t-1})$
        \State $\bar{\Sigma}_t = G_t\Sigma_{t-1}G_t^T + R_t$
        
        \State $K_t = \bar{\Sigma}_t H_t^T (H_t \bar{\Sigma}_t  H_t^T + Q_t)^{-1}$ 
        
        \State $\mu_t = \bar{\mu}_t + K_t(z_t - h(\bar{\mu}_t))$
        
        \State $\Sigma_t = (I - K_tH_t) \bar{\Sigma}_t$
        
        \State $\textbf{return} \;\;\; \mu_t, \Sigma_t$
        \EndFunction
        
    \end{algorithmic}
\end{algorithm}