ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
3

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

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

mortenpj gravatar image

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

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 -0500

BennyRe gravatar image

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

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 image mortenpj  ( 2013-10-17 23:44:28 -0500 )edit
2

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

BennyRe gravatar image BennyRe  ( 2013-10-18 06:30:20 -0500 )edit
0

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

jwhendy gravatar image

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

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

Question Tools

1 follower

Stats

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

Seen: 6,344 times

Last updated: Jul 02 '18