瀏覽代碼

itg3200: Grove 3-axis Gyroscope

Signed-off-by: Mihai Tudor Panu <mihai.t.panu@intel.com>
Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
Mihai Tudor Panu 10 年之前
父節點
當前提交
be8336d0e8

二進制
docs/images/itg3200.jpeg 查看文件


+ 3
- 0
examples/CMakeLists.txt 查看文件

@@ -48,6 +48,7 @@ add_executable (htu21d-example htu21d.cxx)
48 48
 add_executable (mpl3115a2-example mpl3115a2.cxx)
49 49
 add_executable (ldt0028-example ldt0028.cxx)
50 50
 add_executable (am2315-example am2315.cxx)
51
+add_executable (itg3200-example itg3200.cxx)
51 52
 
52 53
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
53 54
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -85,6 +86,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/htu21d)
85 86
 include_directories (${PROJECT_SOURCE_DIR}/src/mpl3115a2)
86 87
 include_directories (${PROJECT_SOURCE_DIR}/src/ldt0028)
87 88
 include_directories (${PROJECT_SOURCE_DIR}/src/am2315)
89
+include_directories (${PROJECT_SOURCE_DIR}/src/itg3200)
88 90
 
89 91
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
90 92
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
@@ -136,3 +138,4 @@ target_link_libraries (htu21d-example htu21d ${CMAKE_THREAD_LIBS_INIT})
136 138
 target_link_libraries (mpl3115a2-example mpl3115a2 ${CMAKE_THREAD_LIBS_INIT})
137 139
 target_link_libraries (ldt0028-example ldt0028 ${CMAKE_THREAD_LIBS_INIT})
138 140
 target_link_libraries (am2315-example am2315 ${CMAKE_THREAD_LIBS_INIT})
141
+target_link_libraries (itg3200-example itg3200 ${CMAKE_THREAD_LIBS_INIT})

+ 51
- 0
examples/itg3200.cxx 查看文件

@@ -0,0 +1,51 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@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 <unistd.h>
26
+#include "itg3200.h"
27
+
28
+int
29
+main(int argc, char **argv)
30
+{
31
+//! [Interesting]
32
+    int16_t *rot;
33
+    float *ang;
34
+
35
+    // Note: Sensor not supported on Intel Edison with Arduino breakout
36
+    upm::Itg3200* gyro = new upm::Itg3200(0);
37
+
38
+    while(true){
39
+        gyro->update(); // Update the data
40
+        rot = gyro->getRawValues(); // Read raw sensor data
41
+        ang = gyro->getRotation(); // Read rotational speed (deg/sec)
42
+        fprintf(stdout, "Raw: %6d %6d %6d\n", rot[0], rot[1], rot[2]);
43
+        fprintf(stdout, "AngX: %5.2f\n", ang[0]);
44
+        fprintf(stdout, "AngY: %5.2f\n", ang[1]);
45
+        fprintf(stdout, "AngZ: %5.2f\n", ang[2]);
46
+        fprintf(stdout, "Temp: %5.2f Raw: %6d\n", gyro->getTemperature(), gyro->getRawTemp());
47
+        sleep(1);
48
+    }
49
+//! [Interesting]
50
+    return 0;
51
+}

+ 5
- 0
src/itg3200/CMakeLists.txt 查看文件

@@ -0,0 +1,5 @@
1
+set (libname "itg3200")
2
+set (libdescription "libupm Digital Gyro")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 155
- 0
src/itg3200/itg3200.cxx 查看文件

