Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

I2C Drivers for MPU-9250, MPU-9255, MPU-9150, MPU-6500, MPU-6555, and MPU-6050

License

NotificationsYou must be signed in to change notification settings

jefmenegazzo/mpu-i2c-drivers-python

Repository files navigation

MPU-9250

🙌 Usedonations to help support your projects! 🙌


Project Status: Active – The project has reached a stable, usable state and is being actively developed.MaintenanceLicense: CC BY-NC-ND 4.0DonateGitHub tag (latest by date)pypiversionsDOIGitHub issuesGitHub forksGitHub starsGitHub watchersGitHub contributorsHitCountPyPI - Downloads

MPU-9250 (MPU-6500 + AK8963) I2C Driver in Python

MPU-9250 is a multi-chip module (MCM) consisting of two dies integrated into a single QFN package. One die theMPU-6500 houses the 3-Axis gyroscope, the 3-Axis accelerometer and temperature sensor. The other die houses theAK8963 3-Axis magnetometer. Hence, the MPU-9250 is a 9-axis MotionTracking device that combines a 3-axis gyroscope, 3-axis accelerometer, 3-axis magnetometer and a Digital Motion Processor™ (DMP). The hardware documentation for MPU-9250 can be found atProduct Specification andRegister Map and Descriptions.

Table of Contents

Instalation

To install via pip use:

pip install mpu9250-jmdev

To use the package for development purposes use:

git clone https://github.com/Intelligent-Vehicle-Perception/MPU-9250-Sensors-Data-Collect.git MPU9250cd MPU9250pip install -e.pip install -r requirements.txt

Known Issues

ℹ️ Notice that this package requires Python 3.6 or higher.

⚠️ If you have both Python 2 and 3 installed on your machine, usepip3 to install andpython3 to run instead.

⚠️ If you run your python source-code withsudo, remember to usesudo withpip install commands as well.

⚠️ If you get zero values and the messageUsing Fake SMBus, thesmbus2 requirement has not been installed correctly. Try installing manually with the commandpip install -r requirements.txt orpython -mpip install smbus2.

❗ Any other problem or questions,open an issue in this repositoryclicking here, do not send me an e-mail!

How To Use

With I2C Bus, you can use the MPU-9250 in two ways: simple mode or advanced mode. The example source-codes are inexamples folder.

Simple Mode - Master Only

In this mode, the MPU-9250 connects directly to Raspberry GPIOs. There are two physical addresses available for the MPU-9250, being 0x68 and 0x69. Therefore, on each I2C Bus you can have up to two MPU-9250 connected. The connection between GPIOs and MPU-9250 is as follows:

MPU9250RaspberryNote
VDD3.3VOn some models of the MPU-9250 5V can be used.
AD03.3VIf used, the MPU-9250's address is changed to 0x69. Otherwise, the address is 0x68.
GNDGND
SDASDA
SCLSCL

Below simple code to test the execution with never ending loop:

