简体   繁体   English

Python,RPi和GPIO-控制引擎

[英]Python, RPi and GPIO - control engine

On my Raspberry Pi I have code written in Python that control engine on my vehicle. 在我的Raspberry Pi上,我用Python编写了控制车辆引擎的代码。 I control engine by GPIO. 我通过GPIO控制引擎。 It works but the problem is that when I set io to go and then I set io to change direction it stops. 它有效,但是问题是当我将io设置为go,然后将io设置为更改方向时,它停止了。 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. 例如,当我使Turnbarrelstop时,油缸止动桶也停止转动。 Maybe the reason is that I call io.cleanup() ? 也许是因为我打电话给io.cleanup()吗? And it stop all GPIO signals? 并停止所有GPIO信号? I want to stop barrel but without stop going tank. 我想停下桶,但不要停下坦克。

Do not call io.cleanup(). 不要调用io.cleanup()。 I would recommend do not call it in gostop() function too. 我建议不要在gostop()函数中也不要调用它。 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. 您只需要更改(电平或PWM,如果适用)特定的io PIN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM