Pārlūkot izejas kodu

hmc5883l: changed to standard heading and added declination

Signed-off-by: Mihai Tudor Panu <mihai.t.panu@intel.com>
Mihai Tudor Panu 10 gadus atpakaļ
vecāks
revīzija
ea7b7a23fa
3 mainītis faili ar 48 papildinājumiem un 5 dzēšanām
  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 Parādīt failu

@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
3 4
  * Copyright (c) 2014 Intel Corporation.
4 5
  *
5 6
  * Permission is hereby granted, free of charge, to any person obtaining
@@ -22,6 +23,7 @@
22 23
  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 24
  */
24 25
 
26
+#include <unistd.h>
25 27
 #include "hmc5883l.h"
26 28
 
27 29
 int
@@ -29,7 +31,17 @@ main(int argc, char **argv)
29 31
 {
30 32
 //! [Interesting]
31 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 45
 //! [Interesting]
34 46
 
35 47
     return 0;

+ 20
- 3
src/hmc5883l/hmc5883l.cxx Parādīt failu

@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
3 4
  * Copyright (c) 2014 Intel Corporation.
4 5
  *
5 6
  * Permission is hereby granted, free of charge, to any person obtaining
@@ -94,7 +95,7 @@ Hmc5883l::Hmc5883l(int bus)
94 95
     Hmc5883l::update();
95 96
 }
96 97
 
97
-int
98
+mraa_result_t
98 99
 Hmc5883l::update(void)
99 100
 {
100 101
     mraa_i2c_address(m_i2c, HMC5883L_I2C_ADDR);
@@ -116,13 +117,17 @@ Hmc5883l::update(void)
116 117
 float
117 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 123
 float
123 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 133
 int*
@@ -130,3 +135,15 @@ Hmc5883l::coordinates(void)
130 135
 {
131 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 Parādīt failu

@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Contributions: Mihai Tudor Panu <mihai.t.panu@intel.com>
3 4
  * Copyright (c) 2014 Intel Corporation.
4 5
  *
5 6
  * Permission is hereby granted, free of charge, to any person obtaining
@@ -78,9 +79,22 @@ public:
78 79
      *
79 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 95
 private:
83 96
     int m_coor[3];
97
+    float m_declination;
84 98
     uint8_t m_rx_tx_buf[MAX_BUFFER_LENGTH];
85 99
     mraa_i2c_context m_i2c;
86 100
 };