Search code examples
pythonraspberry-piserial-portgpio

Python, RPi and GPIO - control engine


On my Raspberry Pi I have code written in Python that control engine on my vehicle. I control engine by GPIO. It works but the problem is that when I set io to go and then I set io to change direction it stops. Why it cannot do two things in one time? This is my code:

import RPi.GPIO as io
import time
import serial

class TankManager:
    pLeft = 0
    pRight = 0
    turnBarrel = 0
    liftBarrel = 0

    def __init__(self):
        io.setmode(io.BCM)

    def goahead(self, speed):
        if(speed > 25) : speed = 25

        io.setup(12, io.OUT)

        TankManager.pLeft = io.PWM(12, 2.2)
        TankManager.pLeft.start(1)

        io.setup(13, io.OUT)

        TankManager.pRight = io.PWM(13, 2.2)
        TankManager.pRight.start(1)

        io.setup(20, io.OUT)
        io.output(20, False)

        io.setup(21, io.OUT)
        io.output(21, False)
        return

 def gostop(self):
        if 'pLeft' in globals():
            TankManager.pLeft.stop()
        if 'pRight' in globals():
            TankManager.pRight.stop()
        io.cleanup();
        return

    def turnright(self):
        io.setup(12, io.OUT)

        TankManager.pLeft = io.PWM(12, 2.2)
        TankManager.pLeft.start(1)

        io.setup(21, io.OUT)
        io.output(21, False)
        return

 def turnbarrelstop(self):
        if 'turnBarrel' in globals():
            TankManager.turnBarrel.stop()

        io.cleanup();
        return

And for example when I make turnbarrelstop the tank stop barrel but it stop going too. Maybe the reason is that I call io.cleanup() ? And it stop all GPIO signals? I want to stop barrel but without stop going tank.


Solution

  • Do not call io.cleanup(). I would recommend do not call it in gostop() function too. And use it only if you are exiting the program.

    You just need to change (level or PWM whatever applicable) of the specific io PIN.