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;
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.
To view pressure;
To view pressure;
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]
Perfect timing. Using the BerryIMU for my Picorder project and was using someone else’s Python library to interface with the IMU. Now I can use yours 🙂
Hi Micheal
In its current state, the code runs fine and gives accurate results.
However, as you start to add more code the loop will take longer to run… resulting in the gyro drifting.
Easy fix is to have this value;
LP = 0.041 # Loop period = 41ms. This needs to match the time it takes each loop to run
match the current the actual loop time. this is printed to the console when the code is running.
There is also a sleep value you can remove… or play with. It is only there to try and make the output easier to read and to try and get a constant loop time.
time.sleep(0.03)
With my C implementation, I am able to have this all worked out automatically. I have yet to do it with Python. Python isn’t one of my strong points 🙁
Works lovely. Just need to integrate it with my Picorder now 🙂 Such a lot of sensor for something the size of my thumb!
Not sure if anyone else had this problem but I had to run this to get the python code to work with a vanilla install of raspbian:
sudo apt-get install python-smbus
I’ve turned your code into a class for ease-of-use 🙂
I know its been alllllloooooong time, but fo you still have that class, i am not grest with python but wuld rather use it as a class!
Is anyone else having issues with the gyro’s figures constantly accumulating with the code provided? Nothing else is, just the Gyro’s figures… Thanks!
This is normal. This drift is a result of noise in the readings in the gyro. All gyros exhibit this behavior. Even gyros found in smartphones.
All sensors are noisy. E.g. gyro, magnetometer, pressure, temperature, etc..
A Gyro is noisy and will drift over long periods of time, but is very,very accurate for short periods.
An accelerometer is also noisy, but has very good angle measurement for extended periods of time.
This is why a complementary or Karman filter is used, to fuse both values to get a very accurate result.
In the above code, the figures with the accurate values are CFangleX(Complimentary Filter AngleX) CFangleY(Complimentary Filter AngleY) and heading.
This post covers it in more detail;
https://ozzmaker.com/berryimu/
Regarding the gryo, It is less of an issue when using C, as python is an interpreted scripting language, it is hard to run the loops at high speed and at a constant time period. Each loop time in my python code completes at a different time. If someone knows away around this, I would be happy to test it out.
Brilliant! Thanks for the reply! It makes much more sense now!
Little question, is it working for example if the BerryIMU is positionned vertically i.e in a plane containing z axis ? If not, what needs to be modified ?
TIA