@@ -498,19 +498,76 @@ def get_duration(self):
498
498
return self .end - self .start
499
499
500
500
class Watchdog (object ):
501
- def __init__ (self , timeout_duration , handler ):
501
+ '''
502
+ Calls specified function after duration, unless reset/stopped beforehand
503
+
504
+ @param timeout_duration how long to wait before calling handler
505
+ @param handler function to call after timeout_duration
506
+ @param args for handler
507
+ @param kwargs for handler
508
+ '''
509
+ def __init__ (self , timeout_duration , handler , args = (), kwargs = {}):
502
510
self .timeout_duration = timeout_duration
503
511
self .handler = handler
504
- self .timer = threading .Timer (self .timeout_duration , self .handler )
505
- self .timer .start ()
512
+ self .handler_args = args
513
+ self .handler_kwargs = kwargs
514
+
515
+ self .thread_checking_time = threading .Thread (target = self ._check_timer_loop )
516
+ self .timer_thread_lock = threading .Lock ()
517
+ self .start_time = time .time ()
518
+ self .canceled = False
519
+
520
+ self .thread_checking_time .start ()
521
+ #self.timer = threading.Timer(self.timeout_duration, self.handler)
522
+ #self.timer.start()
506
523
507
524
def reset (self ):
508
- self .timer .cancel ()
509
- self .timer = threading .Timer (self .timeout_duration , self .handler )
510
- self .timer .start ()
525
+ '''
526
+ Resets the timer
527
+
528
+ Causes the handler function to be called after the next
529
+ timeout duration is reached
530
+
531
+ Also restarts the timer thread if it has existed
532
+ '''
533
+ with self .timer_thread_lock :
534
+ self .start_time = time .time ()
535
+ self .canceled = False
536
+ if not self .thread_checking_time .is_alive ():
537
+ self .thread_checking_time = threading .Thread (target = self ._check_timer_loop )
538
+ self .thread_checking_time .start ()
539
+
540
+ #self.timer.cancel()
541
+ #self.timer = threading.Timer(self.timeout_duration, self.handler)
542
+ #self.timer.start()
511
543
512
544
def stop (self ):
513
- self .timer .cancel ()
545
+ '''
546
+ Stop the watchdog, so it will not call handler
547
+ '''
548
+ with self .timer_thread_lock :
549
+ self .canceled = True
550
+
551
+ def _check_timer_loop (self ):
552
+ '''
553
+ Internal function for timer thread to loop
554
+
555
+ If elapsed time has passed, calls the handler function
556
+ Exists if watchdog was canceled, or handler was called
557
+ '''
558
+ while True :
559
+ with self .timer_thread_lock :
560
+ if self .canceled :
561
+ break
562
+ elapsed_time = time .time () - self .start_time
563
+ if elapsed_time > self .timeout_duration :
564
+ self .handler (* self .handler_args , ** self .handler_kwargs )
565
+ self .canceled = True
566
+ break
567
+ else :
568
+ time .sleep (self .timeout_duration - elapsed_time )
569
+
570
+
514
571
515
572
516
573
def quadraticPlusJointLimitObjective (dq , J , dx , q , q_min , q_max , delta_joint_penalty = 5e-1 , lambda_dqdist = 0.01 , * args ):
0 commit comments