Skip to content

Commit f05ce07

Browse files
author
Sebastian Goscik
committed
Fixed accelerometer functions
The accelerometer pitch and roll functions now return sensible values: - Roll: Returns -180 to 180 degrees - Pitch: Returns -90 to 90 degrees which repeats when the board is upside down due to a lack of yaw measurement. Removed yaw function because it is not possible to measure yaw using just an accelerometer.
1 parent 590c978 commit f05ce07

File tree

2 files changed

+12
-36
lines changed

2 files changed

+12
-36
lines changed

pysense/lib/LIS2HH12.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,14 @@ def acceleration(self):
8686
return (self.x[0] * _mult, self.y[0] * _mult, self.z[0] * _mult)
8787

8888
def roll(self):
89-
self.acceleration()
90-
div = math.sqrt(math.pow(self.y[0], 2) + math.pow(self.z[0], 2))
91-
if div == 0:
92-
div = 0.01
93-
return (180 / 3.14154) * math.atan(self.x[0] / div)
89+
x,y,z = self.acceleration()
90+
rad = math.atan2(-x, z)
91+
return (180 / math.pi) * rad
9492

9593
def pitch(self):
96-
self.acceleration()
97-
if self.z[0] == 0:
98-
div = 1
99-
else:
100-
div = self.z[0]
101-
return (180 / 3.14154) * math.atan(math.sqrt(math.pow(self.x[0], 2) + math.pow(self.y[0], 2)) / div)
102-
103-
def yaw(self):
104-
self.acceleration()
105-
div = math.sqrt(math.pow(self.x[0], 2) + math.pow(self.z[0], 2))
106-
if div == 0:
107-
div = 0.01
108-
return (180 / 3.14154) * math.atan(self.y[0] / div)
94+
x,y,z = self.acceleration()
95+
rad = -math.atan2(y, (math.sqrt(x*x + z*z)))
96+
return (180 / math.pi) * rad
10997

11098
def set_full_scale(self, scale):
11199
self.i2c.readfrom_mem_into(ACC_I2CADDR , CTRL4_REG, self.reg)

pytrack/lib/LIS2HH12.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,26 +86,14 @@ def acceleration(self):
8686
return (self.x[0] * _mult, self.y[0] * _mult, self.z[0] * _mult)
8787

8888
def roll(self):
89-
self.acceleration()
90-
div = math.sqrt(math.pow(self.y[0], 2) + math.pow(self.z[0], 2))
91-
if div == 0:
92-
div = 0.01
93-
return (180 / 3.14154) * math.atan(self.x[0] / div)
89+
x,y,z = self.acceleration()
90+
rad = math.atan2(-x, z)
91+
return (180 / math.pi) * rad
9492

9593
def pitch(self):
96-
self.acceleration()
97-
if self.z[0] == 0:
98-
div = 1
99-
else:
100-
div = self.z[0]
101-
return (180 / 3.14154) * math.atan(math.sqrt(math.pow(self.x[0], 2) + math.pow(self.y[0], 2)) / div)
102-
103-
def yaw(self):
104-
self.acceleration()
105-
div = math.sqrt(math.pow(self.x[0], 2) + math.pow(self.z[0], 2))
106-
if div == 0:
107-
div = 0.01
108-
return (180 / 3.14154) * math.atan(self.y[0] / div)
94+
x,y,z = self.acceleration()
95+
rad = -math.atan2(y, (math.sqrt(x*x + z*z)))
96+
return (180 / math.pi) * rad
10997

11098
def set_full_scale(self, scale):
11199
self.i2c.readfrom_mem_into(ACC_I2CADDR , CTRL4_REG, self.reg)

0 commit comments

Comments
 (0)