1

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() 
1
  • This is a general programming question, really nothing to do with the Raspberry Pi. Commented Oct 5, 2017 at 20:29

1 Answer 1

1

From a general programming point of view, this should be pretty simple to accomplish. I've not really taken the time to properly see how your code above interacts with the stepper motor in details for each function, but i'll try to answer your question from a high level perspective.

Let's say you have 3 known positions the stepper can reach, POS1, POS2, POS3. Each of these relate to a cetain number of steps from the start of the slider, let's assume 100, 200 and 300 respectively. So you could set up a few variables this way:

POS1 = 100 POS2 = 200 POS3 = 300 current_pos = 0 

Anytime you run a function to move the slider to a certain position, you should use and update the current_pos variable. You can see that this variable is initialized with a value of 0, since i'm assuming that at the start of the program, the slider will be at the start of the reel.

So for example you could create just one function to perform the movements as required, in relation to the current position of the slider.

def move_x_steps(current_pos, pos): end_pos = pos - current_pos return end_pos ## end_pos would be the number of steps requried to reach the requried position in relation to the current position. 

Hence if you pass in any of the position variables stated above you would easily evluate the number of steps required to reach the wanted position and also update the current postion of the slider once the funtion has been called.

POS1 = 100 POS2 = 200 POS3 = 300 current_pos = 0 current_pos = move_x_steps(current_pos, POS3) 

You could apply this general logic to your specific problem, to achieve what you were looking for. Hope this helps.

4
  • This is actually a great explanation, very detailed :) Thank you, I'll apply it to my code, and let you know if it works then mark the answer Commented Oct 5, 2017 at 14:03
  • I tried to apply this, but the stepper cant move to that pos without going to reverse, for example with the way the stepper is running in the code, it jumps to 80 steps for example, going this way W1, W2, W3, W4. If it where to jump back 40 steps, I would have to reverse the W order :S Commented Oct 6, 2017 at 9:11
  • Ok, so i guess the best approach to this would be to have a "move_forward" function and a "move_backwards" function. You could determine the number of steps required to reach the wanted position, same way as above. And then you could use an if/else statement to use either a move_forwad or a move_backwards function. Commented Oct 6, 2017 at 11:06
  • Following from previous example: current_pos = move_x_steps(current_pos, POS3) if current_pos > 0: move_forward(W1,W2,W3,W4) else: move_backwards(W4,W3,W2,W1) Something along these lines should work. Commented Oct 6, 2017 at 11:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.