Ver código fonte

gy65: Added new digital pressure sensor

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 anos atrás
pai
commit
c18a9433fc

+ 3
- 0
examples/CMakeLists.txt Ver arquivo

@@ -16,6 +16,7 @@ add_executable (proximity max44000.cxx)
16 16
 add_executable (accelerometer mma7455.cxx)
17 17
 add_executable (lcd st7735.cxx)
18 18
 add_executable (max31855-example max31855.cxx)
19
+add_executable (gy65-example gy65.cxx)
19 20
 
20 21
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
21 22
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -30,6 +31,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/max44000)
30 31
 include_directories (${PROJECT_SOURCE_DIR}/src/mma7455)
31 32
 include_directories (${PROJECT_SOURCE_DIR}/src/st7735)
32 33
 include_directories (${PROJECT_SOURCE_DIR}/src/max31855)
34
+include_directories (${PROJECT_SOURCE_DIR}/src/gy65)
33 35
 
34 36
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
35 37
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -49,3 +51,4 @@ target_link_libraries (proximity max44000 ${CMAKE_THREAD_LIBS_INIT})
49 51
 target_link_libraries (accelerometer mma7455 ${CMAKE_THREAD_LIBS_INIT})
50 52
 target_link_libraries (lcd st7735 ${CMAKE_THREAD_LIBS_INIT})
51 53
 target_link_libraries (max31855-example max31855 ${CMAKE_THREAD_LIBS_INIT})
54
+target_link_libraries (gy65-example gy65 ${CMAKE_THREAD_LIBS_INIT})

+ 77
- 0
examples/gy65.cxx Ver arquivo

@@ -0,0 +1,77 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@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 <iostream>
27
+#include "gy65.h"
28
+#include <signal.h>
29
+
30
+int doWork = 0;
31
+upm::GY65 *sensor = NULL;
32
+
33
+void
34
+sig_handler(int signo)
35
+{
36
+    printf("got signal\n");
37
+    if (signo == SIGINT) {
38
+        printf("exiting application\n");
39
+        doWork = 1;
40
+    }
41
+}
42
+
43
+int
44
+main(int argc, char **argv)
45
+{
46
+    //! [Interesting]
47
+    uint32_t presure = 0;
48
+    float temperature = 0;
49
+    float altitude = 0;
50
+    uint32_t sealevel = 0;
51
+
52
+    sensor = new upm::GY65(0, ADDR);
53
+
54
+    while (!doWork) {
55
+        presure = sensor->getPressure ();
56
+        temperature = sensor->getTemperature ();
57
+        altitude = sensor->getAltitude ();
58
+        sealevel = sensor->getSealevelPressure ();
59
+
60
+        std::cout << "pressure value = " <<
61
+                    presure <<
62
+                    ", atitude value = " <<
63
+                    altitude <<
64
+                    ", sealevel value = " <<
65
+                    sealevel <<
66
+                    ", temperature = " <<
67
+                    temperature << std::endl;
68
+        usleep (100000);
69
+    }
70
+    //! [Interesting]
71
+
72
+    std::cout << "exiting application" << std::endl;
73
+
74
+    delete sensor;
75
+
76
+    return 0;
77
+}

+ 5
- 0
src/gy65/CMakeLists.txt Ver arquivo

@@ -0,0 +1,5 @@
1
+set (libname "gy65")
2
+set (libdescription "upm GY65")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 222
- 0
src/gy65/gy65.cxx Ver arquivo

