00001 #include <nlj_dummy/lj_dummy.h> 00002 00003 LJDummy::LJDummy(const std::string& name, 00004 const std::string& interface_name, 00005 const std::string& set_service_name) : 00006 lama_jockeys::LocalizingJockey(name), 00007 interface_name_(interface_name), 00008 set_service_name_(set_service_name), 00009 mean_localizing_time_(0.1), 00010 max_localizing_delta_(0.03) 00011 { 00012 std::srand(std::time(0)); 00013 } 00014 00015 void LJDummy::onGetVertexDescriptor() 00016 { 00017 ROS_INFO("Received action GET_VERTEX_DESCRIPTOR"); 00018 result_.state = lama_jockeys::LocalizeResult::DONE; 00019 result_.completion_time = ros::Duration(0.001); 00020 server_.setSucceeded(result_); 00021 ROS_INFO("Responded with empty vertex descriptor"); 00022 } 00023 00024 void LJDummy::onGetEdgesDescriptors() 00025 { 00026 ROS_INFO("Received action GET_EDGES_DESCRIPTORS"); 00027 00028 ros::Time start_time = ros::Time::now(); 00029 double localizing_duration = random_duration(); 00030 nlj_dummy::SetDummyDescriptor setter_srv; 00031 00032 // Start the computer intensive localizing (uninterruptable). 00033 double last_feedback_update = 0.0; 00034 while (ros::ok()) 00035 { 00036 ros::Time current_time = ros::Time::now(); 00037 double time_elapsed = current_time.toSec() - start_time.toSec(); 00038 00039 // update the feedback every 0.01 s. 00040 if (time_elapsed - last_feedback_update > 0.01) 00041 { 00042 feedback_.time_elapsed = ros::Duration(time_elapsed); 00043 feedback_.completion = completion(time_elapsed); 00044 server_.publishFeedback(feedback_); 00045 last_feedback_update = time_elapsed; 00046 } 00047 // Eventually get the result. 00048 if (time_elapsed > localizing_duration) 00049 { 00050 // Generate 4 descriptors. 00051 result_.descriptor_links.clear(); 00052 for (int i = 0 ; i < 4 ; i++) 00053 { 00054 setter_srv.request.descriptor.value = random_angle(); 00055 ros::service::call(set_service_name_, setter_srv); 00056 lama_msgs::DescriptorLink descriptor_link; 00057 descriptor_link.object_id = goal_.descriptor_link.object_id; 00058 descriptor_link.descriptor_id = setter_srv.response.id; 00059 descriptor_link.interface_name = interface_name_; 00060 result_.descriptor_links.push_back(descriptor_link); 00061 ROS_INFO("outgoing descriptor_id %i", setter_srv.response.id); 00062 ROS_INFO("outgoing edge %i", setter_srv.request.descriptor.value); 00063 } 00064 00065 result_.state = lama_jockeys::LocalizeResult::DONE; 00066 result_.completion_time = ros::Duration(localizing_duration); 00067 server_.setSucceeded(result_); 00068 break; 00069 } 00070 } 00071 ROS_INFO("Responded with outgoing edges"); 00072 } 00073 00074 void LJDummy::onLocalizeInVertex() 00075 { 00076 ROS_INFO("Received action LOCALIZE_IN_VERTEX"); 00077 result_.state = lama_jockeys::LocalizeResult::DONE; 00078 result_.completion_time = ros::Duration(0.001); 00079 server_.setSucceeded(result_); 00080 ROS_INFO("Responded with localization inside vertex"); 00081 } 00082 00083 void LJDummy::onLocalizeEdge() 00084 { 00085 ROS_INFO("Received action LOCALIZE_EDGE"); 00086 result_.state = lama_jockeys::LocalizeResult::DONE; 00087 result_.completion_time = ros::Duration(0.001); 00088 server_.setSucceeded(result_); 00089 ROS_INFO("Responded with edge localization"); 00090 } 00091 00092 void LJDummy::onGetDissimilarity() 00093 { 00094 ROS_INFO("Received action GET_DISSIMILARITY"); 00095 result_.state = lama_jockeys::LocalizeResult::DONE; 00096 result_.completion_time = ros::Duration(0.001); 00097 server_.setSucceeded(result_); 00098 ROS_INFO("Responded with dissimilarity"); 00099 } 00100 00101 double LJDummy::random_duration() 00102 { 00103 const double min = mean_localizing_time_ - max_localizing_delta_; 00104 const double max = mean_localizing_time_ + max_localizing_delta_; 00105 return min + (max - min) * ((double) std::rand()) / RAND_MAX; 00106 } 00107 00108 /* return an int between 0 and 359 included 00109 */ 00110 int LJDummy::random_angle() 00111 { 00112 return lround(-0.499 + 360 * ((double) std::rand()) / RAND_MAX); 00113 } 00114 00115 double LJDummy::completion(double current_time) 00116 { 00117 if (current_time > mean_localizing_time_) 00118 { 00119 // The maximum completion prediction will be 90 %. 00120 return 0.9; 00121 } 00122 return 0.9 * current_time / mean_localizing_time_; 00123 } 00124