-
Notifications
You must be signed in to change notification settings - Fork 1
/
hx711.py
182 lines (138 loc) · 5.24 KB
/
hx711.py
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
'''
by: tatobari date: September 11, 2016
Source: https://github.com/tatobari/hx711py/blob/master/hx711.py
'''
import RPi.GPIO as GPIO
import time
import sys
import numpy # sudo apt-get python-numpy
if sys.version_info > (3,):
long = int
class HX711:
def __init__(self, dout, pd_sck, gain=128):
self.PD_SCK = pd_sck
self.DOUT = dout
GPIO.setmode(GPIO.BCM)
GPIO.setup(self.PD_SCK, GPIO.OUT)
GPIO.setup(self.DOUT, GPIO.IN)
self.GAIN = 0
self.REFERENCE_UNIT = 1 # The value returned cdby the hx711 that corresponds to your reference unit AFTER dividing by the SCALE.
self.OFFSET = 1
self.lastVal = long(0)
self.LSByte = [2, -1, -1]
self.MSByte = [0, 3, 1]
self.MSBit = [0, 8, 1]
self.LSBit = [7, -1, -1]
self.byte_range_values = self.LSByte
self.bit_range_values = self.MSBit
self.set_gain(gain)
time.sleep(1)
def is_ready(self):
return GPIO.input(self.DOUT) == 0
def set_gain(self, gain):
if gain is 128:
self.GAIN = 1
elif gain is 64:
self.GAIN = 3
elif gain is 32:
self.GAIN = 2
GPIO.output(self.PD_SCK, False)
self.read()
def createBoolList(self, size=8):
ret = []
for i in range(8):
ret.append(False)
return ret
def read(self):
while not self.is_ready():
#print("WAITING")
pass
dataBits = [self.createBoolList(), self.createBoolList(), self.createBoolList()]
dataBytes = [0x0] * 4
for j in range(self.byte_range_values[0], self.byte_range_values[1], self.byte_range_values[2]):
for i in range(self.bit_range_values[0], self.bit_range_values[1], self.bit_range_values[2]):
GPIO.output(self.PD_SCK, True)
dataBits[j][i] = GPIO.input(self.DOUT)
GPIO.output(self.PD_SCK, False)
dataBytes[j] = numpy.packbits(numpy.uint8(dataBits[j]))
#set channel and gain factor for next reading
for i in range(self.GAIN):
GPIO.output(self.PD_SCK, True)
GPIO.output(self.PD_SCK, False)
#check for all 1
#if all(item is True for item in dataBits[0]):
# return long(self.lastVal)
dataBytes[2] ^= 0x80
return dataBytes
def get_binary_string(self):
binary_format = "{0:b}"
np_arr8 = self.read_np_arr8()
binary_string = ""
for i in range(4):
# binary_segment = binary_format.format(np_arr8[i])
binary_segment = format(np_arr8[i], '#010b')
binary_string += binary_segment + " "
return binary_string
def get_np_arr8_string(self):
np_arr8 = self.read_np_arr8()
np_arr8_string = "[";
comma = ", "
for i in range(4):
if i is 3:
comma = ""
np_arr8_string += str(np_arr8[i]) + comma
np_arr8_string += "]";
return np_arr8_string
def read_np_arr8(self):
dataBytes = self.read()
np_arr8 = numpy.uint8(dataBytes)
return np_arr8
def read_long(self):
np_arr8 = self.read_np_arr8()
np_arr32 = np_arr8.view('uint32')
self.lastVal = np_arr32
return long(self.lastVal)
def read_average(self, times=3):
values = long(0)
for i in range(times):
values += self.read_long()
return values / times
def get_value(self, times=3):
return self.read_average(times) - self.OFFSET
def get_weight(self, times=3):
value = self.get_value(times)
value = value / self.REFERENCE_UNIT
return value
def tare(self, times=15):
# Backup REFERENCE_UNIT value
reference_unit = self.REFERENCE_UNIT
self.set_reference_unit(1)
value = self.read_average(times)
self.set_offset(value)
self.set_reference_unit(reference_unit)
def set_reading_format(self, byte_format="LSB", bit_format="MSB"):
if byte_format == "LSB":
self.byte_range_values = self.LSByte
elif byte_format == "MSB":
self.byte_range_values = self.MSByte
if bit_format == "LSB":
self.bit_range_values = self.LSBit
elif bit_format == "MSB":
self.bit_range_values = self.MSBit
def set_offset(self, offset):
self.OFFSET = offset
def set_reference_unit(self, reference_unit):
self.REFERENCE_UNIT = reference_unit
# HX711 datasheet states that setting the PDA_CLOCK pin on high for a more than 60 microseconds would power off the chip.
# I used 100 microseconds, just in case.
# I've found it is good practice to reset the hx711 if it wasn't used for more than a few seconds.
def power_down(self):
GPIO.output(self.PD_SCK, False)
GPIO.output(self.PD_SCK, True)
time.sleep(0.0001)
def power_up(self):
GPIO.output(self.PD_SCK, False)
time.sleep(0.0001)
def reset(self):
self.power_down()
self.power_up()