def do_timer(goal): start_time = time.time() updates_count = 0 if goal.time_to_wait.to_sec() > 60: result = TimerResult() result.updates_sent = updates_count result.time_elapsed = \ rospy.Duration.from_sec(time.time() - start_time) server.set_aborted(result, "Goal Time is too high") return while time.time() <= start_time + goal.time_to_wait.to_sec(): if server.is_preempt_requested(): result = TimerResult() result.time_elapsed = \ rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = updates_count server.set_preempted(result, "Timer Preempted") return feedback = TimerFeedback() feedback.time_elapsed = \ rospy.Duration.from_sec(time.time() - start_time) feedback.time_remaining = (goal.time_to_wait - feedback.time_elapsed) server.publish_feedback(feedback) updates_count += 1 time.sleep(1.0) result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = updates_count server.set_succeeded(result, "Timer completed successfully")
def do_timer(goal): start_time = time.time() update_count = 0 if goal.time_to_wait.to_sec() > 60.0: result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_aborted(result, "Timer aborted due to too-long wait") return while (time.time() - start_time) < goal.time_to_wait.to_sec(): if server.is_preempt_requested(): result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_preempted(result, "Timer preempted") return feedback = TimerFeedback() feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) feedback.time_remaining = goal.time_to_wait - feedback.time_elapsed server.publish_feedback(feedback) update_count += 1 time.sleep(1.0) result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_succeeded(result, "Timer completed successfully")
def do_timer(goal): start_time = time.time() update_count = 0 if goal.time_to_wait.to_sec() > 60.0: result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_aborted(result, "Timer aborted due to too-long wait") return while (time.time() - start_time) < goal.time_to_wait.to_sec(): if server.is_preempt_requested(): result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_preempted(result, "Timer preempted") return feedback = TimerFeedback() feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) feedback.time_remaining = goal.time_to_wait - feedback.time_elapsed server.publish_feedback(feedback) update_count += 1 time.sleep(0.5) result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_succeeded(result, "Timer completed successfully")
def do_timer(goal): start_time = time.time() update_count = 0 while (time.time() - start_time) < goal.time_to_wait.to_sec(): if goal.time_to_wait.to_sec() > 60.0: result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_aborted(result, "Timer aborted due to too-log wait") return feedback = TimerFeedback() feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) server.publish_feedback(feedback) update_count += 1 time.sleep(1.0) result - TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_succeeded(result, "Timer completed successfully")
def do_timer(goal): start_time = time.time() update_count = 0 # Stepping inside our do_timer() callback, # we add a variable (update_count) # that will keep track of # how many times we publish feedback: if goal.time_to_wait.to_sec() > 60.0: # result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_aborted(result, "Timer aborted due to too-long wait") return """ Next, we add some error checking. We don’t want this timer to be used for long waits, so we check whether the requested time_to_wait is greater than 60 seconds, and if so, we explicitly abort the goal by calling set_aborted(). This call sends a message to the client notifying it that the goal has been aborted. Like with set_succeeded(), we include a result; doing this is optional, but a good idea if possible. We also include a status string to help the client understand what happened; in this case, we aborted because the requested wait was too long. Finally, we return from the callback because we’re done with this goal: """ while (time.time() - start_time) < goal.time_to_wait.to_sec(): if server.is_preempt_requested(): result = TimerResult() result.time_elapsed = \ rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_preempted(result, "Timer preempted") return feedback = TimerFeedback() feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) feedback.time_remaining = goal.time_to_wait - feedback.time_elapsed server.publish_feedback(feedback) update_count += 1 time.sleep(1.0) result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_succeeded(result, "Timer completed successfully")
def do_timer(goal): start_time = time.time() # BEGIN PART_2 update_count = 0 # END PART_2 # BEGIN PART_3 if goal.time_to_wait.to_sec() > 60.0: result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_aborted(result, "Timer aborted due to too-long wait") return # END PART_3 # BEGIN PART_4 while (time.time() - start_time) < goal.time_to_wait.to_sec(): # END PART_4 # BEGIN PART_5 if server.is_preempt_requested(): result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_preempted(result, "Timer preempted") return # END PART_5 # BEGIN PART_6 feedback = TimerFeedback() feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) feedback.time_remaining = goal.time_to_wait - feedback.time_elapsed server.publish_feedback(feedback) update_count += 1 # END PART_6 # BEGIN PART_7 time.sleep(1.0) # END PART_7 # BEGIN PART_8 result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_succeeded(result, "Timer completed successfully")
def do_timer(goal): start_time = time.time() update_count = 0 ## GOAL ABORT if goal.time_to_wait.to_sec() > 60: result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_aborted(result, "ABORTED BECAUSE TIME TOO LONG") return while (time.time() - start_time) < goal.time_to_wait.to_sec(): ## GOAL PREEMPT if server.is_preempt_requested(): result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_preempted(result, "TIME PREEMPTED BY CLIENT") return ## FEEDBACK Feedback = TimerFeedback() Feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) Feedback.time_remaining = goal.time_to_wait - Feedback.time_elapsed server.publish_feedback(Feedback) update_count = update_count + 1 rospy.sleep(1.0) ## GOAL SUCCESS result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_succeeded(result, 'TIMER COMPLETED')
def do_timer(goal): start_time = time.time() # Save current time update_count = 0 # Initialize update counter # The maximum time is 60 seconds. Abort if given goal is greater. if goal.time_to_wait.to_sec() > 60.0: result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_aborted(result, "Timer aborted due to too long wait") return # Go over the goal time in 1.0 s increments, testing for preemption and # sending updates in between pauses while (time.time() - start_time) < goal.time_to_wait.to_sec(): # Check for preemption and abort if true if server.is_preempt_requested(): result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_preempted(result, "Timer preempted") return # Send feedback feedback = TimerFeedback() feedback.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) feedback.time_remaining = goal.time_to_wait - feedback.time_elapsed server.publish_feedback(feedback) update_count += 1 # Increment update counter # Sleep for 1.0 sec time.sleep(1.0) # If the loop is concluded, the timer succeeded. Send the final result. result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = update_count server.set_succeeded(result, "Timer completed successfully")