0

I am attempting to use an MPU6050 sensor with a Raspberry Pi, for the purpose of dectecting hte pitch, roll, and yaw of a DIY drone I'm building.

The MPU has worked in the past, somehow I've managed to change something in software, and I am now recieving the following error from my python script when I try to initialise the MPU:

Traceback (most recent call last):
  File "/home/pi/open-drone/Droneside/mpu.py", line 145, in <module>
    mpu.setUp() # Run MPU setuf functions
  File "/home/pi/open-drone/Droneside/mpu.py", line 56, in setUp
    self.bus.write_byte_data(self.address, 0x6B, 0x01)
OSError: [Errno 121] Remote I/O error

I have not changed the hardware connection to the Raspberry Pi's GPIO pins since the sensor was last working, and the sensor is correctly detected on address 0x68 by i2cdetect -y 1:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --             

The relevant sections of code are shown below. I've trimmed these down a little to remove irrelevant print statements, and the multitude of variable initializations for the current pitch / yaw / roll etc.

class MPU: # Handels MPU
    def __init__(self, gyro, acc, tau):

        self.bus = smbus.SMBus(1) # Changing this to a '0' causes file not found error.
        self.address = 0x68
    def setUp(self):

        # Activate the MPU-6050
        self.bus.write_byte_data(self.address, 0x6B, 0x01) # Error occurs here.

        # Configure the accelerometer
        self.bus.write_byte_data(self.address, 0x1C, self.accHex)

        # Configure the gyro
        self.bus.write_byte_data(self.address, 0x1B, self.gyroHex)

mpu = MPU(gyro, acc, tau)
print("Waiting for I2C")
time.sleep(1) # This was a suggestion I saw from another post, didn't help.
print("Done waiting.")
mpu.setUp() # Run MPU setup functions

# Program does not get this far.

mpu.calibrateGyro(500) # Calibrate gyro w/ 500 pts, DRONE MUST BE LEVEL.

So far, I've tried inserting that one second delay shown in the last code block, which I saw suggested here. Didn't help. Also tried a 10s delay, which made no difference.

I've tried changing the bus in self.bus = smbus.SMBus(1) to a '0', as I saw in a few other code examples, which just caused an file not found error.

I've also tried changing self.bus.write_byte_data(self.address, 0x6B, 0x01) to self.bus.write_byte_data(self.address, 0x6B, 0x00), because I saw a couple of examples that had it that way, but that didn't work either.

Tried running everything with sudo, which didn't change anything, and I've checked that the I2C interface is enabled in sudo raspiconfig, it was already enabled.

Any suggestions would be greatly appreciated. Thanks in advance!

EDIT: MPU6050 randomly started working again, briefly. I have made no changes to software of hardware. Whatever this problem is, it's causing the MPU to be unreliable, but not entirely non functional.

0andriy
  • 4,183
  • 1
  • 24
  • 37
Mblizzard
  • 171
  • 1
  • 1
  • 8
  • 1
    Why don;t you use the driver which is in the Linux kernel? https://elixir.bootlin.com/linux/latest/source/drivers/iio/imu/inv_mpu6050 – 0andriy Dec 08 '22 at 16:29
  • Mostly because the rest of my software is in Python, and I'd rather keep it all in the one language. I suppose that could be worth a look as a last resort though. Do you (@0andriy) know of an easy way to make use of that driver with python? Perhaps by calling a command with subprocess? – Mblizzard Dec 08 '22 at 23:38
  • Yes, the kernel driver use is preferred. In Python you can use it over direct accesses to sysfs or utilizing `libiio` (it has official Python bindings): https://analogdevicesinc.github.io/libiio/v0.24/python/index.html. Note, most likely your distribution already packaged it for you and you only need to install the package. – 0andriy Dec 11 '22 at 15:50

0 Answers0