-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibrateBerryIMU.py
88 lines (63 loc) · 1.96 KB
/
calibrateBerryIMU.py
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/python
# This script is used to calibrate the compass on a BerryIMU.
#
# Start this program and rotate your BerryIMU in all directions.
# You will see the maximum and minimum values change.
# After about 30secs or when the values are not changing, press Ctrl-C.
# The script will printout some text which you then need to add to
# berryIMU.py or berryIMU-simple.py
#
# The BerryIMUv1, BerryIMUv2 and BerryIMUv3 are supported
#
# This script is python 2.7 and 3 compatible
#
# Feel free to do whatever you like with this code.
# Distributed as-is; no warranty is given.
#
# http://ozzmaker.com/
import sys,signal,os
import time
import math
import IMU
import datetime
def handle_ctrl_c(signal, frame):
print(" ")
print("magXmin = %i"% (magXmin))
print("magYmin = %i"% (magYmin))
print("magZmin = %i"% (magZmin))
print("magXmax = %i"% (magXmax))
print("magYmax = %i"% (magYmax))
print("magZmax = %i"% (magZmax))
sys.exit(130) # 130 is standard exit code for ctrl-c
IMU.detectIMU()
IMU.initIMU()
#This will capture exit when using Ctrl-C
signal.signal(signal.SIGINT, handle_ctrl_c)
a = datetime.datetime.now()
#Preload the variables used to keep track of the minimum and maximum values
magXmin = 32767
magYmin = 32767
magZmin = 32767
magXmax = -32767
magYmax = -32767
magZmax = -32767
while True:
#Read magnetometer values
MAGx = IMU.readMAGx()
MAGy = IMU.readMAGy()
MAGz = IMU.readMAGz()
if MAGx > magXmax:
magXmax = MAGx
if MAGy > magYmax:
magYmax = MAGy
if MAGz > magZmax:
magZmax = MAGz
if MAGx < magXmin:
magXmin = MAGx
if MAGy < magYmin:
magYmin = MAGy
if MAGz < magZmin:
magZmin = MAGz
print((" magXmin %i magYmin %i magZmin %i ## magXmax %i magYmax %i magZmax %i " %(magXmin,magYmin,magZmin,magXmax,magYmax,magZmax)))
#slow program down a bit, makes the output more readable
time.sleep(0.03)