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.
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
  micropython-stepper 1.0.3 trix 2 588 Nov-20-2023, 06:00 AM
Last Post: trix
  Stepper motor/Easy Driver/HELP Harney 1 1,887 Jul-31-2021, 08:05 AM
Last Post: Harney
  python 3 raspberry pi 4 dual control motor programming problem yome 0 1,974 Mar-21-2021, 05:17 PM
Last Post: yome
  stepper motor and servo key control cel 4 2,598 Jul-27-2020, 06:26 AM
Last Post: DPaul
  Stepper Motor key control cel 2 1,907 Jul-25-2020, 12:19 AM
Last Post: cel
  EOF error using stepper motor cel 1 1,711 Jul-24-2020, 01:27 PM
Last Post: Larz60+
  How to run same process simultaneously exploit123 1 2,444 Sep-19-2019, 10:08 AM
Last Post: Gribouillis
  Moving with objects simultaneously kom2 1 3,019 Apr-20-2019, 07:12 PM
Last Post: SheeppOSU
  How to use servo motor with TFMini Distance Sensor in python script? programerguy 1 3,239 Dec-04-2018, 04:57 PM
Last Post: Larz60+
  using subpocess for both reading and writing simultaneously dauriac 11 9,657 May-30-2018, 10:09 PM
Last Post: killerrex

Forum Jump:

User Panel Messages

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