-
Notifications
You must be signed in to change notification settings - Fork 15
/
calibration.py
126 lines (101 loc) · 3.97 KB
/
calibration.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
"""
HX711 Load cell amplifier Python Library
Original source: https://gist.github.com/underdoeg/98a38b54f889fce2b237
Documentation source: https://github.com/aguegu/ardulibs/tree/master/hx711
Adapted by 2017 Jiri Dohnalek
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
######################################################################
README:
This version runs in python 3.x. It will first prompt the user to
empty the scale. Then prompt user to place an item with a known weight
on the scale and input weight as INT.
The offset and scale will be adjusted accordingly and displayed for
convenience.
The user can choose to [0] exit, [1] recalibrate, or [2] display the
current offset and scale values and weigh a new item to test the accuracy
of the offset and scale values!
#######################################################################
"""
import RPi.GPIO as GPIO
import time
import sys
from hx711 import HX711
# Force Python 3 ###########################################################
if sys.version_info[0] != 3:
raise Exception("Python 3 is required.")
############################################################################
# Make sure you correct these to the correct pins for DOUT and SCK.
# gain is set to 128 as default, change as needed.
hx = HX711(5, 6, gain=128)
def cleanAndExit():
print("Cleaning up...")
GPIO.cleanup()
print("Bye!")
sys.exit()
def setup():
"""
code run once
"""
print("Initializing.\n Please ensure that the scale is empty.")
scale_ready = False
while not scale_ready:
if (GPIO.input(hx.DOUT) == 0):
scale_ready = False
if (GPIO.input(hx.DOUT) == 1):
print("Initialization complete!")
scale_ready = True
def calibrate():
readyCheck = input("Remove any items from scale. Press any key when ready.")
offset = hx.read_average()
print("Value at zero (offset): {}".format(offset))
hx.set_offset(offset)
print("Please place an item of known weight on the scale.")
readyCheck = input("Press any key to continue when ready.")
measured_weight = (hx.read_average()-hx.get_offset())
item_weight = input("Please enter the item's weight in grams.\n>")
scale = int(measured_weight)/int(item_weight)
hx.set_scale(scale)
print("Scale adjusted for grams: {}".format(scale))
def loop():
"""
code run continuously
"""
try:
prompt_handled = False
while not prompt_handled:
val = hx.get_grams()
hx.power_down()
time.sleep(.001)
hx.power_up()
print("Item weighs {} grams.\n".format(val))
choice = input("Please choose:\n"
"[1] Recalibrate.\n"
"[2] Display offset and scale and weigh an item!\n"
"[0] Clean and exit.\n>")
if choice == "1":
calibrate()
elif choice == "2":
print("\nOffset: {}\nScale: {}".format(hx.get_offset(), hx.get_scale()))
elif choice == "0":
prompt_handled = True
cleanAndExit()
else:
print("Invalid selection.\n")
except (KeyboardInterrupt, SystemExit):
cleanAndExit()
##################################
if __name__ == "__main__":
setup()
calibrate()
while True:
loop()