Browse Source

mlx90614: added new sensor (ir temperature), there is an issue with repeated start bit in i2c thus the sensor return incorrect data

Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Kiveisha Yevgeniy 10 years ago
parent
commit
59a66b41bb

+ 3
- 0
examples/CMakeLists.txt View File

@@ -27,6 +27,7 @@ add_executable (max5487-example max5487.cxx)
27 27
 add_executable (nrf8001-broadcast-example nrf8001_broadcast.cxx)
28 28
 add_executable (nrf8001-helloworld-example nrf8001_helloworld.cxx)
29 29
 add_executable (lpd8806-example lpd8806-example.cxx)
30
+add_executable (mlx90614-example mlx90614-example.cxx)
30 31
 
31 32
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
32 33
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
@@ -51,6 +52,7 @@ include_directories (${PROJECT_SOURCE_DIR}/src/max31723)
51 52
 include_directories (${PROJECT_SOURCE_DIR}/src/max5487)
52 53
 include_directories (${PROJECT_SOURCE_DIR}/src/nrf8001)
53 54
 include_directories (${PROJECT_SOURCE_DIR}/src/lpd8806)
55
+include_directories (${PROJECT_SOURCE_DIR}/src/mlx90614)
54 56
 
55 57
 target_link_libraries (compass hmc5883l ${CMAKE_THREAD_LIBS_INIT})
56 58
 target_link_libraries (groveled grove ${CMAKE_THREAD_LIBS_INIT})
@@ -81,3 +83,4 @@ target_link_libraries (max5487-example max5487 ${CMAKE_THREAD_LIBS_INIT})
81 83
 target_link_libraries (nrf8001-broadcast-example nrf8001 ${CMAKE_THREAD_LIBS_INIT})
82 84
 target_link_libraries (nrf8001-helloworld-example nrf8001 ${CMAKE_THREAD_LIBS_INIT})
83 85
 target_link_libraries (lpd8806-example lpd8806 ${CMAKE_THREAD_LIBS_INIT})
86
+target_link_libraries (mlx90614-example mlx90614 ${CMAKE_THREAD_LIBS_INIT})

+ 60
- 0
examples/mlx90614-example.cxx View File

@@ -0,0 +1,60 @@
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 "mlx90614.h"
28
+#include <signal.h>
29
+
30
+int doWork = 0;
31
+upm::MLX90614 *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
+    sensor = new upm::MLX90614(0, DEVICE_ADDR);
48
+
49
+    while (!doWork) {
50
+        std::cout << "Object Temperature (" << sensor->readObjectTempC() << ")  Ambient Temperature (" << sensor->readAmbientTempC() << ")" << std::endl;
51
+        usleep (1000000);
52
+    }
53
+    //! [Interesting]
54
+
55
+    std::cout << "exiting application" << std::endl;
56
+
57
+    delete sensor;
58
+
59
+    return 0;
60
+}

BIN
src/mlx90614/.DS_Store View File


BIN
src/mlx90614/._.DS_Store View File


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

@@ -0,0 +1,5 @@
1
+set (libname "mlx90614")
2
+set (libdescription "Infrared thermometer designed for non-contact emperature sensing")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 8
- 0
src/mlx90614/jsupm_mlx90614.i View File

@@ -0,0 +1,8 @@
1
+%module jsupm_mlx90614
2
+%include "../upm.i"
3
+
4
+%{
5
+    #include "mlx90614.h"
6
+%}
7
+
8
+%include "mlx90614.h"

+ 129
- 0
src/mlx90614/mlx90614.cxx View File

