-
Notifications
You must be signed in to change notification settings - Fork 0
/
grove_compass_lib.py
102 lines (86 loc) · 3.38 KB
/
grove_compass_lib.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python
#
# GrovePi Library for using the Grove Compass module HCM5883 (http://www.seeedstudio.com/depot/Grove-3Axis-Digital-Compass-p-759.html)
#
# The GrovePi connects the Raspberry Pi and Grove sensors. You can learn more about GrovePi here: http://www.dexterindustries.com/GrovePi
#
# Have a question about this example? Ask on the forums here: http://www.dexterindustries.com/forum/?forum=grovepi
#
'''
## License
The MIT License (MIT)
GrovePi for the Raspberry Pi: an open source platform for connecting Grove Sensors to the Raspberry Pi.
Copyright (C) 2015 Dexter Industries
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
# History
# ------------------------------------------------
# Author Date Comments
# Karan 22 July 14 Initial Authoring
#
# NOTE:
# Refer to the datasheet to add additional functionality https://www.seeedstudio.com/wiki/images/4/42/HMC5883.pdf
import smbus
import time
import math
import RPi.GPIO as GPIO
import struct
rev = GPIO.RPI_REVISION
if rev == 2 or rev == 3:
bus = smbus.SMBus(1)
else:
bus = smbus.SMBus(0)
HMC5883L_ADDRESS =0x1E
#CONFIGURATION_REGISTERA =0x00
#CONFIGURATION_REGISTERB =0x01
MODE_REGISTER =0x02
#DATA_REGISTER_BEGIN =0x03
#MEASUREMENT_CONTINUOUS =0x00
#MEASUREMENT_SINGLE_SHOT =0x01
#MEASUREMENT_IDLE =0x03
#Do two's compiment of val (for parsing the input)
#http://stackoverflow.com/a/9147327/1945052
def twos_comp(val, bits):
"""compute the 2's compliment of int value val"""
if( (val&(1<<(bits-1))) != 0 ):
val = val - (1<<bits)
return val
#Compass class for all the values and functions
class compass:
x=0
y=0
z=0
heading=0
headingDegrees=0
def __init__(self):
#Enable the compass
bus.write_byte_data(HMC5883L_ADDRESS,MODE_REGISTER,0)
time.sleep(.1)
data=bus.read_i2c_block_data(HMC5883L_ADDRESS,0)
compass.update(self)
#Update the compass values
def update(self):
data=bus.read_i2c_block_data(HMC5883L_ADDRESS,0)
compass.x=twos_comp(data[3]*256+data[4],16)
compass.z=twos_comp(data[5]*256+data[6],16)
compass.y=twos_comp(data[7]*256+data[8],16)
compass.heading=math.atan2(compass.y, compass.x)
if compass.heading <0:
compass.heading+=2*math.pi
if compass.heading >2*math.pi:
compass.heading-=2*math.pi
compass.headingDegrees=round(math.degrees(compass.heading),2)