#conventional imports import time import sys from time import sleep import RPi.GPIO as GPIO import matplotlib.pyplot as plt from matplotlib.animation import FuncAnimation EMULATE_HX711=False #loadcell libraries stuff if not EMULATE_HX711: import RPi.GPIO as GPIO from hx711 import HX711 else: from emulated_hx711 import HX711 #/////////////////////////////////////// functions /////////////////////////////// def cleanAndExit(): print("Cleaning...") if not EMULATE_HX711: GPIO.cleanup() print("Bye!") sys.exit() #/////////////////////////////////////// variables ////////////////////////////// plt.style.use('dark_background') hfont = {'fontname':'light'} EMULATE_HX711=False referenceUnit = 1 DIR = 20 # high or low GPIO pin, depends on which raspi ur using STEP = 21 # Same but for step CW = 1 # Clockwise rotation CCW = 0 # Counterclockwise rotation SPR = 200 # Steps per revolution, multiplied by 32 due to microstepping delay = 0.0228/32 #Delay between steps hx = HX711(5, 6) ''' HERE COMES THE MOTOR STUFF NEEDED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ''' #///////////////////////////// Motor. Activating some pins and declaring them how to behave/////////// GPIO.setmode(GPIO.BCM) # GPIO.setup(DIR, GPIO.OUT) # dir pin as an output GPIO.setup(STEP, GPIO.OUT) # step pin as an output GPIO.output(DIR, CW) #declaring the output CW high #///////////////////////// microstepping //////////////////////////////////// MODE = (14, 15, 18) # raspi pins that will be used to microstep GPIO.setup(MODE, GPIO.OUT) #same as before, declaring them as outputs #Dictionary here of the type of microstepping you wanna use RESOLUTION = { 'FULL':(0,0,0), 'HALF':(1,0,0), '1/4' :(0,1,0), '1/8' :(1,1,0), '1/16':(0,0,1), '1/32':(1,0,1), } #declaring here which type of microstep we are assigning GPIO.output(MODE, RESOLUTION['FULL']) ''' HERE COMES THE LOADCELL STUFF NEEDED %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ''' # Taring first the loadcell hx.set_reading_format("MSB", "MSB") hx.set_reference_unit(214157/1000) hx.reset() hx.tare() print("Tare done! Add weight now...") ''' NOW WE ARE MAKING AN INFINITE LOOP IN WICH WE WILL READ THE LOAD CELL, PLOT IT AND LATER MAKE THE MOTOR DO ONE THING OR ANOTHER ''' load = [] plt.rcParams['font.weight'] = 'light' def animate(i): val = hx.get_weight(5) load.append(val) print(str(round(val,2)) + ' Grams') hx.power_down() hx.power_up() plt.cla() plt.plot( load, 'pink', linewidth = 3) plt.title('Custom load cell live data', **hfont) plt.ylabel('Load (grams)') if val > 200: for x in range(200): GPIO.output(DIR, CCW) GPIO.output(STEP, GPIO.HIGH) sleep(delay) GPIO.output(STEP, GPIO.LOW) sleep(delay) continue elif val < -200: for x in range(200): GPIO.output(DIR, CW) GPIO.output(STEP, GPIO.HIGH) sleep(delay) GPIO.output(STEP, GPIO.LOW) sleep(delay) continue else: e=1 ani = FuncAnimation(plt.gcf(), animate, interval = 100) plt.show()