使用mqtt on_message python控制电机速度

时间:2019-11-28 12:06:37

标签: python multithreading mqtt global paho

使用代码,我现在可以接收mqtt消息并使用paho.mqtt中的on_message函数来运行电动机。问题是没有调用函数pwmb.ChangeDutyCycle(spd)。我的猜测是函数on_message无法访问pwmb变量。我尝试将标志global放在on_message内,并通过userdata传递变量。它运行正常,我什至可以打印变量(在两种情况下),但是函数pwmb.ChangeDutyCycle(spd)仍然无法运行(我已经检查了打印内容,线程必须在运行此行的代码后立即停止码)。我要附加我的代码和下面的输出:

GPIO.setmode(GPIO.BOARD)      # Set GPIO mode to BCM
GPIO.setwarnings(False);

# PWM Frequency
pwmFreq = 100
# Setup Pins for motor controllers
GPIO.setup(12, GPIO.OUT)    # PWMA  19
GPIO.setup(18, GPIO.OUT)    # AIN2  21
GPIO.setup(16, GPIO.OUT)    # AIN1  23
GPIO.setup(22, GPIO.OUT)    # STBY  24 
GPIO.setup(15, GPIO.OUT)    # BIN1  29
GPIO.setup(13, GPIO.OUT)    # BIN2  26
GPIO.setup(11, GPIO.OUT)    # PWMB  31

pwma = GPIO.PWM(12, pwmFreq)    # pin 18 to PWM  
pwmb = GPIO.PWM(11, pwmFreq)    # pin 13 to PWM

pwma.start(100)
pwmb.start(100)

def on_connect(client, userdata, flags, rc):
    print("Connected with result code "+str(rc))
    client.subscribe("r2b2/7/#")

def on_message(client, userdata, msg):
    print("Msg: "+str(msg.payload)+str(userdata))
    if msg.payload == "stop": motorStop()
    if msg.payload[:3] == "mBF":#1
        m1(msg.payload[4:],userdata)
    if msg.payload[:3] == "mRF":#2
        m2(msg.payload[4:],userdata)


def m2(spd,userdata):
    runMotor(0,spd,0,userdata)

def m1(spd,userdata):
    runMotor(1,spd,0,userdata)

def runMotor(motor, spd, direction,userdata):
    pwmb=userdata
    global pwma
    GPIO.output(22, GPIO.HIGH);
    in1 = GPIO.HIGH
    in2 = GPIO.LOW

    if(direction == 1):
        in1 = GPIO.LOW
        in2 = GPIO.HIGH

    if(motor == 1):#motor n1
        print("runMotor1"+spd+str(pwmb))
        GPIO.output(15, in1)
        GPIO.output(13, in2)
        print("THIS RUNS")
        pwmb.ChangeDutyCycle(spd)
        print("THIS DOESN'T")
    elif(motor == 0):#motor n2
        print("runMotor2"+spd+str(pwma))
        GPIO.output(16, in1)
        GPIO.output(18, in2)
        print("THIS RUNS")
        pwma.ChangeDutyCycle(spd)
        print("THIS DOESN'T")

def motorStop():
    GPIO.output(22, GPIO.LOW)
    GPIO.output(15, GPIO.LOW)
    GPIO.output(16, GPIO.LOW)

def main(args=None):
    # Create an MQTT client and attach our routines to it.
    client = mqtt.Client(userdata=pwmb)
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect(ipR2B2, 1883, 60)
    client.loop_start()

    while True:
         #loop
        pass

if __name__ == "__main__":
    main()

我在运行#1电动机时得到打印(使用userdata参数):

Connected with result code 0
Msg: mBF 50<RPi.GPIO.PWM object at 0xb62b82c0>
runMotor150<RPi.GPIO.PWM object at 0xb62b82c0>
THIS RUNS
Msg: mBF 50<RPi.GPIO.PWM object at 0xb62b82c0>
runMotor150<RPi.GPIO.PWM object at 0xb62b82c0>
THIS RUNS
Msg: mBF 50<RPi.GPIO.PWM object at 0xb62b82c0>
runMotor150<RPi.GPIO.PWM object at 0xb62b82c0>
THIS RUNS

所以我的电动机始终以100%的速度运行,这是初始值(在pwmb.start(100)上定义)。

如果我调用main()函数中所有与电动机相关的电动机,则可以改变速度,所以我的猜测是on_message与它有关。

0 个答案:

没有答案