0

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)
4
  • You could have method called startone(), stopone(), starttwo() and stoptwo() which would do as their names suggest.
    – Mark Smith
    Commented Apr 16, 2019 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. Commented Apr 16, 2019 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
    Commented Apr 16, 2019 at 16:22
  • @MarkSmith It's more complicated than that. I'll post the code for runone() in my question so you can see. Commented Apr 16, 2019 at 18:36

1 Answer 1

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) 
1
  • 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. Commented Apr 28, 2019 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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