Browse Source

hx711: Initial implementation HX711 24bit ADC module

Signed-off-by: Rafael Neri <rafael.neri@gmail.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
Rafael Neri 10 years ago
parent
commit
df302ff5db

BIN
docs/images/hx711.jpeg View File


+ 3
- 1
examples/c++/CMakeLists.txt View File

103
 add_executable (adafruitss-example adafruitss.cxx)
103
 add_executable (adafruitss-example adafruitss.cxx)
104
 add_executable (adafruitms1438-example adafruitms1438.cxx)
104
 add_executable (adafruitms1438-example adafruitms1438.cxx)
105
 add_executable (adafruitms1438-stepper-example adafruitms1438-stepper.cxx)
105
 add_executable (adafruitms1438-stepper-example adafruitms1438-stepper.cxx)
106
+add_executable (hx711-example hx711.cxx)
106
 
107
 
107
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
108
 include_directories (${PROJECT_SOURCE_DIR}/src/hmc5883l)
108
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
109
 include_directories (${PROJECT_SOURCE_DIR}/src/grove)
186
 include_directories (${PROJECT_SOURCE_DIR}/src/groveeldriver)
187
 include_directories (${PROJECT_SOURCE_DIR}/src/groveeldriver)
187
 include_directories (${PROJECT_SOURCE_DIR}/src/adafruitss)
188
 include_directories (${PROJECT_SOURCE_DIR}/src/adafruitss)
188
 include_directories (${PROJECT_SOURCE_DIR}/src/adafruitms1438)
189
 include_directories (${PROJECT_SOURCE_DIR}/src/adafruitms1438)
190
+include_directories (${PROJECT_SOURCE_DIR}/src/hx711)
189
 
191
 
190
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
192
 target_link_libraries (hmc5883l-example hmc5883l ${CMAKE_THREAD_LIBS_INIT})
191
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
193
 target_link_libraries (groveled-example grove ${CMAKE_THREAD_LIBS_INIT})
290
 target_link_libraries (adafruitss-example adafruitss ${CMAKE_THREAD_LIBS_INIT})
292
 target_link_libraries (adafruitss-example adafruitss ${CMAKE_THREAD_LIBS_INIT})
291
 target_link_libraries (adafruitms1438-example adafruitms1438 ${CMAKE_THREAD_LIBS_INIT})
293
 target_link_libraries (adafruitms1438-example adafruitms1438 ${CMAKE_THREAD_LIBS_INIT})
292
 target_link_libraries (adafruitms1438-stepper-example adafruitms1438 ${CMAKE_THREAD_LIBS_INIT})
294
 target_link_libraries (adafruitms1438-stepper-example adafruitms1438 ${CMAKE_THREAD_LIBS_INIT})
293
-
295
+target_link_libraries (hx711-example hx711 ${CMAKE_THREAD_LIBS_INIT})

+ 43
- 0
examples/c++/hx711.cxx View File

1
+/*
2
+*
3
+* Author: Rafael da Mata Neri <rafael.neri@gmail.com>
4
+* Copyright (c) 2015 Intel Corporation.
5
+*
6
+*
7
+* Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+* this software and associated documentation files (the "Software"), to deal in
9
+* the Software without restriction, including without limitation the rights to
10
+* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
+* the Software, and to permit persons to whom the Software is furnished to do so,
12
+* subject to the following conditions:
13
+*
14
+* The above copyright notice and this permission notice shall be included in all
15
+* copies or substantial portions of the Software.
16
+*
17
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19
+* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
+* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21
+* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+*/
24
+#include <unistd.h>
25
+#include <iostream>
26
+#include <signal.h>
27
+
28
+//! [Interesting]
29
+#include "hx711.h"
30
+
31
+int
32
+main(int argc, char **argv)
33
+{
34
+    upm::HX711 *scale = new upm::HX711(3, 2);
35
+
36
+    // 2837: value obtained via calibration
37
+    scale->setScale(2837);
38
+    scale->tare();
39
+    std::cout << scale->getUnits() << std::endl;
40
+
41
+    return 0;
42
+}
43
+//! [Interesting]

