-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLibCompass.cpp
More file actions
189 lines (155 loc) · 5.51 KB
/
LibCompass.cpp
File metadata and controls
189 lines (155 loc) · 5.51 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
LibCompass - A Compass Library for Arduino
Supported compass modules:
Liquidware Compass Module - http://www.liquidware.com/shop/show/SEN-CMP/Compass+Module
Liquidware GeoShield Module - http://www.liquidware.com/category/Sensors
Created by Christopher Ladden at Modern Device on December 2009.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/******************************************************************************
* Includes
******************************************************************************/
#include <inttypes.h>
#include <Wire.h>
#include <wiring.h>
#include <avr/pgmspace.h>
#include "HardwareSerial.h"
#include "LibCompass.h"
/******************************************************************************
* Definitions
******************************************************************************/
#define COMPASS_READ_TIMEOUT 100
/******************************************************************************
* Constructors
******************************************************************************/
/**********************************************************
* Compass
* Initialize the Compass
*
* @param CompassType - The type of compass ex. HMC6352
**********************************************************/
LibCompass::LibCompass(uint8_t CompassType) {
if(CompassType == (uint8_t)COMPASS_HW_LIQUIDWARE_COMPASS) {
pinMode(16, OUTPUT);
digitalWrite(16, LOW); //GND pin
pinMode(17, OUTPUT);
digitalWrite(17, HIGH); //VCC pin
}
Wire.begin();
}
/******************************************************************************
* Global Functions
******************************************************************************/
/**********************************************************
* GetHeading
* Get the current Compass heading.
*
* @return int The compass heading in degrees.
**********************************************************/
float LibCompass::GetHeading(void) {
uint8_t j = 0;
uint8_t data[2];
int16_t frac;
long counter;
Wire.beginTransmission(hmc6352_Address);
Wire.send(hmc6352_GetData);
Wire.endTransmission();
delay(8); //6000 microseconds minimum 6 ms
counter = millis();
Wire.requestFrom(hmc6352_Address, 2);
while(Wire.available() && (j < 2) ) {
data[j] = Wire.receive();
j++;
//Timeout check
if ((millis() - counter) > COMPASS_READ_TIMEOUT) {
return -1; //timeout
}
}
frac = data[0]*256 + data[1];
return (frac/10.0);
}
/**********************************************************
* Calibrate
* Performs a chip calibration
*
* @return bool - The calibration result
**********************************************************/
bool LibCompass::Calibrate(void) {
char out[100];
char inChar;
Serial.begin(9600);
strcpy_P(out, PSTR("Calibration Mode."));
Serial.println(out);
strcpy_P(out, PSTR("You'll need to rotate the sensor 720 degrees"));
Serial.println(out);
strcpy_P(out, PSTR("Send a 'C' to begin or 'Q' to quit"));
Serial.println(out);
//wait for a character
while(1) {
if(Serial.available()) {
if (Serial.read() == 'C') {
//Calibrate!
strcpy_P(out, PSTR("Start rotating..."));
Serial.println(out);
break;
} else {
//Don't Calibrate
strcpy_P(out, PSTR("Quiting."));
Serial.println(out);
delay(3000);
return false;
}
}
}
//Enter Cal mode
Wire.beginTransmission(hmc6352_Address);
Wire.send(hmc6352_EnterCal);
Wire.endTransmission();
delay(3000); //give them some time
strcpy_P(out, PSTR("Send a 'E' character when finished"));
Serial.println(out);
//wait for a character
while(1) {
if(Serial.available()) {
if (Serial.read() == 'E') {
break;
}
}
}
//Exit Cal Mode
Wire.beginTransmission(hmc6352_Address);
Wire.send(hmc6352_ExitCal);
Wire.endTransmission();
strcpy_P(out, PSTR("Done."));
Serial.println(out);
delay(3000);
return true;
}
/**********************************************************
* Sleep
* Send the sleep command to the Compass
**********************************************************/
void LibCompass::Sleep(void) {
Wire.beginTransmission(hmc6352_Address);
Wire.send(hmc6352_Sleep); //S enter sleep mode
Wire.endTransmission();
}
/**********************************************************
* Wake
* Send the wakeup command to the Compass.
**********************************************************/
void LibCompass::Wake(void) {
Wire.beginTransmission(hmc6352_Address);
Wire.send(hmc6352_Wakeup); //W wake up exit sleep mode
Wire.endTransmission();
}