
This is a simple example of how to measure Pitch and Roll using a 3-axis accelerometer as found on the MPU6050.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# ==================================================================== # DISCLAIMER: # This code is provided as-is for educational and experimental # purposes only. The author makes no representations or warranties of # any kind concerning the safety, suitability, or accuracy of this # code. Use at your own risk. The author assumes no liability for any # damages, system failures, security breaches, or network issues # resulting from the use or implementation of this script. # ==================================================================== from imu import MPU6050 from machine import I2C,Pin import math import time i2c=I2C(0, sda=Pin(16), scl=Pin(17), freq=400000) mpu = MPU6050(i2c) roll=0 pitch=0 while True: xAccel=mpu.accel.x yAccel=mpu.accel.y zAccel=mpu.accel.z pitch=math.atan(yAccel/zAccel) roll=math.atan(xAccel/zAccel) pitchDeg=pitch/(2*math.pi)*360 rollDeg=roll/(2*math.pi)*360 print('pitch: ',pitchDeg, 'roll: ',rollDeg) ts=.05 time.sleep(ts) |