All posts by Mark Williams

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/ozzmaker/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

 

 

Add Colour to Text in Python

To make some of your text more readable, you can use ANSI escape codes to change the colour of the text output in your python program. A good use case for this is to to highlight errors.

The escape codes are entered right into the print statement.

print("\033[1;32;40m Bright Green  \n")

 

The above ANSI escape code will set the text colour to bright green. The format is;
\033[  Escape code, this is always the same
1 = Style, 1 for normal.
32 = Text colour, 32 for bright green.
40m = Background colour, 40 is for black.

 

This table shows some of the available formats;

Text colorCodeText styleCodeBackground colorCode
Black30No effect0Black40
Red31Bold1Red41
Green32Underline2Green42
Yellow33Negative13Yellow43
Blue34Negative25Blue44
Purple35Purple45
Cyan36Cyan46
White37White47

 

 

BerryIMU Raspberry Pi Gyroscope Accelerometer

 

 

Here is the code used to create the coloured text in the title image;

print("\033[0;37;40m Normal text\n")
print("\033[2;37;40m Underlined text\033[0;37;40m \n")
print("\033[1;37;40m Bright Colour\033[0;37;40m \n")
print("\033[3;37;40m Negative Colour\033[0;37;40m \n")
print("\033[5;37;40m Negative Colour\033[0;37;40m\n")
print("\033[1;37;40m \033[2;37:40m TextColour BlackBackground          TextColour GreyBackground                WhiteText ColouredBackground\033[0;37;40m\n")
print("\033[1;30;40m Dark Gray      \033[0m 1;30;40m            \033[0;30;47m Black      \033[0m 0;30;47m               \033[0;37;41m Black      \033[0m 0;37;41m")
print("\033[1;31;40m Bright Red     \033[0m 1;31;40m            \033[0;31;47m Red        \033[0m 0;31;47m               \033[0;37;42m Black      \033[0m 0;37;42m")
print("\033[1;32;40m Bright Green   \033[0m 1;32;40m            \033[0;32;47m Green      \033[0m 0;32;47m               \033[0;37;43m Black      \033[0m 0;37;43m")
print("\033[1;33;40m Yellow         \033[0m 1;33;40m            \033[0;33;47m Brown      \033[0m 0;33;47m               \033[0;37;44m Black      \033[0m 0;37;44m")
print("\033[1;34;40m Bright Blue    \033[0m 1;34;40m            \033[0;34;47m Blue       \033[0m 0;34;47m               \033[0;37;45m Black      \033[0m 0;37;45m")
print("\033[1;35;40m Bright Magenta \033[0m 1;35;40m            \033[0;35;47m Magenta    \033[0m 0;35;47m               \033[0;37;46m Black      \033[0m 0;37;46m")
print("\033[1;36;40m Bright Cyan    \033[0m 1;36;40m            \033[0;36;47m Cyan       \033[0m 0;36;47m               \033[0;37;47m Black      \033[0m 0;37;47m")
print("\033[1;37;40m White          \033[0m 1;37;40m            \033[0;37;40m Light Grey \033[0m 0;37;40m               \033[0;37;48m Black      \033[0m 0;37;48m")
\n")

 

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/ozzmaker/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/ozzmaker/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

[wp_ad_camp_4]

Camera Pan and Tilt control with TFT and Touchscreen

Here is an example of how to use a TFT screen to control the pan and tilt of a Raspberry Pi camera.

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

pi@raspberrypi ~ $ git clone http://github.com/mwilliams03/pan-tilt-touchscreen.git

 

There are a number of elements in place to get this working;

  • Detecting input events on the touchscreen.
  • The use of double buffering for the framebuffer.
  • Using fbcp(framebuffer copy) to copy camera image to back buffer. fbcp source has been integrated into the code above.
  • Updating back buffer with text and buttons.
  • Forking the actual process that starts recording.
  • Create a unique file name fore each recording.
  • Software PWM to control servers. (ServoBlaster)

 

The code has been well documented, so I will only cover imported snippets below.

Drawing buttons

panTiltInterface

The function below is used to draw the buttons and slider outlines to the display.

void drawButton(int x, int y, int w, int h, char *text, int backgroundColor, int foregroundColor);

x & y are the top left coordinates of the button.
w is width.
h is height.

Continue reading Camera Pan and Tilt control with TFT and Touchscreen

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/ozzmaker/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

Create a Digital Compass with the Raspberry Pi – Part 3 – “Calibration”

Calibrating a magnetometer can improve the accuracy of the magnetometer readings.
All ferromagnetic materials will cause skew in the results of the measurements take by a magnetometer. This distortion falls into two categories, hard or soft iron distortion.

We can also further fine tune our readings by including the magnetic declination of our current location.

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

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

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

BerryIMU Raspberry Pi Gyroscope Accelerometer

Hard Iron Distortion

Hard iron distortions are created by objects that produce a magnetic field. A speaker or piece of magnetized iron for example will cause a hard iron distortion. If the piece of magnetic material is physically attached to the same PCB as the magnetometer, then this type of hard iron distortion will cause a permanent bias in the sensor output.

Soft Iron Distortion

Soft iron distortions are considered alterations in the existing magnetic field, this is usually caused by ferromagnetic materials around the sensor as well as the Earth's magnetic field (it is different in different locations).

Unfortunately, there is no easy way to calibrate for soft iron distortion and every time the sensor is moved to a new location, the calibration has to be done again.   Most people only apply hard iron calibration as this calibration usually stays constant.

Plotting Your Readings

An easy way to check to see if your sensor needs calibration is to plot the pairwise data from the raw X and Y readings.

The image below was created using Wolfram Mathematica on a Raspberry Pi. The data used was from an uncalibrated compass as.  Take note of the ellipsoid shape and how the center is not centered on the X,Y axis.

Compass uncalibrated
Not-Calibrated Compass

Continue reading Create a Digital Compass with the Raspberry Pi – Part 3 – “Calibration”

Enable boot logging on the Raspberry Pi

When troubleshooting issues on a Raspberry Pi sometimes it is helpful to go back and look at the boot log, especially if you are running a headless (no monitor) Raspberry Pi.

Install bootlogd

 

pi@raspberrypi ~ $ sudo apt-get install bootlogd

 

You will be asked to restart services, select 'Yes'.  And then reboot your Raspberry Pi

View Boot Log

From now on, if you wish to view the bootlog, you can use the command below to format it correctly;

pi@raspberrypi ~ $ sed 's/\^\[/\o33/g;s/\[1G\[/\[27G\[/' /var/log/boot

You will get the output as shown in the image at the top of this post.

 

 

[wp_ad_camp_3]

Create a Digital Compass with the Raspberry Pi - Part 2 - "Tilt Compensation"

This part 2 of a multi part series on how to use a digital compass(magnetometer) with your Raspberry Pi.

Part 1 can be found here and is a prerequisite to part 2.

Code for this guide

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

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

 

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

Calculating Compass Heading

Part 1 covered how to get the heading from the magnetometer, however this is only reliable when the magnetometer is on a flat surface.  If the magnetometer is tilted, the heading will skew and not be correct.

 

The chart below shows a 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

 

Continue reading Create a Digital Compass with the Raspberry Pi - Part 2 - "Tilt Compensation"

How to Create an Inclinometer using a Raspberry Pi and an IMU

This guide covers how to use an Inertial Measurement Unit (IMU) with a Raspberry Pi to create an inclinometer, just like the type you will find in a 4WD.

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.

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..

Continue reading How to Create an Inclinometer using a Raspberry Pi and an IMU