00001 #include <nlj_dummy/nj_dummy.h> 00002 00003 NJDummy::NJDummy(const std::string& name, const std::string& get_service_name) : 00004 lama_jockeys::NavigatingJockey(name), 00005 get_service_name_(get_service_name), 00006 mean_traversing_time_(2.0), 00007 max_traversing_delta_(0.5) 00008 { 00009 } 00010 00011 void NJDummy::onStop() 00012 { 00013 ROS_DEBUG("NJDummy STOP"); 00014 00015 result_.final_state = lama_jockeys::NavigateResult::STOPPED; 00016 result_.completion_time = ros::Duration(0); 00017 server_.setSucceeded(result_); 00018 } 00019 00020 void NJDummy::onTraverse() 00021 { 00022 ROS_DEBUG("NJDummy TRAVERSING"); 00023 00024 ros::Time start_time = ros::Time::now(); 00025 double traversing_duration = random_duration(); 00026 nlj_dummy::GetDummyDescriptor dg; 00027 00028 dg.request.id = goal_.descriptor_link.descriptor_id; 00029 ROS_INFO("goal.descriptor: %d", goal_.descriptor_link.descriptor_id); 00030 ros::service::call(get_service_name_, dg); 00031 ROS_INFO("dummy descriptor is %i", dg.response.descriptor.value); 00032 00033 // start navigating. 00034 while (ros::ok()) 00035 { 00036 if (server_.isPreemptRequested() && !ros::ok()) 00037 { 00038 ROS_INFO_STREAM(jockey_name_ << ": Preempted"); 00039 break; 00040 } 00041 // update the feedback every 0.5 s. 00042 ros::Duration(0.5).sleep(); 00043 ROS_INFO("just slept a bit"); 00044 ros::Time current_time = ros::Time::now(); 00045 ros::Duration real_time_elapsed = current_time - start_time; 00046 ros::Duration time_elapsed_for_task; 00047 if (isInterrupted()) 00048 { 00049 // time_elapsed_for_task is frozen. 00050 time_elapsed_for_task = getInterruptionTime() - start_time - getInterruptionsDuration(); 00051 } 00052 else 00053 { 00054 // time_elapsed_for_task runs. 00055 time_elapsed_for_task = real_time_elapsed - getInterruptionsDuration(); 00056 } 00057 feedback_.current_state = lama_jockeys::NavigateFeedback::TRAVERSING; 00058 feedback_.time_elapsed = time_elapsed_for_task; 00059 feedback_.completion = completion(time_elapsed_for_task); 00060 server_.publishFeedback(feedback_); 00061 00062 // Eventually get the result. 00063 if (time_elapsed_for_task.toSec() > traversing_duration) 00064 { 00065 result_.final_state = lama_jockeys::NavigateResult::DONE; 00066 result_.completion_time = real_time_elapsed; 00067 server_.setSucceeded(result_); 00068 break; 00069 } 00070 } 00071 } 00072 00073 double NJDummy::random_duration() 00074 { 00075 const double min = mean_traversing_time_ - max_traversing_delta_; 00076 const double max = mean_traversing_time_ + max_traversing_delta_; 00077 return min + (max - min) * ((double) std::rand()) / RAND_MAX; 00078 } 00079 00080 double NJDummy::completion(ros::Duration time_elapsed) 00081 { 00082 if (time_elapsed.toSec() > mean_traversing_time_) 00083 { 00084 // The maximum completion prediction will be 90 %. 00085 return 0.9; 00086 } 00087 return 0.9 * time_elapsed.toSec() / mean_traversing_time_; 00088 } 00089