PythonRobotics Documentation

Python sample codes and textbook for robotics algorithms.

RAW Doc

1. Project Overview & Quickstart (AtsushiSakai/PythonRobotics)

File: README.md

PythonRobotics

Python codes and textbook for robotics algorithm.

Table of Contents

What is PythonRobotics?

PythonRobotics is a Python code collection and a textbook of robotics algorithms.

Features:

  1. Easy to read for understanding each algorithm's basic idea.

  2. Widely used and practical algorithms are selected.

  3. Minimum dependency.

See this documentation

or this Youtube video:

or this paper for more details:

Requirements to run the code

For running each sample code:

For development:

Documentation (Textbook)

This README only shows some examples of this project.

If you are interested in other examples or mathematical backgrounds of each algorithm,

You can check the full documentation (textbook) online: Welcome to PythonRobotics’s documentation! — PythonRobotics documentation

All animation gifs are stored here: AtsushiSakai/PythonRoboticsGifs: Animation gifs of PythonRobotics

How to use

  1. Clone this repo.

    terminal
    git clone https://github.com/AtsushiSakai/PythonRobotics.git
  2. Install the required libraries.

  • using conda :

    terminal
    conda env create -f requirements/environment.yml
  • using pip :

    terminal
    pip install -r requirements/requirements.txt
  1. Execute python script in each directory.

  2. Add star to this repo if you like it :smiley:.

Localization

Extended Kalman Filter localization

Reference

GPS/IMU Fusion Localization with Bias Estimation

An extended Kalman filter fuses body-frame accelerometer and gyroscope readings
with lower-rate GPS positions, estimates sensor biases, and continues inertial
prediction during a temporary GPS outage. The animation compares it with an
EKF without bias estimation and IMU-only dead reckoning using the same sensor
measurements. It also plots the accelerometer and gyroscope bias estimates
against their true values, with covariance-derived 3σ position ellipses and
bias uncertainty bands.

Particle filter localization

This is a sensor fusion localization with Particle Filter(PF).

The blue line is true trajectory, the black line is dead reckoning trajectory,

and the red line is an estimated trajectory with PF.

It is assumed that the robot can measure a distance from landmarks (RFID).

These measurements are used for PF localization.

Reference

Histogram filter localization

This is a 2D localization example with Histogram filter.

The red cross is true position, black points are RFID positions.

The blue grid shows a position probability of histogram filter.

In this simulation, x,y are unknown, yaw is known.

The filter integrates speed input and range observations from RFID for localization.

Initial position is not needed.

Reference

Mapping

Gaussian grid map

This is a 2D Gaussian grid mapping example.

Ray casting grid map

This is a 2D ray casting grid mapping example.

Lidar to grid map

This example shows how to convert a 2D range measurement to a grid map.

k-means object clustering

This is a 2D object clustering with k-means algorithm.

Rectangle fitting

This is a 2D rectangle fitting for vehicle detection.

SLAM

Simultaneous Localization and Mapping(SLAM) examples

Iterative Closest Point (ICP) Matching

This is a 2D ICP matching example with singular value decomposition.

It can calculate a rotation matrix, and a translation vector between points and points.

Reference

FastSLAM 1.0

This is a feature based SLAM example using FastSLAM 1.0.

The blue line is ground truth, the black line is dead reckoning, the red line is the estimated trajectory with FastSLAM.

The red points are particles of FastSLAM.

Black points are landmarks, blue crosses are estimated landmark positions by FastSLAM.

Reference

Path Planning

Dynamic Window Approach

This is a 2D navigation sample code with Dynamic Window Approach.

Dijkstra algorithm

This is a 2D grid based the shortest path planning with Dijkstra's algorithm.

In the animation, cyan points are searched nodes.

A\* algorithm

This is a 2D grid based the shortest path planning with A star algorithm.

In the animation, cyan points are searched nodes.

Its heuristic is 2D Euclid distance.

D\* algorithm

This is a 2D grid based the shortest path planning with D star algorithm.

The animation shows a robot finding its path avoiding an obstacle using the D* search algorithm.

Reference

D\* Lite algorithm

This algorithm finds the shortest path between two points while rerouting when obstacles are discovered. It has been implemented here for a 2D grid.

The animation shows a robot finding its path and rerouting to avoid obstacles as they are discovered using the D* Lite search algorithm.

Refs:

Potential Field algorithm

This is a 2D grid based path planning with Potential Field algorithm.

In the animation, the blue heat map shows potential value on each grid.

Reference

Grid based coverage path planning

This is a 2D grid based coverage path planning simulation.

Particle Swarm Optimization (PSO)

This is a 2D path planning simulation using the Particle Swarm Optimization algorithm.

PSO is a metaheuristic optimization algorithm inspired by bird flocking behavior. In path planning, particles explore the search space to find collision-free paths while avoiding obstacles.

The animation shows particles (blue dots) converging towards the optimal path (yellow line) from start (green area) to goal (red star).

References

State Lattice Planning

This script is a path planning code with state lattice planning.

This code uses the model predictive trajectory generator to solve boundary problem.

Reference

Biased polar sampling

Lane sampling

Probabilistic Road-Map (PRM) planning

This PRM planner uses Dijkstra method for graph search.

In the animation, blue points are sampled points,

Cyan crosses means searched points with Dijkstra method,

The red line is the final path of PRM.

Reference

  

Rapidly-Exploring Random Trees (RRT)

RRT\*

This is a path planning code with RRT*

Black circles are obstacles, green line is a searched tree, red crosses are start and goal positions.

Reference

RRT\* with reeds-shepp path

Path planning for a car robot with RRT* and reeds shepp path planner.

LQR-RRT\*

This is a path planning simulation with LQR-RRT*.

A double integrator motion model is used for LQR local planner.

Reference

Quintic polynomials planning

Motion planning with quintic polynomials.

It can calculate a 2D path, velocity, and acceleration profile based on quintic polynomials.

Reference

Reeds Shepp planning

A sample code with Reeds Shepp path planning.

Reference

LQR based path planning

A sample code using LQR based path planning for double integrator model.

Optimal Trajectory in a Frenet Frame

This is optimal trajectory generation in a Frenet Frame.

The cyan line is the target course and black crosses are obstacles.

The red line is the predicted path.

Reference

Path Tracking

move to a pose control

This is a simulation of moving to a pose control

Reference

Stanley control

Path tracking simulation with Stanley steering control and PID speed control.

Reference

Rear wheel feedback control

Path tracking simulation with rear wheel feedback steering control and PID speed control.

Reference

Linear–quadratic regulator (LQR) speed and steering control

Path tracking simulation with LQR speed and steering control.

Reference

Model predictive speed and steering control

Path tracking simulation with iterative linear model predictive speed and steering control.

Reference

Nonlinear Model predictive control with C-GMRES

A motion planning and path tracking simulation with NMPC of C-GMRES

Reference

Arm Navigation

N joint arm to point control

N joint arm to a point control simulation.

This is an interactive simulation.

You can set the goal position of the end effector with left-click on the plotting area.

In this simulation N = 10, however, you can change it.

Arm navigation with obstacle avoidance

Arm navigation with obstacle avoidance simulation.

Aerial Navigation

drone 3d trajectory following

This is a 3d trajectory following simulation for a quadrotor.

rocket powered landing

This is a 3d trajectory generation simulation for a rocket powered landing.

Reference

Bipedal

bipedal planner with inverted pendulum

This is a bipedal planner for modifying footsteps for an inverted pendulum.

You can set the footsteps, and the planner will modify those automatically.

License

MIT

Use-case

If this project helps your robotics project, please let me know with creating an issue.

Your robot's video, which is using PythonRobotics, is very welcome!!

This is a list of user's comment and references:users_comments

Contribution

Any contribution is welcome!!

Please check this document:How To Contribute — PythonRobotics documentation

Citing

If you use this project's code for your academic work, we encourage you to cite our papers

If you use this project's code in industry, we'd love to hear from you as well; feel free to reach out to the developers directly.

Supporting this project

If you or your company would like to support this project, please consider:

If you would like to support us in some other way, please contact with creating an issue.

Sponsors

