Ask Your Question
2

rospy subscribing to topic, how can i save a variable?

asked 2013-10-17 21:37:15 -0600

mortenpj gravatar image

updated 2013-10-17 23:35:04 -0600

Hi

I'm fairly new to programming in python and ROS, but my problem is as follows: I am using a robot gripper, and for control purposes i need to read the status of the gripper and save it to a variable, so I can use if in control sequences. I am getting the status of the gripper by subscribing to a topic.

import roslib; roslib.load_manifest('robotiq_s_model_control')
import rospy
from robotiq_s_model_control.msg import _SModel_robot_input  as inputMsg
from robotiq_s_model_control.msg import _SModel_robot_output  as outputMsg

def Callback(data):
    myvar = data

rospy.init_node('rq3_proxy')

while not rospy.is_shutdown():
    rospy.Subscriber("SModelRobotInput", inputMsg.SModel_robot_input, Callback)
    rospy.sleep(1)

    print "myvar is: " + myvar

    if myvar == 1:
         #do something"
    elif myvar == 0:
         #do something else
    else :
         #error

So my question is: how can I make use of the myvar variable outside the Callback function?

Thanks in advance for any help you can provide, and if you need additional info, just ask for it.

/Morten

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2013-10-17 21:55:11 -0600

BennyRe gravatar image

updated 2013-10-17 23:41:09 -0600

import roslib; roslib.load_manifest('robotiq_s_model_control')
import rospy
from robotiq_s_model_control.msg import _SModel_robot_input  as inputMsg
from robotiq_s_model_control.msg import _SModel_robot_output  as outputMsg

myvar = None

def Callback(data):
    global myvar
    myvar = data

rospy.init_node('rq3_proxy')

while not rospy.is_shutdown():
    rospy.Subscriber("SModelRobotInput", inputMsg.SModel_robot_input, Callback)
    rospy.sleep(1)

   print "myvar is: " + myvar

   if myvar == 1:
         #do something"
    elif myvar == 0:
         #do something else
    else :
         #error
edit flag offensive delete link more

Comments

Thanks! It's is exactly what I needed! Sorry for my poor python skills! :)

mortenpj gravatar imagemortenpj ( 2013-10-17 23:44:28 -0600 )edit
2

You're welcome. If it works, mark the answer as correct, please.

BennyRe gravatar imageBennyRe ( 2013-10-18 06:30:20 -0600 )edit
0

answered 2018-07-02 16:25:47 -0600

jwhendy gravatar image

updated 2018-07-02 16:26:20 -0600

Super late, and this is more of a general python question, but you can avoid global variables by just passing things around...

def do_something(myvar):
   print "myvar is: " + myvar
   if myvar == 1:
         #do something"
    elif myvar == 0:
         #do something else
    else :
         #error    

def Callback(data):
    myvar = data
    do_something(myvar)

rospy.init_node('rq3_proxy')

while not rospy.is_shutdown():
    rospy.Subscriber("SModelRobotInput", inputMsg.SModel_robot_input, Callback)
    rospy.sleep(1)
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2013-10-17 21:37:15 -0600

Seen: 3,535 times

Last updated: Jul 02 '18