+ 36
- 0
examples/javascript/hx711.js View File

1
+/*
2
+*
3
+* Author: Rafael da Mata Neri <rafael.neri@gmail.com>
4
+* Copyright (c) 2015 Intel Corporation.
5
+*
6
+*
7
+* Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+* this software and associated documentation files (the "Software"), to deal in
9
+* the Software without restriction, including without limitation the rights to
10
+* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
+* the Software, and to permit persons to whom the Software is furnished to do so,
12
+* subject to the following conditions:
13
+*
14
+* The above copyright notice and this permission notice shall be included in all
15
+* copies or substantial portions of the Software.
16
+*
17
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19
+* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
+* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21
+* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+*/
24
+
25
+var hx711 = require('jsupm_hx711');
26
+// Instantiate a HX711 data on digital pin D3 and clock on digital pin D2
27
+var scale = new hx711.HX711(3, 2);
28
+
29
+setTimeout(function(){
30
+	// 2837: value obtained via calibration
31
+	scale.setScale(2837);
32
+	scale.tare(2);
33
+	setInterval(function(){
34
+		console.log(scale.getUnits());
35
+	}, 1000);
36
+}, 1000);

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

1
+set (libname "hx711")
2
+set (libdescription "HX711 24bit ADC")
3
+set (module_src ${libname}.cxx)
4
+set (module_h ${libname}.h)
5
+upm_module_init()

+ 148
- 0
src/hx711/hx711.cxx View File

