forked from Good-For-Food-Admin/Hx711-Driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHX711.js
280 lines (247 loc) · 6.94 KB
/
HX711.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"use strict";
const Gpio = require("pigpio").Gpio;
const microtime = require("microtime");
const sleep = require("sleep");
/**
* GPIO pins references BCM pin numbers
*/
class HX711 {
constructor(dataPin, clockPin, scale, channelGain) {
this.__dataPin = new Gpio(dataPin, {
mode: Gpio.INPUT,
pullUpDown: Gpio.PUD_OFF
});
this.__clockPin = new Gpio(clockPin, {
mode: Gpio.OUTPUT,
pullUpDown: Gpio.PUD_OFF
});
this.__offset = 0;
this.__scale = scale || 35;
this.latestReading = -1337; // avoid undefined or null
/**
* Default Configurations
*/
this.__channelGain = channelGain || HX711.CHANNEL_A_GAIN_128;
/**
* Binding of class methods
*/
this.__isReady.bind(this);
this.read.bind(this);
this.readAverage.bind(this);
this.__readRawAverage.bind(this);
this.__readRaw.bind(this);
this.__begin.bind(this);
this.__applyNormalisation.bind(this);
this.calibrateScale.bind(this);
this.__setChannelGain.bind(this);
// Initialises HX711 by taring
this.__begin();
}
__begin() {
if (this.__channelGain !== HX711.CHANNEL_A_GAIN_128) {
this.__setChannelGain(this.__channelGain);
}
while (Math.abs(this.read()) >= 0.5) {
this.tare();
}
return true;
}
__setChannelGain(channelGain, isInit) {
let timesToPulse;
if (channelGain === HX711.CHANNEL_A_GAIN_64) {
timesToPulse = 3;
} else if (channelGain === HX711.CHANNEL_B_GAIN_32) {
timesToPulse = 2;
} else {
timesToPulse = 1;
}
if (isInit) {
timesToPulse += 24;
}
/**
* Wait for HX711 to be ready
*/
while (isInit && !this.__isReady()) {
sleep.msleep(1);
}
for (let timesPulsed = 0; timesPulsed < timesToPulse; timesPulsed++) {
// start timer
const startCounter = microtime.now();
// request next bit from HX711
this.__clockPin.digitalWrite(1);
this.__clockPin.digitalWrite(0);
// stop timer
const endCounter = microtime.now();
const timeElapsed = endCounter - startCounter;
// check if the HX711 did not turn off:
// if pd_sck pin is HIGH for 60 us and more than the HX 711 enters power down mode.
if (timeElapsed >= 60) {
console.log(
`Reading data took longer than 60µs. Time elapsed: ${timeElapsed}`
);
return this.__setChannelGain(channelGain, true);
}
}
return true;
}
/**
*
* @returns boolean
* @private
*/
__isReady() {
return this.__dataPin.digitalRead() === 0;
}
/**
*
* @returns {boolean}
*/
tare() {
this.__offset = -1 * this.__readRawAverage(10);
return true;
}
/**
*
* @param rawValue
* @returns {number}
* @private
*/
__applyNormalisation(rawValue) {
return (rawValue + this.__offset) / this.__scale;
}
calibrateScale(knownWeight) {
this.__scale = (this.__readRawAverage(5) + this.__offset) / knownWeight;
return this.__scale;
}
readAverage(numberOfTimes) {
this.latestReading = this.__applyNormalisation(
this.__readRawAverage(numberOfTimes)
);
return this.latestReading;
}
/**
*
* @param numberOfTimes
* @returns {number}
*/
__readRawAverage(numberOfTimes) {
numberOfTimes = numberOfTimes < 0 ? 1 : numberOfTimes;
let sum = 0;
for (let i = 0; i < numberOfTimes; i++) {
let result = false;
while (result === false) {
result = this.__readRaw();
}
sum += result;
}
return sum / numberOfTimes;
}
readMedian(numberOfTimes) {
this.latestReading = this.__applyNormalisation(
this.__readRawMedian(numberOfTimes)
);
return this.latestReading;
}
/**
*
* @param numberOfTimes
* @returns {number}
*/
__readRawMedian(numberOfTimes) {
numberOfTimes = numberOfTimes < 0 ? 1 : numberOfTimes;
let sum = 0;
let values = [];
for (let i = 0; i < numberOfTimes; i++) {
let result = false;
while (result === false) {
result = this.__readRaw();
}
values.push(result);
}
return __getMedian(values);
}
/**
*
* @param values
* @returns {number}
*/
__getMedian(values) {
const mid = Math.floor(values.length / 2),
nums = [...values].sort((a, b) => a - b);
return values.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};
read() {
return this.readAverage(1);
}
/**
*
* @returns {number | boolean}
*/
__readRaw() {
const maxAttempts = 9999;
// start by setting the pd_sck to false
this.__clockPin.digitalWrite(0);
// init the counter
let attemptsCounter = 0;
// loop until HX711 is ready
// halt when maximum number of tries is reached
for (let attemptsCounter = 0; !this.__isReady(); attemptsCounter++) {
sleep.msleep(1);
if (attemptsCounter >= maxAttempts) {
// console.log('Exceeded max attempts, Hx711 is not ready yet');
return false;
}
}
// Note that in JS, every number is a 64bit float
let data_in = 0x000000; // 2's complement data from hx 711
// read first 24 bits of data
for (let i = 0; i < 24; i++) {
// start timer
const startCounter = microtime.now();
// request next bit from HX711
this.__clockPin.digitalWrite(1);
this.__clockPin.digitalWrite(0);
// stop timer
const endCounter = microtime.now();
const timeElapsed = endCounter - startCounter;
// check if the hx 711 did not turn off:
// if pd_sck pin is HIGH for 60 us and more than the HX 711 enters power down mode.
if (timeElapsed >= 60) {
console.log(
`Reading data took longer than 60µs. Time elapsed: ${timeElapsed}`
);
return false;
}
// Shift the bits as they come to data_in variable.
// Left shift by one bit then bitwise OR with the new bit.
data_in = (data_in << 1) | this.__dataPin.digitalRead();
// console.log(`Binary value as it has come: ${data_in.toString(2)} bit${i}`);
}
this.__setChannelGain(this.__channelGain, false);
sleep.msleep(1);
// check if data is valid
// 0x800000 and 0x7fffff are emitted from HX711 when data is out of range
if (data_in === 0x7fffff || data_in === 0x800000) {
console.log("Invalid data detected: " + data_in);
return false;
}
// calculate int from 2's complement
let signed_data = 0x000000;
if (data_in & 0x800000) {
// 0b1000 0000 0000 0000 0000 0000 check if the sign bit is 1. Negative number.
signed_data = -1 * ((data_in ^ 0xffffff) + 1); // convert from 2's complement to int
} else {
// else do not do anything the value is positive number
signed_data = data_in;
}
// console.log('Converted 2\'s complement value: ' + (signed_data));
return signed_data;
}
}
/**
* Static enums
*/
HX711.CHANNEL_A_GAIN_128 = {};
HX711.CHANNEL_B_GAIN_32 = {};
HX711.CHANNEL_A_GAIN_64 = {};
module.exports = HX711;