Python Code 25.1.1 MPU6050RAW
First observe the project result, and then analyze the code.
1. Use cd command to enter 25.1.1_MPU6050RAW directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/25.1.1_MPU6050RAW
2. Use python command to execute code "MPU6050RAW.py".
python MPU6050RAW.py
After the program is executed, the terminal will display the original acceleration and gyroscope data of
MPU6050, as well as the conversion to gravity acceleration and angular velocity as the unit of data. As shown
in the following figure:
The following is the program code:
im port MPU6050
im port time
mpu = MPU6050.MPU6050() #instantiate a MPU6050 class object
accel = [ 0]*3 #store accelerometer data
gyro = [0]*3 #store gyroscope data
def setup():
mpu. dmp_initialize() #initialize MPU6050
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)
if __name__ == '__main__': # Program start from here
p rint("Program is starting ... ")
setup()
try:
loop()