Browse Source

initial commit of upm with hmc5883l support

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
Brendan Le Foll 10 years ago
commit
d986654455
8 changed files with 239 additions and 0 deletions
  1. 14
    0
      CMakeLists.txt
  2. 5
    0
      README
  3. 5
    0
      examples/CMakeLists.txt
  4. 34
    0
      examples/compass.cxx
  5. 1
    0
      src/CMakeLists.txt
  6. 5
    0
      src/hmc5883l/CMakeLists.txt
  7. 130
    0
      src/hmc5883l/hmc5883l.cxx
  8. 45
    0
      src/hmc5883l/hmc5883l.h

+ 14
- 0
CMakeLists.txt View File

@@ -0,0 +1,14 @@
1
+cmake_minimum_required (VERSION 2.8)
2
+project (upm)
3
+
4
+set (SWIG_EXECUTABLE /usr/bin/swig)
5
+find_package (SWIG REQUIRED)
6
+include (${SWIG_USE_FILE})
7
+
8
+find_package(PkgConfig REQUIRED)
9
+pkg_check_modules(MAA maa>=0.1.1)
10
+
11
+set (CMAKE_SWIG_FLAGS "")
12
+
13
+add_subdirectory (src)
14
+add_subdirectory (examples)

+ 5
- 0
README View File

@@ -0,0 +1,5 @@
1
+UPM - Sensor/Actuator repository for Maa
2
+
3
+UPM is a high level repository for sensors that use maa.
4
+
5
+For more information on maa, see the maa README

+ 5
- 0
examples/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+add_executable (compass compass.cxx)
2
+
3
+include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
4
+
5
+target_link_libraries (compass hmc5883l)

+ 34
- 0
examples/compass.cxx View File

@@ -0,0 +1,34 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include "hmc5883l.h"
26
+
27
+int
28
+main(int argc, char **argv)
29
+{
30
+    upm::Hmc5883l* compass = new upm::Hmc5883l();                                                                                                      
31
+    fprintf(stdout, "heading: %f\n", compass->heading());
32
+
33
+    return 0;
34
+}

+ 1
- 0
src/CMakeLists.txt View File

@@ -0,0 +1 @@
1
+add_subdirectory (hmc5883l)

+ 5
- 0
src/hmc5883l/CMakeLists.txt View File

@@ -0,0 +1,5 @@
1
+add_library (hmc5883l SHARED hmc5883l.cxx)
2
+
3
+include_directories (${MAA_INCLUDE_DIR})
4
+
5
+target_link_libraries (hmc5883l ${MAA_LIBRARIES})

+ 130
- 0
src/hmc5883l/hmc5883l.cxx View File

