Chapter 25 Attitude Sensor MPU6050
e xcept KeyboardInterrupt: # When 'Ctrl+C' is pressed,the program will exit.
pass
A module "MPU6050.py" is used in the code. The module include a class used to operate MPU6050. When
used, first instantiate an object.
In the setup function, the MPU6050 is initialized.
def setup():
mpu. dmp_initialize()
In the loop function, read the original data of MPU6050 and print them out, and then convert the original
data into the corresponding acceleration and angular velocity, then print the converted data out.
def loop():
w hile(True):
accel = mpu. get_acceleration() #get accelerometer data
gyro = mpu. get_rotation() #get gyroscope data
print("a/g:%d\t%d\t%d\t%d\t%d\t%d
"%(accel[0],accel[1],accel[2],gyro[0],gyro[1],gyro[2]))
print("a/g:%.2f g\t%.2f g\t%.2f g\t%.2f d/s\t%.2f d/s\t%.2f
d/s"%(accel[0]/16384.0,accel[1]/16384.0,
accel[2]/16384.0,gyro[0]/131.0,gyro[1]/131.0,gyro[2]/131.0))
time.sleep(0.1)
This is a class library used to operate MPU6050, which can directly read and set MPU6050. Here are some
member functions:
def __init__(self, a _bus=1, a_address=C.MPU6050_DEFAULT_ADDRESS,
a_xAOff=None, a _yAOff=None, a _zAOff=None, a_xGOff=None,
a_yGOff=None, a _zGOff=None, a _debug=False):
Constructor
def dmp_initialize(self):
Initialization function, used to wake up MPU6050. Range of accelerometer is ±2g and range of gyroscope
is ±250 degrees/sec.
def get_acceleration(self): & def get_rotation(self):
Get the original data of accelerometer and gyroscope.
For details of more relevant member functions, please refer to MPU6050.py under code folder.