Python Forum

Full Version: Control 2 stepper motor simultaneously
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I want control 2 stepper motors simultaneously for cnc tracer application. The positions of the picture points are (x_pos and y_pos), read from a G-code file.

My goal is to control the stepperx so it can move (q) steps while steppery moves (1) step for a certain condition until it reach their final positions (x_pos and y_pos)
In this program, I didn't include the library neither the configurations for the pins.
I am wondering if this program is syntactically correct

I would appreciate any advice you could give me and thank you in advance


 
dx= 0.075
x_pos=20#mm
y_pos=10
delay =0.0208 
DIR1=20
STEP1=21
DIR2 = #pin
STEP2= #pin
CW=1
CCW=0

step_count_x =int (round(x_pos/dx)) 
step_count_y =int (round(y_pos/dx)) 
print ('step_count_x =', step_count_x )
print ('step_count_y =', step_count_y )

def clockwise1():
   GPIO.output(DIR1, CW) 
   GPIO.output(STEP1, GPIO.HIGH)
   sleep(delay)
   GPIO.output(STEP1, GPIO.LOW)
   sleep(delay)

def anticlockwise1():
   GPIO.output(DIR1, CCW)  
   GPIO.output(STEP1, GPIO.LOW)
   sleep(delay)
   GPIO.output(STEP1, GPIO.HIGH)
   sleep(delay)
        
def clockwise2():
   GPIO.output(DIR2, CW) 
   GPIO.output(STEP2, GPIO.HIGH)
   sleep(delay)
   GPIO.output(STEP2, GPIO.LOW)
   sleep(delay)

def anticlockwise2():
   GPIO.output(DIR2, CCW1)  
   GPIO.output(STEP2, GPIO.LOW)
   sleep(delay)
   GPIO.output(STEP2, GPIO.HIGH)
   sleep(delay)
        
def stepperx (step_count_x):
  for x in range(step_count_x):
      if step_count_x > 0 :
       clockwise1()
      else: 
       anticlockwise1()
  return 0;

  
def steppery (step_count_y):
  for y in range(step_count_y):
      if step_count_y > 0:
       clockwise2()
      else: 
       anticlockwise2()
  return 0;


def motorcontrol (step_count_x,step_count_y):
 if (step_count_x> step_count_y): 
  q=step_count_x//step_count_y
  print ('q =', q )
  for k in range(1,step_count_y):
    stepperx(q)
    steppery(1)

 r= step_count_x%step_count_y
 print ('r =', r )
 stepperx(r)      


 if (step_count_y> step_count_y): 
  q=step_count_y//step_count_x
  print ('q =', q )
  for k in range(1,step_count_x):
    stepperx(1)
    steppery(q)

 r= step_count_y%step_count_x
 print ('r =', r )
 steppery(r)    

 if (step_count_y == step_count_x): 
  
  for k in range(1,step_count_x):
    stepperx(1)
    steppery(1)) 
following my unanswered post, Does anyone have a method or idea on how to set back two stepper motor to there original position ? cause it seems when the step_count is negative, the motor doesn't turn Huh
and thank you
Driving step motors with pure Python will cause timing problems.
You have no control, how long the interpreter needs to execute the next statement.


This should result into inaccurate movement (slower, faster).
But, if you google for Python stepper, you'll find libraries.

For example, you can use this one: https://github.com/luxedo/RPistepper
But this library is written in pure Python. I don't know how good this library is.
Just try it. There are also another libraries.