I've got a large python script, which I already think can be shorter, but I'm new to python so I may have repeated allot of the variables and re-stated them. I'm using a stepper motor with a raspberry pi, to control a 1M Camera slider with a pulley belt. It works well at the moment, and I already have the max steps it takes to reach the end stated in the calibration function. However, what I'm trying to do, is have the script know where the motor is and has been, this way I dont need to reset the position every time I run a function. For example. If I where to click Pos 1 (It moves a quarter along the slider), if I click pos 2, it will move half, from where it already is. So that would be half and a quarter. I need the code to some how know where it is by logging the steps its taken and the direction. Although, I don't know where to start or how to do so. I've looked through different tutorials, and researched allot but nothing seems to match what I'm trying to do.
Here is my long python code :P
import BlynkLib import time import RPi.GPIO as GPIO AUTH = '2410eb3a943f4b86a4beddca465b1283' # Initialize Blynk blynk = BlynkLib.Blynk(AUTH, "127.0.0.1") # Variables GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) # Enable GPIO pins for ENA and ENB for stepper enable_a = 18 enable_b = 22 # Enable pins for IN1-4 to control step sequence coil_A_1_pin = 6 coil_A_2_pin = 13 coil_B_1_pin = 19 coil_B_2_pin = 26 # Set pin states GPIO.setup(enable_a, GPIO.OUT) GPIO.setup(enable_b, GPIO.OUT) GPIO.setup(coil_A_1_pin, GPIO.OUT) GPIO.setup(coil_A_2_pin, GPIO.OUT) GPIO.setup(coil_B_1_pin, GPIO.OUT) GPIO.setup(coil_B_2_pin, GPIO.OUT) # Set ENA and ENB to high to enable stepper # Function for step sequence # Calibration @blynk.VIRTUAL_WRITE(0) def my_write_handler(value): if value == '1': print(value) steps = 850 delay = 0.001 def setStep(w4, w3, w2, w1): GPIO.output(enable_a, True) GPIO.output(enable_b, True) GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) for i in range(0, steps): setStep(1,0,1,0) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(1,0,0,1) time.sleep(delay) GPIO.output(enable_a, False) GPIO.output(enable_b, False) def setStep(w1, w2, w3, w4): GPIO.output(enable_a, True) GPIO.output(enable_b, True) GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) for i in range(0, steps): setStep(1,0,1,0) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(1,0,0,1) time.sleep(delay) GPIO.output(enable_a, False) GPIO.output(enable_b, False) # Left @blynk.VIRTUAL_WRITE(5) def my_write_handler(value): if value == '1': print(value) steps = 850 delay = 0.001 def setStep(w4, w3, w2, w1): GPIO.output(enable_a, True) GPIO.output(enable_b, True) GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) for i in range(0, steps): setStep(1,0,1,0) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(1,0,0,1) time.sleep(delay) GPIO.output(enable_a, False) GPIO.output(enable_b, False) # Right @blynk.VIRTUAL_WRITE(7) def my_write_handler(value): if value == '1': print(value) steps = 850 delay = 0.001 def setStep(w1, w2, w3, w4): GPIO.output(enable_a, True) GPIO.output(enable_b, True) GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) for i in range(0, steps): setStep(1,0,1,0) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(1,0,0,1) time.sleep(delay) GPIO.output(enable_a, False) GPIO.output(enable_b, False) # POS 1 @blynk.VIRTUAL_WRITE(1) def my_write_handler(value): if value == '1': print(value) pos1 = 212 delay = 0.001 def setStep(w4, w3, w2, w1): GPIO.output(enable_a, True) GPIO.output(enable_b, True) GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) for i in range(0, pos1): setStep(1,0,1,0) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(1,0,0,1) time.sleep(delay) GPIO.output(enable_a, False) GPIO.output(enable_b, False) @blynk.VIRTUAL_WRITE(2) def my_write_handler(value): if value == '1': print(value) steps = 424 delay = 0.001 def setStep(w4, w3, w2, w1): GPIO.output(enable_a, True) GPIO.output(enable_b, True) GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) # loop through step sequence based on number of steps for i in range(0, steps): setStep(1,0,1,0) time.sleep(delay) setStep(0,1,1,0) time.sleep(delay) setStep(0,1,0,1) time.sleep(delay) setStep(1,0,0,1) time.sleep(delay) GPIO.output(enable_a, False) GPIO.output(enable_b, False) @blynk.VIRTUAL_WRITE(3) def my_write_handler(value): if value == '1': print(value) steps = 636 step_delay = 0.001 def setStep(w4, w3, w2, w1): GPIO.output(enable_a, True) GPIO.output(enable_b, True) GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) # loop through step sequence based on number of steps for i in range(0, steps): setStep(1,0,1,0) time.sleep(step_delay) setStep(0,1,1,0) time.sleep(step_delay) setStep(0,1,0,1) time.sleep(step_delay) setStep(1,0,0,1) time.sleep(step_delay) GPIO.output(enable_a, False) GPIO.output(enable_b, False) @blynk.VIRTUAL_WRITE(4) def my_write_handler(value): if value == '1': print(value) steps = 850 step_delay = 0.001 def setStep(w4, w3, w2, w1): GPIO.output(enable_a, True) GPIO.output(enable_b, True) GPIO.output(coil_A_1_pin, w1) GPIO.output(coil_A_2_pin, w2) GPIO.output(coil_B_1_pin, w3) GPIO.output(coil_B_2_pin, w4) # loop through step sequence based on number of steps for i in range(0, steps): setStep(1,0,1,0) time.sleep(step_delay) setStep(0,1,1,0) time.sleep(step_delay) setStep(0,1,0,1) time.sleep(step_delay) setStep(1,0,0,1) time.sleep(step_delay) GPIO.output(enable_a, False) GPIO.output(enable_b, False) GPIO.output(enable_a, False) GPIO.output(enable_b, False) blynk.run()