Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1993 | - | 1 | /* |
2 | HMC5843_basic.pde - Basic reading example for the HMC5843 library |
||
3 | Copyright (C) 2011 Fabio Varesano <fabio at varesano dot net> |
||
4 | |||
5 | |||
6 | This program is free software: you can redistribute it and/or modify |
||
7 | it under the terms of the version 3 GNU General Public License as |
||
8 | published by the Free Software Foundation. |
||
9 | |||
10 | This program is distributed in the hope that it will be useful, |
||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
13 | GNU General Public License for more details. |
||
14 | |||
15 | You should have received a copy of the GNU General Public License |
||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
17 | |||
18 | */ |
||
19 | |||
20 | #include <Wire.h> |
||
21 | #include <HMC58X3.h> |
||
22 | |||
23 | HMC58X3 magn; |
||
24 | |||
25 | void setup(void) { |
||
26 | Serial.begin(9600); |
||
27 | Wire.begin(); |
||
28 | |||
29 | // no delay needed as we have already a delay(5) in HMC5843::init() |
||
30 | magn.init(false); // Dont set mode yet, we'll do that later on. |
||
31 | // Calibrate HMC using self test, not recommended to change the gain after calibration. |
||
32 | magn.calibrate(1); // Use gain 1=default, valid 0-7, 7 not recommended. |
||
33 | // Single mode conversion was used in calibration, now set continuous mode |
||
34 | magn.setMode(0); |
||
35 | } |
||
36 | |||
37 | void loop() { |
||
38 | int ix,iy,iz; |
||
39 | float fx,fy,fz; |
||
40 | delay(10); |
||
41 | // Get values, as ints and floats. |
||
42 | magn.getValues(&ix,&iy,&iz); |
||
43 | magn.getValues(&fx,&fy,&fz); |
||
44 | // also available HMC5843::getValues(float *xyz) you can pass to it an array of 3 floats |
||
45 | |||
46 | // Print int values |
||
47 | Serial.print("Ints x:"); |
||
48 | Serial.print(ix); |
||
49 | Serial.print(","); |
||
50 | Serial.print(iy); |
||
51 | Serial.print(","); |
||
52 | Serial.print(iz); |
||
53 | Serial.print(","); |
||
54 | |||
55 | // Print float values |
||
56 | Serial.print(" Floats x:"); |
||
57 | Serial.print(fx); |
||
58 | Serial.print(" y:"); |
||
59 | Serial.print(fy); |
||
60 | Serial.print(" z:"); |
||
61 | Serial.print(fz); |
||
62 | |||
63 | // a simple heading, assuming it's close to horizontal. See: |
||
64 | // M.J. Caruso. Applications of magnetic sensors for low cost compass systems. |
||
65 | // In Position Location and Navigation Symposium, IEEE, 2000. Available from: |
||
66 | // http://hnc.ru/lib/a%26c%20%28automatic%20%26%20controls%29/sensors/DataSheet/Magnit/Honeywell/lowcost.pdf |
||
67 | |||
68 | Serial.print(" Heading: "); |
||
69 | float heading = atan2(fy, fx); |
||
70 | if(heading < 0) { |
||
71 | heading += 2 * M_PI; |
||
72 | } |
||
73 | Serial.println(heading * 180/M_PI); |
||
74 | // x and y axis are swapped above with respect to the above paper as our Z axis points to the sky while in the paper it points to the bottom |
||
75 | } |