-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibrateCompass.py
79 lines (54 loc) · 1.71 KB
/
calibrateCompass.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
#!/usr/bin/python
# This program is used to calibrate the compass on a BerryIMUv1 or
# BerryIMUv2.
#
# 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 program will printout some text which you then need to add to
# berryIMU.py or berryIMU-simple.py
import sys,signal,os
import time
import math
import IMU
import datetime
def handle_ctrl_c(signal, frame):
print " "
print "magXmin = ", magXmin
print "magYmin = ", magYmin
print "magZmin = ", magZmin
print "magXmax = ", magXmax
print "magYmax = ", magYmax
print "magZmax = ", magZmax
sys.exit(130) # 130 is standard exit code for ctrl-c
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)