@@ -0,0 +1,155 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@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 <iostream>
26
+#include <unistd.h>
27
+#include "math.h"
28
+#include "itg3200.h"
29
+
30
+#define READ_BUFFER_LENGTH 8
31
+
32
+//address and id
33
+#define ITG3200_I2C_ADDR 0x68
34
+#define ITG3200_ID 0x00
35
+
36
+//configuration registers
37
+#define ITG3200_SMPLRT_DIV 0x15
38
+#define ITG3200_DLPF_FS 0x16
39
+
40
+//interrupt registers
41
+#define ITG3200_INT_CFG 0x17
42
+#define ITG3200_INT_STATUS 0x1A
43
+
44
+//data registers (read only)
45
+#define ITG3200_TEMP_H 0x1B
46
+#define ITG3200_TEMP_L 0x1C
47
+#define ITG3200_XOUT_H 0x1D
48
+#define ITG3200_XOUT_L 0x1E
49
+#define ITG3200_YOUT_H 0x1F
50
+#define ITG3200_YOUT_L 0x20
51
+#define ITG3200_ZOUT_H 0x21
52
+#define ITG3200_ZOUT_L 0x22
53
+#define DATA_REG_SIZE 8
54
+
55
+//power management
56
+#define ITG3200_PWR_MGM 0x3E
57
+
58
+//useful values
59
+#define ITG3200_RESET 0x80
60
+#define ITG3200_SLEEP 0x40
61
+#define ITG3200_WAKEUP 0x00
62
+
63
+using namespace upm;
64
+
65
+Itg3200::Itg3200(int bus)
66
+{
67
+    //init bus and reset chip
68
+    m_i2c = mraa_i2c_init(bus);
69
+
70
+    mraa_i2c_address(m_i2c, ITG3200_I2C_ADDR);
71
+    m_buffer[0] = ITG3200_PWR_MGM;
72
+    m_buffer[1] = ITG3200_RESET;
73
+    mraa_i2c_write(m_i2c, m_buffer, 2);
74
+
75
+    Itg3200::calibrate();
76
+    Itg3200::update();
77
+}
78
+
79
+Itg3200::~Itg3200()
80
+{
81
+    mraa_i2c_stop(m_i2c);
82
+}
83
+
84
+mraa_result_t
85
+Itg3200::calibrate(void)
86
+{
87
+    int reads = 600;
88
+    int delay = 4000; // 4 milliseconds
89
+    int skip = 5; // initial samples to skip
90
+    int temp[3] = {0};
91
+
92
+    for(int i = 0; i < reads; i++){
93
+
94
+        Itg3200::update();
95
+        if (i > skip){
96
+            for (int j = 0; j < 3; j++){
97
+                temp[j] += m_rotation[j];
98
+            }
99
+        }
100
+        usleep(delay);
101
+    }
102
+
103
+    for(int i = 0; i < 3; i++){
104
+        m_offsets[i] = (-1) * temp[i] / (reads - skip);
105
+    }
106
+}
107
+
108
+float
109
+Itg3200::getTemperature()
110
+{
111
+    return 35.0 + (m_temperature + 13200.0) / 280.0;
112
+}
113
+
114
+float*
115
+Itg3200::getRotation()
116
+{
117
+    for(int i = 0; i < 3; i++){
118
+        m_angle[i] = m_rotation[i]/14.375;
119
+    }
120
+    return &m_angle[0];
121
+}
122
+
123
+int16_t*
124
+Itg3200::getRawValues()
125
+{
126
+    return &m_rotation[0];
127
+}
128
+
129
+int16_t
130
+Itg3200::getRawTemp()
131
+{
132
+    return m_temperature;
133
+}
134
+
135
+mraa_result_t
136
+Itg3200::update(void)
137
+{
138
+    mraa_i2c_address(m_i2c, ITG3200_I2C_ADDR);
139
+    mraa_i2c_write_byte(m_i2c, ITG3200_TEMP_H);
140
+
141
+    mraa_i2c_address(m_i2c, ITG3200_I2C_ADDR);
142
+    mraa_i2c_read(m_i2c, m_buffer, DATA_REG_SIZE);
143
+
144
+    //temp
145
+    //
146
+    m_temperature = (m_buffer[0] << 8 ) | m_buffer[1];
147
+    // x
148
+    m_rotation[0] = ((m_buffer[2] << 8 ) | m_buffer[3]) + m_offsets[0];
149
+    // y
150
+    m_rotation[1] = ((m_buffer[4] << 8 ) | m_buffer[5]) + m_offsets[1];
151
+    // z
152
+    m_rotation[2] = ((m_buffer[6] << 8 ) | m_buffer[7]) + m_offsets[2];
153
+
154
+    return MRAA_SUCCESS;
155
+}

