Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

At the very minimum, you should make x a global variable. For example, at the top of your program

x = None

to bring it into the global scope and in your function

def callback(msg):
    global x
    x = msg.pose.position.x

Now, this is ok for small, toy programs but you will quickly run into problems with anything more significant. Therefore, should look into how classes work in Python or into event-driven programming.

At the very minimum, you should make x a global variable. For example, at the top of your program

x = None

to bring it into the global scope and in your function

def callback(msg):
    global x
    x = msg.pose.position.x

Now, this using globals is ok for small, toy programs but you will quickly run into problems with anything more significant. Therefore, should look into how classes work in Python or into event-driven programming.

At the very minimum, you should make x a global variable. For example, at the top of your program

x = None

to bring it into the global scope and in your function

def callback(msg):
    global x
    x = msg.pose.position.x

Now, using globals is ok for small, toy programs but you will quickly run into problems with anything more significant. Therefore, should look into how classes work in Python or into event-driven programming.


Edit:

Part of the issue that you're having is that rospy.spin() just blocks until the node is shutdown and the other part is you're only printing once. If you want to print more than once, either put the print inside your callback or switch to a loop in your main.

At the very minimum, you should make x a global variable. For example, at the top of your program

x = None

to bring it into the global scope and in your function

def callback(msg):
    global x
    x = msg.pose.position.x

Now, using globals is ok for small, toy programs but you will quickly run into problems with anything more significant. Therefore, should look into how classes work in Python or into event-driven programming.


Edit:

Part of the issue that you're having is that rospy.spin() just blocks until the node is shutdown and the other part is you're only printing once. If you want to print more than once, either put the print inside your callback or switch to a loop in your main.