@@ -0,0 +1,130 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+
25
+#include "math.h"
26
+#include "hmc5883l.h"
27
+
28
+#define MAX_BUFFER_LENGTH 6
29
+#define HMC5883L_I2C_ADDR 0x1E
30
+
31
+//configuration registers
32
+#define HMC5883L_CONF_REG_A 0x00
33
+#define HMC5883L_CONF_REG_B 0x01
34
+
35
+//mode register
36
+#define HMC5883L_MODE_REG 0x02
37
+
38
+//data register
39
+#define HMC5883L_X_MSB_REG 0
40
+#define HMC5883L_X_LSB_REG 1
41
+#define HMC5883L_Z_MSB_REG 2
42
+#define HMC5883L_Z_LSB_REG 3
43
+#define HMC5883L_Y_MSB_REG 4
44
+#define HMC5883L_Y_LSB_REG 5
45
+#define DATA_REG_SIZE 6
46
+
47
+//status register
48
+#define HMC5883L_STATUS_REG 0x09
49
+
50
+//ID registers
51
+#define HMC5883L_ID_A_REG 0x0A
52
+#define HMC5883L_ID_B_REG 0x0B
53
+#define HMC5883L_ID_C_REG 0x0C
54
+
55
+#define HMC5883L_CONT_MODE 0x00
56
+#define HMC5883L_DATA_REG 0x03
57
+
58
+//scales
59
+#define GA_0_88_REG 0x00 << 5
60
+#define GA_1_3_REG 0x01 << 5
61
+#define GA_1_9_REG 0x02 << 5
62
+#define GA_2_5_REG 0x03 << 5
63
+#define GA_4_0_REG 0x04 << 5
64
+#define GA_4_7_REG 0x05 << 5
65
+#define GA_5_6_REG 0x06 << 5
66
+#define GA_8_1_REG 0x07 << 5
67
+
68
+//digital resolutions
69
+#define SCALE_0_73_MG 0.73
70
+#define SCALE_0_92_MG 0.92
71
+#define SCALE_1_22_MG 1.22
72
+#define SCALE_1_52_MG 1.52
73
+#define SCALE_2_27_MG 2.27
74
+#define SCALE_2_56_MG 2.56
75
+#define SCALE_3_03_MG 3.03
76
+#define SCALE_4_35_MG 4.35
77
+
78
+using namespace upm;
79
+
80
+Hmc5883l::Hmc5883l()
81
+{
82
+    i2c = new maa::I2CSlave(26, 27);
83
+
84
+    i2c->address(HMC5883L_I2C_ADDR);
85
+    rx_tx_buf[0] = HMC5883L_CONF_REG_B;
86
+    rx_tx_buf[1] = GA_1_3_REG;
87
+    i2c->write(rx_tx_buf, 2);
88
+
89
+    i2c->address(HMC5883L_I2C_ADDR);
90
+    rx_tx_buf[0] = HMC5883L_MODE_REG;
91
+    rx_tx_buf[1] = HMC5883L_CONT_MODE;
92
+    i2c->write(rx_tx_buf, 2);
93
+
94
+    Hmc5883l::update();
95
+}
96
+
97
+int
98
+Hmc5883l::update(void)
99
+{
100
+    i2c->address(HMC5883L_I2C_ADDR);
101
+    i2c->write(HMC5883L_DATA_REG);
102
+
103
+    i2c->address(HMC5883L_I2C_ADDR);
104
+    i2c->read(rx_tx_buf, DATA_REG_SIZE);
105
+
106
+    // x
107
+    coor[0] = (rx_tx_buf[HMC5883L_X_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_X_LSB_REG] ;
108
+    // z
109
+    coor[2] = (rx_tx_buf[HMC5883L_Z_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Z_LSB_REG] ;
110
+    // y
111
+    coor[1] = (rx_tx_buf[HMC5883L_Y_MSB_REG] << 8 ) | rx_tx_buf[HMC5883L_Y_LSB_REG] ;
112
+}
113
+
114
+float
115
+Hmc5883l::direction(void)
116
+{
117
+    return atan2(coor[1] * SCALE_0_92_MG, coor[0] * SCALE_0_92_MG);
118
+}
119
+
120
+float
121
+Hmc5883l::heading(void)
122
+{
123
+    return Hmc5883l::direction() * 180/M_PI;
124
+}
125
+
126
+int*
127
+Hmc5883l::coordinates(void)
128
+{
129
+    return &coor[0];
130
+}

+ 45
- 0
src/hmc5883l/hmc5883l.h View File

@@ -0,0 +1,45 @@
1
+/*
2
+ * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files (the
7
+ * "Software"), to deal in the Software without restriction, including
8
+ * without limitation the rights to use, copy, modify, merge, publish,
9
+ * distribute, sublicense, and/or sell copies of the Software, and to
10
+ * permit persons to whom the Software is furnished to do so, subject to
11
+ * the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+#pragma once
25
+
26
+#include <maa/maa.h>
27
+
28
+#define MAX_BUFFER_LENGTH 6
29
+
30
+namespace upm {
31
+
32
+class Hmc5883l {
33
+public:
34
+    Hmc5883l();
35
+    float direction();
36
+    float heading();
37
+    int* coordinates();
38
+    int update();
39
+private:
40
+    int coor[3];
41
+    char rx_tx_buf[MAX_BUFFER_LENGTH];
42
+    maa::I2CSlave* i2c;
43
+};
44
+
45
+}