-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52d900f
commit ffd69c2
Showing
7 changed files
with
201 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import mraa | ||
|
||
class Hx711: | ||
def __init__(self,dout,pd_sck, gain = 128): | ||
# self.PD_SCK = pd_sck | ||
# self.DOUT = dout | ||
self.PD_SCK = mraa.Gpio(pd_sck) | ||
self.DOUT = mraa.Gpio(dout) | ||
self.PD_SCK.dir(mraa.DIR_OUT) | ||
self.DOUT.dir(mraa.DIR_IN) | ||
self.GAIN = 1 | ||
self.OFFSET = 0 | ||
self.SCALE = 1 | ||
self.set_gain(gain) | ||
def set_gain(self,gain): | ||
if gain == 128: | ||
self.GAIN = 1 | ||
elif gain == 64: | ||
self.GAIN = 3 | ||
elif gain == 32: | ||
self.GAIN = 2 | ||
self.PD_SCK.write(0) # is for low signal | ||
self.read() | ||
def read(self): | ||
while self.DOUT.read() == 1: | ||
continue | ||
|
||
value = 0 | ||
data = 0 | ||
filler = 0x00 | ||
for i in range(24): | ||
self.PD_SCK.write(1) | ||
value = value<<1 | ||
self.PD_SCK.write(0) | ||
if(self.DOUT.read()): | ||
value = value + 1 | ||
self.PD_SCK.write(1) | ||
value = value ^ 0x800000 | ||
self.PD_SCK.write(0) | ||
return value | ||
|
||
def read_average(self,times): | ||
abuf = [] | ||
for i in range(10): | ||
abuf.append(self.read()) | ||
abuf.sort() | ||
aval = (abuf[4] + abuf[3] + abuf[5] + abuf[6])/4 | ||
return aval | ||
# sum0 = 0 | ||
# for i in range(times): | ||
# sum0 += self.read() | ||
# return sum0/times; | ||
|
||
|
||
def get_value(self,times): | ||
return self.read_average(times) - self.OFFSET | ||
|
||
|
||
def get_units(self,times): | ||
return self.get_value(times) /self.SCALE | ||
|
||
|
||
def tare(self, times): | ||
abuf = [] | ||
for i in range(10): | ||
abuf.append(self.read()) | ||
abuf.sort() | ||
aval = (abuf[4] + abuf[3] + abuf[5] + abuf[6])/4 | ||
#sum = self.read_average(times) | ||
self.set_offset(aval) | ||
|
||
def set_scale(self,scale): | ||
self.SCALE = scale | ||
|
||
def get_scale(self): | ||
return self.SCALE | ||
|
||
def get_offset(self): | ||
return self.OFFSET | ||
|
||
|
||
|
||
def set_offset(self,offset): | ||
self.OFFSET = offset; | ||
|
||
def power_down(self): | ||
self.PD_SCK.write(0) | ||
self.PD_SCK.write(1) | ||
|
||
def power_up(self): | ||
self.PD_SCK.write(0) | ||
|
||
|
||
# def shiftIn(self): | ||
# value =0 | ||
# for i in range(24): | ||
# self.PD_SCK.write(1) | ||
# value = value<<1 | ||
# self.PD_SCK.write(0) | ||
# if(self.DOUT.read()): | ||
# value = value + 1 | ||
|
||
# return value | ||
|
||
# def is_ready(self): | ||
# if self.DOUT.read() == 0: | ||
# return True | ||
# else: | ||
# return False |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
from hx711_2 import Hx711 | ||
from time import sleep | ||
calibration_factor = 17650.0 # set the calibratino factor from -7050 for pounds to -176550 for kg's | ||
a1 = Hx711(36,20) | ||
a2 = Hx711(48,14) | ||
a1.set_scale(calibration_factor) | ||
a2.set_scale(calibration_factor) | ||
a1.tare(1) | ||
a2.tare(1) | ||
# borders = [10,20,30,40,50,60] | ||
# l1 = 5 | ||
# l2 = 55 | ||
# threshold_w = 1.0 | ||
# threshold_x = 4 | ||
# x =float() | ||
# w = float() | ||
# x_p = 0.0 | ||
# w_p = 0.0 | ||
# x_n = float() | ||
# w_n = float() | ||
# mark = [0.0,0.0,0.0,0.0,0.0,0.0] # places on the shelf for containers | ||
# mark2 = [0,0,0,0,0,0] | ||
while 1: | ||
# arr = [] | ||
# arr2 = [] | ||
# for i in range(10): | ||
# arr.append(a1.get_units(1)) | ||
# arr2.append(a2.get_units(1)) | ||
# arr.sort() | ||
# arr2.sort() | ||
|
||
r1 = a1.get_units(1) | ||
r2 = a2.get_units(1)#(arr2[6] + arr2[5] + arr2[3] + arr2[4])/4 | ||
|
||
w = r1 + r2 | ||
print str(r1) + " " + str(r2) + " " + str(w) | ||
|
||
# if w > threshold_w: | ||
# x = (r1*l1 + r2*l2)/w | ||
|
||
# if w-w_p > threshold_w or w_p-w > threshold_w or x-x_p > threshold_x or x_p-x > threshold_x : | ||
# w_n = w - w_p | ||
# x_n = (w*x - w_p*x_p)/w_n | ||
|
||
# for i in range(6): | ||
# if x_n < borders[i]: | ||
# if w_n > 0: | ||
# mark[i] = w_n # replacing 1 with the weight added | ||
# mark2[i] = 1 | ||
# print "Container Added at : " + str(i + 1) | ||
# elif w_n < 0: | ||
# mark2[i] = 0 | ||
# mark[i] = 0 | ||
# print "Container Removed from : " + str(i+1) | ||
# break | ||
|
||
# else: | ||
# for i in range(6): | ||
# mark2[i] = 0 | ||
# mark[i] = 0.0 # not sure about this slot | ||
# x = 0 | ||
# w_p = w | ||
# x_p = x | ||
# string = str() | ||
# string2 = str() | ||
# for i in range(6): | ||
# string = string + str(mark[i]) + " " | ||
# string2 = string2 + str(mark2[i]) + " " | ||
# print string | ||
# print string2 | ||
# with open('weights.txt','w') as w: | ||
# w.write(string) | ||
|
||
|
||
a1.power_down() | ||
a2.power_down() | ||
sleep(1) | ||
a1.power_up() | ||
a2.power_up() | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters