Hello World!
My name is Jeferson Menegazzo. I am a Machine Learning and Artificial Intelligence Engineer, as well as a Senior Software Engineer.
I2C Drivers for MPU-9250, MPU-9255, MPU-9150, MPU-6500, MPU-6555, and MPU-6050
Home Page: https://jefmenegazzo.github.io/mpu-i2c-drivers-python/
License: Other
Hello World!
My name is Jeferson Menegazzo. I am a Machine Learning and Artificial Intelligence Engineer, as well as a Senior Software Engineer.
only x68 shows up,
acceleration / velocity work fine running master-slave-mode.py
raspberry pi 3b, latest OS (Jan 2021) just loaded ran the install scripts as desired from the instructions.
running master-only-mode.py
gets
File "/usr/local/lib/python3.7/dist-packages/mpu9250_jmdev/mpu_9250.py", line 1012, in writeAK
self.bus.write_byte_data(self.address_ak, register, value)
File "/usr/local/lib/python3.7/dist-packages/smbus2/smbus2.py", line 434, in write_byte_data
ioctl(self.fd, I2C_SMBUS, msg)
OSError: [Errno 121] Remote I/O error
thread sampling - run.py gets the same I/O error.
VCC 5v, GND, , SCL / SDA connected
I am trying to debug why the magnetic sensor of my MPU9250/6500 won't connect to my Raspberry Pi 4+, but all the other sensors will. I did a bad soldering job on my first one of these sensors and it just wouldn't connect. I got two new ones though and both of them are detected and give the 0x68 address and work except for the magnetic sensor (if I comment out self.configureAK8963(self.mfs, self.mode)
, I don't get any errors).
So. my question is, is it possible I just have two bad solders that give the same error message or is it a software problem?
I can't see any problems with the physical unit:
Code
import sys
sys.path.append('/home/tor/robot/MPU-9250-Sensors-Data-Collect/MPU-9250-Sensors-Data-Collect/mpu9250_jmdev')
from registers import *
from mpu_9250 import MPU9250
import time
import logging
##################################################
# Create #
##################################################
mpu = MPU9250(
address_ak=AK8963_ADDRESS,
address_mpu_master=MPU9050_ADDRESS_68, # In 0x68 Address
address_mpu_slave=None,
bus=1,
gfs=GFS_1000,
afs=AFS_8G,
mfs=AK8963_BIT_16,
mode=AK8963_MODE_C8HZ)
##################################################
# Configure #
##################################################
mpu.configure() # Apply the settings to the registers.
##################################################
# Show Values #
##################################################
while True:
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)
Results
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 97, in configure
self.configureAK8963(self.mfs, self.mode)
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 233, in configureAK8963
self.writeAK(AK8963_CNTL1, 0x00, 0.1)
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 1013, in writeAK
self.bus.write_byte_data(self.address_ak, register, value)
File "/usr/local/lib/python3.8/dist-packages/smbus2/smbus2.py", line 458, in write_byte_data
ioctl(self.fd, I2C_SMBUS, msg)
OSError: [Errno 121] Remote I/O error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 97, in configure
self.configureAK8963(self.mfs, self.mode)
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 233, in configureAK8963
self.writeAK(AK8963_CNTL1, 0x00, 0.1)
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 1013, in writeAK
self.bus.write_byte_data(self.address_ak, register, value)
File "/usr/local/lib/python3.8/dist-packages/smbus2/smbus2.py", line 458, in write_byte_data
ioctl(self.fd, I2C_SMBUS, msg)
OSError: [Errno 121] Remote I/O error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "sensorTest.py", line 13, in <module>
mpu.configure() # Apply the settings to the registers.
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 102, in configure
self.configure(retry - 1)
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 102, in configure
self.configure(retry - 1)
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 105, in configure
raise err
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 97, in configure
self.configureAK8963(self.mfs, self.mode)
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 233, in configureAK8963
self.writeAK(AK8963_CNTL1, 0x00, 0.1)
File "/usr/local/lib/python3.8/dist-packages/mpu9250_jmdev/mpu_9250.py", line 1013, in writeAK
self.bus.write_byte_data(self.address_ak, register, value)
File "/usr/local/lib/python3.8/dist-packages/smbus2/smbus2.py", line 458, in write_byte_data
ioctl(self.fd, I2C_SMBUS, msg)
OSError: [Errno 121] Remote I/O error
If I put try/except blocks around smbus2.py (ioctl(self.fd, I2C_SMBUS, msg)
), I can get data for the other three sensors:
|.....MPU9250 in 0x68 Address.....|
Accelerometer [-1.210205078125, -8.0, -8.0]
Gyroscope [43.9453125, -1.373291015625, -21.270751953125]
[OSError(121, 'Remote I/O error'),
<smbus2d.SMBus object at 0xffff8d0f20a0>,
12,
3,
7,
None]
Magnetometer [0.0, 0.0, 0.0]
Temperature 24.258753407014705
What the offending function looks like:
def read_i2c_block_data(self, i2c_addr, register, length, force=None):
"""
Read a block of byte data from a given register.
:param i2c_addr: i2c address
:type i2c_addr: int
:param register: Start register
:type register: int
:param length: Desired block length
:type length: int
:param force:
:type force: Boolean
:return: List of bytes
:rtype: list
"""
if length > I2C_SMBUS_BLOCK_MAX:
raise ValueError("Desired block length over %d bytes" % I2C_SMBUS_BLOCK_MAX)
self._set_address(i2c_addr, force=force)
msg = i2c_smbus_ioctl_data.create(
read_write=I2C_SMBUS_READ, command=register, size=I2C_SMBUS_I2C_BLOCK_DATA
)
msg.data.contents.byte = length
try:
ioctl(self.fd, I2C_SMBUS, msg)
except Exception as e:
if hasattr(e, 'message'):
print(e.message)
pprint([e, self, i2c_addr, register, length, force])
return msg.data.contents.block[1:length + 1]
The sensor is readable and is detecting the correct address:
tor@tor:~/robot$ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- 48 -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
Thanks!
Describe the bug
If you set the address_mpu_master
to be MPU9050_ADDRESS_69
and try to calibrate, it throws an OSError: [Errno 121] Remote I/O error
. When checking the i2c
connected address with i2cdetect -y 1
my particular "bad" 9250
sensor is showing up as 69
even when AD0
is open/not attached to anything. I am only using 1
sensor. Doing the address change fixes the data stream however calibration I can't seem to make it only update 69
. I saw one of the "definitions" here seems to cover both 68
and 69
so I wanted to ask if you could pass in a bus address to the mpu.calibrate()
. This probably doesn't make sense if you're setting the address for mpu
. But yeah that's my issue.
To Reproduce
Steps to reproduce the behavior:
69
in i2cdetect
prompt instead of default 68
.OSError ... Remote I/O error
Expected behavior
Maybe calibrate the 69
address based on "instantiated" mpu
with MPU9050_ADDRESS_69
Screenshots
My wiring (note no AD0, generally 68 is what shows up but a couple of boards showing up as 69)
Raspberry Pi 4
Python 3.7.3
Additional context
I was trying to flex this so if I do have a bad board and it shows up as 69 I can set that/calibrate it through CLI-args.
Running these two commands in sequence, after changing to 69 seems to be problematic. But if you run them one at a time it works... so I'm not sure.
mpu.calibrateMPU6500()
mpu.calibrateAK8963()
I am calibrating the sensors with the functions. The Accelerometer and Gyroscope readings appear to be fine, however the readings I am getting from the Magnetometer are showing a wide range of values.
I have the MPU-9250 mounted with two motors/wheels in a robot on a raised platform (so it doesn't move during testing). I calibrate the Magnetometer which reads near zero values. Upon starting and stopping the motors (again no real movement), the Magnetometer values swing pretty wildly (-10 + 20). What is really strange is when the motors are stopped, the Magnetometer values never goes back to near zero but it keeps these high values.
I also isolated the 3.3v power to a clean source to make sure noise was not a factor.
I suspect this is either a problem with the Magnetometer calibration function, or the physical layout / circuit/device. Any thoughts are much appreciated.
Thanks.
Raspberry Pi Zero W
SparkFun IMU MPU-9250 using I2C communication
Describe the bug
Hello,
I've been strugling with a second MPU9250 I bought after thinking the first one might be defective, I can't get any reading from the magnetometer on my Raspberry.
I connected the VCC to 3.3V, SDA + SCL and GND accordingly.
i2cdetect -y 1
only shows 0x68 address, no magnetometer address.
I've seen that I might need to change a register to allow some I2C pass through, but I thought this library might do it on its own.
Do I need to do something on my own or set something up on my top-level script to address this issue ?
Thank you for your help.
First thank you for this amazing work!
I had to dig into the other files to realize I needed to Install smbus2. I had smbus and it was automatically using fake smbus. Maybe user error since im a newbie.\
Is there a way to get each measurement data as a separate value? for example:
acceleration in X = ##
acceleration in Y = ##
acceleration in Z = ##
gyro X = ##
gyro Y = ##
etc.
Thank you very much!
Describe the bug
The readings from the board are changing dramatically whilst the board is completely still.
Hello :)
I recently aquired the MPU9250 as an upgrade to get the magnetometer to be able to read the heading.
Whilst looking for a library, this one seemed to be by far the most solid in docs and examples.
So unless I am a complete dummy (which is possible) something seems wrong with the readings I am getting.
|.....MPU9250 in 0x68 Address.....|
Accelerometer [-0.19921875, -1.550048828125, -7.0]
Gyroscope [-0.0682830810546875, -0.15316009521484353, 0.21915435791015614]
Magnetometer [-0.2911545341232835, 0.2014987670749353, 1.4122445841195845]
Temperature 28.475963698445504
145.31412714135627
[1 sec later]
|.....MPU9250 in 0x68 Address.....|
Accelerometer [-0.197265625, -1.538330078125, -7.0]
Gyroscope [0.0537872314453125, -0.06160736083984353, -0.11653900146484386]
Magnetometer [-0.2911545341232835, 1.4104913695245713, 1.0356460283543585]
Temperature 28.284272321562284
101.66321840226273
Because it is not moving, the accelerometer seems to be rather stable.
The gyro and the magnetometer though are behaving as if there was movement.
I do not have another MPU9250 to compare this to.
Any good guess on what might be going on / what I am doing wrong ?
Many thanks,
To Reproduce
import time
import math
from mpu9250_jmdev.registers import *
from mpu9250_jmdev.mpu_9250 import MPU9250
mpu = MPU9250(
address_ak=AK8963_ADDRESS,
address_mpu_master=MPU9050_ADDRESS_69,
address_mpu_slave=None,
bus=1,
gfs=GFS_1000,
afs=AFS_8G,
mfs=AK8963_BIT_16,
mode=AK8963_MODE_C100HZ,
)
mpu.configure()
mpu.calibrate()
while True:
print("|.....MPU9250 in 0x68 Address.....|")
print("Accelerometer", mpu.readAccelerometerMaster())
print("Gyroscope", mpu.readGyroscopeMaster())
print("Magnetometer", mpu.readMagnetometerMaster())
print("Temperature", mpu.readTemperatureMaster())
magnet = mpu.readMagnetometerMaster()
print(90 - math.atan2(magnet[0], magnet[1]) * 180 / math.pi)
time.sleep(1)
Expected behavior
Expecting the values to be constant when the module is not moving at all
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
N
Hi,
this is more like a potential feature I might want to add and discuss it with you.
I experienced ground braking issues with the the magnetometer in chaning temperatur conditions and a quick research + looking at my data shows me that there a temperatur compensation might actually be needed.
Does anyone of you have experience with it or can give me any recommendation how to implement it?
I assume it is a linear scale factor?
Thanks for your help!
I installed the library without smbus2 at first and it was running fake smbus correctly. After I installed smbus2 the example code stopped working. it gives an error 121. what should I do?
Describe the bug
When I run:
python3 master-only-mode.py
I get:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/mpu9250_jmdev/mpu_9250.py", line 95, in configure
self.configureMPU6500(self.gfs, self.afs)
File "/usr/local/lib/python3.6/site-packages/mpu9250_jmdev/mpu_9250.py", line 135, in configureMPU6500
self.writeMaster(PWR_MGMT_1, 0x00, 0.1)
File "/usr/local/lib/python3.6/site-packages/mpu9250_jmdev/mpu_9250.py", line 1022, in writeMaster
self.bus.write_byte_data(self.address_mpu_master, register, value)
File "/usr/local/lib/python3.6/site-packages/smbus2/smbus2.py", line 429, in write_byte_data
self._set_address(i2c_addr, force=force)
File "/usr/local/lib/python3.6/site-packages/smbus2/smbus2.py", line 333, in _set_address
ioctl(self.fd, I2C_SLAVE, address)
OSError: [Errno 16] Device or resource busy
(3 times)
To Reproduce
Enabled I2C using:
echo -e "dtparam=i2c_arm=on\ndtoverlay=i2c-rtc,ds3231" >> /boot/config.txt
echo -e "i2c-dev" >> /etc/modules-load.d/i2c.conf
yum -y install i2c-tools && reboot
Then I enabled the SMBus2 by using from MPU9250 folder:
pip3 install -r requirements.txt
Then ran:
python3 master-only-mode.py
Expected behavior
I expected to start reading values from address 68.
Desktop:
Running Centos 7 on a Raspberry Pi 3 B+.
I have both python 2 and python3 so I need to be using pip3 and python3.
Hello! Thanks so much for your work - greatly appreciated. I'm working with two MPU9250s for a simulator project and am having trouble getting data from the slave consistently. When I start up my Pi for the first time and run the master-slave-mode.py example without the calibration, I get reliable readings for the master and slave. For example, accelerometer readings of [0,0,1] on flat ground and [0,0,-1] when flipped upside down. Gyroscope readings are close to [0,0,0] while stationary for the most part.
When I run mpu.calibrate() after, I run into this error:
Calibrating 0x68 - AK8963
Traceback (most recent call last):
File "/home/pi/MPU9250/examples/basic-usage/master-slave-mode.py", line 36, in
mpu.calibrate() # Calibrate sensors
File "/home/pi/MPU9250/mpu9250_jmdev/mpu_9250.py", line 592, in calibrate
self.calibrateAK8963()
File "/home/pi/MPU9250/mpu9250_jmdev/mpu_9250.py", line 887, in calibrateAK8963
avg_rad / mag_scale[0],
ZeroDivisionError: float division by zero
Since I am most concerned with getting accel & gyro readings for my project, I switch to mpu.calibrate6500(), but run into this error next:
Could not connect to slave to calibrate
|.....MPU9250 in 0x68 Biases.....|
Accelerometer Master [0.12724958147321427, 0.08103143601190477, -0.13961937313988093]
Accelerometer Slave [0, 0, 0]
Gyroscope Master [-2.0375932965959818, -0.30699230375744047, -0.20599365234375]
Gyroscope Slave [0, 0, 0]
Magnetometer SID [1, 1, 1]
Magnetometer HID [0, 0, 0]
At first I thought it might be a wiring problem with the slave IMU. However, since the initial readings look fine I am not sure if that is the case. I've reproduced this pattern (data collection works until I attempt calibration) 3 times consecutively. Any guidance would be appreciated!
Using Fake SMBus
Install requirements.
|.....MPU9250 in 0x68 Address.....|
Accelerometer [0.0, 0.0, 0.0]
Gyroscope [0.0, 0.0, 0.0]
Magnetometer [0.0, 0.0, 0.0]
Temperature 21.0
mpu is connected correctly.
Dear Colleagues,
I am getting the following error, when attempting to write calibration values to the AK8963 in the Invensense MPU9250 module. I am able to calibrate the accelerometer and gyro.
File "mpu9250_cal.py", line 232, in <module>
main()
File "mpu9250_cal.py", line 218, in main
calmag(mpu)
File "mpu9250_cal.py", line 142, in calmag
mpu.configureAK8963() #resets the calibration values of the AK8963
TypeError: configureAK8963() missing 2 required positional arguments: 'mfs' and 'mode'
Here are the libraries I've imported:
1 import sys
2 import math
3 import time
4 from smbus2 import SMBus
5 from mpu9250_jmdev.registers import *
6 from mpu9250_jmdev.mpu_9250 import MPU9250
Here is the magnetometer calibration function in my test code-- when the error appeared, i created these clunky variables and got rid of a lot of other things to try to troubleshoot:
99 #normalize hard and soft iron biases of magnetometer -- robot needs to be waved in the air in figure 8 patterns for 30 sec.
100 def calmag (mpu):
101 global masterMag
102 global mbias
103 global magScale
104 magMax = [0,0,0] #placeholder for mag data
105 magMin = [0,0,0] #placeholder for min mag data
106 mbiasn = 0 #hard iron x bias value to be written to AK8963
107 mbiase = 0 #hard iron y bias value to be written to AK8963
108 mbiasd = 0 #hard iron z bias value to be written to AK8963
109 mscalen = 0 #soft iron x estimate to be written to AK8963
110 mscalee = 0 #soft iron y estimate to be written to AK8963
111 mscaled = 0 #soft iron z estimate to b written to AK8963
112
113 #write values to AK8963 magnetometer
114 mpu.configureAK8963() #resets the calibration values of the AK8963
115 mpu.configure()
116 mpu.bias = [mbiasn, mbiase, mbiasd] #write estimated hard iron biases
117 mpu.magScale = [mscalen, mscalee, mscaled] #write estimated soft iron biases
118
119 #print values
120 print("hard iron values ",mpu.mbias)
121 print("soft iron values ",mpu.magScale)
122 print("\n")
123 return
and here's where I call the function from main()
189 input("When you have the robot in your hand and are ready, press the return key")
190 calmag(mpu)
Running on headless raspberry pi 4b+ using debian buster
I'd appreciate your thoughts on how to resolve this. I haven't been able to find information on the positional arguments in the error message.
Thanks, Mark
Is it on purpose that file master-only-mode.py
uses address_mpu_master=MPU9050_ADDRESS_69
?
shouldn't it be address_mpu_master=MPU9050_ADDRESS_68
because AFAIR 0x68 is used by default.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.