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

Revision history [back]

click to hide/show revision 1
initial version

Funny, I did look around a lot but i didn't really know what to look for. After typing this question, i was about to continue to another part of the project as i came across this link It suggested to 'dump' the stored images by letting the callback " temporarily do nothing". So i implemented this to my code and it did solve my problem. The updated subscriber and publisher are below.

subscriber:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def callback(data):
    if rospy.get_param("/do_callback_test") == True:
        rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
        rospy.sleep(2)
    else:
        pass

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

    rospy.Subscriber("chatter", String, callback)
    print("done with callback")
    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()

The output of the subscriber gave the follwoing:

[INFO] [1557910478.891510]: /listener_18047_1557910462361I heard hello world 1557910472.84
[INFO] [1557910480.897341]: /listener_18047_1557910462361I heard hello world 1557910473.84
[INFO] [1557910482.902606]: /listener_18047_1557910462361I heard hello world 15579104**74**.84
[INFO] [1557910498.843681]: /listener_18047_1557910462361I heard hello world 15579104**98**.84
[INFO] [1557910500.852232]: /listener_18047_1557910462361I heard hello world 1557910499.84

After the 4th print i set the parameter "/do_callback" to false, which made the subscriber just "pass" the callback. Then, when i made the parameter high again after 24 seconds, the subscriber printed out the information again, using the newest received data!

Funny, I did look around a lot but i didn't really know what to look for. After typing this question, i was about to continue to another part of the project as i came across this link It suggested to 'dump' the stored images by letting the callback " temporarily do nothing". So i implemented this to my code and it did solve my problem. The updated subscriber and publisher are below.

subscriber:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def callback(data):
    if rospy.get_param("/do_callback_test") == True:
        rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
        rospy.sleep(2)
    else:
        pass

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

    rospy.Subscriber("chatter", String, callback)
    print("done with callback")
    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()

The output of the subscriber gave the follwoing:

[INFO] [1557910478.891510]: /listener_18047_1557910462361I heard hello world 1557910472.84
[INFO] [1557910480.897341]: /listener_18047_1557910462361I heard hello world 1557910473.84
[INFO] [1557910482.902606]: /listener_18047_1557910462361I heard hello world 15579104**74**.84
1557910474.84
[INFO] [1557910498.843681]: /listener_18047_1557910462361I heard hello world 15579104**98**.84
1557910498.84
[INFO] [1557910500.852232]: /listener_18047_1557910462361I heard hello world 1557910499.84

After the 4th 3rd print i set the parameter "/do_callback" to false, which made the subscriber just "pass" the callback. Then, when i made the parameter high again after 24 seconds, the subscriber printed out the information again, using the newest received data!

Funny, I did look around a lot but i didn't really know what to look for. After typing this question, i was about to continue to another part of the project as i came across this link It suggested to 'dump' the stored images by letting the callback " temporarily do nothing". So i implemented this to my code and it did solve my problem. The updated subscriber and publisher are below.

subscriber:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String

def callback(data):
    if rospy.get_param("/do_callback_test") == True:
        rospy.loginfo(rospy.get_caller_id() + "I heard %s", data.data)
        rospy.sleep(2)
    else:
        pass

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

    rospy.Subscriber("chatter", String, callback)
    print("done with callback")
    # spin() simply keeps python from exiting until this node is stopped
    rospy.spin()

if __name__ == '__main__':
    listener()

The output of the subscriber gave the follwoing:following:

[INFO] [1557910478.891510]: /listener_18047_1557910462361I heard hello world 1557910472.84
[INFO] [1557910480.897341]: /listener_18047_1557910462361I heard hello world 1557910473.84
[INFO] [1557910482.902606]: /listener_18047_1557910462361I heard hello world 1557910474.84
[INFO] [1557910498.843681]: /listener_18047_1557910462361I heard hello world 1557910498.84
[INFO] [1557910500.852232]: /listener_18047_1557910462361I heard hello world 1557910499.84

After the 3rd print i set the parameter "/do_callback" to false, which made the subscriber just "pass" the callback. Then, when i made the parameter high again after 24 seconds, the subscriber printed out the information again, using the newest received data!