Tag Archives: BerryIMU

BerryIMU Python Code Update – Kalman Filter and More

We have updated to the python code in our git repo.

It now includes;

  • The elusive Kalman filter.
  • Math needed when the IMU is upside down
  • Automatically calculate loop period.
  • A lot more comments.

What is a Kalman filter?  In a nutshell;
A Kalman filter is, it is an algorithm which uses a series of measurements observed over time, in this context an accelerometer and a gyroscope. These measurements will contain noise that will contribute to the error of the measurement. The Kalman filter will then try to estimate the state of the system, based on the current and previous states, that tend to be more precise that than the measurements alone.

A Kalman filter is more precise than a Complementary filter. This can be seen in the image below, which is the output of a complementary filter (CFangleX) and a Kalman filter (kalmanX) from the X axis plotted in a graph.

The red line (KalmanX) is better at filtering out noisep;

Python Kalman filter Raspberry Pi

 

The code can be found here in our Git repository here
And  can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone https://github.com/mwilliams03/BerryIMU.git

BerryIMU Raspberry Pi Gyroscope Accelerometer

A summary of the code;


def kalmanFilterY ( accAngle, gyroRate, DT):
        y=0.0
        S=0.0

        global KFangleY
        global Q_angle
        global Q_gyro
        global y_bias
        global XP_00
        global XP_01
        global XP_10
        global XP_11
        global YP_00
        global YP_01
        global YP_10
        global YP_11

        KFangleY = KFangleY + DT * (gyroRate - y_bias)

        YP_00 = YP_00 + ( - DT * (YP_10 + YP_01) + Q_angle * DT )
        YP_01 = YP_01 + ( - DT * YP_11 )
        YP_10 = YP_10 + ( - DT * YP_11 )
        YP_11 = YP_11 + ( + Q_gyro * DT )

        y = accAngle - KFangleY
        S = YP_00 + R_angle
        K_0 = YP_00 / S
        K_1 = YP_10 / S

        KFangleY = KFangleY + ( K_0 * y )
        y_bias = y_bias + ( K_1 * y )

        YP_00 = YP_00 - ( K_0 * YP_00 )
        YP_01 = YP_01 - ( K_0 * YP_01 )
        YP_10 = YP_10 - ( K_1 * YP_00 )
        YP_11 = YP_11 - ( K_1 * YP_01 )

        return KFangleY

BerryIMU code for Arduino – Accelerometer, Gyroscope and Magnetometer

Our GIT repository has been updated with an Arduino sketch which calculates angles using a complementary filter.  The heading is also calculated using the magnetometer.

 BerryIMU Arduino

 

BerryIMU Arduino Wiring

 

 

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

Detailed Guides and Tutorials

In this order;
Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi
Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi – Kalman Filter
Create a Digital Compass with the Raspberry Pi – Part 1 – “The Basics”
Create a Digital Compass with the Raspberry Pi – Part 2 – “Tilt Compensation”
Create a Digital Compass with the Raspberry Pi – Part 3 – “Calibration”

 

These cover both BerryIMU and displaying graphics with SDL on a Raspberry Pi;
How to Create an Inclinometer using a Raspberry Pi and an IMU

BerryIMU Python Code now Includes Tilt Compensation

The python code for BerryIMU now includes tilt compensation for the compass heading.

Code

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone https://github.com/mwilliams03/BerryIMU.git

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

Tilt Compensation

Without tilt compensation the heading (magnetometer reading) will be skewed when the IMU is tilted.

The below graph below shows a magnetometer (or compass)being held at 200 degrees and being tilted in various directions. The blue line is the raw heading, the orange line is the heading after applying tilt compensation. As you can see, without tilt compensation the heading will change if the compass is tilted.

Compass Tilt Compensation

More information about tilt compensation can be found in this post

 

 

Digital Compass with the Raspberry Pi – Part 4- “Smartphone Replica”

y

This guide shows how to use a BerryIMU and a small TFT to create a digital compass, similar to those that can be found on smartphones.

  • The TFT used in this guide is a PiScreen
  • The IMU is a BerryIMU – Magnetometer,  gyroscope, accelerometer and pressure sensor
  • SDL is used to display the output to the TFT
  • Tilt compensations is used
  • A low pass filter is used to reduce noise
  • Compass calibration is needed

