ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
In my opinion, it is better to implement timers with threads. In your case I would have a class that implements a function that simply waits some time and that can be stopped. If it arrives to the timeout, it turns your variable to False. If you receive a new string, you stop the thread and start it again.
Something like this
@staticmethod
def watch_until_string_a(max_time_stopped, stop):
old_time = rospy.Time.now().to_sec()
diff_time = 0
while diff_time < max_time_stopped and not stop():
time.sleep(0.2)
diff_time = rospy.Time.now().to_sec() - old_time
if not stop():
self.a_current = False
then you can start a timer just by doing
th = threading.Thread(target=self.watch_until, args=(time_before, lambda: stop[0]))
th.start()
with stop being a list of a single boolean
stop = [False]
If you want to stop your thread, you just have to change the value of stop to
stop = [True]
and you thread will stop without changing the value of the variable. Then just wait for them to finish
th.join()
and you can launch it again.
The code above is for a single string, but you can easily modify it to be used with the two strings (remember that you will need one thread per string). I hope it helps.