Sfoglia il codice sorgente

mma7455: added new accelerometer module

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 anni fa
parent
commit
1dd8626044

+ 3
- 0
examples/CMakeLists.txt Vedi File

@@ -13,6 +13,7 @@ add_executable (son-hcsr04 hcsr04.cxx)
13 13
 add_executable (oled-1308 oled-1308.cxx)
14 14
 add_executable (oled-1327 oled-1327.cxx)
15 15
 add_executable (proximity max44000.cxx)
16
+add_executable (accelerometer mma7455.cxx)
16 17
 
17 18
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
18 19
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -24,6 +25,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/nrf24l01)
24 25
 include_directories (${PROJECT_SOURCE_DIR}/src/servo)
25 26
 include_directories (${PROJECT_SOURCE_DIR}/src/hcsr04)
26 27
 include_directories (${PROJECT_SOURCE_DIR}/src/max44000)
28
+include_directories (${PROJECT_SOURCE_DIR}/src/mma7455)
27 29
 
28 30
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
29 31
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -40,3 +42,4 @@ target_link_libraries (son-hcsr04 hcsr04 ${CMAKE_THREAD_LIBS_INIT})
40 42
 target_link_libraries (oled-1308 i2clcd ${CMAKE_THREAD_LIBS_INIT})
41 43
 target_link_libraries (oled-1327 i2clcd ${CMAKE_THREAD_LIBS_INIT})
42 44
 target_link_libraries (proximity max44000 ${CMAKE_THREAD_LIBS_INIT})
45
+target_link_libraries (accelerometer mma7455 ${CMAKE_THREAD_LIBS_INIT})

+ 64
- 0
examples/mma7455.cxx Vedi File

@@ -0,0 +1,64 @@
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 "mma7455.h"
28
+#include <signal.h>
29
+#include <pthread.h>
30
+
31
+int doWork = 0;
32
+upm::MMA7455 *sensor = NULL;
33
+
34
+void
35
+sig_handler(int signo)
36
+{
37
+    printf("got signal\n");
38
+    if (signo == SIGINT) {
39
+        printf("exiting application\n");
40
+        doWork = 1;
41
+    }
42
+}
43
+
44
+int
45
+main(int argc, char **argv)
46
+{
47
+    //! [Interesting]
48
+    sensor = new upm::MMA7455(0, ADDR);
49
+    
50
+    short x, y, z;
51
+    while (!doWork) {
52
+        sensor->readData(&x, &y, &z);
53
+        std::cout << "Accelerometer X(" << x << ") Y(" << y << ") Z(" << z << ")" << std::endl;
54
+        usleep (100000);
55
+    }
56
+
57
+    //! [Interesting]
58
+
59
+    std::cout << "exiting application" << std::endl;
60
+
61
+    delete sensor;
62
+
63
+    return 0;
64
+}

+ 4
- 0
src/mma7455/CMakeLists.txt Vedi File

@@ -0,0 +1,4 @@
1
+set (libname "mma7455")
2
+set (libdecription "upm MMA7455")
3
+add_library (mma7455 SHARED mma7455.cxx)
4
+upm_module_init()

+ 7
- 0
src/mma7455/jsupm_mma7455.i Vedi File

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

+ 176
- 0
src/mma7455/mma7455.cxx Vedi File

