Python Forum
Control 2 stepper motor simultaneously
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Control 2 stepper motor simultaneously
#1
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)) 
Reply
#2
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
Reply
#3
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.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  stepper motor and servo key control cel 4 337 Jul-27-2020, 06:26 AM
Last Post: DPaul
  Stepper Motor key control cel 2 315 Jul-25-2020, 12:19 AM
Last Post: cel
  EOF error using stepper motor cel 1 343 Jul-24-2020, 01:27 PM
Last Post: Larz60+
  How to run same process simultaneously exploit123 1 536 Sep-19-2019, 10:08 AM
Last Post: Gribouillis
  Moving with objects simultaneously kom2 1 690 Apr-20-2019, 07:12 PM
Last Post: SheeppOSU
  How to use servo motor with TFMini Distance Sensor in python script? programerguy 1 978 Dec-04-2018, 04:57 PM
Last Post: Larz60+
  using subpocess for both reading and writing simultaneously dauriac 11 3,737 May-30-2018, 10:09 PM
Last Post: killerrex
  controlling multiple server simultaneously. caligola 3 1,273 May-11-2018, 05:44 PM
Last Post: wavic
  How to define two functions run simultaneously within a function? Alberto 4 1,652 Feb-06-2018, 10:08 PM
Last Post: Alberto
  Modifying / extracting multiple list items simultaneously using variable from range ehammarlund 4 1,675 Dec-06-2017, 08:15 PM
Last Post: ehammarlund

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020