SHOW:
|
|
- or go back to the newest paste.
1 | import RPi.GPIO as GPIO | |
2 | - | import board |
2 | + | from hx711 import HX711 |
3 | - | import busio |
3 | + | import smbus |
4 | - | import adafruit_vl53l0x |
4 | + | |
5 | - | import pyqtgraph as pg |
5 | + | import os |
6 | - | from PyQt5.QtWidgets import QApplication |
6 | + | import threading |
7 | - | from PyQt5.QtCore import QThread, QTimer, pyqtSignal |
7 | + | |
8 | def Load_Cell(bus): | |
9 | - | timestamps = [] |
9 | + | GPIO.setmode(GPIO.BCM) |
10 | - | distances = [] |
10 | + | hx = HX711(5, 6) |
11 | hx.set_reading_format("MSB", "MSB") | |
12 | - | i2c = busio.I2C(board.SCL, board.SDA) |
12 | + | hx.set_reference_unit(-2199) # Set your reference unit value here (e.g., for grams) |
13 | - | sensor = adafruit_vl53l0x.VL53L0X(i2c) |
13 | + | readings = 0 |
14 | hx.reset() | |
15 | - | class SensorThread(QThread): |
15 | + | hx.tare() |
16 | - | data_ready = pyqtSignal(float, float) |
16 | + | |
17 | folder_path = "./DATA" | |
18 | - | def run(self): |
18 | + | if not os.path.exists(folder_path): |
19 | - | try: |
19 | + | os.makedirs(folder_path) |
20 | - | while True: |
20 | + | |
21 | - | start_time = time.monotonic() |
21 | + | file_name = os.path.join(folder_path, "LC.txt") |
22 | with open(file_name, "w") as file: | |
23 | - | # Take a measurement |
23 | + | start_time = time.monotonic() |
24 | - | distance = sensor.range - 15 |
24 | + | |
25 | while True: | |
26 | - | # Get current timestamp |
26 | + | weight = hx.get_weight(1) |
27 | - | timestamp = time.monotonic() |
27 | + | timestamp = time.monotonic() - start_time |
28 | timestamp = round(timestamp, 5) | |
29 | - | # Emit the data |
29 | + | if weight > 0: |
30 | - | self.data_ready.emit(timestamp, distance) |
30 | + | file.write(f"Weight: {weight:.2f} g\t{timestamp:.3f}\n") # Limit decimal precision as needed |
31 | file.flush() | |
32 | - | # Calculate the elapsed time |
32 | + | time.sleep(0.001) # Reduced sleep time to improve data acquisition |
33 | - | elapsed_time = time.monotonic() - start_time |
33 | + | |
34 | def Time_of_Flight(bus): | |
35 | - | # Delay between measurements |
35 | + | bus.write_byte_data(0x29, 0x80, 0x01) |
36 | - | #time.sleep(0.01) # Adjust the delay as needed |
36 | + | # ... (rest of the ToF sensor code) |
37 | # Use the provided bus parameter instead of creating a new one | |
38 | - | except KeyboardInterrupt: |
38 | + | |
39 | - | pass |
39 | + | def MPU_6050(bus): |
40 | bus.write_byte_data(0x68, 0x6b, 0) | |
41 | - | def update_plot(timestamp, distance): |
41 | + | # ... (rest of the MPU6050 sensor code) |
42 | - | timestamps.append(timestamp) |
42 | + | # Use the provided bus parameter instead of creating a new one |
43 | - | distances.append(distance) |
43 | + | |
44 | if __name__ == "__main__": | |
45 | - | # Convert timestamps to milliseconds |
45 | + | try: |
46 | - | timestamps_ms = [ts - timestamps[0] for ts in timestamps] |
46 | + | bus = smbus.SMBus(1) # Use I2C bus 1 |
47 | - | curve.setData(timestamps_ms, distances) |
47 | + | bus.set_clock(10000) |
48 | # Creating and starting the threads | |
49 | - | # Adjust x-axis range to display only the latest data |
49 | + | thread1 = threading.Thread(target=Load_Cell, args=(bus,)) |
50 | - | x_range = max(timestamps_ms) - (max(timestamps_ms) - 1) |
50 | + | thread2 = threading.Thread(target=Time_of_Flight, args=(bus,)) |
51 | - | plot.setXRange(max(timestamps_ms) - x_range, max(timestamps_ms)) |
51 | + | thread3 = threading.Thread(target=MPU_6050, args=(bus,)) |
52 | ||
53 | - | sensor_thread = SensorThread() |
53 | + | thread1.start() |
54 | - | sensor_thread.data_ready.connect(update_plot) |
54 | + | thread2.start() |
55 | - | sensor_thread.start() |
55 | + | thread3.start() |
56 | ||
57 | - | app = QApplication([]) |
57 | + | # Wait for threads to finish (this won't be reached unless threads are stopped) |
58 | thread1.join() | |
59 | - | win = pg.GraphicsLayoutWidget(title="Real-time Distance Plot") |
59 | + | thread2.join() |
60 | - | plot = win.addPlot() |
60 | + | thread3.join() |
61 | - | plot.setLabel('left', 'Distance (mm)') |
61 | + | |
62 | - | plot.setLabel('bottom', 'Time (ms)') |
62 | + | except KeyboardInterrupt: |
63 | - | y_range = 60 |
63 | + | print("Exiting...") |
64 | - | plot.setRange(yRange=(0, y_range)) |
64 | + | GPIO.cleanup() |
65 | - | curve = plot.plot(pen='b') |
65 | + |