Skip to content

Commit

Permalink
mpython.py: Accelerometer类添加欧拉角计算
Browse files Browse the repository at this point in the history
  • Loading branch information
tangliufeng committed Jan 9, 2021
1 parent bbd54a5 commit 6659b30
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions port/boards/mpython/modules/mpython.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,20 @@ def get_z(self):
else:
raise Exception("i2c read/write error!")

def roll_pitch_angle(self):
x, y, z = self.get_x(), self.get_y(), -self.get_z()
# vector normalize
mag = math.sqrt(x ** 2 + y ** 2+z ** 2)
x /= mag
y /= mag
z /= mag
roll = math.degrees(-math.asin(y))
pitch = math.degrees(math.atan2(x, z))

return roll, pitch



# 3 axis accelerometer
accelerometer = Accelerometer()

Expand Down

0 comments on commit 6659b30

Please sign in to comment.