-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadNau7802.py
More file actions
executable file
·75 lines (56 loc) · 1.72 KB
/
readNau7802.py
File metadata and controls
executable file
·75 lines (56 loc) · 1.72 KB
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
import PyNAU7802
import smbus2
import sched
import time
import mmap
import struct
import mmap
import struct
import os
# Define the shared memory format
FORMAT = 'f' # Float format
SIZE = struct.calcsize(FORMAT) # Size of the shared memory region
# Create a shared memory file
shm_file = os.open("my_shared_memory", os.O_CREAT | os.O_RDWR)
# Set the size of the shared memory file
os.ftruncate(shm_file, SIZE)
# Map the shared memory file into memory
shm = mmap.mmap(shm_file, SIZE)
# Create the bus
bus = smbus2.SMBus(0)
# Create the scale and initialize it
scale = PyNAU7802.NAU7802()
if scale.begin(bus):
print("Connected!\n")
else:
print("Can't find the scale, exiting ...\n")
exit()
# Calculate the zero offset
print("Calculating the zero offset...")
scale.setGain(8)
scale.calculateZeroOffset()
print("The zero offset is: {0}\n".format(scale.getZeroOffset()))
print("Put a known mass on the scale.")
cal = 61274.138#float(input("Mass in kg? "))
scale.setCalibrationFactor(cal)
# Calculate the calibration factor
#scale.calculateCalibrationFactor(cal)
print("The calibration factor is: {0:0.3f}\n".format(scale.getCalibrationFactor()))
#input("Press [Enter] to start measuring masses. ")
# Initialize the scheduler
scheduler = sched.scheduler(time.time, time.sleep)
interval = 0.5
def measure_weight():
weight = scale.getWeight()
# Write data to the shared memory
# Write data to the shared memory
value = weight
packed_data = struct.pack(FORMAT, value)
# Write to the shared memory
shm.seek(0)
shm.write(packed_data)
print("Raw: {0:0.3f} kg".format(weight))
scheduler.enter(interval, 1, measure_weight)
# Start the measurements
scheduler.enter(0, 1, measure_weight)
scheduler.run()