Page 1 of 1

how to control 2 or more motors with async using PIGPIO Library?

Posted: Wed May 12, 2021 8:13 am
by muhosevo
hi everyone,
i have 2 NEMA 34 stepper motors and i want to control them different frequency/PWM at the same time. these are my example codes. In this code just one motor works. i do not know why 2 motors are not running. how can i fix the problem?

Code: Select all

import pigpio

pulse_1 = 21
dir_1 = 20

pulse_2 = 5
dir_2 = 17

pi = pigpio.pi()

pi.set_mode(pulse_1, pigpio.OUTPUT)
pi.set_mode(dir_1, pigpio.OUTPUT)


pi.set_mode(pulse_2, pigpio.OUTPUT)
pi.set_mode(dir_2, pigpio.OUTPUT)

class MOTOR_1:
    def __init__(self, ramp):
        self.generate_ramp(ramp)

    def generate_ramp(self, ramp):
        pi.wave_clear() #delete all waveforms
        length = len(ramp)
        wid = [-1] * length

        for i in range(length):
            frequency = ramp[i][0]
            half_periods = int(500000 / frequency)
            wf = []
            wf.append(pigpio.pulse(1<< pulse_1, 0, half_periods))
            wf.append(pigpio.pulse(0, 1<<pulse_1, half_periods))
            pi.wave_add_generic(wf)
            wid[i] = pi.wave_create()

        chain = []
        for i in range(length):
            steps = ramp[i][1]
            x = steps & 255
            y = steps >> 8
            chain += [255, 0, wid[i], 255, 1, x, y]
        
        pi.wave_chain(chain)


class MOTOR_2:
    def __init__(self,ramp):
        self.generate_ramp(ramp)

    def generate_ramp(self, ramp):
        #pi.wave_clear() #delete all wave forms
        length = len(ramp)
        wid = [-1] * length

        for i in range(length):
            frequency = ramp[i][0]
            half_periods = int(500000 / frequency)
            wf = []
            wf.append(pigpio.pulse(0, 1<<pulse_2, half_periods / 2))
            wf.append(pigpio.pulse(1<< pulse_2, 0, half_periods / 2))
            
            pi.wave_add_generic(wf)
            wid[i] = pi.wave_create()

        chain = []
        for i in range(length):
            steps = ramp[i][1]
            x = steps & 255
            y = steps >> 8
            chain += [255, 0, wid[i], 255, 1, x, y]
        
        pi.wave_chain(chain)

motor_1 = MOTOR_1([[1000, 2400],
                [2500, 2400],
                [5000, 2400],
                [7500, 2400],
                [9500, 2400],
                [10000, 2400],
                [9500, 2400],
                [8000, 2400],
                [6500, 2400],
                [5000, 2400],
                [4000, 2400],
                [2000, 2400],
                [1000, 2400]])


motor_2 = MOTOR_2([[1500, 9600],
                  [1700, 9600],
                  [2300, 9600],
                  [2500, 9600],
		  [2700, 9600],
		  [3000, 9600]])


Re: how to control 2 or more motors with async using PIGPIO Library?

Posted: Wed May 12, 2021 10:28 am
by joan
There can only be one wave being sent at any one time. If you send a new wave it will replace any existing one. You can drive multiple GPIO with one wave so you can certainly drive more than one stepper with one wave.

Re: how to control 2 or more motors with async using PIGPIO Library?

Posted: Wed May 12, 2021 11:31 am
by muhosevo
joan wrote:
Wed May 12, 2021 10:28 am
There can only be one wave being sent at any one time. If you send a new wave it will replace any existing one. You can drive multiple GPIO with one wave so you can certainly drive more than one stepper with one wave.
is it possible to make different acceleration and different frequency and different pulses can you write a couple of code or fix the given example?

Re: how to control 2 or more motors with async using PIGPIO Library?

Posted: Thu May 13, 2021 6:32 am
by pootle
muhosevo wrote:
Wed May 12, 2021 11:31 am

is it possible to make different acceleration and different frequency and different pulses can you write a couple of code or fix the given example?
Yes absolutely possible, but not simple. I have a library here that drives multiple steppers using waves using A4988 style driver chips. - you have not said what driver hardware you are using tho'.

I use a generator to yield pulse timings for each motor independently, and then merge these into a single stream of timings. It would be difficult to adapt your code to create merged waves.

Re: how to control 2 or more motors with async using PIGPIO Library?

Posted: Thu May 13, 2021 11:11 am
by muhosevo
pootle wrote:
Thu May 13, 2021 6:32 am
muhosevo wrote:
Wed May 12, 2021 11:31 am

is it possible to make different acceleration and different frequency and different pulses can you write a couple of code or fix the given example?
Yes absolutely possible, but not simple. I have a library here that drives multiple steppers using waves using A4988 style driver chips. - you have not said what driver hardware you are using tho'.

I use a generator to yield pulse timings for each motor independently, and then merge these into a single stream of timings. It would be difficult to adapt your code to create merged waves.
İ looked your library, you succeded great job, Congrats! I am using Nema34 stepper motor as i said, my drives is CWD860. Actually i want to make my own cnc control software, like mach3 but not 3 axis, may be up to 10 axes. And i dont wan to be dependent any external library. But i will definetely benefit from your library, if you want to share a couple of codes related with multiple gpio using different pwm/frequemcy each other, i will be glad to.