1
+/*
2
+*
3
+* Author: Rafael da Mata Neri <rafael.neri@gmail.com>
4
+* Copyright (c) 2015 Intel Corporation.
5
+*
6
+*
7
+* Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+* this software and associated documentation files (the "Software"), to deal in
9
+* the Software without restriction, including without limitation the rights to
10
+* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
+* the Software, and to permit persons to whom the Software is furnished to do so,
12
+* subject to the following conditions:
13
+*
14
+* The above copyright notice and this permission notice shall be included in all
15
+* copies or substantial portions of the Software.
16
+*
17
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19
+* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
+* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21
+* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+*/
24
+#include <iostream>
25
+#include <fstream>
26
+#include <unistd.h>
27
+#include <stdlib.h>
28
+#include <string.h>
29
+#include "hx711.h"
30
+
31
+using namespace upm;
32
+using namespace std;
33
+
34
+struct HX711Exception : public std::exception {
35
+    std::string message;
36
+    HX711Exception (std::string msg) : message (msg) { }
37
+    ~HX711Exception () throw () { }
38
+    const char* what() const throw () { return message.c_str(); }
39
+};
40
+
41
+HX711::HX711(uint8_t data, uint8_t sck, uint8_t gain) {
42
+    mraa_result_t error = MRAA_SUCCESS;
43
+    
44
+    this->m_dataPinCtx = mraa_gpio_init(data);
45
+    if (this->m_dataPinCtx == NULL) {
46
+        throw HX711Exception ("Couldn't initilize DATA pin.");
47
+    }
48
+    
49
+    this->m_sckPinCtx = mraa_gpio_init(sck);
50
+    if (this->m_sckPinCtx == NULL) {
51
+        throw HX711Exception ("Couldn't initilize CLOCK pin.");
52
+    }
53
+    
54
+    error = mraa_gpio_dir (this->m_dataPinCtx, MRAA_GPIO_IN);
55
+    if (error != MRAA_SUCCESS) {
56
+        throw HX711Exception ("Couldn't set direction for DATA pin.");
57
+    }
58
+    
59
+    error = mraa_gpio_dir (this->m_sckPinCtx, MRAA_GPIO_OUT);
60
+    if (error != MRAA_SUCCESS) {
61
+        throw HX711Exception ("Couldn't set direction for CLOCK pin.");
62
+    }
63
+
64
+    this->setGain(gain);
65
+}
66
+
67
+HX711::~HX711() {
68
+    mraa_result_t error = MRAA_SUCCESS;
69
+    
70
+    error = mraa_gpio_close (this->m_dataPinCtx);
71
+    if (error != MRAA_SUCCESS) {
72
+        mraa_result_print(error);
73
+    }
74
+    
75
+    error = mraa_gpio_close (this->m_sckPinCtx);
76
+    if (error != MRAA_SUCCESS) {
77
+        mraa_result_print(error);
78
+    }
79
+}
80
+
81
+unsigned long HX711::read() {
82
+    unsigned long Count = 0;
83
+    
84
+    while (mraa_gpio_read(this->m_dataPinCtx));
85
+                                  
86
+    for (int i=0; i<GAIN; i++)
87
+    {
88
+        mraa_gpio_write(this->m_sckPinCtx, 1);
89
+        Count = Count << 1;
90
+        mraa_gpio_write(this->m_sckPinCtx, 0);
91
+        if(mraa_gpio_read(this->m_dataPinCtx))
92
+        {
93
+            Count++;
94
+        }
95
+    }
96
+
97
+    mraa_gpio_write(this->m_sckPinCtx, 1);
98
+    Count = Count ^ 0x800000;
99
+    mraa_gpio_write(this->m_sckPinCtx, 0);
100
+
101
+    return (Count);
102
+}
103
+
104
+void HX711::setGain(uint8_t gain){
105
+    switch (gain) {
106
+        case 128:       // channel A, gain factor 128
107
+            GAIN = 24;
108
+            break;
109
+        case 64:        // channel A, gain factor 64
110
+            GAIN = 26;
111
+            break;
112
+        case 32:        // channel B, gain factor 32
113
+            GAIN = 25;
114
+            break;
115
+    }
116
+
117
+    mraa_gpio_write(this->m_sckPinCtx, 0);
118
+    read();
119
+}
120
+
121
+unsigned long HX711::readAverage(uint8_t times){
122
+    unsigned long sum = 0;
123
+    for (uint8_t i = 0; i < times; i++) {
124
+        sum += read();
125
+    }
126
+    return sum / times;
127
+}
128
+
129
+double HX711::getValue(uint8_t times){
130
+    return readAverage(times) - OFFSET;
131
+}
132
+
133
+float HX711::getUnits(uint8_t times){
134
+    return getValue(times) / SCALE;
135
+}
136
+
137
+void HX711::tare(uint8_t times){
138
+    double sum = readAverage(times);
139
+    setOffset(sum);
140
+}
141
+
142
+void HX711::setScale(float scale){
143
+    SCALE = scale;
144
+}
145
+
146
+void HX711::setOffset(long offset){
147
+    OFFSET = offset;
148
+}

+ 135
- 0
src/hx711/hx711.h View File

