From 6659b305cff70a1aa8d132bfee8af844a2527d1c Mon Sep 17 00:00:00 2001 From: tangliufeng <137513285@qq.com> Date: Sat, 9 Jan 2021 12:33:30 +0800 Subject: [PATCH] =?UTF-8?q?mpython.py:=20Accelerometer=E7=B1=BB=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E6=AC=A7=E6=8B=89=E8=A7=92=E8=AE=A1=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- port/boards/mpython/modules/mpython.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/port/boards/mpython/modules/mpython.py b/port/boards/mpython/modules/mpython.py index 5a7c4ec6..7df73447 100755 --- a/port/boards/mpython/modules/mpython.py +++ b/port/boards/mpython/modules/mpython.py @@ -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()