@@ -0,0 +1,222 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@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 <stdlib.h>
28
+
29
+#include "gy65.h"
30
+
31
+using namespace upm;
32
+
33
+GY65::GY65 (int bus, int devAddr, uint8_t mode) {
34
+    m_name = "GY65";
35
+
36
+    m_controlAddr = devAddr;
37
+    m_bus = bus;
38
+
39
+    m_i2ControlCtx = maa_i2c_init(m_bus);
40
+
41
+    maa_result_t ret = maa_i2c_address(m_i2ControlCtx, m_controlAddr);
42
+    if (ret != MAA_SUCCESS) {
43
+        fprintf(stderr, "Messed up i2c bus\n");
44
+    }
45
+
46
+    if (i2cReadReg_8 (0xD0) != 0x55)  {
47
+        std::cout << "Error :: Cannot continue" << std::endl;
48
+        return;
49
+    }
50
+
51
+    if (mode > BMP085_ULTRAHIGHRES) {
52
+        mode = BMP085_ULTRAHIGHRES;
53
+    }
54
+    oversampling = mode;
55
+
56
+    /* read calibration data */
57
+    ac1 = i2cReadReg_16 (BMP085_CAL_AC1);
58
+    ac2 = i2cReadReg_16 (BMP085_CAL_AC2);
59
+    ac3 = i2cReadReg_16 (BMP085_CAL_AC3);
60
+    ac4 = i2cReadReg_16 (BMP085_CAL_AC4);
61
+    ac5 = i2cReadReg_16 (BMP085_CAL_AC5);
62
+    ac6 = i2cReadReg_16 (BMP085_CAL_AC6);
63
+
64
+    b1 = i2cReadReg_16 (BMP085_CAL_B1);
65
+    b2 = i2cReadReg_16 (BMP085_CAL_B2);
66
+
67
+    mb = i2cReadReg_16 (BMP085_CAL_MB);
68
+    mc = i2cReadReg_16 (BMP085_CAL_MC);
69
+    md = i2cReadReg_16 (BMP085_CAL_MD);
70
+}
71
+
72
+GY65::~GY65() {
73
+    maa_i2c_stop(m_i2ControlCtx);
74
+}
75
+
76
+int32_t
77
+GY65::getPressure () {
78
+    int32_t UT, UP, B3, B5, B6, X1, X2, X3, p;
79
+    uint32_t B4, B7;
80
+
81
+    UT = getTemperatureRaw();
82
+    UP = getPressureRaw();
83
+    B5 = computeB5(UT);
84
+
85
+    // do pressure calcs
86
+    B6 = B5 - 4000;
87
+    X1 = ((int32_t)b2 * ( (B6 * B6)>>12 )) >> 11;
88
+    X2 = ((int32_t)ac2 * B6) >> 11;
89
+    X3 = X1 + X2;
90
+    B3 = ((((int32_t)ac1*4 + X3) << oversampling) + 2) / 4;
91
+
92
+    X1 = ((int32_t)ac3 * B6) >> 13;
93
+    X2 = ((int32_t)b1 * ((B6 * B6) >> 12)) >> 16;
94
+    X3 = ((X1 + X2) + 2) >> 2;
95
+    B4 = ((uint32_t)ac4 * (uint32_t)(X3 + 32768)) >> 15;
96
+    B7 = ((uint32_t)UP - B3) * (uint32_t)( 50000UL >> oversampling );
97
+
98
+    if (B7 < 0x80000000) {
99
+        p = (B7 * 2) / B4;
100
+    } else {
101
+        p = (B7 / B4) * 2;
102
+    }
103
+    X1 = (p >> 8) * (p >> 8);
104
+    X1 = (X1 * 3038) >> 16;
105
+    X2 = (-7357 * p) >> 16;
106
+
107
+    p = p + ((X1 + X2 + (int32_t)3791)>>4);
108
+
109
+    return p;
110
+}
111
+
112
+int32_t
113
+GY65::getPressureRaw () {
114
+    uint32_t raw;
115
+
116
+    i2cWriteReg (BMP085_CONTROL, BMP085_READPRESSURECMD + (oversampling << 6));
117
+
118
+    if (oversampling == BMP085_ULTRALOWPOWER) {
119
+        usleep(5000);
120
+    } else if (oversampling == BMP085_STANDARD) {
121
+        usleep(8000);
122
+    } else if (oversampling == BMP085_HIGHRES) {
123
+        usleep(14000);
124
+    } else {
125
+        usleep(26000);
126
+    }
127
+
128
+    raw = i2cReadReg_16 (BMP085_PRESSUREDATA);
129
+
130
+    raw <<= 8;
131
+    raw |= i2cReadReg_8 (BMP085_PRESSUREDATA + 2);
132
+    raw >>= (8 - oversampling);
133
+
134
+    return raw;
135
+}
136
+
137
+int16_t
138
+GY65::getTemperatureRaw () {
139
+    i2cWriteReg (BMP085_CONTROL, BMP085_READTEMPCMD);
140
+    usleep(5000);
141
+    return i2cReadReg_16 (BMP085_TEMPDATA);
142
+}
143
+
144
+float
145
+GY65::getTemperature () {
146
+    int32_t UT, B5;     // following ds convention
147
+    float temp;
148
+
149
+    UT = getTemperatureRaw ();
150
+
151
+    B5 = computeB5 (UT);
152
+    temp = (B5 + 8) >> 4;
153
+    temp /= 10;
154
+
155
+    return temp;
156
+}
157
+
158
+int32_t
159
+GY65::getSealevelPressure(float altitudeMeters) {
160
+    float pressure = getPressure ();
161
+    return (int32_t)(pressure / pow(1.0-altitudeMeters/44330, 5.255));
162
+}
163
+
164
+float
165
+GY65::getAltitude (float sealevelPressure) {
166
+    float altitude;
167
+
168
+    float pressure = getPressure ();
169
+
170
+    altitude = 44330 * (1.0 - pow(pressure /sealevelPressure,0.1903));
171
+
172
+    return altitude;
173
+}
174
+
175
+int32_t
176
+GY65::computeB5(int32_t UT) {
177
+    int32_t X1 = (UT - (int32_t)ac6) * ((int32_t)ac5) >> 15;
178
+    int32_t X2 = ((int32_t)mc << 11) / (X1+(int32_t)md);
179
+
180
+    return X1 + X2;
181
+}
182
+
183
+maa_result_t
184
+GY65::i2cWriteReg (uint8_t reg, uint8_t value) {
185
+    maa_result_t error = MAA_SUCCESS;
186
+
187
+    uint8_t data[2] = { reg, value };
188
+    error = maa_i2c_address (m_i2ControlCtx, m_controlAddr);
189
+    error = maa_i2c_write (m_i2ControlCtx, data, 2);
190
+
191
+    return error;
192
+}
193
+
194
+uint16_t
195
+GY65::i2cReadReg_16 (int reg) {
196
+    uint16_t data;
197
+
198
+    maa_i2c_address(m_i2ControlCtx, m_controlAddr);
199
+    maa_i2c_write_byte(m_i2ControlCtx, reg);
200
+
201
+    maa_i2c_address(m_i2ControlCtx, m_controlAddr);
202
+    maa_i2c_read(m_i2ControlCtx, (uint8_t *)&data, 0x2);
203
+
204
+    uint8_t high = (data & 0xFF00) >> 8;
205
+    data = (data << 8) & 0xFF00;
206
+    data |= high;
207
+
208
+    return data;
209
+}
210
+
211
+uint8_t
212
+GY65::i2cReadReg_8 (int reg) {
213
+    uint8_t data;
214
+
215
+    maa_i2c_address(m_i2ControlCtx, m_controlAddr);
216
+    maa_i2c_write_byte(m_i2ControlCtx, reg);
217
+
218
+    maa_i2c_address(m_i2ControlCtx, m_controlAddr);
219
+    maa_i2c_read(m_i2ControlCtx, &data, 0x1);
220
+
221
+    return data;
222
+}

