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() 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 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 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 # 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() time.sleep(goal.time_to_wait.to_sec()) result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = 0 server.set_succeeded(result)
def do_timer(goal): # Next, we define do_timer(), # the function that will be invoked # when we receive a new goal. # In this function, we handle the new goal in-place # and set a result before returning. # The type of the "goal" argument that is passed # to do_timer() is TimerGoal, # which corresponds to the goal part of Timer.action. start_time = time.time() # We save the current time, # using the standard Python time.time() function time.sleep(goal.time_to_wait.to_sec() ) # then sleep for the time requested in the goal, # converting the time_to_wait field # from a ROS duration to seconds: result = TimerResult() # The next step is to build up the result message, # which will be of type TimerResult; # corresponds to the result part of Timer.action. result.time_elapsed = rospy.Duration.from_sec( time.time() - start_time) # We fill in the time_elapsed field # by subtracting our saved "start time" from # "current time," and simultaneously # convert the result to a ROS duration. result.updates_sent = 0 # We set updates_sent to zero, # because we didn’t send any updates # along the way (we’ll add that part shortly): server.set_succeeded(result) # Our final step in the callback
def do_timer(goal): # only the TimerGoal gets passed to do_timer start_time = time.time() time.sleep(goal.time_to_wait.to_sec()) result = TimerResult() result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = 8 #random no. server.set_succeeded(result)
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): #goal is of type TimerGoal which corresponds to the goal part of timer.action start_time = time.time() time.sleep(goal.time_to_wait.to_sec()) result = TimerResult() #fill out the time_elapsed field by subtracting start time from currnet time and converting to a ROS duration result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = 0 #do not send any updates server.set_succeeded(result) #tell SimpleActionServer we successfully achieved the goal
def do_timer( goal ): #goal is of type TimerGoal which corresponds to the goal part of timer.action start_time = time.time() time.sleep(goal.time_to_wait.to_sec()) result = TimerResult() #fill out the time_elapsed field by subtracting start time from currnet time and converting to a ROS duration result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) result.updates_sent = 0 #do not send any updates server.set_succeeded( result) #tell SimpleActionServer we successfully achieved the goal
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 time.sleep(goal.time_to_wait.to_sec()) # Wait the time given in the goal # Build the result message result = TimerResult() # Fill in the time_elapsed (ROS duration format) result.time_elapsed = rospy.Duration.from_sec(time.time() - start_time) # The updates functionality is not implemented here result.updates_sent = 0 # Inform that we succeeded and return the result server.set_succeeded(result)
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")