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

Subscriber logic

asked 2018-02-05 17:42:30 -0500

julimen5 gravatar image

Hi guys,

I'm stuck here now,

I have this common listener with a subscriber:

def listener():
    rospy.init_node('teleop_list', anonymous=True)

    rospy.Subscriber('key_vel', Twist, callback)

    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

I want to know if there is a way to call the callback when no data is received (I already know the callback theory, when something happens the callback is triggered, and this is why I'm asking) or find a way to do something while the callback is not triggered

Ty

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-02-05 18:35:58 -0500

kparikh gravatar image

updated 2018-02-05 20:55:00 -0500

You can make a new thread in your Python program and have it call a function called callback that doesn't take inputs.

def callback():
    while True:
        crunchData()

# ... somewhere in the daemon startup code ...
t = threading.Thread(target=callback)
t.daemon = True
t.start()

https://stackoverflow.com/questions/1...

I copied over the sample example you might want to reference for what you want to do.

edit flag offensive delete link more

Comments

I can't imagine how can I connect what you are saing with the rospy.Subscriber. I have this:

def callback(data):
    #do something with the data

def listener():
    rospy.Subscriber('key_vel', Twist, callback)

listener is the daemon startup code.

julimen5 gravatar image julimen5  ( 2018-02-05 19:06:01 -0500 )edit

I'm telling you how to do something when the subscriber isn't triggered. Plus, you can't call callback(data) without providing data. You can either make another function which doesn't require the data or provide a predefined piece of data.

kparikh gravatar image kparikh  ( 2018-02-05 19:11:16 -0500 )edit

yeah, after think a bit and read the whole stackoverflow post I think i get it. Thank you!

julimen5 gravatar image julimen5  ( 2018-02-05 19:12:55 -0500 )edit

If you want to connect it, then try.

rospy.init_node('teleop_list', anonymous=True)
rospy.Subscriber('key_vel', Twist, callback)
def listener():
    #some logic to call a function or spin

t = threading.Thread(target=listener)
t.daemon = True
t.start()
kparikh gravatar image kparikh  ( 2018-02-05 19:14:59 -0500 )edit

Mark as answered and close if it works.

kparikh gravatar image kparikh  ( 2018-02-05 19:16:06 -0500 )edit

the thread is called the same time with the Subscriber

def callback(data):
       #
def listener():
    #do smt

def main ():
    rospy.init_node('teleop_list', anonymous=True)
    rospy.Subscriber('key_vel', Twist, callback)

    t = threading.Thread(target=listener)
julimen5 gravatar image julimen5  ( 2018-02-05 20:05:49 -0500 )edit

I got it, with some mods but thank you!!!

julimen5 gravatar image julimen5  ( 2018-02-05 20:51:30 -0500 )edit

Yeah, not sure it would do what you want otherwise.

kparikh gravatar image kparikh  ( 2018-02-05 20:52:54 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2018-02-05 17:42:30 -0500

Seen: 360 times

Last updated: Feb 05 '18