@@ -0,0 +1,176 @@
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
+#include <string.h>
29
+#include <pthread.h>
30
+#include <math.h>
31
+
32
+#include "mma7455.h"
33
+
34
+using namespace upm;
35
+
36
+MMA7455::MMA7455 (int bus, int devAddr) {
37
+    unsigned char data   = 0;
38
+    int           nBytes = 0;
39
+    
40
+    m_name = "MMA7455";
41
+
42
+    m_controlAddr = devAddr;
43
+    m_bus = bus;
44
+
45
+    m_i2ControlCtx = maa_i2c_init(m_bus);
46
+
47
+    maa_result_t error = maa_i2c_address(m_i2ControlCtx, m_controlAddr);
48
+    if (error != MAA_SUCCESS) {
49
+        fprintf(stderr, "Messed up i2c bus\n");
50
+        return;
51
+    }
52
+    
53
+    // setting GLVL 0x1 (64LSB/g) and MODE 0x1 (Measurement Mode)
54
+    data = (BIT (MMA7455_GLVL0) | BIT (MMA7455_MODE0));
55
+    error = ic2WriteReg (MMA7455_MCTL, &data, 0x1);
56
+    if (error != MAA_SUCCESS) {
57
+        std::cout << "ERROR :: MMA7455 instance wan not created (Mode)" << std::endl;
58
+        return;
59
+    }
60
+    
61
+    if (MAA_SUCCESS != calibrate ()) {
62
+        std::cout << "ERROR :: MMA7455 instance wan not created (Calibrate)" << std::endl;
63
+        return;
64
+    }
65
+}
66
+
67
+MMA7455::~MMA7455() {
68
+    maa_i2c_stop(m_i2ControlCtx);
69
+}
70
+
71
+maa_result_t 
72
+MMA7455::calibrate () {
73
+    maa_result_t error = MAA_SUCCESS;
74
+    int i = 0;
75
+    
76
+    accelData xyz;
77
+    xyz.value.x = xyz.value.y = xyz.value.z = 0;
78
+    
79
+    do {
80
+        error = readData (&xyz.value.x, &xyz.value.y, &xyz.value.z);
81
+        if (MAA_SUCCESS != error) {
82
+            return error;
83
+        }
84
+        
85
+        xyz.value.x += 2 * -xyz.value.x;
86
+        xyz.value.y += 2 * -xyz.value.y;
87
+        xyz.value.z += 2 * -(xyz.value.z - 64);
88
+        
89
+        error = ic2WriteReg (MMA7455_XOFFL,  (unsigned char *) &xyz, 0x6);
90
+        if (error != MAA_SUCCESS) {
91
+            return error;
92
+        }
93
+    
94
+    } while ( ++i < 3 );
95
+    
96
+    return error;
97
+}
98
+
99
+maa_result_t 
100
+MMA7455::readData (short * ptrX, short * ptrY, short * ptrZ) {
101
+    accelData xyz;
102
+    unsigned char data = 0;
103
+    int nBytes = 0;
104
+    
105
+    /*do {
106
+        nBytes = ic2ReadReg (MMA7455_STATUS, &data, 0x1);
107
+    } while ( !(data & MMA7455_DRDY) && nBytes == MAA_SUCCESS);
108
+    
109
+    if (nBytes == MAA_SUCCESS) {
110
+        std::cout << "NO_GDB :: 1" << std::endl;
111
+        return MAA_SUCCESS;
112
+    }*/
113
+    
114
+    nBytes = ic2ReadReg (MMA7455_XOUTL, (unsigned char *) &xyz, 0x6);
115
+    if (nBytes == 0) {
116
+        std::cout << "NO_GDB :: 2" << std::endl;
117
+        return MAA_ERROR_UNSPECIFIED;
118
+    }
119
+    
120
+    if (xyz.reg.x_msb & 0x02) {
121
+        xyz.reg.x_msb |= 0xFC;
122
+    }
123
+    
124
+    if (xyz.reg.y_msb & 0x02) {
125
+        xyz.reg.y_msb |= 0xFC;
126
+    }
127
+    
128
+    if (xyz.reg.z_msb & 0x02) {
129
+        xyz.reg.z_msb |= 0xFC;
130
+    }
131
+
132
+    // The result is the g-force in units of 64 per 'g'.
133
+    *ptrX = xyz.value.x;
134
+    *ptrY = xyz.value.y;
135
+    *ptrZ = xyz.value.z;
136
+    
137
+    return MAA_SUCCESS;
138
+}
139
+
140
+int 
141
+MMA7455::ic2ReadReg (unsigned char reg, unsigned char * buf, unsigned char size) {
142
+    if (MAA_SUCCESS != maa_i2c_address(m_i2ControlCtx, m_controlAddr)) {
143
+        return 0;
144
+    }
145
+    
146
+    if (MAA_SUCCESS != maa_i2c_write_byte(m_i2ControlCtx, reg)) {
147
+        return 0;
148
+    }
149
+
150
+    if (MAA_SUCCESS != maa_i2c_address(m_i2ControlCtx, m_controlAddr)) {
151
+        return 0;
152
+    }
153
+    
154
+    return (int) maa_i2c_read(m_i2ControlCtx, buf, size);
155
+}
156
+
157
+maa_result_t 
158
+MMA7455::ic2WriteReg (unsigned char reg, unsigned char * buf, unsigned char size) {
159
+    maa_result_t error = MAA_SUCCESS;
160
+
161
+    uint8_t data[size + 1];
162
+    data[0] = reg;
163
+    memcpy(&data[1], buf, size);
164
+
165
+    error = maa_i2c_address (m_i2ControlCtx, m_controlAddr);
166
+    if (error != MAA_SUCCESS) {
167
+        return error;
168
+    }
169
+    error = maa_i2c_write (m_i2ControlCtx, data, size + 1);
170
+    if (error != MAA_SUCCESS) {
171
+        return error;
172
+    }
173
+
174
+    return error;
175
+}
176
+

+ 223
- 0
src/mma7455/mma7455.h Vedi File