+ 161
- 0
src/gy65/gy65.h Ver arquivo

@@ -0,0 +1,161 @@
1
+/*
2
+ * Author: Yevgeniy Kiveisha <yevgeniy.kiveisha@intel.com>
3
+ * Copyright (c) 2014 Intel Corporation.
4
+ *
5
+ * Credits to Adafruit.
6
+ * Based on Adafruit BMP085 library.
7
+ *
8
+ * Permission is hereby granted, free of charge, to any person obtaining
9
+ * a copy of this software and associated documentation files (the
10
+ * "Software"), to deal in the Software without restriction, including
11
+ * without limitation the rights to use, copy, modify, merge, publish,
12
+ * distribute, sublicense, and/or sell copies of the Software, and to
13
+ * permit persons to whom the Software is furnished to do so, subject to
14
+ * the following conditions:
15
+ *
16
+ * The above copyright notice and this permission notice shall be
17
+ * included in all copies or substantial portions of the Software.
18
+ *
19
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
+ */
27
+#pragma once
28
+
29
+#include <string>
30
+#include <maa/i2c.h>
31
+#include <math.h>
32
+
33
+#define ADDR               0x77 // device address
34
+
35
+// registers address
36
+#define BMP085_ULTRALOWPOWER 0
37
+#define BMP085_STANDARD      1
38
+#define BMP085_HIGHRES       2
39
+#define BMP085_ULTRAHIGHRES  3
40
+#define BMP085_CAL_AC1           0xAA  // R   Calibration data (16 bits)
41
+#define BMP085_CAL_AC2           0xAC  // R   Calibration data (16 bits)
42
+#define BMP085_CAL_AC3           0xAE  // R   Calibration data (16 bits)
43
+#define BMP085_CAL_AC4           0xB0  // R   Calibration data (16 bits)
44
+#define BMP085_CAL_AC5           0xB2  // R   Calibration data (16 bits)
45
+#define BMP085_CAL_AC6           0xB4  // R   Calibration data (16 bits)
46
+#define BMP085_CAL_B1            0xB6  // R   Calibration data (16 bits)
47
+#define BMP085_CAL_B2            0xB8  // R   Calibration data (16 bits)
48
+#define BMP085_CAL_MB            0xBA  // R   Calibration data (16 bits)
49
+#define BMP085_CAL_MC            0xBC  // R   Calibration data (16 bits)
50
+#define BMP085_CAL_MD            0xBE  // R   Calibration data (16 bits)
51
+
52
+#define BMP085_CONTROL           0xF4
53
+#define BMP085_TEMPDATA          0xF6
54
+#define BMP085_PRESSUREDATA      0xF6
55
+#define BMP085_READTEMPCMD       0x2E
56
+#define BMP085_READPRESSURECMD   0x34
57
+
58
+#define HIGH               1
59
+#define LOW                0
60
+
61
+namespace upm {
62
+
63
+/**
64
+ * @brief C++ API for GY65 chip (Atmospheric Pressure Sensor)
65
+ *
66
+ * This file defines the gy65 C++ interface for libgy65
67
+ *
68
+ * @snippet gy65.cxx Interesting
69
+ */
70
+class GY65 {
71
+    public:
72
+        /**
73
+         * Instanciates a GY65 object
74
+         *
75
+         * @param bus number of used bus
76
+         * @param devAddr addres of used i2c device
77
+         */
78
+        GY65 (int bus, int devAddr, uint8_t mode = BMP085_ULTRAHIGHRES);
79
+
80
+        /**
81
+         * GY65 object destructor, basicaly it close i2c connection.
82
+         */
83
+        ~GY65 ();
84
+
85
+        /**
86
+         * Return calculated pressure
87
+         */
88
+        int32_t getPressure ();
89
+
90
+        /**
91
+         *
92
+         * Get raw pressure data
93
+         */
94
+        int32_t getPressureRaw ();
95
+
96
+        /**
97
+         * Get raw temperature data from chip
98
+         */
99
+        int16_t getTemperatureRaw ();
100
+
101
+        /**
102
+         * Return calculated temperature
103
+         */
104
+        float getTemperature ();
105
+
106
+        /**
107
+         * With given absolute altitude sea level can be calculated
108
+         *
109
+         * @param altitudeMeters altitude
110
+         */
111
+        int32_t getSealevelPressure(float altitudeMeters = 0);
112
+
113
+        /**
114
+         * With given sea level altitude in meters can be calculated
115
+         *
116
+         * @param sealevelPressure Sea level
117
+         */
118
+        float getAltitude (float sealevelPressure = 101325);
119
+
120
+        /**
121
+         * Calculation of B5 (check spec for more information)
122
+         *
123
+         * @param UT
124
+         */
125
+        int32_t computeB5 (int32_t UT);
126
+
127
+        /**
128
+         * Read two bytes register
129
+         *
130
+         * @param reg address of a register
131
+         */
132
+        uint16_t i2cReadReg_16 (int reg);
133
+
134
+        /**
135
+         * Write to one byte register
136
+         *
137
+         * @param reg address of a register
138
+         * @param value byte to be written
139
+         */
140
+        maa_result_t i2cWriteReg (uint8_t reg, uint8_t value);
141
+
142
+        /**
143
+         * Read one byte register
144
+         *
145
+         * @param reg address of a register
146
+         */
147
+        uint8_t i2cReadReg_8 (int reg);
148
+
149
+    private:
150
+        std::string m_name;
151
+
152
+        int m_controlAddr;
153
+        int m_bus;
154
+        maa_i2c_context m_i2ControlCtx;
155
+
156
+        uint8_t oversampling;
157
+        int16_t ac1, ac2, ac3, b1, b2, mb, mc, md;
158
+        uint16_t ac4, ac5, ac6;
159
+};
160
+
161
+}

+ 7
- 0
src/gy65/jsupm_gy65.i Ver arquivo

@@ -0,0 +1,7 @@
1
+%module jsupm_gy65
2
+
3
+%{
4
+    #include "gy65.h"
5
+%}
6
+
7
+%include "gy65.h"

+ 10
- 0
src/gy65/pyupm_gy65.i Ver arquivo

@@ -0,0 +1,10 @@
1
+%module pyupm_gy65
2
+
3
+%include "stdint.i"
4
+
5
+%feature("autodoc", "3");
6
+
7
+%include "gy65.h"
8
+%{
9
+    #include "gy65.h"
10
+%}