@@ -0,0 +1,129 @@
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 "mlx90614.h"
30
+
31
+using namespace upm;
32
+
33
+struct MLX90614Exception : public std::exception {
34
+    std::string message;
35
+    MLX90614Exception (std::string msg) : message (msg) { }
36
+    ~MLX90614Exception () throw () { }
37
+    const char* what() const throw () { return message.c_str(); }
38
+};
39
+
40
+MLX90614::MLX90614 (int bus, int devAddr) {
41
+    m_name = "MLX90614";
42
+
43
+    m_i2cAddr = devAddr;
44
+    m_bus = bus;
45
+
46
+    m_i2Ctx = mraa_i2c_init(m_bus);
47
+
48
+    mraa_result_t ret = mraa_i2c_address(m_i2Ctx, m_i2cAddr);
49
+    if (ret != MRAA_SUCCESS) {
50
+        throw MLX90614Exception ("Couldn't initilize I2C.");
51
+    }
52
+}
53
+
54
+MLX90614::~MLX90614() {
55
+    mraa_i2c_stop(m_i2Ctx);
56
+}
57
+
58
+float
59
+MLX90614::readObjectTempF(void) {
60
+    return (readTemperature (MLX90614_TOBJ1) * 9 / 5) + 32;
61
+}
62
+
63
+float
64
+MLX90614::readAmbientTempF(void) {
65
+    return (readTemperature (MLX90614_TA) * 9 / 5) + 32;
66
+}
67
+
68
+float
69
+MLX90614::readObjectTempC(void) {
70
+    return readTemperature (MLX90614_TOBJ1);
71
+}
72
+
73
+float
74
+MLX90614::readAmbientTempC(void) {
75
+    return readTemperature (MLX90614_TA);
76
+}
77
+
78
+/*
79
+ * **************
80
+ *  private area
81
+ * **************
82
+ */
83
+uint16_t
84
+MLX90614::i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer) {
85
+    int readByte = 0;
86
+
87
+    if (m_i2Ctx == NULL) {
88
+        throw MLX90614Exception ("Couldn't find initilized I2C.");
89
+    }
90
+
91
+    mraa_i2c_address(m_i2Ctx, m_i2cAddr);
92
+    mraa_i2c_write_byte(m_i2Ctx, reg);
93
+
94
+    readByte = mraa_i2c_read(m_i2Ctx, buffer, len);
95
+    return readByte;
96
+}
97
+
98
+mraa_result_t
99
+MLX90614::i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer) {
100
+    mraa_result_t error = MRAA_SUCCESS;
101
+
102
+    if (m_i2Ctx == NULL) {
103
+        throw MLX90614Exception ("Couldn't find initilized I2C.");
104
+    }
105
+
106
+    error = mraa_i2c_address (m_i2Ctx, m_i2cAddr);
107
+    error = mraa_i2c_write (m_i2Ctx, buffer, len);
108
+
109
+    return error;
110
+}
111
+
112
+float
113
+MLX90614::readTemperature (uint8_t address) {
114
+    uint8_t     buffer[3];
115
+    float       temperature = 0;
116
+
117
+    /*  Reading temperature from sensor.
118
+        Answer contained of 3 bytes (TEMP_LSB | TEMP_MSB | PEC)
119
+     */
120
+    if (i2cReadReg_N (address, 3, buffer) > 2) {
121
+        temperature = buffer[0];
122
+        temperature = buffer[1] << 8;
123
+
124
+        temperature *= .02;
125
+        temperature -= 273.15;
126
+    }
127
+
128
+    return temperature;
129
+}

+ 115
- 0
src/mlx90614/mlx90614.h View File

@@ -0,0 +1,115 @@
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 <mraa/i2c.h>
28
+
29
+#define DEVICE_ADDR         0x5A // device address
30
+
31
+// RAM
32
+#define MLX90614_RAWIR1     0x04
33
+#define MLX90614_RAWIR2     0x05
34
+#define MLX90614_TA         0x06
35
+#define MLX90614_TOBJ1      0x07
36
+#define MLX90614_TOBJ2      0x08
37
+// EEPROM
38
+#define MLX90614_TOMAX      0x20
39
+#define MLX90614_TOMIN      0x21
40
+#define MLX90614_PWMCTRL    0x22
41
+#define MLX90614_TARANGE    0x23
42
+#define MLX90614_EMISS      0x24
43
+#define MLX90614_CONFIG     0x25
44
+#define MLX90614_ADDR       0x0E
45
+#define MLX90614_ID1        0x3C
46
+#define MLX90614_ID2        0x3D
47
+#define MLX90614_ID3        0x3E
48
+#define MLX90614_ID4        0x3F
49
+
50
+#define HIGH                    1
51
+#define LOW                     0
52
+
53
+namespace upm {
54
+
55
+/**
56
+ * @brief C++ API for MLX90614 
57
+ *
58
+ * This file defines the MLX90614 C++ interface for libmlx90614
59
+ *
60
+ */
61
+class MLX90614 {
62
+    public:
63
+
64
+        /**
65
+         * Instanciates a MLX90614 object
66
+         *
67
+         * @param bus number of used bus
68
+         * @param devAddr addres of used i2c device
69
+         */
70
+        MLX90614 (int bus, int devAddr);
71
+
72
+        /**
73
+         * MLX90614 object destructor, basicaly it close i2c connection.
74
+         */
75
+        ~MLX90614 ();
76
+
77
+        /**
78
+         * Read object temperature in Fahrenheit
79
+         */
80
+        float readObjectTempF(void);
81
+
82
+        /**
83
+         * Read ambient temperature in Fahrenheit
84
+         */
85
+        float readAmbientTempF(void);
86
+        /**
87
+         * Read object temperature in Celsius
88
+         */
89
+        float readObjectTempC(void);
90
+
91
+        /**
92
+         * Read ambient temperature in Celsius
93
+         */
94
+        float readAmbientTempC(void);
95
+
96
+        /**
97
+         * Return name of the component
98
+         */
99
+        std::string name()
100
+        {
101
+            return m_name;
102
+        }
103
+    private:
104
+        std::string m_name;
105
+
106
+        int m_i2cAddr;
107
+        int m_bus;
108
+        mraa_i2c_context m_i2Ctx;
109
+
110
+        uint16_t i2cReadReg_N (int reg, unsigned int len, uint8_t * buffer);
111
+        mraa_result_t i2cWriteReg_N (uint8_t reg, unsigned int len, uint8_t * buffer);
112
+        float readTemperature (uint8_t address);
113
+};
114
+
115
+}

+ 9
- 0
src/mlx90614/pyupm_mlx90614.i View File

@@ -0,0 +1,9 @@
1
+%module pyupm_mlx90614
2
+%include "../upm.i"
3
+
4
+%feature("autodoc", "3");
5
+
6
+%include "mlx90614.h"
7
+%{
8
+    #include "mlx90614.h"
9
+%}