@@ -0,0 +1,223 @@
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
+#pragma once
25
+
26
+#include <string>
27
+#include <maa/i2c.h>
28
+
29
+#define ADDR               0x1D // device address
30
+
31
+// Register names according to the datasheet.
32
+// Register 0x1C is sometimes called 'PW', and sometimes 'PD'.
33
+// The two reserved registers can not be used.
34
+#define MMA7455_XOUTL 0x00      // Read only, Output Value X LSB
35
+#define MMA7455_XOUTH 0x01      // Read only, Output Value X MSB
36
+#define MMA7455_YOUTL 0x02      // Read only, Output Value Y LSB
37
+#define MMA7455_YOUTH 0x03      // Read only, Output Value Y MSB
38
+#define MMA7455_ZOUTL 0x04      // Read only, Output Value Z LSB
39
+#define MMA7455_ZOUTH 0x05      // Read only, Output Value Z MSB
40
+#define MMA7455_XOUT8 0x06      // Read only, Output Value X 8 bits
41
+#define MMA7455_YOUT8 0x07      // Read only, Output Value Y 8 bits
42
+#define MMA7455_ZOUT8 0x08      // Read only, Output Value Z 8 bits
43
+#define MMA7455_STATUS 0x09     // Read only, Status Register
44
+#define MMA7455_DETSRC 0x0A     // Read only, Detection Source Register
45
+#define MMA7455_TOUT 0x0B       // Temperature Output Value (Optional)
46
+#define MMA7455_RESERVED1 0x0C  // Reserved
47
+#define MMA7455_I2CAD 0x0D      // Read/Write, I2C Device Address
48
+#define MMA7455_USRINF 0x0E     // Read only, User Information (Optional)
49
+#define MMA7455_WHOAMI 0x0F     // Read only, "Who am I" value (Optional)
50
+#define MMA7455_XOFFL 0x10      // Read/Write, Offset Drift X LSB
51
+#define MMA7455_XOFFH 0x11      // Read/Write, Offset Drift X MSB
52
+#define MMA7455_YOFFL 0x12      // Read/Write, Offset Drift Y LSB
53
+#define MMA7455_YOFFH 0x13      // Read/Write, Offset Drift Y MSB
54
+#define MMA7455_ZOFFL 0x14      // Read/Write, Offset Drift Z LSB
55
+#define MMA7455_ZOFFH 0x15      // Read/Write, Offset Drift Z MSB
56
+#define MMA7455_MCTL 0x16       // Read/Write, Mode Control Register 
57
+#define MMA7455_INTRST 0x17     // Read/Write, Interrupt Latch Reset
58
+#define MMA7455_CTL1 0x18       // Read/Write, Control 1 Register
59
+#define MMA7455_CTL2 0x19       // Read/Write, Control 2 Register
60
+#define MMA7455_LDTH 0x1A       // Read/Write, Level Detection Threshold Limit Value
61
+#define MMA7455_PDTH 0x1B       // Read/Write, Pulse Detection Threshold Limit Value
62
+#define MMA7455_PD 0x1C         // Read/Write, Pulse Duration Value
63
+#define MMA7455_LT 0x1D         // Read/Write, Latency Time Value (between pulses)
64
+#define MMA7455_TW 0x1E         // Read/Write, Time Window for Second Pulse Value
65
+#define MMA7455_RESERVED2 0x1F  // Reserved
66
+
67
+// Defines for the bits, to be able to change 
68
+// between bit number and binary definition.
69
+// By using the bit number, programming the MMA7455 
70
+// is like programming an AVR microcontroller.
71
+// But instead of using "(1<<X)", or "_BV(X)", 
72
+// the Arduino "bit(X)" is used.
73
+#define MMA7455_D0 0
74
+#define MMA7455_D1 1
75
+#define MMA7455_D2 2
76
+#define MMA7455_D3 3
77
+#define MMA7455_D4 4
78
+#define MMA7455_D5 5
79
+#define MMA7455_D6 6
80
+#define MMA7455_D7 7
81
+
82
+// Status Register
83
+#define MMA7455_DRDY MMA7455_D0
84
+#define MMA7455_DOVR MMA7455_D1
85
+#define MMA7455_PERR MMA7455_D2
86
+
87
+// Mode Control Register
88
+#define MMA7455_MODE0 MMA7455_D0
89
+#define MMA7455_MODE1 MMA7455_D1
90
+#define MMA7455_GLVL0 MMA7455_D2
91
+#define MMA7455_GLVL1 MMA7455_D3
92
+#define MMA7455_STON MMA7455_D4
93
+#define MMA7455_SPI3W MMA7455_D5
94
+#define MMA7455_DRPD MMA7455_D6
95
+
96
+// Control 1 Register
97
+#define MMA7455_INTPIN MMA7455_D0
98
+#define MMA7455_INTREG0 MMA7455_D1
99
+#define MMA7455_INTREG1 MMA7455_D2
100
+#define MMA7455_XDA MMA7455_D3
101
+#define MMA7455_YDA MMA7455_D4
102
+#define MMA7455_ZDA MMA7455_D5
103
+#define MMA7455_THOPT MMA7455_D6
104
+#define MMA7455_DFBW MMA7455_D7
105
+
106
+// Control 2 Register
107
+#define MMA7455_LDPL MMA7455_D0
108
+#define MMA7455_PDPL MMA7455_D1
109
+#define MMA7455_DRVO MMA7455_D2
110
+
111
+// Interrupt Latch Reset Register
112
+#define MMA7455_CLR_INT1 MMA7455_D0
113
+#define MMA7455_CLR_INT2 MMA7455_D1
114
+
115
+// Detection Source Register
116
+#define MMA7455_INT1 MMA7455_D0
117
+#define MMA7455_INT2 MMA7455_D1
118
+#define MMA7455_PDZ MMA7455_D2
119
+#define MMA7455_PDY MMA7455_D3
120
+#define MMA7455_PDX MMA7455_D4
121
+#define MMA7455_LDZ MMA7455_D5
122
+#define MMA7455_LDY MMA7455_D6
123
+#define MMA7455_LDX MMA7455_D7
124
+
125
+// I2C Device Address Register
126
+#define MMA7455_I2CDIS MMA7455_D7
127
+
128
+#define HIGH               1
129
+#define LOW                0
130
+
131
+namespace upm {
132
+    
133
+union accelData {
134
+    struct {
135
+        unsigned char x_lsb;
136
+        unsigned char x_msb;
137
+        unsigned char y_lsb;
138
+        unsigned char y_msb;
139
+        unsigned char z_lsb;
140
+        unsigned char z_msb;
141
+    } reg;
142
+    
143
+    struct {
144
+        short x;
145
+        short y;
146
+        short z;
147
+    } value;
148
+};
149
+
150
+#define BIT(n) (1<<n)
151
+
152
+/**
153
+ * @brief C++ API for MMA7455 chip (accelerometer)
154
+ *
155
+ * This file defines the MMA7455 C++ interface for libmma7455
156
+ *
157
+ * @snippet mma7455.cxx Interesting
158
+ *
159
+ */
160
+class MMA7455 {
161
+    public:
162
+        /**
163
+         * Instanciates a MMA7455 object
164
+         *
165
+         * @param bus number of used bus
166
+         * @param devAddr addres of used i2c device
167
+         */
168
+        MMA7455 (int bus, int devAddr);
169
+
170
+        /**
171
+         * MMA7455 object destructor, basicaly it close i2c connection.
172
+         */
173
+        ~MMA7455 ();
174
+
175
+        /**
176
+         * Return name of the component
177
+         */
178
+        std::string name()
179
+        {
180
+            return m_name;
181
+        }
182
+        
183
+        /**
184
+         * Calibrate the sensor
185
+         */
186
+        maa_result_t calibrate ();
187
+        
188
+        /**
189
+         * Read X, Y and Z acceleration data
190
+         *
191
+         * @param ptrX X axis
192
+         * @param ptrY Y axis
193
+         * @param ptrZ Z axis
194
+         */
195
+        maa_result_t readData (short * ptrX, short * ptrY, short * ptrZ);
196
+        
197
+        /**
198
+         * 
199
+         *
200
+         * @param reg register address
201
+         * @param buf register data buffer
202
+         * @param size buffer size
203
+         */
204
+        int ic2ReadReg (unsigned char reg, unsigned char * buf, unsigned char size);
205
+        
206
+        /**
207
+         * 
208
+         *
209
+         * @param reg register address
210
+         * @param buf register data buffer 
211
+         * @param size buffer size
212
+         */
213
+        maa_result_t ic2WriteReg (unsigned char reg, unsigned char * buf, unsigned char size);
214
+
215
+    private:
216
+        std::string m_name;
217
+
218
+        int              m_controlAddr;
219
+        int              m_bus;
220
+        maa_i2c_context  m_i2ControlCtx;
221
+};
222
+
223
+}

+ 8
- 0
src/mma7455/pyupm_mma7455.i Vedi File

@@ -0,0 +1,8 @@
1
+%module pyupm_mma7455
2
+
3
+%feature("autodoc", "3");
4
+
5
+%include "mma7455.h"
6
+%{
7
+    #include "mma7455.h"
8
+%}