Advertisement
rikisu_

working_lc_lib

Jul 16th, 2023 (edited)
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import RPi.GPIO as GPIO
  2. from hx711 import HX711
  3. import smbus
  4. import time
  5. import os
  6. import threading
  7.  
  8. def Load_Cell(bus):
  9.     GPIO.setmode(GPIO.BCM)
  10.     hx = HX711(5, 6)
  11.     hx.set_reading_format("MSB", "MSB")
  12.     hx.set_reference_unit(-2199)  # Set your reference unit value here (e.g., for grams)
  13.     readings = 0
  14.     hx.reset()
  15.     hx.tare()
  16.  
  17.     folder_path = "./DATA"
  18.     if not os.path.exists(folder_path):
  19.         os.makedirs(folder_path)
  20.  
  21.     file_name = os.path.join(folder_path, "LC.txt")
  22.     with open(file_name, "w") as file:
  23.         start_time = time.monotonic()
  24.  
  25.         while True:
  26.             weight = hx.get_weight(1)
  27.             timestamp = time.monotonic() - start_time
  28.             timestamp = round(timestamp, 5)
  29.             if weight > 0:
  30.                 file.write(f"Weight: {weight:.2f} g\t{timestamp:.3f}\n")  # Limit decimal precision as needed
  31.                 file.flush()
  32.                 time.sleep(0.001)  # Reduced sleep time to improve data acquisition
  33.  
  34. def Time_of_Flight(bus):
  35.     bus.write_byte_data(0x29, 0x80, 0x01)
  36.     # ... (rest of the ToF sensor code)
  37.     # Use the provided bus parameter instead of creating a new one
  38.  
  39. def MPU_6050(bus):
  40.     bus.write_byte_data(0x68, 0x6b, 0)
  41.     # ... (rest of the MPU6050 sensor code)
  42.     # Use the provided bus parameter instead of creating a new one
  43.  
  44. if __name__ == "__main__":
  45.     try:
  46.         bus = smbus.SMBus(1)  # Use I2C bus 1
  47.         bus.set_clock(10000)
  48.         # Creating and starting the threads
  49.         thread1 = threading.Thread(target=Load_Cell, args=(bus,))
  50.         thread2 = threading.Thread(target=Time_of_Flight, args=(bus,))
  51.         thread3 = threading.Thread(target=MPU_6050, args=(bus,))
  52.  
  53.         thread1.start()
  54.         thread2.start()
  55.         thread3.start()
  56.  
  57.         # Wait for threads to finish (this won't be reached unless threads are stopped)
  58.         thread1.join()
  59.         thread2.join()
  60.         thread3.join()
  61.  
  62.     except KeyboardInterrupt:
  63.         print("Exiting...")
  64.         GPIO.cleanup()
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement