1

I want to make a motorized rc paddle boat type machine for fun. I plan to use two stepper motors to spin two wheels on either side of the boat. I am controlling it with python. I have a master script to act as a front end, and two other scripts to manage the motors. Front end:

#!/usr/bin/env python 
from m1 import runone
from m1 import setup
from m2 import runtwo
import RPi.GPIO as GPIO  
import time
GPIO.setmode(GPIO.BCM)
setup()
runone()
runtwo()

runone() runs the motor on one side, runtwo() the other, and setup() set's up the pins, and stes them to output. My problem is that it won't that runtwo() can't run until runone() finishes and runone() won't finish until the motor stops. For the ability to turn, the motors need to turn on and off independent of each other. So I can only run one motor! I've tried multithreading and encountered the same error! Thanks in advance. If I need to post more of my code just tell me.

The code for runone()

def runone():
    while True:
        forward1(0.005, 10000)
def forward1(delay, steps):    
    for i in range(0, steps):  
        setStep1(1, 0, 0, 0)  
        time.sleep(delay)  
        setStep1(0, 1, 0, 0)  
        time.sleep(delay)  
        setStep1(0, 0, 1, 0)  
        time.sleep(delay)
        setStep1(0, 0, 0, 1)  
        time.sleep(delay)  
def setStep1(w11, w12, w13, w14):  

    GPIO.output(IN11, w11)  
    GPIO.output(IN12, w12)  
    GPIO.output(IN13, w13)  
    GPIO.output(IN14, w14)
  • You could have method called startone(), stopone(), starttwo() and stoptwo() which would do as their names suggest. – Mark Smith Apr 16 at 16:02
  • @MarkSmith wouldn't that result in the same error? what exactly would startone() do? EDIT: What if it controlled a variable which controlled whether the motors where on? will try that now. – G. R. Penland Apr 16 at 16:07
  • Perhaps you need to explain how you are controlling the motors. I'm assuming you were setting a GPIO. You can just set a GPIO - it stays set until you change it. startone() would just set the GPIO pin and return. It would stay set until you stopone() which would clear it. – Mark Smith Apr 16 at 16:22
  • @MarkSmith It's more complicated than that. I'll post the code for runone() in my question so you can see. – G. R. Penland Apr 16 at 18:36
0

The most obvious solution would be to create a function that runs both simultaneously. Wouldn’t this work ?

def forward_both (delay, steps):    
    for i in range(0, steps):  
        setStep1(1, 0, 0, 0) 
        setStep2(1, 0, 0, 0) 
        time.sleep(delay)  
        setStep1(0, 1, 0, 0) 
        setStep2(0, 1, 0, 0)  
        time.sleep(delay)  
        setStep1(0, 0, 1, 0) 
        setStep2(0, 0, 1, 0) 
        time.sleep(delay)
        setStep1(0, 0, 0, 1) 
        setStep2(0, 0, 0, 1) 
        time.sleep(delay) 
  • Yes, but no. I need to be able to turn the separate motors on and off so that it can turn. A rudder is possible I guess but I can't think how to do that. – G. R. Penland Apr 28 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.