importtimefrommpu9250_jmdev.registersimport*frommpu9250_jmdev.mpu_9250importMPU9250mpu=MPU9250(address_ak=AK8963_ADDRESS,address_mpu_master=MPU9050_ADDRESS_68,# In 0x68 Addressaddress_mpu_slave=None,bus=1,gfs=GFS_1000,afs=AFS_8G,mfs=AK8963_BIT_16,mode=AK8963_MODE_C100HZ)mpu.configure()# Apply the settings to the registers.whileTrue:print("|.....MPU9250 in 0x68 Address.....|")print("Accelerometer",mpu.readAccelerometerMaster())print("Gyroscope",mpu.readGyroscopeMaster())print("Magnetometer",mpu.readMagnetometerMaster())print("Temperature",mpu.readTemperatureMaster())print("\n")time.sleep(1)

Advanced Mode - Master-Slave

If you want to have more than two MPU-9250 on one I2C Bus, you must use Master-Slave mode. In this case, first configure the MPU-9250 according to the previous section, they will be used as Master. To configure the MPU-9250 Slaves, connect as follows:

MPU9250 SlaveMPU9250 MasterRaspberry PINote
VDD3.3VOn some models of the MPU-9250 5V can be used.
AD03.3VIf used, the MPU-9250's address is changed to 0x69. Otherwise, the address is 0x68.
GNDGND
SDAEDA
SCLECL

This way you will have an MPU-9250 Master connecting SDA and SLC directly to the GPIO in Raspberry PI, and an MPU-9250 Slave connecting SDA and SLC to the EDA and ELC in MPU-9250 Master.

Below simple code to test the execution with never ending loop:

importtimefrommpu9250_jmdev.registersimport*frommpu9250_jmdev.mpu_9250importMPU9250mpu=MPU9250(address_ak=AK8963_ADDRESS,address_mpu_master=MPU9050_ADDRESS_68,# Master has 0x68 Addressaddress_mpu_slave=MPU9050_ADDRESS_68,# Slave has 0x68 Addressbus=1,gfs=GFS_1000,afs=AFS_8G,mfs=AK8963_BIT_16,mode=AK8963_MODE_C100HZ)mpu.configure()# Apply the settings to the registers.whileTrue:print("|.....MPU9250 in 0x68 I2C Bus - Master.....|")print("Accelerometer",mpu.readAccelerometerMaster())print("Gyroscope",mpu.readGyroscopeMaster())print("Magnetometer",mpu.readMagnetometerMaster())print("Temperature",mpu.readTemperatureMaster())print("\n")print("|.....MPU9250 in 0x68 I2C Bus - Slave in 0x68 auxiliary sensor address.....|")print("Accelerometer",mpu.readAccelerometerSlave())print("Gyroscope",mpu.readGyroscopeSlave())print("Temperature",mpu.readTemperatureSlave())print("\n")time.sleep(1)

Getting Data

All sensors and measurement units of the MPU-9250 are described below:

SensorUnit
Accelerometerg (1g = 9.80665 m/s²)
Gyroscopedegrees per second (°/s)
Magnetometermicrotesla (μT)
Temperaturecelsius degrees (°C)

Before read the sensor data, make sure that you have executed the command:

mpu.configure()# Apply the settings to the registers.

Reading Accelerometer

The accelerometer measures acceleration in three axes (X, Y, Z). To read your data, use the commands:

masterData=mpu.readAccelerometerMaster()slaveData=mpu.readAccelerometerSlave()# If there is a slave

Reading Gyroscope

The gyroscope measures rotation rate in three axes (X, Y, Z). To read your data, use the commands:

masterData=mpu.readGyroscopeMaster()slaveData=mpu.readGyroscopeSlave()# If there is a slave

Reading Magnetometer

The magnetometer measures geomagnetic field in three axes (X, Y, Z). To read your data, use the command:

masterData=mpu.readMagnetometerMaster()

When used in Simple Mode (Master Only), the magnetometer will be available on the I2C Bus with address 0x0C. When in Advanced Mode (Master-Slave), the magnetometer will also behave as a slave, and address 0x0C will not appear on the I2C Bus, acting as an auxiliary sensor.

Reading Temperature

The temperature sensor measures data in Celsius degrees. To read your data, use the command:

masterData=mpu.readTemperatureMaster()slaveData=mpu.readTemperatureSlave()# If there is a slave

Reading All Data

If you want to read data from all sensors (master and slave) at the same time, use the commands below (useful for saving to csv):

labels=mpu.getAllDataLabels()# return labels with data description for each array positiondata=mpu.getAllData()# returns a array with data from all sensors

Reading All Settings

If you want to read settings (biases, resolutions, scale factors) from all sensors (master and slave) at the same time, use the commands below (useful for saving to csv):

labels=mpu.getAllSettingsLabels()# return labels with settings description for each array positiondata=mpu.getAllSettings()# returns a array with settings from all sensors

Calibrating Sensors

This library has functions ready for calibration accelerometer, gyroscope and magnetometer sensors. To calibrate all sensors at once, use the command:

mpu.calibrate()# Calibrate sensorsmpu.configure()# The calibration function resets the sensors, so you need to reconfigure them

Accelerometer and Gyroscope

To calibrate the accelerometer and gyroscope sensors, make sure that the sensors remain fixed and stationary. Align the accelerometer's Z axis with gravity, i.e., gravity (1g) should only appear on the sensor's Z axis (place the sensor in a flat place). To perform calibration run the command:

mpu.calibrateMPU6500()# Calibrate sensorsmpu.configure()# The calibration function resets the sensors, so you need to reconfigure themabias=mpu.abias# Get the master accelerometer biasesabias_slave=mpu.abias_slave# Get the slave accelerometer biasesgbias=mpu.gbias# Get the master gyroscope biasesgbias_slave=mpu.gbias_slave# Get the slave gyroscope biases

The biases are programmatically applied to the sensor data. Therefore, when reading the sensor data, the biases will be applied internally, returning corrected data. If you have calculated the biases of these sensors once, and want the controller to use them, simply parameterize as follows:

mpu.abias= [0,0,0]# Set the master accelerometer biasesmpu.abias_slave= [0,0,0]# Set the slave accelerometer biasesmpu.gbias= [0,0,0]# Set the master gyroscope biasesmpu.gbias_slave= [0,0,0]# Set the slave gyroscope biases

Magnetometer

To perform calibration run the command:

mpu.calibrateAK8963()# Calibrate sensorsmpu.configure()# The calibration function resets the sensors, so you need to reconfigure themmagScale=mpu.magScale# Get magnetometer soft iron distortionmbias=mpu.mbias# Get magnetometer hard iron distortion

If you have calculated the biases of these sensor once, and want the controller to use them, simply parameterize as follows:

mpu.magScale= [0,0,0]# Set magnetometer soft iron distortionmpu.mbias= [0,0,0]# Set magnetometer hard iron distortion

Reset Registers

If you want to reset the values in all registers of all sensors in all MPU-9250, execute the command below:

mpu.reset()# Reset sensorsmpu.configure()# After resetting you need to reconfigure the sensors

Final Notes

The foldermpu9250 consist of the high level library. The folderexamples contains files with basic execution and threaded examples.

Sponsors and Donations

This project does not have any funding. To help maintain the project, consider making a donation 🙌.

paypal

What We're Working on Now

New features are currently under development:

  • V2: new major version will be released soon, much more robust than the current one. It will supports several MPU models, such as 9250, 9150, 6050, 6500.
  • Complete support documentation for configuring RPi, I2C, VNC, SSH, etc.

How To Cite

To cite this repository, use the reference below:

@software{menegazzo3960441,author ={Jeferson Menegazzo and Aldo von Wangenheim},title ={{MPU-9250 Sensors Data Collect}},month = jul,year =2020,publisher ={Zenodo},version ={1.0.12},doi ={10.5281/zenodo.3960441},url ={https://github.com/Intelligent-Vehicle-Perception/MPU-9250-Sensors-Data-Collect/}}

License

This project is under Attribution-NonCommercial-NoDerivatives 4.0 International License (CC BY-NC-ND 4.0). Please seeLicense File for more information.


[8]ページ先頭

©2009-2025 Movatter.jp