If you don’t a small TFT like the PiScreen, you can still use this guide to display the output to a monitor via HDMI.

 

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone http://github.com/mwilliams03/BerryIMU.

 

The code for this guide can be found under the compass_tutorial04_graphical_output directory.

 

Prerequisites for this guide;

  • A working TFT (not covered in this guide)
  • A working magnetometer (compass) with tilt compensation. A guide can be found here
  • Understand how to perform Hard Iron calibration. A guide can be found here

We will be covering some basic SDL which will be used to produce our graphics.

 

The IMU used in this guide is the BerryIMU. However, other IMUs or accelerometers and gyroscopes can be used.. Eg Pololu MinIMU, Adafruit IMU and Sparkfun IMUs

Continue reading Digital Compass with the Raspberry Pi – Part 4- “Smartphone Replica”

Python Code for BerryIMU – Accelerometer, Gyroscope, Magnetometer & Pressure Sensor

We have updated our git repository with python code for the BerryIMU.

This is specific for the BerryIMU, however the math and code can be applied to any digital IMU, just some minor modifications need to be made. E.g  Pololu MinIMU, Adafruit IMU and Sparkfun IMUs

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone http://github.com/mwilliams03/BerryIMU.git

 

We have left the code as simple as it can be to make it easier to understand.

The code currently performs angle measurements using the gyroscope and accelerometer , which are fused using a complementary filter. The heading is also calculated using the magnetometer, without tilt compensation.
BerryIMU Raspberry Pi Gyroscope Accelerometer
To view pressure;

pi@raspberrypi ~ $ sudo python berryIMU.py

To view pressure;

pi@raspberrypi ~ $ sudo python bmp180.py

Detailed Guides and Tutorials

In this order;
Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi – Kalman Filter
Create a Digital Compass with the Raspberry Pi – Part 1 – “The Basics”
Create a Digital Compass with the Raspberry Pi – Part 2 – “Tilt Compensation”
Create a Digital Compass with the Raspberry Pi – Part 3 – “Calibration”
Create a Digital Compass with the Raspberry Pi – Part 4- “Smartphone Replica”
Converting values from an Accelerometer to Gs – “ Proper Acceleration”

How to Create an Inclinometer using a Raspberry Pi and an IMU
Raspberry Pi Digital Spirit Level

Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi – Kalman Filter

In this guide we will go over some very basics on the use of a Kalman filter for sensor fusion. There is some very complex math involved which is well over my head, however we do have some working code and very good reference sites.

A prerequisite for this guide is to have a gyro and accelerometer from an IMU already up and running on your Raspberry Pi. A guide to interfacing an IMU with a Raspberry Pi can be found here.

 

Git repository here
The code can be pulled down to your Raspberry Pi with;

pi@raspberrypi ~ $ git clone https://github.com/mwilliams03/BerryIMU.git

 

The code for this guide can be found under the gyro_accelerometer_tutorial03_kalman_filter directory.

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

 

Kalman Filter

The Kalman filter, also known as linear quadratic estimation (LQE), is an algorithm that uses a series of measurements observed over time, containing noise (random variations) and other inaccuracies, and produces estimates of unknown variables that tend to be more precise than those based on a single measurement alone. More formally, the Kalman filter operates recursively on streams of noisy input data to produce a statistically optimal estimate of the underlying system state. The filter is named for Rudolf (Rudy) E. Kálmán, one of the primary developers of its theory.

 

The Kalman filter has numerous applications in technology. A common application is for guidance, navigation and control of vehicles, particularly aircraft and spacecraft. Furthermore, the Kalman filter is a widely applied concept in time series analysis used in fields such as signal processing and econometric.

FYI: A Kalman filter was used to assist with navigation in the Apollo 11 moon landing.

If we had to explain it in one sentence what a Kalman filter is: “It’s a method of predicting the future state of a system based on the previous ones.”

Continue reading Guide to interfacing a Gyro and Accelerometer with a Raspberry Pi – Kalman Filter