how to get data from a variable in Def to use it outside and remains updated?

El-Salatinie

Im trying to make a small 3 connected robots. They have to send and receive sensor data from each other and take an action to make the reads tends to zero again . Iam trying to exchange the angle of the movements to let the other one follow him . Iam using the MQTT and it works very well . But when i try to get the data from def on_message to use it in the next while the variable isn't global . the code as follow :

 ############### 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")

now has anyone an Idea how can i use the variable "masterangel" in the while loop ? by the way the print order of works fine .

  print ( masterangel ) 

thank you in Advance and i appreciate your Help

hardillb

You need to initialise the variable outside the scope of the onMessage function and then mark it as a global variable in the onMessage functions.

Take a look at the python global keyword.

 ############### 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")

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

variable not updated outside if statement

From Dev

How to get updated variable by a python function from another python function?

From Dev

How do I use variables of my def function outside?

From Dev

How use a variable outside a callback

From Java

How to force Entity Framework to always get updated data from the database?

From Dev

How to call php function to get updated data from MYSQL with jQuery

From Dev

XSLT - How to get value from for-each loop and use outside it

From Dev

How to use a javascript variable in jquery GET 'data: '

From Dev

how to bring out data out from def

From Dev

how to bring out data out from def

From Dev

How to get data from instancetype variable in python

From Dev

How to use a variable outside a PARFOR loop in MATLAB?

From Dev

swift 2 how to use variable outside the function?

From Dev

How to use a variable outside the function in PHP?

From Dev

How to use variable outside the task in gradle script

From Dev

How to use the def function to take player input and change a variable

From Dev

How to get variable value outside a function IN jquery

From Dev

How to get variable outside event listener in a JFrame?

From Dev

How to get variable value outside angular function

From Dev

How to get data from the internet for use with matplotlib?

From Dev

How to get updated data from a table after selecting from dropdown using Selenium?

From Dev

How can we use inner variable from function calling in node js at outside?

From Dev

How to set a member variable from outside of class and make use of that varible inside class in C++ ?

From Dev

How to access a FREE variable of a function from outside

From Dev

How to access a CardLayout variable from an outside class?

From Dev

How to set a variable from outside of PHP tags

From Dev

How to access a FREE variable of a function from outside

From Dev

How to get and set private instance variable/ property from outside a class Objective C?

From Dev

How to get and set private instance variable/ property from outside a class Objective C?

Related Related

  1. 1

    variable not updated outside if statement

  2. 2

    How to get updated variable by a python function from another python function?

  3. 3

    How do I use variables of my def function outside?

  4. 4

    How use a variable outside a callback

  5. 5

    How to force Entity Framework to always get updated data from the database?

  6. 6

    How to call php function to get updated data from MYSQL with jQuery

  7. 7

    XSLT - How to get value from for-each loop and use outside it

  8. 8

    How to use a javascript variable in jquery GET 'data: '

  9. 9

    how to bring out data out from def

  10. 10

    how to bring out data out from def

  11. 11

    How to get data from instancetype variable in python

  12. 12

    How to use a variable outside a PARFOR loop in MATLAB?

  13. 13

    swift 2 how to use variable outside the function?

  14. 14

    How to use a variable outside the function in PHP?

  15. 15

    How to use variable outside the task in gradle script

  16. 16

    How to use the def function to take player input and change a variable

  17. 17

    How to get variable value outside a function IN jquery

  18. 18

    How to get variable outside event listener in a JFrame?

  19. 19

    How to get variable value outside angular function

  20. 20

    How to get data from the internet for use with matplotlib?

  21. 21

    How to get updated data from a table after selecting from dropdown using Selenium?

  22. 22

    How can we use inner variable from function calling in node js at outside?

  23. 23

    How to set a member variable from outside of class and make use of that varible inside class in C++ ?

  24. 24

    How to access a FREE variable of a function from outside

  25. 25

    How to access a CardLayout variable from an outside class?

  26. 26

    How to set a variable from outside of PHP tags

  27. 27

    How to access a FREE variable of a function from outside

  28. 28

    How to get and set private instance variable/ property from outside a class Objective C?

  29. 29

    How to get and set private instance variable/ property from outside a class Objective C?

HotTag

Archive