2017-02-13 13 views
0

小さな3つの接続されたロボットを作ろうとしています。彼らは互いにセンサーデータを送受信しなければならず、読み取りをゼロにする傾向があります。 Iamは動きの角度を交換して、他の人が彼に従うようにしようとしています。 IamはMQTTを使用しており、非常にうまく動作します。 しかし、私はdef on_messageからデータを取得しようとすると、その変数はグローバルではなく、次の変数で使用されます。次のように コード:Defの変数からデータを取得して外部で使用する方法は?

############### MQTT section ################## 

# when connecting to mqtt do this; 
def on_connect(client, userdata, flags, rc): 
print("Connected with result code "+str(rc)) 
client.subscribe(sub_topic) 

# when receiving a mqtt message do this; 

def on_message(client, userdata, msg): 
masterangel = int(msg.payload) 
print (masterangel) 



client = mqtt.Client() 
client.on_connect = on_connect 
client.on_message = on_message 
client.connect(Broker, 1883, 60) 
client.loop_start() 

# Start the Program 
psm.screen.termPrintAt(1, "Press Go to stop program") 

while(not doExit): 

oldAngel = Angel 

Angel = gyro.readValue() 

Angelshow = "Current Angel ="+" "+str (Angel) 

if (oldAngel != Angel): 
    psm.screen.termPrintAt(5, Angelshow) 





if (Angel < masterangel) : 
    psm.BBM1.setSpeedSync(20) 
    psm.BAM1.floatSync() 

elif (masterangel < Angel) : 
    psm.BAM1.setSpeedSync(20) 
    psm.BBM1.floatSync() 

    client.publish(pub_topic, "test") 

が、今私は、whileループで変数「masterangel」を使用することができますどのように誰もがアイデアを持っていますか?ちなみに印刷順序は問題ありません。

print (masterangel) 

は、事前にあなたに感謝し、私はあなたがonMessage機能の範囲外の変数を初期化してからonMessage機能のグローバル変数としてそれをマークする必要があり、ヘルプ

答えて

0

を感謝しています。

python globalキーワードを見てください。

############### MQTT section ################## 

# when connecting to mqtt do this; 
def on_connect(client, userdata, flags, rc): 
print("Connected with result code "+str(rc)) 
client.subscribe(sub_topic) 

# when receiving a mqtt message do this; 

def on_message(client, userdata, msg): 
# use global version not local 
global masterangel 
masterangel = int(msg.payload) 
print (masterangel) 



client = mqtt.Client() 
client.on_connect = on_connect 
client.on_message = on_message 
client.connect(Broker, 1883, 60) 
client.loop_start() 

# initialise variable to starting value 
masterangel = 0 

# Start the Program 
psm.screen.termPrintAt(1, "Press Go to stop program") 

while(not doExit): 

oldAngel = Angel 

Angel = gyro.readValue() 

Angelshow = "Current Angel ="+" "+str (Angel) 

if (oldAngel != Angel): 
    psm.screen.termPrintAt(5, Angelshow) 





if (Angel < masterangel) : 
    psm.BBM1.setSpeedSync(20) 
    psm.BAM1.floatSync() 

elif (masterangel < Angel) : 
    psm.BAM1.setSpeedSync(20) 
    psm.BBM1.floatSync() 

    client.publish(pub_topic, "test") 
+0

ありがとうございました。助けてくれてありがとう:) –

-1

return文を活用し、代わりにglobalキーワードを使用しての、後で変数のスコープと来るかもしれない問題(すなわち、グローバル変数とローカル変数を混合)を避けるために。

つまり、リターン関数に渡す引数を実際に使用し、別の変数に格納することができます。これに対して、print()は画面に表示します。

############### MQTT section ################## 

# when connecting to mqtt do this; 
def on_connect(client, userdata, flags, rc): 
print("Connected with result code "+str(rc)) 
client.subscribe(sub_topic) 

# when receiving a mqtt message do this; 

def on_message(client, userdata, msg): 
masterangel = int(msg.payload) 
return (masterangel) # Use return() instead of print() 



client = mqtt.Client() 
client.on_connect = on_connect 
client.on_message = on_message 
client.connect(Broker, 1883, 60) 
client.loop_start() 

# Start the Program 
psm.screen.termPrintAt(1, "Press Go to stop program") 


#Assign the value of masterangel being returned by calling the on_message 
#function and storing it into its own variable. 

new_var = on_message() # This stores the returned value of masterangel 


while(not doExit): 

oldAngel = Angel 

Angel = gyro.readValue() 

Angelshow = "Current Angel ="+" "+str (Angel) 

if (oldAngel != Angel): 
    psm.screen.termPrintAt(5, Angelshow) 





if (Angel < new_var) : 
    psm.BBM1.setSpeedSync(20) 
    psm.BAM1.floatSync() 

elif (new_var < Angel) : 
    psm.BAM1.setSpeedSync(20) 
    psm.BBM1.floatSync() 

    client.publish(pub_topic, "test") 
+0

ローカルコード内のブローカからメッセージが配信されたときに 'on_message'がネットワークループによって呼び出されるので、これはうまくいきません。 – hardillb

+0

@hardillb、それを指摘してくれてありがとう。 –

関連する問題