File tree Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Expand file tree Collapse file tree 3 files changed +77
-0
lines changed Original file line number Diff line number Diff line change 32
32
from .ultrasonic_i2c import ULTRASONIC_I2CUnit
33
33
from .limit import LIMITUnit
34
34
from .op import OPUnit
35
+ from .weight import WEIGHTUnit
36
+ from .scales import SCALESUnit
Original file line number Diff line number Diff line change 37
37
# "can.py",
38
38
"limit.py" ,
39
39
"op.py" ,
40
+ "weight.py" ,
41
+ "scales.py" ,
40
42
),
41
43
base_path = ".." ,
42
44
opt = 0 ,
Original file line number Diff line number Diff line change
1
+ from machine import Pin
2
+ import time
3
+ from micropython import const
4
+
5
+ CHANNEL_A_128 = const (1 )
6
+ CHANNEL_A_64 = const (3 )
7
+ CHANNEL_B_32 = const (2 )
8
+
9
+ DATA_BITS = const (24 )
10
+ MAX_VALUE = const (0x7fffff )
11
+ MIN_VALUE = const (0x800000 )
12
+
13
+ class WEIGHTUnit :
14
+ def __init__ (self , port ) -> None :
15
+ self .hx711data = Pin (port [0 ], Pin .IN )
16
+ self .hx711clk = Pin (port [1 ], Pin .OUT )
17
+ self .zero_value = 0
18
+ self .hx711clk .value (0 )
19
+ self ._channel = 1
20
+ self ._scale = 1.0
21
+
22
+ @property
23
+ def get_raw_weight (self ) -> int :
24
+ count = 0
25
+ if self .is_ready_wait ():
26
+ for i in range (24 ):
27
+ self .hx711clk .value (1 )
28
+ time .sleep_us (1 )
29
+ self .hx711clk .value (0 )
30
+ time .sleep_us (1 )
31
+ if self .hx711data .value ():
32
+ count += 1
33
+ count = count << 1
34
+ else :
35
+ return 0
36
+
37
+ gain_m = self ._channel
38
+ while gain_m :
39
+ self .hx711clk .value (1 )
40
+ time .sleep_us (1 )
41
+ self .hx711clk .value (0 )
42
+ time .sleep_us (1 )
43
+ gain_m -= 1
44
+ count = count ^ 0x800000
45
+ return count
46
+
47
+ @property
48
+ def get_scale_weight (self ) -> int :
49
+ return int ((self .get_raw_weight - self .zero_value ) * self ._scale )
50
+
51
+ def set_tare (self ) -> None :
52
+ self .zero_value = self .get_raw_weight
53
+
54
+ def set_calibrate_scale (self , weight ):
55
+ self ._scale = (1.0 * weight ) / (self .get_raw_weight - self .zero_value )
56
+
57
+ def is_ready_wait (self ) -> bool :
58
+ times = 0
59
+ while self .hx711data .value ():
60
+ times += 1
61
+ time .sleep_ms (10 )
62
+ if times > 25 :
63
+ break
64
+ return not self .hx711data .value ()
65
+
66
+ def set_channel (self , chan : int ) -> None :
67
+ self ._channel = chan
68
+ for i in range (self ._channel ):
69
+ self .hx711clk .value (1 )
70
+ time .sleep_us (1 )
71
+ self .hx711clk .value (0 )
72
+ time .sleep_us (1 )
73
+
You can’t perform that action at this time.
0 commit comments