[JetBrains](https://www.jetbrains.com/)

They are providing a free license of their IDEs for this OSS development.

[1Password](https://github.com/1Password/for-open-source)

They are providing a free license of their 1Password team license for this OSS project.

Authors


File: docs/modules/13_mission_planning/behavior_tree/behavior_tree_main.rst

Behavior Tree

Behavior Tree is a modular, hierarchical decision model that is widely used in robot control, and game development.
It presents some similarities to hierarchical state machines with the key difference that the main building block of a behavior is a task rather than a state.
Behavior Trees have been shown to generalize several other control architectures (https://ieeexplore.ieee.org/document/7790863)

Code Link

text
Control Node
++++++++++++

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.ControlNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.SequenceNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.SelectorNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.WhileDoElseNode

Action Node
++++++++++++

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.ActionNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.EchoNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.SleepNode

Decorator Node
++++++++++++++

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.DecoratorNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.InverterNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.TimeoutNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.DelayNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.ForceSuccessNode

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.ForceFailureNode

Behavior Tree Factory
+++++++++++++++++++++

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.BehaviorTreeFactory
  :members:

Behavior Tree
+++++++++++++

.. autoclass:: MissionPlanning.BehaviorTree.behavior_tree.BehaviorTree
  :members:

Example
~~~~~~~

Visualize the behavior tree by `xml-tree-visual <https://xml-tree-visual.vercel.app/>`_.

.. image:: ./robot_behavior_case.svg

Print the behavior tree

.. code-block:: text

    Behavior Tree
    [Robot Main Controller]
        [Battery Management]
            (Low Battery Detection)
                <Check Battery>
            <Low Battery Warning>
            <Charge Battery>
        [Patrol Task]
            <Start Task>
            [Move to Position A]
                <Move to A>
                [Obstacle Handling A]
                    [Obstacle Present]
                        <Detect Obstacle>
                        <Avoid Obstacle>
                    <No Obstacle>
                
            [Move to Position B]
                (Short Wait)
                    
                <Move to B>
                (Limited Time Obstacle Handling)
                    [Obstacle Present]
                        <Detect Obstacle>
                        <Avoid Obstacle>
                
            [Conditional Move to C]
                <Check Sufficient Battery>
                [Perform Position C Task]
                    <Move to C>
                    (Ensure Completion)
                        
                <Skip Position C>
            <Complete Patrol>
            <Return to Charging Station>
    Behavior Tree

---

## File: docs/modules/13_mission_planning/state_machine/state_machine_main.rst

State Machine
-------------

A state machine is a model used to describe the transitions of an object between different states. It clearly shows how an object changes state based on events and may trigger corresponding actions.

Core Concepts
  • State: A distinct mode or condition of the system (e.g. "Idle", "Running"). Managed by State class with optional on_enter/on_exit callbacks
  • Event: A trigger signal that may cause state transitions (e.g. "start", "stop")
  • Transition: A state change path from source to destination state triggered by an event
  • Action: An operation executed during transition (before entering new state)
  • Guard: A precondition that must be satisfied to allow transition

Code Link

text
.. autoclass:: MissionPlanning.StateMachine.state_machine.StateMachine
   :members: add_transition, process, register_state
   :special-members: __init__

PlantUML Support

The generate_plantuml() method creates diagrams showing:

  • Current state (marked with [*] arrow)
  • All possible transitions
  • Guard conditions in [brackets]
  • Actions prefixed with /

Example

text
state machine diagram:
+++++++++++++++++++++++
.. image:: robot_behavior_case.png

state transition table:
+++++++++++++++++++++++
.. list-table:: State Transitions
   :header-rows: 1
   :widths: 20 15 20 20 20

   * - Source State
     - Event
     - Target State
     - Guard
     - Action
   * - patrolling
     - detect_task
     - executing_task
     - 
     - 
   * - executing_task
     - task_complete
     - patrolling
     - 
     - reset_task
   * - executing_task
     - low_battery
     - returning_to_base
     - is_battery_low
     - 
   * - returning_to_base
     - reach_base
     - charging
     - 
     - 
   * - charging
     - charge_complete
     - patrolling
     - 
     -

---

## File: docs/modules/13_mission_planning/mission_planning_main.rst

.. _`Mission Planning`:

Mission Planning
================

Mission planning includes tools such as finite state machines and behavior trees used to describe robot behavior and high level task planning.  

.. toctree::
   :maxdepth: 2
   :caption: Contents

   state_machine/state_machine
   behavior_tree/behavior_tree

---

## File: docs/modules/12_appendix/appendix_main.rst

.. _`Appendix`:

Appendix
==============

.. toctree::
   :maxdepth: 2
   :caption: Contents

   steering_motion_model
   Kalmanfilter_basics
   Kalmanfilter_basics_2
   internal_sensors
   external_sensors

---

## File: docs/modules/12_appendix/external_sensors_main.rst

.. _`External Sensors for Robots`:

External Sensors for Robots
============================

This project, `PythonRobotics`, focuses on algorithms, so hardware is not included.
However, having basic knowledge of hardware in robotics is also important for understanding algorithms.
Therefore, we will provide an overview.

Introduction
------------

In recent years, the application of robotic technology has advanced, particularly in areas such as autonomous vehicles and disaster response robots. A crucial element in these technologies is external recognition—the robot's ability to understand its surrounding environment, identify safe zones, and detect moving objects using onboard sensors. Achieving effective external recognition involves various techniques, but equally important is the selection of appropriate sensors. Robots, like the sensors they employ, come in many forms, but external recognition sensors can be broadly categorized into three types. Developing an advanced external recognition system requires a thorough understanding of each sensor's principles and characteristics to determine their optimal application. This article summarizes the principles and features of these sensors for personal study purposes.

Laser Sensors
-------------

Laser sensors measure distances by utilizing light, commonly referred to as Light Detection and Ranging (LIDAR). They operate by emitting light towards an object and calculating the distance based on the time it takes for the reflected light to return, using the speed of light as a constant.

Radar Sensors
-------------

Radar measures distances using radio waves, commonly referred to as Radio Detection and Ranging (RADAR). It operates by transmitting radio signals towards an object and calculating the distance based on the time it takes for the reflected waves to return, using the speed of radio waves as a constant. 


Monocular Cameras
-----------------

Monocular cameras utilize a single camera to recognize the external environment. Compared to other sensors, they can detect color and brightness information, making them primarily useful for object recognition. However, they face challenges in independently measuring distances to surrounding objects and may struggle in low-light or dark conditions.

Requirements for Cameras and Image Processing in Robotics

While camera sensors are widely used in applications like surveillance, deploying them in robotics necessitates meeting specific requirements:

  1. High dynamic range to adapt to various lighting conditions
  2. Wide measurement range
  3. Capability for three-dimensional measurement through techniques like motion stereo
  4. Real-time processing with high frame rates
  5. Cost-effectiveness

Stereo Cameras

Stereo cameras employ multiple cameras to measure distances to surrounding objects. By knowing the positions and orientations of each camera and analyzing the disparity in the images (parallax), the distance to a specific point (the object represented by a particular pixel) can be calculated.

Characteristics of Stereo Cameras

text
Advantages of stereo cameras include the ability to obtain high-precision and high-density distance information at close range, depending on factors like camera resolution and the distance between cameras (baseline). This makes them suitable for indoor robots that require precise shape recognition of nearby objects. Additionally, stereo cameras are relatively cost-effective compared to other sensors, leading to their use in consumer products like Subaru's EyeSight system. However, stereo cameras are less effective for long-distance measurements due to a decrease in accuracy proportional to the square of the distance. They are also susceptible to environmental factors such as lighting conditions.

Ultrasonic Sensors
------------------

Ultrasonic sensors are commonly used in indoor robots and some automotive autonomous driving systems. Their features include affordability compared to laser or radar sensors, the ability to detect very close objects, and the capability to sense materials like glass, which may be challenging for lasers or cameras. However, they have limitations such as shorter maximum measurement distances and lower resolution and accuracy.

References
----------

- Wikipedia articles:

  - `Lidar Sensors <https://en.wikipedia.org/wiki/Lidar>`_
  - `Radar Sensors <https://en.wikipedia.org/wiki/Radar>`_
  - `Stereo Cameras <https://en.wikipedia.org/wiki/Stereo_camera>`_
  - `Ultrasonic Sensors <https://en.wikipedia.org/wiki/Ultrasonic_transducer>`_

---

## File: docs/modules/12_appendix/internal_sensors_main.rst

.. _`Internal Sensors for Robots`:

Internal Sensors for Robots
============================

This project, `PythonRobotics`, focuses on algorithms, so hardware is not included. However, having basic knowledge of hardware in robotics is also important for understanding algorithms. Therefore, we will provide an overview.

Introduction
-------------

In robotic systems, internal sensors play a crucial role in monitoring the robot’s internal state, such as orientation, acceleration, angular velocity, altitude, and temperature. These sensors provide essential feedback that supports control, localization, and safety mechanisms. While external sensors perceive the environment, internal sensors give the robot self-awareness of its own motion and condition. Understanding the principles and characteristics of these sensors is vital to fully utilize their information within algorithms and decision-making systems. This section outlines the main internal sensors used in robotics.

Global Navigation Satellite System (GNSS)
-----------------------------------------

GNSS is a satellite-based navigation system that provides global positioning and time information by analyzing signals from multiple satellites. It is commonly used in outdoor environments for absolute positioning. Although GNSS offers global coverage without infrastructure dependency, its performance is limited indoors or in obstructed areas, and it suffers from low update rates and susceptibility to signal noise. It is widely used in outdoor navigation for drones, vehicles, and delivery robots.

Gyroscope
----------

A gyroscope measures angular velocity around the robot’s axes, enabling orientation and attitude estimation through detection of the Coriolis effect. Gyroscopes are compact, cost-effective, and provide high update rates, but they are prone to drift and require calibration or sensor fusion for long-term accuracy. These sensors are essential in drones, balancing robots, and IMU-based systems for motion tracking.

Accelerometer
---------------

An accelerometer measures linear acceleration along one or more axes, typically by detecting mass displacement due to motion. It is small, affordable, and useful for detecting movement, tilt, or vibration. However, accelerometers are limited by noisy output and cannot independently determine position without integration and fusion. They are commonly applied in wearable robotics, step counters, and vibration sensing.

Magnetometer
--------------

A magnetometer measures the direction and intensity of magnetic fields, enabling heading estimation based on Earth’s magnetic field. It is lightweight and useful for orientation, especially in GPS-denied environments, though it is sensitive to interference from electronics and requires calibration. Magnetometers are often used in conjunction with IMUs for navigation and directional awareness.

Inertial Measurement Unit (IMU)
--------------------------------

An IMU integrates a gyroscope, accelerometer, and sometimes a magnetometer to estimate a robot's motion and orientation through sensor fusion techniques such as Kalman filters. IMUs are compact and provide high-frequency data, which is essential for localization and navigation in GPS-denied areas. Nonetheless, they accumulate drift over time and require complex filtering to maintain accuracy. IMUs are found in drones, mobile robots, and motion tracking systems.

Pressure Sensor
----------------

Pressure sensors detect atmospheric or fluid pressure by measuring the force exerted on a diaphragm. They are effective for estimating altitude and monitoring environmental conditions, especially in drones and underwater robots. Although cost-effective and efficient, their accuracy may degrade due to temperature variation and limitations in low-altitude resolution.

Temperature Sensor
--------------------

Temperature sensors monitor environmental or internal component temperatures, using changes in resistance or voltage depending on sensor type (e.g., RTD or thermocouple). They are simple and reliable for safety and thermal regulation, though they may respond slowly and be affected by nearby electronics. Common applications include battery and motor monitoring, safety systems, and ambient sensing.

References
----------

- *Introduction to Autonomous Mobile Robots*, Roland Siegwart, Illah Nourbakhsh, Davide Scaramuzza
- *Probabilistic Robotics*, Sebastian Thrun, Wolfram Burgard, Dieter Fox
- Wikipedia articles:

  - `Inertial Measurement Unit (IMU) <https://en.wikipedia.org/wiki/Inertial_measurement_unit>`_
  - `Accelerometer <https://en.wikipedia.org/wiki/Accelerometer>`_
  - `Gyroscope <https://en.wikipedia.org/wiki/Gyroscope>`_
  - `Magnetometer <https://en.wikipedia.org/wiki/Magnetometer>`_
  - `Pressure sensor <https://en.wikipedia.org/wiki/Pressure_sensor>`_
  - `Temperature sensor <https://en.wikipedia.org/wiki/Thermometer>`_
- `Adafruit Sensor Guides <https://learn.adafruit.com/>`_

---

## File: docs/modules/12_appendix/Kalmanfilter_basics_2_main.rst

KF Basics - Part 2
------------------

Probabilistic Generative Laws
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

1st Law:
^^^^^^^^

The belief representing the state :math:`x_{t}`, is conditioned on all
past states, measurements and controls. This can be shown mathematically
by the conditional probability shown below:

.. math:: p(x_{t} | x_{0:t-1},z_{1:t-1},u_{1:t})

1) :math:`z_{t}` represents the **measurement**

2) :math:`u_{t}` the **motion command**

3) :math:`x_{t}` the **state** (can be the position, velocity, etc) of
   the robot or its environment at time t.

‘If we know the state :math:`x_{t-1}` and :math:`u_{t}`, then knowing
the states :math:`x_{0:t-2}`, :math:`z_{1:t-1}` becomes immaterial
through the property of **conditional independence**’. The state
:math:`x_{t-1}` introduces a conditional independence between
:math:`x_{t}` and :math:`z_{1:t-1}`, :math:`u_{1:t-1}`

Therefore the law now holds as:

.. math:: p(x_{t} | x_{0:t-1},z_{1:t-1},u_{1:t})=p(x_{t} | x_{t-1},u_{t})

2nd Law:
^^^^^^^^

If :math:`x_{t}` is complete, then:

.. math:: p(z_{t} | x-_{0:t},z_{1:t-1},u_{1:t})=p(z_{t} | x_{t})

:math:`x_{t}` is **complete** means that the past states, controls or
measurements carry no additional information to predict future.

:math:`x_{0:t-1}`, :math:`z_{1:t-1}` and :math:`u_{1:t}` are
**conditionally independent** of :math:`z_{t}` given :math:`x_{t}` of
complete.

The filter works in two parts:

:math:`p(x_{t} | x_{t-1},u_{t})` -> **State Transition Probability**

:math:`p(z_{t} | x_{t})` -> **Measurement Probability**

Conditional dependence and independence example:

:math:\bullet\ Independent but conditionally dependent

Let’s say you flip two fair coins

A - Your first coin flip is heads

B - Your second coin flip is heads

C - Your first two flips were the same

A and B here are independent. However, A and B are conditionally
dependent given C, since if you know C then your first coin flip will
inform the other one.

:math:\bullet\ Dependent but conditionally independent