+ 115
- 0
src/itg3200/itg3200.h 查看文件

@@ -0,0 +1,115 @@
1
+/*
2
+ * Author: Mihai Tudor Panu <mihai.t.panu@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 <mraa/i2c.h>
27
+
28
+#define READ_BUFFER_LENGTH 8
29
+
30
+namespace upm {
31
+
32
+/**
33
+ * @brief Itg3200 gyroscope library
34
+ * @defgroup itg3200 libupm-itg3200
35
+ */
36
+
37
+/**
38
+ * @brief C++ API for Itg3200 (3-axis digital gyroscope)
39
+ *
40
+ * The InvenSense Itg3200 is a 3-axis digital gyroscope.
41
+ * (https://www.sparkfun.com/datasheets/Sensors/Gyro/PS-ITG-3200-00-01.4.pdf)
42
+ * This sensor has been tested and can run at either 3V3 or 5V on the Intel Galileo.<br>
43
+ * <strong>However</strong>, it is incompatible and will not be detected on the I2C bus
44
+ * by the Intel Edison using the Arduino breakout board.
45
+ *
46
+ * @ingroup itg3200 i2c
47
+ * @snippet itg3200.cxx Interesting
48
+ * @image html itg3200.jpeg
49
+ */
50
+class Itg3200 {
51
+public:
52
+    /**
53
+     * Creates an Itg3200 object
54
+     *
55
+     * @param bus number of used i2c bus
56
+     */
57
+    Itg3200(int bus);
58
+
59
+    /**
60
+     * Itg3200 object destructor
61
+     */
62
+    ~Itg3200();
63
+
64
+    /**
65
+     * Calibrates the sensor to 0 on all axes. Sensor needs to be resting for accurate calibration.
66
+     * Takes about 3 seconds and is also called by constructor on object creation.
67
+     *
68
+     * @return 0 for successful calibration
69
+     */
70
+    mraa_result_t calibrate();
71
+
72
+    /**
73
+     * Returns the temperature reading from the integrated temperature sensor in Celsius degrees
74
+     *
75
+     * @return float temperature in Celsius degrees
76
+     */
77
+    float getTemperature();
78
+
79
+    /**
80
+     * Returns a pointer to an float[3] that contains computed rotational speeds (angular velocities)
81
+     *
82
+     * @return float* to an float[3]
83
+     */
84
+    float* getRotation();
85
+
86
+    /**
87
+     * Returns a pointer to an int[3] that contains the raw register values for X, Y and Z
88
+     *
89
+     * @return int* to an int[3]
90
+     */
91
+    int16_t* getRawValues();
92
+
93
+    /**
94
+     * Returns an int that contains the raw register value for the temperature
95
+     *
96
+     * @return int raw temperature
97
+     */
98
+    int16_t getRawTemp();
99
+
100
+    /**
101
+     * Updates the rotational values and temperature by reading from i2c bus
102
+     *
103
+     * @return 0 for success
104
+     */
105
+    mraa_result_t update();
106
+private:
107
+    float m_angle[3];
108
+    int16_t m_rotation[3];
109
+    int16_t m_offsets[3];
110
+    int16_t m_temperature;
111
+    uint8_t m_buffer[READ_BUFFER_LENGTH];
112
+    mraa_i2c_context m_i2c;
113
+};
114
+
115
+}

+ 10
- 0
src/itg3200/jsupm_itg3200.i 查看文件

@@ -0,0 +1,10 @@
1
+%module jsupm_itg3200
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "itg3200.h"
6
+%}
7
+
8
+%include "itg3200.h"
9
+
10
+%include <carrays.i>

+ 13
- 0
src/itg3200/pyupm_itg3200.i 查看文件

@@ -0,0 +1,13 @@
1
+%module pyupm_itg3200
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+#ifdef DOXYGEN
7
+%include "itg3200_doc.i"
8
+#endif
9
+
10
+%include "itg3200.h"
11
+%{
12
+    #include "itg3200.h"
13
+%}