All Projects → niru-5 → imusensor

niru-5 / imusensor

Licence: MIT License
Python library for communication between raspberry pi and MPU9250 imu

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to imusensor

MPU-9250-Sensors-Data-Collect
MPU9250 (MPU6500 + AK8963) I2C Driver in Python for Raspbery PI
Stars: ✭ 51 (+8.51%)
Mutual labels:  gyroscope, accelerometer, mpu9250
pymetawear
Community developed SDK around the Python bindings for the C++ SDK
Stars: ✭ 42 (-10.64%)
Mutual labels:  gyroscope, accelerometer, imu
hoverboard-sideboard-hack-GD
Hoverboard sideboard hack for GD32 boards
Stars: ✭ 68 (+44.68%)
Mutual labels:  gyroscope, accelerometer, imu
CodeDroneDIY
The most simple, but working, quadricopter flight controller from scratch, using Arduino Uno/Nano.
Stars: ✭ 68 (+44.68%)
Mutual labels:  gyroscope, accelerometer, imu
Balance-Bot
A two-wheel self-balancing robot based on the ATmega2560 micro-controller.
Stars: ✭ 33 (-29.79%)
Mutual labels:  gyroscope, accelerometer, imu
ios logger
Application for camera and sensor data logging (iOS)
Stars: ✭ 60 (+27.66%)
Mutual labels:  gyroscope, accelerometer, imu
SlimeVR-Server
Server app for SlimeVR ecosystem
Stars: ✭ 361 (+668.09%)
Mutual labels:  imu, imu-sensor
ESP32 IMU BARO GPS VARIO
GPS altimeter/variometer with LCD display, routes with waypoints, data/gps track logging, bluetooth NMEA sentence transmission, wifi AP + webpage configuration
Stars: ✭ 72 (+53.19%)
Mutual labels:  imu, kalman-filter
MPU9250 asukiaaa
A library for arduino to read value of MPU9250.
Stars: ✭ 69 (+46.81%)
Mutual labels:  accelerometer, mpu9250
BetterJoyForDolphin
Allows the Nintendo Switch Pro Controller and Joycons to be used with the Dolphin Emulator
Stars: ✭ 44 (-6.38%)
Mutual labels:  gyroscope, accelerometer
DeepLIO
Deep Lidar Inertial Odometry
Stars: ✭ 46 (-2.13%)
Mutual labels:  imu, imu-sensor
GY-85
Arduino implementation for GY-85 (ADXL345 accelerometer, ITG3200 gyroscope and HMC5883L magnetometer)
Stars: ✭ 63 (+34.04%)
Mutual labels:  gyroscope, accelerometer
Linux-System-Info-Webpage
Material Design Dashboard for Linux System Info. Great for RPi and other linux Distros
Stars: ✭ 19 (-59.57%)
Mutual labels:  rpi, raspberry-pi-3
COVID-away
Repo of paper title 'Avoid touching your face: A hand-to-face 3d motion dataset (covid-away) and trained models for smartwatches'
Stars: ✭ 18 (-61.7%)
Mutual labels:  gyroscope, accelerometer
dana
DANA: Dimension-Adaptive Neural Architecture (UbiComp'21)( ACM IMWUT)
Stars: ✭ 28 (-40.43%)
Mutual labels:  gyroscope, accelerometer
Embedded UKF Library
A compact Unscented Kalman Filter (UKF) library for Teensy4/Arduino system (or any real time embedded system in general)
Stars: ✭ 31 (-34.04%)
Mutual labels:  imu, kalman-filter
Face-Detection-and-Tracking
Computer Vision model to detect face in the first frame of a video and to continue tracking it in the rest of the video. This is implemented in OpenCV 3.3.0 and Python 2.7
Stars: ✭ 24 (-48.94%)
Mutual labels:  kalman-filter, kalman
LaunchPadFlightController
TM4C123G based Flight Controller
Stars: ✭ 62 (+31.91%)
Mutual labels:  accelerometer, imu
Rpi gpio
Ruby conversion of RPi.GPIO Python module
Stars: ✭ 185 (+293.62%)
Mutual labels:  rpi, raspberry-pi-3
CtrlUI
CtrlUI (Controller User Interface) is a Windows application, game and emulator launcher for your game controller, DirectXInput converts your game controller to a Xbox (XInput) controller, Fps Overlayer is a tool that shows the frames per second and the cpu, gpu and memory information.
Stars: ✭ 39 (-17.02%)
Mutual labels:  gyroscope, accelerometer

Update

Adding blog links to provide more information about the math behind the code.
IMU Intro - It gives an introduction into IMU working and the math behind calibration and basic idea behind finding roll, pitch and yaw.
Sensor Fusion - This blog goes into math behind kalman filter, Madgwick filter and how they are applied here.
Hands-on Intro - A general overview of getting started.

imusensor

The repo provides a bridge between MPU9250 and raspberry pi. It also lists various caliberation code and filters for getting an accurate orientation from MPU9250 This repo mostly concentrates on the problem of connecting IMU(MPU9250) to raspberry pi through I2C communication.

Pre-requisites

Some of the requirements are to enable I2C in rpi. Installing I2C tools and smbus

sudo apt-get install i2c-tools
sudo pip install smbus

Connect the MPU9250 with rpi using the below connections

Rpi pin MPU9250 pins
pin 3 -> SDA pin
pin 5 -> SCL pin
pin 6 -> Ground(GND)
pin 1 -> VCC

After you have made the connections, type the following command -

sudo i2cdetect -y 1

If you see 68 in the output, then that means the sensor is connected to the rpi and 68 is the address of the sensor.

Basic Usage

The below code is a basic starter for the library

import os
import sys
import time
import smbus

from imusensor.MPU9250 import MPU9250

address = 0x68
bus = smbus.SMBus(1)
imu = MPU9250.MPU9250(bus, address)
imu.begin()
# imu.caliberateGyro()
# imu.caliberateAccelerometer()
# or load your own caliberation file
#imu.loadCalibDataFromFile("/home/pi/calib_real_bolder.json")

while True:
	imu.readSensor()
	imu.computeOrientation()

	print ("roll: {0} ; pitch : {1} ; yaw : {2}".format(imu.roll, imu.pitch, imu.yaw))
	time.sleep(0.1)

Other Functionalities

Setting Accelerometer Range

The accelerometer in MPU9250 has the following ranges of +-2g, +-4g, +-8g and +-16g
You can set this setting by the below command

imu.setAccelRange("AccelRangeSelect2G")

Simiarly for 4g use "AccelRangeSelect4G" and follow similary for 8g and 16g ranges.

Setting Gyroscope Range

Gyroscope sensor in MPU9250 has the following ranges +-250DPS, +-500DPS, +-1000DPS and +-2000DPS
You can set this setting by the below command

imu.setGyroRange("GyroRangeSelect250DPS")

Simiarly for 500DPS use "GyroRangeSelect500DPS" and follow similary for 1000DPS and 2000DPS ranges.
Note: DPS means degrees per second

Setting internal low pass filter frequency

The sensor has an internal low pass filter to remove some basic noise in the values generated by accelerometer and gyrscope.
Use the following command

imu.setLowPassFilterFrequency("AccelLowPassFilter184")
frequency str
5Hz AccelLowPassFilter5
10Hz AccelLowPassFilter10
20Hz AccelLowPassFilter20
41Hz AccelLowPassFilter41
92Hz AccelLowPassFilter92
184Hz AccelLowPassFilter184

Gyroscope Caliberation

Though most sensors are caliberated during manufacturing, however, there still could be a need for caliberation due to various cahnges like being soldered to a breakout board. Gyroscope normally comes with a bias. This can be found by averaging the values when it is kept still and then subtract those values to get the appropriate values.

imu.caliberateGyro()

This will calculate the bias and it is stored in imu.GyroBias You can also set its value, but make sure you give 3x1 numpy array.

Accelerometer Caliberation

This caliberation includes an extra parameter called scale apart from bias. Use the below command

imu.caliberateAccelerometer()

The above function will store the scale and bias in the following variables imu.Accels and imu.AccelBias respectively.

Magnometer Caliberation

This has two types of caliberation

  • imu.caliberateMagApprox() : As the name suggests, this is a near approximation of scale and bias parameters. It saves time however, might not be always accurate. In this the scale and bias are stored in imu.Mags and imu.MagBias respectively.
  • imu.caliberateMagPrecise() : It tries to fit the data to an ellipsoid and is more complicated and time consuming. It gives a 3x3 symmetric transformation matrix(imu.Magtransform) instead of a common 3x1 scale values. The bias variable is imu.MagBias
    For more details on this, have a look at mag_caliberation folder in examples.

IMU Orientation

The computed orientation is in terms of eurler angles. roll for x axis, pitch for y axis and yaw for z axis. We use NED format which basically means, the sensor's x-axis is aligned with north, sensor's y-axis is aligned with east and sensor's x-axis is aligned with down. imu.computeOrientation() The roll, pitch and yaw can be accessed by imu.roll, imu.pitch and imu.yaw. Note: The euler angles will only make sense when all the sensors are properly caliberated.

Filters for sensorfusion

Orientation from accelerometer and magnetometer are noisy, while estimating orientation from gyroscope is noise free but accumulates drift over time. We will combining both of these to obtain more stable orientation. There are multiple ways to do it and we have given two options of kalman and madgwick. You are free to write your own algorithms.

Kalman

It uses gyroscope to estimate the new state. Accelerometer and magnetometer provide the new measured state. The kalman filter aims to find a corrected state from the above two by assuming that both are forms of gaussian distributions. look at kalmanExample.py in examples

import os
import sys
import time
import smbus
import numpy as np

from imusensor.MPU9250 import MPU9250
from imusensor.filters import kalman 

address = 0x68
bus = smbus.SMBus(1)
imu = MPU9250.MPU9250(bus, address)
imu.begin()
# imu.caliberateAccelerometer()
# print ("Acceleration calib successful")
# imu.caliberateMag()
# print ("Mag calib successful")
# or load your caliberation file
# imu.loadCalibDataFromFile("/home/pi/calib_real_bolder.json")

sensorfusion = kalman.Kalman()

imu.readSensor()
imu.computeOrientation()
sensorfusion.roll = imu.roll
sensorfusion.pitch = imu.pitch
sensorfusion.yaw = imu.yaw

count = 0
currTime = time.time()
while True:
	imu.readSensor()
	imu.computeOrientation()
	newTime = time.time()
	dt = newTime - currTime
	currTime = newTime

	sensorfusion.computeAndUpdateRollPitchYaw(imu.AccelVals[0], imu.AccelVals[1], imu.AccelVals[2], imu.GyroVals[0], imu.GyroVals[1], imu.GyroVals[2],\
												imu.MagVals[0], imu.MagVals[1], imu.MagVals[2], dt)

	print("Kalmanroll:{0} KalmanPitch:{1} KalmanYaw:{2} ".format(sensorfusion.roll, sensorfusion.pitch, sensorfusion.yaw))

	time.sleep(0.01)

Madgwick

This is slightly better than kalman and more smooth in giving out the orientation. However, for this to work properly, the sensor fusion needs to run at least 10 times faster frequency than the sensor sampling frequency. look at madgwickExample.py in examples

import os
import sys
import time
import smbus


from imusensor.MPU9250 import MPU9250
from imusensor.filters import madgwick

sensorfusion = madgwick.Madgwick(0.5)

address = 0x68
bus = smbus.SMBus(1)
imu = MPU9250.MPU9250(bus, address)
imu.begin()

# imu.caliberateGyro()
# imu.caliberateAccelerometer()
# or load your own caliberation file
#imu.loadCalibDataFromFile("/home/pi/calib_real4.json")

currTime = time.time()
print_count = 0
while True:
	imu.readSensor()
	for i in range(10):
		newTime = time.time()
		dt = newTime - currTime
		currTime = newTime

		sensorfusion.updateRollPitchYaw(imu.AccelVals[0], imu.AccelVals[1], imu.AccelVals[2], imu.GyroVals[0], \
									imu.GyroVals[1], imu.GyroVals[2], imu.MagVals[0], imu.MagVals[1], imu.MagVals[2], dt)

	if print_count == 2:
		print ("mad roll: {0} ; mad pitch : {1} ; mad yaw : {2}".format(sensorfusion.roll, sensorfusion.pitch, sensorfusion.yaw))
		print_count = 0

	print_count = print_count + 1
	time.sleep(0.01)

For the detailed explanation -> link

Filter comparison

We have also done a small filter comparison of all the filters. This data can be streamed to your computer using zmq and also you can visualize the imu orientation using pygame_viz.py in examples/filters_comparison.

Acknowledgments

Most of the documentation for interfacing MPU9250 with arduino is present. Our work has been inspired by the following works.

  1. bolderflight/MPU9250: This is a nice library for interfacing MPU9250 with arduino.
  2. kriswiner/MPU9250: This is a library for getting some accurate orientation from MPU9250. The author has answered a lot of questions in the issues and most of them are very enlightening for anybody working with IMUs. Highly recommend it.
  3. TKJElectronics/KalmanFilter : This is an implementation of second order kalman filter for IMU when using with arduino.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].