python ros callback with if-condition timer
I have a call back set up to receive a string. that string can be 2 different things. Im trying to figure out how i can set up a timer that gets set each time each individual string possibility is recieved. and if that specific string has not been received for more than X seconds, set some flag for current status to false. What i have does not seem to work sense the timer wont persist through the if statement. however if i declare it as timer_a = None
before hand , it breaks the timeout calculation.
def string_cb(string):
if string == 'A':
timer_a = rospy.Time.now()
if string == 'B':
timer_b = rospy.Time.now()
timeout_a = (rospy.Time.now() - timer_a).to_sec()
timeout_b = (rospy.Time.now() - timer_b).to_sec()
if timeout_a > 5.0:
A_current = false
if timeout_b > 5.0:
B_current = false
you can make timers global, or make your node class based and add them as class attributes