johnmosquerav
Posts: 2
Joined: Fri Feb 08, 2019 4:42 am

HELP WITH Two stepper motors at same time

Tue Jan 14, 2020 4:13 am

I am triying do control of two stepper motor with drive HB808C, when I do control for only one, this is fine, however, when I do control with two stepper motors, the program in python don´t work correctly.

The principal idea is do control for 4 stepper motor at same time.

This is the main program:

Code: Select all

import threading
import random
import time
from servo2 import servo1
from servo3 import servo2
# how many threads we want to start
THREADS = { 'task1':2 , 'task2':3, 'task3':2}

def task1(threadName):
 servo1()
 

def task2(threadName):
    servo2()
    print ("TEST")
def task3(threadName):
    while True:
        print "I am %s and I execute task3" % threadName
        time.sleep(0.5)

def generic_workflow(threadName, task_type):
    if task_type == 'task1':
        task1(threadName)
    elif task_type == 'task2':
        task2(threadName)
    elif task_type == 'task3':
        task3(threadName)

class Thread_task(threading.Thread):
    def __init__(self, task_type):
        threading.Thread.__init__(self)
        self.task_type = task_type
    def run(self):
        threadName = threading.currentThread().getName()
        generic_workflow(threadName, task_type)

print 'Checking for threads for every task...'

total_threads = 0
for task in THREADS:
    total_threads += THREADS[task]

for task in THREADS:
    print " ** Starting %d threads for %s **" % (THREADS[task], task)
    for i in range(THREADS[task]):
        task_type = task
        td = Thread_task(task)
        td.start()

Theese are the servo1 and servo2 program:

def servo1():
 import RPi.GPIO as GPIO
 import time
 GPIO.setmode(GPIO.BCM)
 GPIO.setup(21, GPIO.OUT)
 GPIO.setup(16,GPIO.OUT)
 limite=250
 frecuencia=(0.0001)
 def movi1 ():
  GPIO.output(21,True)
  time.sleep(frecuencia)
  GPIO.output(21, False)
  time.sleep(frecuencia)
 
 def outOn1():
  GPIO.output(16,True)
 def outOff1():
  GPIO.output(16,False)

 while True:
  for i in range(limite+1):
   movi1()
   print (i)
  outOn1()
  time.sleep(0.5)
  for j in range(limite+1):
   movi1()
   print (j)
  outOff1()
  time.sleep(0.5)

-------------------------------------------------------

def servo2():
 import RPi.GPIO as GPIO
 import time
 GPIO.setmode(GPIO.BCM)
 GPIO.setup(20,GPIO.OUT)
 GPIO.setup(12,GPIO.OUT)
 limite=250
 frecuencia=(0.0001)
 def movi1 ():
  GPIO.output(20,True)
  time.sleep(frecuencia)
  GPIO.output(20, False)
  time.sleep(frecuencia)
 
 def outOn1():
  GPIO.output(12,True)
 def outOff1():
  GPIO.output(12,False)

 while True:
  for i in range(limite+1):
   movi1()
   print (i)
  outOn1()
  time.sleep(0.5)
  for j in range(limite+1):
   movi1()
   print (j)
  outOff1()
  time.sleep(0.5)

Last edited by johnmosquerav on Fri Jan 17, 2020 7:19 pm, edited 1 time in total.

pootle
Posts: 351
Joined: Wed Sep 04, 2013 10:20 am
Location: Staffordshire
Contact: Website

Re: HELP WITH Two stepper motors at same time

Wed Jan 15, 2020 8:36 am

it's hard to follow your code when all the indentation is lost, You're starting in the right place with threading though. Can you post the code with indentation present.....?

pcmanbob
Posts: 7668
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: HELP WITH Two stepper motors at same time

Wed Jan 15, 2020 11:39 am

johnmosquerav wrote:
Tue Jan 14, 2020 4:13 am
I am triying do control of two stepper motor with drive HB808C, when I do control for only one, this is fine, however, when I do control with two stepper motors, the program in python don´t work correctly.

The principal idea is do control for 4 stepper motor at same time.

This is the main program:

Code: Select all

import threading
import random
import time
from servo2 import servo1
from servo3 import servo2
# how many threads we want to start
THREADS = { 'task1':2 , 'task2':3, 'task3':2}

def task1(threadName):
 servo1()
 

def task2(threadName):
    servo2()
    print ("TEST")
def task3(threadName):
    while True:
        print "I am %s and I execute task3" % threadName
        time.sleep(0.5)

def generic_workflow(threadName, task_type):
    if task_type == 'task1':
        task1(threadName)
    elif task_type == 'task2':
        task2(threadName)
    elif task_type == 'task3':
        task3(threadName)

class Thread_task(threading.Thread):
    def __init__(self, task_type):
        threading.Thread.__init__(self)
        self.task_type = task_type
    def run(self):
        threadName = threading.currentThread().getName()
        generic_workflow(threadName, task_type)

print 'Checking for threads for every task...'

total_threads = 0
for task in THREADS:
    total_threads += THREADS[task]

for task in THREADS:
    print " ** Starting %d threads for %s **" % (THREADS[task], task)
    for i in range(THREADS[task]):
        task_type = task
        td = Thread_task(task)
        td.start()

Theese are the servo1 and servo2 program:

def servo1():
 import RPi.GPIO as GPIO
 import time
 GPIO.setmode(GPIO.BCM)
 GPIO.setup(21, GPIO.OUT)
 GPIO.setup(16,GPIO.OUT)
 limite=250
 frecuencia=(0.0001)
 def movi1 ():
  GPIO.output(21,True)
  time.sleep(frecuencia)
  GPIO.output(21, False)
  time.sleep(frecuencia)
 
 def outOn1():
  GPIO.output(16,True)
 def outOff1():
  GPIO.output(16,False)

 while True:
  for i in range(limite+1):
   movi1()
   print (i)
  outOn1()
  time.sleep(0.5)
  for j in range(limite+1):
   movi1()
   print (j)
  outOff1()
  time.sleep(0.5)

-------------------------------------------------------

def servo2():
 import RPi.GPIO as GPIO
 import time
 GPIO.setmode(GPIO.BCM)
 GPIO.setup(20,GPIO.OUT)
 GPIO.setup(12,GPIO.OUT)
 limite=250
 frecuencia=(0.0001)
 def movi1 ():
  GPIO.output(20,True)
  time.sleep(frecuencia)
  GPIO.output(20, False)
  time.sleep(frecuencia)
 
 def outOn1():
  GPIO.output(12,True)
 def outOff1():
  GPIO.output(12,False)

 while True:
  for i in range(limite+1):
   movi1()
   print (i)
  outOn1()
  time.sleep(0.5)
  for j in range(limite+1):
   movi1()
   print (j)
  outOff1()
  time.sleep(0.5)



 

Have quoted original post and added code tags.

johnmosquerav when using code tags you need to put the code between the tages

Add [code] at the top

code goes here

and [/code] at the bottom.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

johnmosquerav
Posts: 2
Joined: Fri Feb 08, 2019 4:42 am

Re: HELP WITH Two stepper motors at same time

Fri Jan 17, 2020 7:21 pm

Thanks, I do the change in the post, now your can understand better, thanks, I will attend to news response.

Return to “Python”