0
$\begingroup$

Rosanswers logo

Hello !

I want to have a value to be returned when I call a callback function while subscribing to a topic. I am using ROS-Hydro I have wrote a demo code that what I want. Please also suggest some new way to achieve this if callback function can't return anything !

def bridge_opencv():
    image_pub = rospy.Publisher("quadrotor/videocamera1/camera_info",Image)
    cv2.namedWindow("Image window", 1)
    image_sub = rospy.Subscriber("quadrotor/videocamera1/image",Image, callback)
    get value of y from callback function when it is returning 
    y = z
    return z            
def callback(data):
    bridge = CvBridge()
    try:
        cv_image = bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError, e:
        print e
    (rows,cols,channels) = cv_image.shape
    if cols > 60 and rows > 60 :
                     cv2.circle(cv_image, (50,50), 10, 255)
           cv2.imshow("Image window", cv_image)
           return y
def my_function()
           x=cv_bridge()
           if x = 1: 
               return "true"
           else:
                perform the task
               
def pilot():
rospy.init_node("pilot")
    my_value = my_function()
    if my_value = true:
               stop everything...
    else: do the above process again
     rospy.spin()
if __name__ == '__main__':
    pilot()

Thanks a lot ! Looking forward for answers !


Originally posted by jashanvir on ROS Answers with karma: 68 on 2014-06-22

Post score: 4


Original comments

Comment by Tvlad on 2016-10-24:
Hello. I have the same issue. Do you have solutions? Please help.

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

Hi jashanvir, one common way to do this is to use define a class and make the callback function a class member : http://wiki.ros.org/roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks. In the body of the callback function, you can modify a class attribute, and then get the value in your main() using a get method.

Edit : I don't think the tutorial is available in Python, but you can have a look at how the callback function is implemented here : http://wiki.ros.org/rospy_tutorials/Tutorials/WritingImagePublisherSubscriber


Originally posted by al-dev with karma: 883 on 2014-06-22

This answer was ACCEPTED on the original site

Post score: 8


Original comments

Comment by jashanvir on 2014-06-23:
Thanks a lot for your time al-dev ! I want to ask if that tutorial is available in python ?

Comment by beluga on 2019-01-10:
I found this one: https://stackoverflow.com/questions/37373211/update-the-global-variable-in-rospy

$\endgroup$
0
$\begingroup$

Rosanswers logo

If I understood you well, you want your callback to process some data and then deliver some result that will be used by other functions. What I do in that case is that instead of return y I publish y to a topic making it accessible to other functions: callback_pub.publish(y) where callback_pub is declared as

callback_pub = rospy.Publisher("callback_y",std_msgs/int32)

As a concrete example you can imagine an image_callback detecting faces and publishing faces positions.

Another easy way to do that is to just set y as a global variable and make the callback function change its value whenever it is called. I don't know how python internally works but you should maybe consider using mutex to avoid Write/Read on y on the same time.

Finally and as al-dev suggested, using a class but you should bind it as a callback using boost. I don't know if there is some tutorial for that in python.


Originally posted by Mehdi. with karma: 3339 on 2014-06-22

This answer was NOT ACCEPTED on the original site

Post score: 5


Original comments

Comment by hpurohit on 2017-05-18:
Thanks for this ! works for me

Comment by Mikku on 2017-11-23:
I am getting error that stds_msgs not defined.

Comment by Ibrahim101 on 2017-11-28:
try adding: import sensor_msgs.msg

$\endgroup$

This site is temporarily in read-only mode and not accepting new answers.