2

I want to make multiple stepper motors turning at same time, with L298N driver.

My idea is using relative coordinate on Milling machine, and separate x, y, z value from input coordinate before doing relative positioning calculation.

The code is

def coordinate(inputlist, d):
    D = int(d)
    l = ast.literal_eval(f"[{inputlist}]")#do not use "eval()" code
    lx = [x for x,y,z in l]
    ly = [y for x,y,z in l]
    lz = [z for x,y,z in l]
    x_value(lx, axisx, D)
    y_value(ly, axisy, D)
    z_value(lz, axisz, D)

def x_value(lx, axisx, D):
    delay = D
    stepper = axisx
    x = lx
    xv = [] 
    for f in range(len(x)-1):
        bx = x[f+1] - x[f]  
        xv.append(bx)   
    print(xv)
    for steps in xv:
        if  (steps >=0):
            forword(delay, steps, stepper)
        elif (steps <=0):
            backward(delay, steps, stepper)
def y_value(ly, axisy, D):
    delay = D
    stepper = axisy
    y = ly
    yv = [] 
    for g in range(len(y)-1):
        by = y[g+1] - y[g]  
        yv.append(by)   
    print(yv)
    for steps in yv:
        if  (steps >=0):
            forword(delay, steps, stepper)
        elif (steps <=0):
            backward(delay, steps, stepper)
def z_value(lz, axisz, D):
    delay = D
    stepper = axisz
    z = lz
    zv = [] 
    for h in range(len(z)-1):
        bz = z[h+1] - z[h]  
        zv.append(bz)   
    print(zv)
    for steps in zv:
        if  (steps >=0):
            forword(delay, steps, stepper)
        elif (steps <=0):
            backward(delay, steps, stepper)

def forword(delay, steps, stepper):
    for i in range(steps):
        for halfstep in range(8):
            for pin in range(4):
                GPIO.output(stepper[pin], seq[halfstep][pin])
            time.sleep(int(delay) / 10000)

def backward(delay, steps, stepper):
    seq.reverse()
    for i in range(-int(steps)):
        for halfstep in range(8):
            for pin in range (4):
                GPIO.output(stepper[pin], seq[halfstep][pin])
            time.sleep(int(delay) / 10000)
    seq.reverse()

try:
    while True:
        inputlist = input("Coordinate List (Unit is 10um, 1mm = 100 (10um)):")
        d = input("speed of stepper(do not less then 7):")
        coordinate(inputlist, d)

No matter how I tried, the three stepper motors are turning separately.

I want to make stepper motors turning at same time, no matter what value I put in with coordinate form (x1,y1,z1), (x2,y2,z2)......., (xn,yn,zn)

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Fayz Huang
  • 27
  • 2
  • Does this answer your question? [How do I iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-do-i-iterate-through-two-lists-in-parallel) – mkrieger1 Mar 09 '23 at 19:11
  • In fact, you already have what you need. Create a single loop over `l`, don't use `lx`, `ly`, `lz`. – mkrieger1 Mar 09 '23 at 19:22

1 Answers1

2

You have organized your code using functions. Good.

However, they all bottom out with halfstep iterations and a call to

            time.sleep(int(delay) / 10_000)

which is not compatible with your design goals.

You cannot sleep there. Why? Because you have other plates to keep spinning, other motors to keep stepping.


linspace is a very simple function. You don't have to call it directly, but you should create a list or generator that captures what it does.

We need to produce a schedule across all motors. Pick some convenient resolution, maybe 1 msec or 10 msec, and produce delta x,y,z figures that you want to achieve starting at each timestamp. When all deltas are zero, you're done and can stop emitting schedule rows, as all motors have achieved desired positions.

Now we need to implement that schedule. Step through each timestamp, then through each motor, and issue halfstep commands as needed. This took a moment to do. Consult the current time and sleep till next interval. Lather, rinse, repeat till all schedule rows are consumed.

What have we accomplished here? We pulled the sleep() up a level, so it's not peculiar to a specific motor. We command one or more motors to halfstep if needed, then pause while they all do that, then loop again to see if there's any more commands that still need to be issued.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • just so - instead of _waiting_, it's probably fairly easy to switch to setting the _next time_ an action should take place and skip if it's not that time _yet_ – ti7 Mar 09 '23 at 19:23