A box contains two coins: a regular coin and one fake two-headed coin
((P(H)=1). I choose a coin at random and toss it twice. Define the
following events.

A= First coin toss results in an H.

B= Second coin toss results in an H.

C= Coin 1 (regular) has been selected.

If we know A has occurred (i.e., the first coin toss has resulted in
heads), we would guess that it is more likely that we have chosen Coin 2
than Coin 1. This in turn increases the conditional probability that B
occurs. This suggests that A and B are not independent. On the other
hand, given C (Coin 1 is selected), A and B are independent.

Bayes Rule:

text
Posterior =

.. math:: \frac{Likelihood*Prior}{Marginal} 

Here,

**Posterior** = Probability of an event occurring based on certain
evidence.

**Likelihood** = How probable is the evidence given the event.

**Prior** = Probability of the just the event occurring without having
any evidence.

**Marginal** = Probability of the evidence given all the instances of
events possible.

Example:

1% of women have breast cancer (and therefore 99% do not). 80% of
mammograms detect breast cancer when it is there (and therefore 20% miss
it). 9.6% of mammograms detect breast cancer when its not there (and
therefore 90.4% correctly return a negative result).

We can turn the process above into an equation, which is Bayes Theorem.
Here is the equation:

:math:`\displaystyle{\Pr(\mathrm{A}|\mathrm{X}) = \frac{\Pr(\mathrm{X}|\mathrm{A})\Pr(\mathrm{A})}{\Pr(\mathrm{X|A})\Pr(\mathrm{A})+ \Pr(\mathrm{X | not \ A})\Pr(\mathrm{not \ A})}}`

:math:`\bullet`\ Pr(A|X) = Chance of having cancer (A) given a positive
test (X). This is what we want to know: How likely is it to have cancer
with a positive result? In our case it was 7.8%.

:math:`\bullet`\ Pr(X|A) = Chance of a positive test (X) given that you
had cancer (A). This is the chance of a true positive, 80% in our case.

:math:`\bullet`\ Pr(A) = Chance of having cancer (1%).

:math:`\bullet`\ Pr(not A) = Chance of not having cancer (99%).

:math:`\bullet`\ Pr(X|not A) = Chance of a positive test (X) given that
you didn’t have cancer (~A). This is a false positive, 9.6% in our case.

Bayes Filter Algorithm

The basic filter algorithm is:

for all :math:x_{t}:

  1. :math:\overline{bel}(x_t) = \int p(x_t | u_t, x_{t-1}) bel(x_{t-1})dx

  2. :math:bel(x_t) = \eta p(z_t | x_t) \overline{bel}(x_t)

end.

:math:\rightarrow\ The first step in filter is to calculate the prior
for the next step that uses the bayes theorem. This is the
Prediction step. The belief, :math:\overline{bel}(x_t), is
before incorporating measurement(\ :math:z_{t}) at time t=t. This
is the step where the motion occurs and information is lost because the
means and covariances of the gaussians are added. The RHS of the
equation incorporates the law of total probability for prior
calculation.

:math:\rightarrow This is the Correction or update step that
calculates the belief of the robot after taking into account the
measurement(\ :math:z_{t}) at time t=t. This is where we incorporate
the sensor information for the whereabouts of the robot. We gain
information here as the gaussians get multiplied here. (Multiplication
of gaussian values allows the resultant to lie in between these numbers
and the resultant covariance is smaller.

Bayes filter localization example:

text
.. code-block:: ipython3

    from IPython.display import Image
    Image(filename="bayes_filter.png",width=400)




.. image:: Kalmanfilter_basics_2_files/Kalmanfilter_basics_2_5_0.png
   :width: 400px



Given - A robot with a sensor to detect doorways along a hallway. Also,
the robot knows how the hallway looks like but doesn’t know where it is
in the map.

1. Initially(first scenario), it doesn’t know where it is with respect
   to the map and hence the belief assigns equal probability to each
   location in the map.

2. The first sensor reading is incorporated and it shows the presence of
   a door. Now the robot knows how the map looks like but cannot
   localize yet as map has 3 doors present. Therefore it assigns equal
   probability to each door present.

3. The robot now moves forward. This is the prediction step and the
   motion causes the robot to lose some of the information and hence the
   variance of the gaussians increase (diagram 4.). The final belief is
   **convolution** of posterior from previous step and the current state
   after motion. Also, the means shift on the right due to the motion.

4. Again, incorporating the measurement, the sensor senses a door and
   this time too the possibility of door is equal for the three door.
   This is where the filter’s magic kicks in. For the final belief
   (diagram 5.), the posterior calculated after sensing is mixed or
   **convolution** of previous posterior and measurement. It improves
   the robot’s belief at location near to the second door. The variance
   **decreases** and **peaks**.

5. Finally after series of iterations of motion and correction, the
   robot is able to localize itself with respect to the
   environment.(diagram 6.)

Do note that the robot knows the map but doesn’t know where exactly it
is on the map.

Bayes and Kalman filter structure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The basic structure and the concept remains the same as bayes filter for
Kalman. The only key difference is the mathematical representation of
Kalman filter. The Kalman filter is nothing but a bayesian filter that
uses Gaussians.

For a bayes filter to be a Kalman filter, **each term of belief is now a
gaussian**, unlike histograms. The basic formulation for the **bayes
filter** algorithm is:

.. math::

   \begin{aligned} 
   \bar {\mathbf x} &= \mathbf x \ast f_{\mathbf x}(\bullet)\, \, &\text{Prediction} \\
   \mathbf x &= \mathcal L \cdot \bar{\mathbf x}\, \, &\text{Correction}
   \end{aligned}

:math:`\bar{\mathbf x}` is the *prior*

:math:`\mathcal L` is the *likelihood* of a measurement given the prior
:math:`\bar{\mathbf x}`

:math:`f_{\mathbf x}(\bullet)` is the *process model* or the gaussian
term that helps predict the next state like velocity to track position
or acceleration.

:math:`\ast` denotes *convolution*.

Kalman Gain
~~~~~~~~~~~

.. math::  x = (\mathcal L \bar x)

Where x is posterior and :math:`\mathcal L` and :math:`\bar x` are
gaussians.

Therefore the mean of the posterior is given by:

.. math::


   \mu=\frac{\bar\sigma^2\, \mu_z + \sigma_z^2 \, \bar\mu} {\bar\sigma^2 + \sigma_z^2}

.. math:: \mu = \left( \frac{\bar\sigma^2}{\bar\sigma^2 + \sigma_z^2}\right) \mu_z + \left(\frac{\sigma_z^2}{\bar\sigma^2 + \sigma_z^2}\right)\bar\mu

In this form it is easy to see that we are scaling the measurement and
the prior by weights:

.. math:: \mu = W_1 \mu_z + W_2 \bar\mu

The weights sum to one because the denominator is a normalization term.
We introduce a new term, :math:`K=W_1`, giving us:

.. math::

   \begin{aligned}
   \mu &= K \mu_z + (1-K) \bar\mu\\
   &= \bar\mu + K(\mu_z - \bar\mu)
   \end{aligned}

where

.. math:: K = \frac {\bar\sigma^2}{\bar\sigma^2 + \sigma_z^2}

The variance in terms of the Kalman gain:

.. math::

   \begin{aligned}
   \sigma^2 &= \frac{\bar\sigma^2 \sigma_z^2 } {\bar\sigma^2 + \sigma_z^2} \\
   &= K\sigma_z^2 \\
   &= (1-K)\bar\sigma^2 
   \end{aligned}

:math:`K` is the *Kalman gain*. It’s the crux of the Kalman filter. It
is a scaling term that chooses a value partway between :math:`\mu_z` and
:math:`\bar\mu`.

Kalman Filter - Univariate and Multivariate

\ Prediction\

:math:\begin{array}{|l|l|l|} \hline \text{Univariate} & \text{Univariate} & \text{Multivariate}\\ & \text{(Kalman form)} & \\ \hline \bar \mu = \mu + \mu_{f_x} & \bar x = x + dx & \bar{\mathbf x} = \mathbf{Fx} + \mathbf{Bu}\\ \bar\sigma^2 = \sigma_x^2 + \sigma_{f_x}^2 & \bar P = P + Q & \bar{\mathbf P} = \mathbf{FPF}^\mathsf T + \mathbf Q \\ \hline \end{array}

:math:\mathbf x,\, \mathbf P are the state mean and covariance. They
correspond to :math:x and :math:\sigma^2.

:math:\mathbf F is the state transition function. When multiplied by
:math:\bf x it computes the prior.

:math:\mathbf Q is the process covariance. It corresponds to
:math:\sigma^2_{f_x}.

:math:\mathbf B and :math:\mathbf u are model control inputs to the
system.

\ Correction\

:math:\begin{array}{|l|l|l|} \hline \text{Univariate} & \text{Univariate} & \text{Multivariate}\\ & \text{(Kalman form)} & \\ \hline & y = z - \bar x & \mathbf y = \mathbf z - \mathbf{H\bar x} \\ & K = \frac{\bar P}{\bar P+R}& \mathbf K = \mathbf{\bar{P}H}^\mathsf T (\mathbf{H\bar{P}H}^\mathsf T + \mathbf R)^{-1} \\ \mu=\frac{\bar\sigma^2\, \mu_z + \sigma_z^2 \, \bar\mu} {\bar\sigma^2 + \sigma_z^2} & x = \bar x + Ky & \mathbf x = \bar{\mathbf x} + \mathbf{Ky} \\ \sigma^2 = \frac{\sigma_1^2\sigma_2^2}{\sigma_1^2+\sigma_2^2} & P = (1-K)\bar P & \mathbf P = (\mathbf I - \mathbf{KH})\mathbf{\bar{P}} \\ \hline \end{array}

:math:\mathbf H is the measurement function.

:math:\mathbf z,\, \mathbf R are the measurement mean and noise
covariance. They correspond to :math:z and :math:\sigma_z^2 in the
univariate filter. :math:\mathbf y and :math:\mathbf K are the
residual and Kalman gain.

The details will be different than the univariate filter because these
are vectors and matrices, but the concepts are exactly the same:

  • Use a Gaussian to represent our estimate of the state and error
  • Use a Gaussian to represent the measurement and its error
  • Use a Gaussian to represent the process model
  • Use the process model to predict the next state (the prior)
  • Form an estimate part way between the measurement and the prior

Reference

text
1. Roger Labbe’s
   `repo <https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python>`__
   on Kalman Filters. (Majority of text in the notes are from this)

2. Probabilistic Robotics by Sebastian Thrun, Wolfram Burgard and Dieter
   Fox, MIT Press.

---

## File: docs/modules/12_appendix/Kalmanfilter_basics_main.rst

KF Basics - Part I
------------------

Introduction

What is the need to describe belief in terms of PDF’s?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

This is because robot environments are stochastic. A robot environment
may have cows with Tesla by side. That is a robot and it’s environment
cannot be deterministically modelled(e.g as a function of something like
time t). In the real world sensors are also error prone, and hence
there’ll be a set of values with a mean and variance that it can take.
Hence, we always have to model around some mean and variances
associated.

What is Expectation of a Random Variables?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expectation is nothing but an average of the probabilites

.. math:: \mathbb E[X] = \sum_{i=1}^n p_ix_i

In the continous form,

.. math:: \mathbb E[X] = \int_{-\infty}^\infty x, f(x) ,dx

.. code-block:: ipython3

text
import numpy as np
import random
x=[3,1,2]
p=[0.1,0.3,0.4]
E_x=np.sum(np.multiply(x,p))
print(E_x)

.. parsed-literal::

text
1.4000000000000001

What is the advantage of representing the belief as a unimodal as opposed to multimodal?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Obviously, it makes sense because we can’t multiple probabilities to a
car moving for two locations. This would be too confusing and the
information will not be useful.

Variance, Covariance and Correlation

text
Variance
^^^^^^^^

Variance is the spread of the data. The mean doesn’t tell much **about**
the data. Therefore the variance tells us about the **story** about the
data meaning the spread of the data.

.. math:: \mathit{VAR}(X) = \frac{1}{n}\sum_{i=1}^n (x_i - \mu)^2

.. code-block:: ipython3

    x=np.random.randn(10)
    np.var(x)




.. parsed-literal::

    1.0224618077401504



Covariance
^^^^^^^^^^

This is for a multivariate distribution. For example, a robot in 2-D
space can take values in both x and y. To describe them, a normal
distribution with mean in both x and y is needed.

For a multivariate distribution, mean :math:`\mu` can be represented as
a matrix,

.. math::


   \mu = \begin{bmatrix}\mu_1\\\mu_2\\ \vdots \\\mu_n\end{bmatrix}

Similarly, variance can also be represented.

But an important concept is that in the same way as every variable or
dimension has a variation in its values, it is also possible that there
will be values on how they **together vary**. This is also a measure of
how two datasets are related to each other or **correlation**.

For example, as height increases weight also generally increases. These
variables are correlated. They are positively correlated because as one
variable gets larger so does the other.

We use a **covariance matrix** to denote covariances of a multivariate
normal distribution:

.. math::


   \Sigma = \begin{bmatrix}
     \sigma_1^2 & \sigma_{12} & \cdots & \sigma_{1n} \\
     \sigma_{21} &\sigma_2^2 & \cdots & \sigma_{2n} \\
     \vdots  & \vdots  & \ddots & \vdots  \\
     \sigma_{n1} & \sigma_{n2} & \cdots & \sigma_n^2
    \end{bmatrix}

**Diagonal** - Variance of each variable associated.

**Off-Diagonal** - covariance between ith and jth variables.

.. math::

   \begin{aligned}VAR(X) = \sigma_x^2 &=  \frac{1}{n}\sum_{i=1}^n(X - \mu)^2\\
   COV(X, Y) = \sigma_{xy} &= \frac{1}{n}\sum_{i=1}^n[(X-\mu_x)(Y-\mu_y)\big]\end{aligned}

.. code-block:: ipython3

    x=np.random.random((3,3))
    np.cov(x)




.. parsed-literal::

    array([[0.08868895, 0.05064471, 0.08855629],
           [0.05064471, 0.06219243, 0.11555291],
           [0.08855629, 0.11555291, 0.21534324]])



Covariance taking the data as **sample** with :math:`\frac{1}{N-1}`

.. code-block:: ipython3

    x_cor=np.random.rand(1,10)
    y_cor=np.random.rand(1,10)
    np.cov(x_cor,y_cor)




.. parsed-literal::

    array([[ 0.1571437 , -0.00766623],
           [-0.00766623,  0.13957621]])



Covariance taking the data as **population** with :math:`\frac{1}{N}`

.. code-block:: ipython3

    np.cov(x_cor,y_cor,bias=1)




.. parsed-literal::

    array([[ 0.14142933, -0.0068996 ],
           [-0.0068996 ,  0.12561859]])



Gaussians
~~~~~~~~~

Central Limit Theorem
^^^^^^^^^^^^^^^^^^^^^

According to this theorem, the average of n samples of random and
independent variables tends to follow a normal distribution as we
increase the sample size.(Generally, for n>=30)

.. code-block:: ipython3

    import matplotlib.pyplot as plt
    import random
    a=np.zeros((100,))
    for i in range(100):
        x=[random.uniform(1,10) for _ in range(1000)]
        a[i]=np.sum(x,axis=0)/1000
    plt.hist(a)




.. parsed-literal::

    (array([ 1.,  4.,  9., 12., 12., 20., 16., 16.,  4.,  6.]),
     array([5.30943011, 5.34638597, 5.38334183, 5.42029769, 5.45725355,
            5.49420941, 5.53116527, 5.56812114, 5.605077  , 5.64203286,
            5.67898872]),
     <a list of 10 Patch objects>)




.. image:: Kalmanfilter_basics_files/Kalmanfilter_basics_14_1.png


Gaussian Distribution
^^^^^^^^^^^^^^^^^^^^^

A Gaussian is a *continuous probability distribution* that is completely
described with two parameters, the mean (:math:`\mu`) and the variance
(:math:`\sigma^2`). It is defined as:

.. math::

    
   f(x, \mu, \sigma) = \frac{1}{\sigma\sqrt{2\pi}} \exp\big [{-\frac{(x-\mu)^2}{2\sigma^2} }\big ]


Range is :math:`[-\inf,\inf]`

This is just a function of mean(\ :math:`\mu`) and standard deviation
(:math:`\sigma`) and what gives the normal distribution the
charecteristic **bell curve**.

.. code-block:: ipython3

    import matplotlib.mlab as mlab
    import math
    import scipy.stats
    
    mu = 0
    variance = 5
    sigma = math.sqrt(variance)
    x = np.linspace(mu - 5*sigma, mu + 5*sigma, 100)
    plt.plot(x,scipy.stats.norm.pdf(x, mu, sigma))
    plt.show()




.. image:: Kalmanfilter_basics_files/Kalmanfilter_basics_16_0.png


Why do we need Gaussian distributions?
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Since it becomes really difficult in the real world to deal with
multimodal distribution as we cannot put the belief in two seperate
location of the robots. This becomes really confusing and in practice
impossible to comprehend. Gaussian probability distribution allows us to
drive the robots using only one mode with peak at the mean with some
variance.

Gaussian Properties
~~~~~~~~~~~~~~~~~~~

**Multiplication**

For the measurement update in a Bayes Filter, the algorithm tells us to
multiply the Prior P(X_t) and measurement P(Z_t|X_t) to calculate the
posterior:

.. math:: P(X \mid Z) = \frac{P(Z \mid X)P(X)}{P(Z)}

Here for the numerator, :math:`P(Z \mid X),P(X)` both are gaussian.

:math:`N(\bar\mu, \bar\sigma^1)` and :math:`N(\bar\mu, \bar\sigma^2)`
are their mean and variances.

New mean is

.. math:: \mu_\mathtt{new} = \frac{\sigma_z^2\bar\mu + \bar\sigma^2z}{\bar\sigma^2+\sigma_z^2}


New variance is

.. math::


   \sigma_\mathtt{new} = \frac{\sigma_z^2\bar\sigma^2}{\bar\sigma^2+\sigma_z^2}

.. code-block:: ipython3

    import matplotlib.mlab as mlab
    import math
    mu1 = 0
    variance1 = 2
    sigma = math.sqrt(variance1)
    x1 = np.linspace(mu1 - 3*sigma, mu1 + 3*sigma, 100)
    plt.plot(x1,scipy.stats.norm.pdf(x1, mu1, sigma),label='prior')
    
    mu2 = 10
    variance2 = 2
    sigma = math.sqrt(variance2)
    x2 = np.linspace(mu2 - 3*sigma, mu2 + 3*sigma, 100)
    plt.plot(x2,scipy.stats.norm.pdf(x2, mu2, sigma),"g-",label='measurement')
    
    
    mu_new=(mu1*variance2+mu2*variance1)/(variance1+variance2)
    print("New mean is at: ",mu_new)
    var_new=(variance1*variance2)/(variance1+variance2)
    print("New variance is: ",var_new)
    sigma = math.sqrt(var_new)
    x3 = np.linspace(mu_new - 3*sigma, mu_new + 3*sigma, 100)
    plt.plot(x3,scipy.stats.norm.pdf(x3, mu_new, var_new),label="posterior")
    plt.legend(loc='upper left')
    plt.xlim(-10,20)
    plt.show()



.. parsed-literal::

    New mean is at:  5.0
    New variance is:  1.0



.. image:: Kalmanfilter_basics_files/Kalmanfilter_basics_19_1.png


**Addition**

The motion step involves a case of adding up probability (Since it has
to abide the Law of Total Probability). This means their beliefs are to
be added and hence two gaussians. They are simply arithmetic additions
of the two.

.. math::

   \begin{gathered}\mu_x = \mu_p + \mu_z \\
   \sigma_x^2 = \sigma_z^2+\sigma_p^2\, \end{gathered}

.. code-block:: ipython3

    import matplotlib.mlab as mlab
    import math
    mu1 = 5
    variance1 = 1
    sigma = math.sqrt(variance1)
    x1 = np.linspace(mu1 - 3*sigma, mu1 + 3*sigma, 100)
    plt.plot(x1,scipy.stats.norm.pdf(x1, mu1, sigma),label='prior')
    
    mu2 = 10
    variance2 = 1
    sigma = math.sqrt(variance2)
    x2 = np.linspace(mu2 - 3*sigma, mu2 + 3*sigma, 100)
    plt.plot(x2,scipy.stats.norm.pdf(x2, mu2, sigma),"g-",label='measurement')
    
    
    mu_new=mu1+mu2
    print("New mean is at: ",mu_new)
    var_new=(variance1+variance2)
    print("New variance is: ",var_new)
    sigma = math.sqrt(var_new)
    x3 = np.linspace(mu_new - 3*sigma, mu_new + 3*sigma, 100)
    plt.plot(x3,scipy.stats.norm.pdf(x3, mu_new, var_new),label="posterior")
    plt.legend(loc='upper left')
    plt.xlim(-10,20)
    plt.show()


.. parsed-literal::

    New mean is at:  15
    New variance is:  2



.. image:: Kalmanfilter_basics_files/Kalmanfilter_basics_21_1.png


.. code-block:: ipython3

    #Example from:
    #https://scipython.com/blog/visualizing-the-bivariate-gaussian-distribution/
    import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import cm
    from mpl_toolkits.mplot3d import Axes3D
    
    # Our 2-dimensional distribution will be over variables X and Y
    N = 60
    X = np.linspace(-3, 3, N)
    Y = np.linspace(-3, 4, N)
    X, Y = np.meshgrid(X, Y)
    
    # Mean vector and covariance matrix
    mu = np.array([0., 1.])
    Sigma = np.array([[ 1. , -0.5], [-0.5,  1.5]])
    
    # Pack X and Y into a single 3-dimensional array
    pos = np.empty(X.shape + (2,))
    pos[:, :, 0] = X
    pos[:, :, 1] = Y
    
    def multivariate_gaussian(pos, mu, Sigma):
        """Return the multivariate Gaussian distribution on array pos.
    
        pos is an array constructed by packing the meshed arrays of variables
        x_1, x_2, x_3, ..., x_k into its _last_ dimension.
    
        """
    
        n = mu.shape[0]
        Sigma_det = np.linalg.det(Sigma)
        Sigma_inv = np.linalg.inv(Sigma)
        N = np.sqrt((2*np.pi)**n * Sigma_det)
        # This einsum call calculates (x-mu)T.Sigma-1.(x-mu) in a vectorized
        # way across all the input variables.
        fac = np.einsum('...k,kl,...l->...', pos-mu, Sigma_inv, pos-mu)
    
        return np.exp(-fac / 2) / N
    
    # The distribution on the variables X, Y packed into pos.
    Z = multivariate_gaussian(pos, mu, Sigma)
    
    # Create a surface plot and projected filled contour plot under it.
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    ax.plot_surface(X, Y, Z, rstride=3, cstride=3, linewidth=1, antialiased=True,
                    cmap=cm.viridis)
    
    cset = ax.contourf(X, Y, Z, zdir='z', offset=-0.15, cmap=cm.viridis)
    
    # Adjust the limits, ticks and view angle
    ax.set_zlim(-0.15,0.2)
    ax.set_zticks(np.linspace(0,0.2,5))
    ax.view_init(27, -21)
    
    plt.show()




.. image:: Kalmanfilter_basics_files/Kalmanfilter_basics_22_0.png


This is a 3D projection of the gaussians involved with the lower surface
showing the 2D projection of the 3D projection above. The innermost
ellipse represents the highest peak, that is the maximum probability for
a given (X,Y) value.

\*\* numpy einsum examples \*\*

.. code-block:: ipython3

    a = np.arange(25).reshape(5,5)
    b = np.arange(5)
    c = np.arange(6).reshape(2,3)
    print(a)
    print(b)
    print(c)



.. parsed-literal::

    [[ 0  1  2  3  4]
     [ 5  6  7  8  9]
     [10 11 12 13 14]
     [15 16 17 18 19]
     [20 21 22 23 24]]
    [0 1 2 3 4]
    [[0 1 2]
     [3 4 5]]


.. code-block:: ipython3

    #this is the diagonal sum, i repeated means the diagonal
    np.einsum('ij', a)
    #this takes the output ii which is the diagonal and outputs to a
    np.einsum('ii->i',a)
    #this takes in the array A represented by their axes 'ij' and  B by its only axes'j' 
    #and multiples them element wise
    np.einsum('ij,j',a, b)




.. parsed-literal::

    array([ 30,  80, 130, 180, 230])



.. code-block:: ipython3

    A = np.arange(3).reshape(3,1)
    B = np.array([[ 0,  1,  2,  3],
                  [ 4,  5,  6,  7],
                  [ 8,  9, 10, 11]])
    C=np.multiply(A,B)
    np.sum(C,axis=1)




.. parsed-literal::

    array([ 0, 22, 76])



.. code-block:: ipython3

    D = np.array([0,1,2])
    E = np.array([[ 0,  1,  2,  3],
                  [ 4,  5,  6,  7],
                  [ 8,  9, 10, 11]])
    
    np.einsum('i,ij->i',D,E)




.. parsed-literal::

    array([ 0, 22, 76])



.. code-block:: ipython3

    from scipy.stats import multivariate_normal
    x, y = np.mgrid[-5:5:.1, -5:5:.1]
    pos = np.empty(x.shape + (2,))
    pos[:, :, 0] = x; pos[:, :, 1] = y
    rv = multivariate_normal([0.5, -0.2], [[2.0, 0.9], [0.9, 0.5]])
    plt.contourf(x, y, rv.pdf(pos))
    





.. parsed-literal::

    <matplotlib.contour.QuadContourSet at 0x139196438>




.. image:: Kalmanfilter_basics_files/Kalmanfilter_basics_28_1.png


Reference
~~~~~~~~~~~

1. Roger Labbe’s
   `repo <https://github.com/rlabbe/Kalman-and-Bayesian-Filters-in-Python>`__
   on Kalman Filters. (Majority of the examples in the notes are from
   this)

2. Probabilistic Robotics by Sebastian Thrun, Wolfram Burgard and Dieter
   Fox, MIT Press.

3. Scipy
   `Documentation <https://scipython.com/blog/visualizing-the-bivariate-gaussian-distribution/>`__

---

## File: docs/modules/12_appendix/steering_motion_model_main.rst

Steering Motion Model
-----------------------

Turning radius calculation by steering motion model

The turning Radius represents the radius of the circle when the robot turns, as shown in the diagram below.

.. image:: steering_motion_model/steering_model.png

When the steering angle is tilted by :math:\delta,
the turning radius :math:R can be calculated using the following equation,
based on the geometric relationship between the wheelbase (WB),
which is the distance between the rear wheel center and the front wheel center,
and the assumption that the turning radius circle passes through the center of
the rear wheels in the diagram above.

:math:R = \frac{WB}{tan\delta}

The curvature :math:\kappa is the reciprocal of the turning radius:

:math:\kappa = \frac{tan\delta}{WB}

In the diagram above, the angular difference :math:\Delta \theta in the vehicle’s heading between two points on the turning radius :math:R
is the same as the angle of the vector connecting the two points from the center of the turn.

From the formula for the length of an arc and the radius,

:math:\Delta \theta = \frac{s}{R}

Here, :math:s is the distance between two points on the turning radius.

So, yaw rate :math:\omega can be calculated as follows.

:math:\omega = \frac{v}{R}

and

:math:\omega = v\kappa

here, :math:v is the velocity of the vehicle.

Turning radius calculation by 2 consecutive positions of the robot trajectory

text
In this section, we will derive the formula for the turning radius from 2 consecutive positions of the robot trajectory.

.. image:: steering_motion_model/turning_radius_calc1.png

As shown in the upper diagram above, the robot moves from a point at time :math:`t` to a point at time :math:`t+1`.
Each point is represented by a 2D position :math:`(x_t, y_t)` and an orientation :math:`\theta_t`.

The distance between the two points is :math:`d = \sqrt{(x_{t+1} - x_t)^2 + (y_{t+1} - y_t)^2}`.

The angle between the two vectors from the turning center to the two points is :math:`\theta = \theta_{t+1} - \theta_t`.
Here, by drawing a perpendicular line from the center of the turning radius
to a straight line of length :math:`d` connecting two points,
the following equation can be derived from the resulting right triangle.

:math:`sin\frac{\theta}{2} = \frac{d}{2R}`

So, the turning radius :math:`R` can be calculated as follows.

:math:`R = \frac{d}{2sin\frac{\theta}{2}}`

The curvature :math:`\kappa` is the reciprocal of the turning radius.
So, the curvature can be calculated as follows.

:math:`\kappa = \frac{2sin\frac{\theta}{2}}{d}`

Target speed by maximum steering speed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If the maximum steering speed is given as :math:`\dot{\delta}_{max}`,
the maximum curvature change rate :math:`\dot{\kappa}_{max}` can be calculated as follows:

:math:`\dot{\kappa}_{max} = \frac{tan\dot{\delta}_{max}}{WB}`

From the curvature calculation by 2 consecutive positions of the robot trajectory,

the maximum curvature change rate :math:`\dot{\kappa}_{max}` can be calculated as follows:

:math:`\dot{\kappa}_{max} = \frac{\kappa_{t+1}-\kappa_{t}}{\Delta t}`

If we can assume that the vehicle will not exceed the maximum curvature change rate,

the target minimum velocity :math:`v_{min}` can be calculated as follows:

:math:`v_{min} = \frac{d_{t+1}+d_{t}}{\Delta t} = \frac{d_{t+1}+d_{t}}{(\kappa_{t+1}-\kappa_{t})}\frac{tan\dot{\delta}_{max}}{WB}`


Reference
~~~~~~~~~~~

- `Vehicle Dynamics and Control <https://link.springer.com/book/10.1007/978-1-4614-1433-9>`_

## 2. Official Technical Reference & Guides (AtsushiSakai/docs)

## File: README.md

# Julia VS Code Documentation


[](https://www.julia-vscode.org/docs/stable/)
[](https://www.julia-vscode.org/docs/dev/)

This repository contains the documentation for the [julia VS Code extension](https://marketplace.visualstudio.com/items?itemName=julialang.language-julia).


## Contributing to the Documentation

In order to contribute to the documentation, simply modify the files in the `/docs/src` folder and open a PR against the `master` branch.
Once a PR has been opened, the CI will automatically run [Documenter.jl](https://github.com/JuliaDocs/Documenter.jl) and generate a new documentation against the PR.
You can see the generate documentation on `https://www.julia-vscode.org/docs/previews/PR##`, where `##` is your PR's number.


## Local build of the Documentation

Although it's not necessary, if you want to check how the new documentation pages would look like in advance of opening a PR,
you can build documentations locally and check the results generated in `docs/build` by the following step:

> from the root of this repository:

```sh
$ julia --project=docs/ -e 'using Pkg; Pkg.develop(PackageSpec(path = pwd())); Pkg.instantiate()'
$ julia --project=docs/ docs/make.jl
```

---

## File: docs/src/release-notes/v0_16.md

# Julia VS Code extension v0.16 release

Welcome to the v0.16 release of the Julia VS Code extension.

## Inline evaluation

Code evaluation has been overhauled for this release.

The `Julia: Execute *` (e.g. `Julia: Execute Code And Move`, by default accessible with `Alt-Enter`) commands now asynchronously evaluate the selection, current code block, or file. These commands also evaluate the code in the context of their containing module, which is indicated in the status bar:



You can change the module for a file by clicking on the module indicator or by calling the `Julia: Select Current Module` command (e.g. with the `Ctrl-J Ctrl-M` keybinding).

Evaluating code directly into a module is especially helpful for a Revise-less workflow -- it allows you to easily update methods or globals.

Another new feature in this release are inline results for code evaluated from the editor. To enable them, select the `inline` result type in the extension's `Julia > Execution: Result Type` setting.

Inline results display the first line of the result's plain-text representation (via `Base.show`). Details are available by hovering over the result, like in the screenshot below (make sure you have the `Editor > Hover: Enabled` setting ticked):



The inline result on the current line can be cleared with `Julia: Clear Current Inline Result` (`Ctrl-I Ctrl-D`); all inline results in the active text editor are cleared with `Julia: Clear All Inline Results` (`Ctrl-I Ctrl-C`).

We're still working on inline evaluation, inline results, and related features, so expect more changes in the following releases.

!!! note

    The `Julia: Send Current Line or Selection to REPL` command simply copies the current line or selection and pastes it into the REPL, so it does not take the code's module into account.

## Workspace view

It's now possible to inspect the current REPL's runtime state in the Workspace pane, which you can open with the `View: Show Julia Explorer` command or by clicking on the Julia symbol in the activity bar.

You can drill down into modules and find out what they define, inspect complex `struct`s or dictionaries, and even inspect arbitrarily large arrays:



All of this lazy-loaded, so don't worry about recursive types or big arrays -- we're taking great care not to influence the editor's performance or mess with Julia's GC.

Supported types like arrays (or other table-like objects) and plots can be shown in the integrated table viewer or plot pane with the click of a button.

## Faster REPL startup

All code the extension loads into the REPL process is now in a (precompiled) module, which should speed up startup.

## Bugfixes

We've also fixed a plethora of issues in the Language Server and the interactive code.

---

## File: docs/src/release-notes/v0_17.md

# Julia VS Code extension v0.17 release

Welcome to the v0.17 release of the Julia VS Code extension.

## Global variable support in the debugger variable explorer

The Variables pane in the integrated debugger now supports global variables:



`Globals` shows global variables used in the current function's scope, whereas `Global (...)` shows all global variables defined in the current scope's enclosing module. All of these can be edited, but be vary of unexpected behaviour.

## Debug and run buttons above Julia files

We added a run and debug button above the file editor area when you open a Julia file that makes it easier to run the currently active file:



## Support for step in targets in the debugger

VSCode now supports a new "Step into Targets" debugger UI, which you can access by opening the context menu in an editor while debugging. It will allow you to step into a function call on the current line:



## Profile viewing support

We added new functionality that allows you to save profiling results in a `*.cpuprofile` file. Recent versions of VS Code have built-in support to analyze and visualize these profiling result files.

The easiest way to use this functionality is the `@profview` macro, which executes your code under the Julia profiler and then saves the results as a `*.cpuprofile` file. To use it, start a Julia REPL, and then call `@profview` like this:

```julia
julia> @profview my_function_that_should_be_profiled()
```

Alternatively you can call the function `view_profile` in the REPL which will save the profiling results from the previous profiling session.

Once you have created a `*.cpuprofile` file with one of these methods, you can open it in VS Code. The initial view shown is a tabular display of the profiling results. The search bar at the top allows you to filter the profiling results and provides a rich [query language](https://github.com/microsoft/vscode-js-profile-visualizer/blob/master/query-language.md) for more advanced queries:



You can also look at the same information in a flamegraph by clicking the little fire button next to the search area. The first time you do so, you will be asked to install another extension, go ahead and do that. The flamegraph visualization then looks like this:



Special thanks to [Valentin](https://github.com/vchuravy) and [Connor](https://github.com/connor4312) for their help with this feature!

## Inline result styling improvements

The stylings of inline result bubbles are greatly improved;
the left-border color will make it easier to distinguish them from the actual code:

- in dark theme ([Iceberg](https://marketplace.visualstudio.com/items?itemName=harg.iceberg)): 
- in light theme ([Night Owl Light](https://marketplace.visualstudio.com/items?itemName=sdras.night-owl)): 

## Stackframe highlighting for inline evaluations

Now our inline execution will highlight all lines in backtrace when you hit an error:


If you hover your mouse over any of the lines, the detail of the error will show up and you can goto whichever frame in a backtrace by clicking the links:


Additionally, the topmost buttons allow you to go to first/previous/next/last frames, and clear all the traces:


We also added the improvement to cut off the julia-vscode extension's internal lines from backtrace so that they won't appear in users' stackframe.

## Configuration option to exclude folders from linting

We've added an new configuration option `julia.lint.disabledDirs`.
It specifies sub-directories in [a package directory](https://docs.julialang.org/en/v1/manual/code-loading/#Package-directories-1) where only basic linting is enabled,
and this will drastically lowers the chance for false positives.
By default, `test` and `docs` are set, so most problematic errors (e.g. "Missing reference ...") will be suppressed there.

Note this config is only a temporary fix for our environment handling --
we will work on redesigning the whole environment handling, which will hopefully get rid of most of those false positives,
and then we may probably remove this configuration in the future.

## Extension API

This extension now exports an API, which can be consumed by other VSCode extensions (see the [VSCode docs](https://code.visualstudio.com/api/references/vscode-api#extensions) for more info). Version 1 of the API exports the `async getJuliaPath()` and `async getEnvironment()` functions.

## Bugfixes

We've also fixed a plethora of issues across the entire extension.

---

## File: docs/src/userguide/codenavigation.md

# Code Navigation

## Quick File Navigation

> **Tip**: Hold `Ctrl+P` **(Quick Open)** to open any file by its name.

There are two commands which enable you to navigate in and across files with easy-to-use key bindings. 

- Hold `Ctrl` and press `Tab` to view a list of all files open in an editor group. 
- To open one of these files, press `Tab` again to pick the file you want to navigate to, then release `Ctrl` to open it.



Alternatively, you can use `Alt+Left` and `Alt+Right` to navigate between files and edit locations. 
If you are jumping around between different lines of the same file, these shortcuts allow you to navigate between those locations easily.

## Open Package Directory

### Go to Definition

You can go to the definition of a symbol by pressing `F12`.

If you press `Ctrl` and hover over a symbol, a preview of the declaration will appear:



> **Tip**: You can jump to the definition with `Ctrl+Click` or open the definition to the side with `Ctrl+Alt+Click`.

### Go to Symbols

You can navigate symbols inside a file with `Ctrl+Shift+O`. 
Enter `:` and the symbols will be grouped by category. 
Press `Up` or `Down` and navigate to the place you want.



### Open Symbol by Name

You can open a particular symbol by its name with `Ctrl+T`, regardless of which file contains it. Type the name of a symbol you want to navigate to, and press `Enter`

---

## File: docs/src/userguide/compilesysimage.md

# Compiling Sysimages

The Julia VS Code extension enables you to compile a custom sysimage for your Julia environments. The extension automatically uses sysimages for the current environment when it starts a new Julia REPL in VS Code. 

Custom sysimages can significantly reduce load times for packages. 

## Creating a sysimage for the active environment

The Julia extension adds a build task for custom sysimages to the Visual Studio Code task system. To run the build task, select the command `Tasks: Run Build Task` and then select `Julia: Build custom sysimage for current environment`. 

The build task creates a sysimage that includes all packages in the current Julia environment. This sysimage is saved in the same folder where the `Project.toml` and `Manifest.toml` of the current Julia environment are stored. The name of the sysimage file will be `JuliaSysimage.dll` (Windows) or `JuliaSysimage.so`. 

## Using custom sysimages

When the Julia extension starts a new Julia REPL process, the extension first looks for a custom sysimage file for the current Julia environment. The extension then verifies whether the custom sysimage file is up to date by comparing the last modified date of the sysimage file to the last modified date of the `Manifest.toml` file. If an up to date sysimage file exists, the Julia extension automatically starts the Julia REPL process with the custom sysimage loaded.

---

## File: docs/src/userguide/debugging.md

# Debugging

The Julia extension supports debugging of all types of Julia programs and applications.  

For general debugging features such as inspecting variables, setting breakpoints, and other activities that aren't language-dependent, review [VS Code debugging](https://code.visualstudio.com/docs/editor/debugging).  

## Example
1. Open a Julia file in VS Code. 
2. Click the **Run** button. You will now see the default debugger start panel:
3. Click **Run and Debug** (or select `F5`) to run the active Julia fila in the debugger. Output is displayed in the Julia Debug terminal. 



In this example the whole program ran through in one go and finished without any problem. Lets make this example a bit more useful by setting a breakpoint on `line 11`. We do this by simple clicking with the mouse in the left most column of the code editor:



The red dot shows us that we have now set a breakpoint. Next we start the program again (either by clicking on `Run and Debug` or pressing `F5`). When the program reaches `line 11`, it will pause:



The yellow line shows us the location that we will execute next if we continue to run the program. We can also see where we are in the call stack and a list of all breakpoints. At the top of the text editor we now see a toolbar with commands for common debug actions: `Continue`, `Step over`, `Step Into`, `Step Out` etc. Lets click once on `Step Over` and then `Step Into`. We are now paused on the first line of the bar function:



The `Variables` view now shows us what local variables we have in this function and what their current values are. As we step through the program, and eventually reach the end of the bar function, the list of local variables gets longer, i.e. we now also see the values for `c` and `d`:



Let us set another breakpoint on `line 15` and then continue the program until it hits that breakpoint. Then we click on `Debug Console` and see a view like this:



In this view we can evaluate arbitrary Julia code in the context of the current function. For example, we can compute the log of x by running `log(x)` here:



We can also change the value of any local variable while the program is paused. For example, to change the value of x, we can double click in the `Variables` section on the value 27 next to x and then enter any arbitrary Julia expression. The value this expression returns will become the new value for the variable x. In the following example We changed the value of x to a string:



This concludes the very basic walk through. I’ll now want to highlight some other features.

### Ways to start the debugger

There are two different ways to start the debugger.
- The first you already learned in the walk through: you run a Julia file in the debugger.
- The second allows you to debug code in the interactive REPL.

### Running Julia files

In our example we started the currently active Julia file in the debugger. This is the most basic way to start debugging, but there are many more options that you can configure in a VS Code launch.json file. Examples include setting a fixed Julia file as the startup file, configuring command line arguments etc. The launch.json functionality is described in more detail in the VS Code debugger documentation.

### Debugging code from the REPL

You can also start the debugger from the REPL. In that situation the debugger will attach to the already running REPL. To start such a debug session you use two macros in the REPL: the `@enter` and `@run` macro. Both are very simple: they will start the debugger on the code that was passed to the macro. The `@run` macro will run the code until a breakpoint is hit, while the `@enter` macro will pause the debugger on the first line of the code. For example, you can start debugging the println function from the REPL by entering `@enter println("Test")`

### Breakpoints

You already learned how you can easily set breakpoints in the source code itself. There are two more options for breakpoints:
function breakpoints and condition on breakpoints.

### Function breakpoints

If you click on the little `+` sign in the `BREAKPOINTS` view, you can add a function breakpoint. Simply enter the name of the function you want to break on. You can also configure it to only break on specific methods by specifying a signature like `foo(::String, ::Number)`.

### Breakpoint conditions

If you click with the right mouse onto a breakpoint in the editor, you can select an option `Edit breakpoint...`, and then you can add a condition on the breakpoint. You can enter any valid Julia expression that returns a `Bool` value here. You have of course full access to all local variables in this expression.

### Variables viewer

Composite variables, arrays and dictionaries have full tree drill down support in the variables viewer:



### Watch section
The watch section allows you to enter arbitrary Julia expressions that are evaluated whenever the program pauses and the result is shown:



### Call stack

The call stack section allows you to look at the content of any stack frame, i.e. when you click on a different function there it will show the local variables for the selected stack frame. You can also restart code execution at any stack frame by clicking the small restart icon next to a given entry here:



Note that this last feature can be quite brittle, in particular if your functions modify any global state.

### Exceptions

If your code throws an exception, you get a nice exception view:

You can also configure the behavior of the debugger in the face of exceptions in the BREAKPOINTS part of the UI.

### Compile mode

The breakpoints view has another option called Enable compile mode:



**The functionality of this option is the following:**

If you select this option, breakpoints that are set in any function that is called from the current stack frame will no longer pause code execution. In our example, if you have paused in function foo and then select this option, a breakpoint in bar would no longer pause execution. Breakpoints in foo would still pause the debugger.

**Why would you ever want to use this feature?**

Your code will run a lot faster with this option enabled.

### Troubleshooting
If you encounter any issue when using the debugger, Please do let us know about it over at the [Julia VS Code](https://github.com/julia-vscode/julia-vscode) repository.