Skip to content

Commit 17cb968

Browse files
committed
Added 9DoF Sensor Calibration script
1 parent c7ba4b9 commit 17cb968

File tree

2 files changed

+183
-1
lines changed

2 files changed

+183
-1
lines changed

CPB_AMS_Gizmo_BLE/cpb_ams_gizmo_ble.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""
1+
x"""
22
This example solicits that apple devices that provide notifications connect to it, initiates
33
pairing, prints existing notifications and then prints any new ones as they arrive.
44
"""
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import threading
2+
import time
3+
import board
4+
import busio
5+
from adafruit_lsm6ds import LSM6DSOX
6+
from adafruit_lis3mdl import LIS3MDL
7+
8+
SAMPLE_SIZE = 500
9+
10+
class KeyListener:
11+
"""Object for listening for input in a separate thread"""
12+
def __init__(self):
13+
self._input_key = None
14+
self._listener_thread = None
15+
16+
def _key_listener(self):
17+
while True:
18+
self._input_key = input()
19+
20+
def start(self):
21+
"""Start Listening"""
22+
if self._listener_thread is None:
23+
self._listener_thread = threading.Thread(
24+
target=self._key_listener, daemon=True
25+
)
26+
if not self._listener_thread.is_alive():
27+
self._listener_thread.start()
28+
29+
def stop(self):
30+
"""Stop Listening"""
31+
if self._listener_thread is not None and self._listener_thread.is_alive():
32+
self._listener_thread.join()
33+
34+
@property
35+
def pressed(self):
36+
"Return whether enter was pressed since last checked"""
37+
result = False
38+
if self._input_key is not None:
39+
self._input_key = None
40+
result = True
41+
return result
42+
43+
44+
def main():
45+
i2c = busio.I2C(board.SCL, board.SDA)
46+
47+
gyro_accel = LSM6DSOX(i2c)
48+
magnetometer = LIS3MDL(i2c)
49+
key_listener = KeyListener()
50+
key_listener.start()
51+
52+
53+
"""
54+
Magnetometer Calibration
55+
"""
56+
57+
print("Magnetometer Calibration")
58+
print("Start moving the board in all directions")
59+
print("When the magnetic Hard Offset values stop")
60+
print("changing, press ENTER to go to the next step")
61+
print("Press ENTER to continue...")
62+
while not key_listener.pressed:
63+
pass
64+
65+
mag_x, mag_y, mag_z = magnetometer.magnetic
66+
min_x = max_x = mag_x
67+
min_y = max_y = mag_y
68+
min_z = max_z = mag_z
69+
70+
while not key_listener.pressed:
71+
mag_x, mag_y, mag_z = magnetometer.magnetic
72+
73+
print(
74+
"Magnetometer: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format(
75+
mag_x, mag_y, mag_z
76+
)
77+
)
78+
79+
min_x = min(min_x, mag_x)
80+
min_y = min(min_y, mag_y)
81+
min_z = min(min_z, mag_z)
82+
83+
max_x = max(max_x, mag_x)
84+
max_y = max(max_y, mag_y)
85+
max_z = max(max_z, mag_z)
86+
87+
offset_x = (max_x + min_x) / 2
88+
offset_y = (max_y + min_y) / 2
89+
offset_z = (max_z + min_z) / 2
90+
91+
field_x = (max_x - min_x) / 2
92+
field_y = (max_y - min_y) / 2
93+
field_z = (max_z - min_z) / 2
94+
95+
print(
96+
"Hard Offset: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format(
97+
offset_x, offset_y, offset_z
98+
)
99+
)
100+
print(
101+
"Field: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format(
102+
field_x, field_y, field_z
103+
)
104+
)
105+
print("")
106+
time.sleep(0.01)
107+
108+
mag_calibration = (offset_x, offset_y, offset_z)
109+
print(
110+
"Final Magnetometer Calibration: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} uT".format(
111+
offset_x, offset_y, offset_z
112+
)
113+
)
114+
115+
"""
116+
Gyroscope Calibration
117+
"""
118+
119+
gyro_x, gyro_y, gyro_z = gyro_accel.gyro
120+
min_x = max_x = gyro_x
121+
min_y = max_y = gyro_y
122+
min_z = max_z = gyro_z
123+
124+
print("")
125+
print("")
126+
print("Gyro Calibration")
127+
print("Place your gyro on a FLAT stable surface.")
128+
print("Press ENTER to continue...")
129+
while not key_listener.pressed:
130+
pass
131+
132+
for _ in range(SAMPLE_SIZE):
133+
gyro_x, gyro_y, gyro_z = gyro_accel.gyro
134+
135+
print(
136+
"Gyroscope: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format(
137+
gyro_x, gyro_y, gyro_z
138+
)
139+
)
140+
141+
min_x = min(min_x, gyro_x)
142+
min_y = min(min_y, gyro_y)
143+
min_z = min(min_z, gyro_z)
144+
145+
max_x = max(max_x, gyro_x)
146+
max_y = max(max_y, gyro_y)
147+
max_z = max(max_z, gyro_z)
148+
149+
offset_x = (max_x + min_x) / 2
150+
offset_y = (max_y + min_y) / 2
151+
offset_z = (max_z + min_z) / 2
152+
153+
noise_x = max_x - min_x
154+
noise_y = max_y - min_y
155+
noise_z = max_z - min_z
156+
157+
print(
158+
"Zero Rate Offset: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format(
159+
offset_x, offset_y, offset_z
160+
)
161+
)
162+
print(
163+
"Rad/s Noise: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format(
164+
noise_x, noise_y, noise_z
165+
)
166+
)
167+
print("")
168+
169+
gyro_calibration = (offset_x, offset_y, offset_z)
170+
print(
171+
"Final Zero Rate Offset: X: {0:8.2f}, Y:{1:8.2f}, Z:{2:8.2f} rad/s".format(
172+
offset_x, offset_y, offset_z
173+
)
174+
)
175+
print("")
176+
print("------------------------------------------------------------------------")
177+
print("Final Magnetometer Calibration Values: ", mag_calibration)
178+
print("Final Gyro Calibration Values: ", gyro_calibration)
179+
180+
181+
if __name__ == "__main__":
182+
main()

0 commit comments

Comments
 (0)