Browse Source

hmc5883l: changed to standard heading and added declination

Signed-off-by: Mihai Tudor Panu <mihai.t.panu@intel.com>
Mihai Tudor Panu 10 years ago
parent
commit
ea7b7a23fa
3 changed files with 48 additions and 5 deletions
  1. 13
    1
      examples/hmc5883l.cxx
  2. 20
    3
      src/hmc5883l/hmc5883l.cxx
  3. 15
    1
      src/hmc5883l/hmc5883l.h

+ 13
- 1
examples/hmc5883l.cxx View File

1
 /*
1
 /*
2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
3
  * Copyright (c) 2014 Intel Corporation.
4
  * Copyright (c) 2014 Intel Corporation.
4
  *
5
  *
5
  * Permission is hereby granted, free of charge, to any person obtaining
6
  * Permission is hereby granted, free of charge, to any person obtaining
22
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
  */
24
  */
24
 
25
 
26
+#include <unistd.h>
25
 #include "hmc5883l.h"
27
 #include "hmc5883l.h"
26
 
28
 
27
 int
29
 int
29
 {
31
 {
30
 //! [Interesting]
32
 //! [Interesting]
31
     upm::Hmc5883l* compass = new upm::Hmc5883l(0);
33
     upm::Hmc5883l* compass = new upm::Hmc5883l(0);
32
-    fprintf(stdout, "heading: %f\n", compass->heading());
34
+    int *pos;
35
+
36
+    compass->set_declination(0.2749); // Set your declination from true north in radians
37
+
38
+    while(true){
39
+        compass->update(); // Update the coordinates
40
+        pos = compass->coordinates();
41
+        fprintf(stdout, "coor: %5d %5d %5d ", pos[0], pos[1], pos[2]);
42
+        fprintf(stdout, "heading: %5.2f direction: %3.2f\n", compass->heading(), compass->direction());
43
+        sleep(1);
44
+    }
33
 //! [Interesting]
45
 //! [Interesting]
34
 
46
 
35
     return 0;
47
     return 0;

+ 20
- 3
src/hmc5883l/hmc5883l.cxx View File

1
 /*
1
 /*
2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
3
  * Copyright (c) 2014 Intel Corporation.
4
  * Copyright (c) 2014 Intel Corporation.
4
  *
5
  *
5
  * Permission is hereby granted, free of charge, to any person obtaining
6
  * Permission is hereby granted, free of charge, to any person obtaining
94
     Hmc5883l::update();
95
     Hmc5883l::update();
95
 }
96
 }
96
 
97
 
97
-int
98
+mraa_result_t
98
 Hmc5883l::update(void)
99
 Hmc5883l::update(void)
99
 {
100
 {
100
     mraa_i2c_address(m_i2c, HMC5883L_I2C_ADDR);
101
     mraa_i2c_address(m_i2c, HMC5883L_I2C_ADDR);
116
 float
117
 float
117
 Hmc5883l::direction(void)
118
 Hmc5883l::direction(void)
118
 {
119
 {
119
-    return atan2(m_coor[1] * SCALE_0_92_MG, m_coor[0] * SCALE_0_92_MG);
120
+    return atan2(m_coor[1] * SCALE_0_92_MG, m_coor[0] * SCALE_0_92_MG) + m_declination;
120
 }
121
 }
121
 
122
 
122
 float
123
 float
123
 Hmc5883l::heading(void)
124
 Hmc5883l::heading(void)
124
 {
125
 {
125
-    return Hmc5883l::direction() * 180/M_PI;
126
+    float dir = Hmc5883l::direction() * 180/M_PI;
127
+    if(dir < 0){
128
+        dir += 360.0;
129
+    }
130
+    return dir;
126
 }
131
 }
127
 
132
 
128
 int*
133
 int*
130
 {
135
 {
131
     return &m_coor[0];
136
     return &m_coor[0];
132
 }
137
 }
138
+
139
+void
140
+Hmc5883l::set_declination(float dec)
141
+{
142
+    m_declination = dec;
143
+}
144
+
145
+float
146
+Hmc5883l::get_declination()
147
+{
148
+    return m_declination;
149
+}

+ 15
- 1
src/hmc5883l/hmc5883l.h View File

1
 /*
1
 /*
2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
3
  * Copyright (c) 2014 Intel Corporation.
4
  * Copyright (c) 2014 Intel Corporation.
4
  *
5
  *
5
  * Permission is hereby granted, free of charge, to any person obtaining
6
  * Permission is hereby granted, free of charge, to any person obtaining
78
      *
79
      *
79
      * @return 0 for success
80
      * @return 0 for success
80
      */
81
      */
81
-    int update();
82
+    mraa_result_t update();
83
+
84
+    /**
85
+     * Sets the magnetic declination for better calibration
86
+     */
87
+    void set_declination(float dec);
88
+
89
+    /**
90
+     * Gets the current magnetic declination value
91
+     *
92
+     * @return magnetic declination as a float
93
+     */
94
+    float get_declination();
82
 private:
95
 private:
83
     int m_coor[3];
96
     int m_coor[3];
97
+    float m_declination;
84
     uint8_t m_rx_tx_buf[MAX_BUFFER_LENGTH];
98
     uint8_t m_rx_tx_buf[MAX_BUFFER_LENGTH];
85
     mraa_i2c_context m_i2c;
99
     mraa_i2c_context m_i2c;
86
 };
100
 };