1
+/*
2
+*
3
+* Author: Rafael da Mata Neri <rafael.neri@gmail.com>
4
+* Copyright (c) 2015 Intel Corporation.
5
+*
6
+*
7
+* Permission is hereby granted, free of charge, to any person obtaining a copy of
8
+* this software and associated documentation files (the "Software"), to deal in
9
+* the Software without restriction, including without limitation the rights to
10
+* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11
+* the Software, and to permit persons to whom the Software is furnished to do so,
12
+* subject to the following conditions:
13
+*
14
+* The above copyright notice and this permission notice shall be included in all
15
+* copies or substantial portions of the Software.
16
+*
17
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19
+* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20
+* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21
+* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22
+* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+*/
24
+#pragma once
25
+
26
+
27
+#include <unistd.h>
28
+#include <stdlib.h>
29
+#include <stdint.h>
30
+#include <string.h>
31
+#include <mraa/gpio.h>
32
+
33
+namespace upm {
34
+
35
+     /**
36
+      * @brief HX711 24bit ADC library
37
+      * @defgroup hx711 libupm-hx711
38
+      */
39
+
40
+     /**
41
+      * @brief C++ API for HX711
42
+      *
43
+      * [HX711](http://www.dfrobot.com/image/data/SEN0160/hx711_english.pdf) is 
44
+      * a precision 24-bit analog- to-digital converter (ADC) designed for weigh
45
+      * scales and industrial control applications to interface directly with a 
46
+      * bridge sensor. This module was tested on the Intel Galileo Gen2.
47
+      *
48
+      * @ingroup hx711 hx711
49
+      * @snippet hx711.cxx Interesting
50
+      * @image html hx711.jpeg
51
+      */
52
+      class HX711 {
53
+      public:
54
+            /**
55
+            * HX711 module constructor
56
+            *
57
+            * @param data define the data pin
58
+            * @param sck define the clock pin
59
+            * @param gain define the gain factor
60
+            * Valid values are 128 or 64 for channel A; channel B works with 32 gain factor only
61
+            */
62
+            HX711(uint8_t data, uint8_t sck, uint8_t gain = 128);
63
+
64
+            /**
65
+            * HX711 module Destructor
66
+            */
67
+            ~HX711();
68
+
69
+
70
+            /**
71
+            * Waits for the chip to be ready and returns a reading
72
+            *
73
+            * @return raw adc read
74
+            */
75
+            unsigned long read();
76
+
77
+            /**
78
+            * Set the gain factor; takes effect only after a call to read()
79
+            * channel A can be set for a 128 or 64 gain; channel B has a fixed 32 gain
80
+            * depending on the parameter, the channel is also set to either A or B
81
+            * @param gain define the gain factor
82
+            */
83
+            void setGain(uint8_t gain = 128);
84
+
85
+            /**
86
+            * Returns an average reading
87
+            * @param times define how many times to read
88
+            * @return the avarage reading
89
+            */
90
+            unsigned long readAverage(uint8_t times = 10);
91
+
92
+            /**
93
+            * Returns (readAverage() - OFFSET)
94
+            * @param times define how many readings to do
95
+            * @return the current value without the tare weight
96
+            */
97
+            double getValue(uint8_t times = 10);
98
+
99
+            /**
100
+            * Returns getValue() divided by SCALE
101
+            * @param times define how many readings to do
102
+            * @return the raw value divided by a value obtained via calibration
103
+            */
104
+            float getUnits(uint8_t times = 1);
105
+
106
+            /**
107
+            * Set the OFFSET value for tare weight
108
+            * @param times define how many times to read the tare value
109
+            */
110
+            void tare(uint8_t times = 10);
111
+
112
+            /**
113
+            * Set the SCALE value 
114
+            * This value is used to convert the raw data to "human readable" data (measure units)
115
+            * @param scale value obtained via calibration
116
+            */
117
+            void setScale(float scale = 1.f);
118
+       private:
119
+            mraa_gpio_context m_sckPinCtx; // Power Down and Serial Clock Input Pin
120
+            mraa_gpio_context m_dataPinCtx; // Serial Data Output Pin
121
+            
122
+            uint8_t GAIN; // amplification factor
123
+            unsigned long OFFSET; // used for tare weight
124
+            float SCALE; // used to return weight in grams, kg, ounces, whatever
125
+
126
+            
127
+            /**
128
+            * Set the OFFSET value 
129
+            * The value that's subtracted from the actual reading (tare weight)
130
+            * @param scale value obtained via calibration
131
+            */
132
+            void setOffset(long offset = 0);
133
+     };
134
+
135
+}

+ 10
- 0
src/hx711/jsupm_hx711.i View File

1
+//! [Interesting]
2
+%module jsupm_hx711
3
+%include "../upm.i"
4
+
5
+%{
6
+    #include "hx711.h"
7
+%}
8
+
9
+%include "hx711.h"
10
+//! [Interesting]

+ 10
- 0
src/hx711/pyupm_hx711.i View File

1
+%module pyupm_hx711
2
+%include "../upm.i"
3
+%include "stdint.i"
4
+
5
+%feature("autodoc", "3");
6
+
7
+%include "hx711.h"
8
+%{
9
+    #include "hx711.h"
10
+%}