update_logger.cpp
Go to the documentation of this file.
00001 
00027 #include <stomp_moveit/update_filters/update_logger.h>
00028 #include <boost/filesystem.hpp>
00029 #include <ros/console.h>
00030 #include <pluginlib/class_list_macros.h>
00031 #include <ros/package.h>
00032 #include <Eigen/Core>
00033 
00034 PLUGINLIB_EXPORT_CLASS(stomp_moveit::update_filters::UpdateLogger,stomp_moveit::update_filters::StompUpdateFilter);
00035 
00036 namespace stomp_moveit
00037 {
00038 namespace update_filters
00039 {
00040 
00041 UpdateLogger::UpdateLogger():
00042     name_("UpdateLogger")
00043 {
00044 
00045 }
00046 
00047 UpdateLogger::~UpdateLogger()
00048 {
00049   // TODO Auto-generated destructor stub
00050 }
00051 
00052 bool UpdateLogger::initialize(moveit::core::RobotModelConstPtr robot_model_ptr,
00053                         const std::string& group_name,const XmlRpc::XmlRpcValue& config)
00054 {
00055   format_ = Eigen::IOFormat(Eigen::StreamPrecision,0," ","\n");
00056   group_name_ = group_name;
00057   return configure(config);
00058 }
00059 
00060 bool UpdateLogger::configure(const XmlRpc::XmlRpcValue& config)
00061 {
00062   // check parameter presence
00063   auto members = {"filename","directory","package"};
00064   for(auto& m : members)
00065   {
00066     if(!config.hasMember(m))
00067     {
00068       ROS_ERROR("%s failed to find one or more required parameters",getName().c_str());
00069       return false;
00070     }
00071   }
00072 
00073   XmlRpc::XmlRpcValue c = config;
00074   try
00075   {
00076     filename_ = static_cast<std::string>(c["filename"]);
00077     directory_ = static_cast<std::string>(c["directory"]);
00078     package_ = static_cast<std::string>(c["package"]);
00079   }
00080   catch(XmlRpc::XmlRpcException& e)
00081   {
00082     ROS_ERROR("%s failed to find the required parameters",getName().c_str());
00083     return false;
00084   }
00085 
00086   return true;
00087 }
00088 
00089 bool UpdateLogger::setMotionPlanRequest(const planning_scene::PlanningSceneConstPtr& planning_scene,
00090                  const moveit_msgs::MotionPlanRequest &req,
00091                  const stomp_core::StompConfiguration &config,
00092                  moveit_msgs::MoveItErrorCodes& error_code)
00093 {
00094   using namespace boost::filesystem;
00095 
00096   stomp_config_ = config;
00097 
00098   std::string full_dir_name = ros::package::getPath(package_) + "/" + directory_;
00099   full_file_name_ = full_dir_name + "/" + filename_;
00100   path dir_path(full_dir_name);
00101 
00102   if(!boost::filesystem::is_directory(dir_path))
00103   {
00104     // create directory
00105     if(!boost::filesystem::create_directory(dir_path))
00106     {
00107       ROS_ERROR("Unable to create the update logging directory in the path %s",full_dir_name.c_str());
00108       return false;
00109     }
00110   }
00111 
00112   // open file
00113   file_stream_.open(full_file_name_);
00114   if(file_stream_ < 0)
00115   {
00116     ROS_ERROR("Unable to create/open update log file %s",full_file_name_.c_str());
00117     return false;
00118   }
00119 
00120   // clear stream
00121   stream_.str("");
00122 
00123   return true;
00124 }
00125 
00126 bool UpdateLogger::filter(std::size_t start_timestep,std::size_t num_timesteps,int iteration_number,
00127                           const Eigen::MatrixXd& parameters,
00128                           Eigen::MatrixXd& updates,
00129                           bool& filtered)
00130 {
00131 
00132   stream_<<updates.format(format_)<<std::endl;
00133   filtered = false;
00134   return true;
00135 }
00136 
00137 void UpdateLogger::done(bool success, int total_iterations,double final_cost,const Eigen::MatrixXd& parameters)
00138 {
00139   // creating header
00140   std::string header = R"(# num_iterations: @iterations
00141 # num_timesteps: @timesteps
00142 # num_dimensions: @dimensions
00143 # matrix_rows: @rows
00144 # matrix_cols: @cols)";
00145 
00146   // replacing values into header
00147   int rows = stomp_config_.num_dimensions * total_iterations;
00148   int cols = stomp_config_.num_timesteps;
00149   header.replace(header.find("@iterations"),std::string("@iterations").length(),std::to_string(total_iterations));
00150   header.replace(header.find("@timesteps"),std::string("@timesteps").length(),std::to_string(stomp_config_.num_timesteps));
00151   header.replace(header.find("@dimensions"),std::string("@dimensions").length(),std::to_string(stomp_config_.num_dimensions));
00152   header.replace(header.find("@rows"),std::string("@rows").length(),std::to_string(rows));
00153   header.replace(header.find("@cols"),std::string("@cols").length(),std::to_string(cols));
00154 
00155   // writing to file stream
00156   file_stream_<<header<<std::endl;
00157   file_stream_<<stream_.str();
00158   file_stream_.close();
00159 
00160   // clear
00161   stream_.str("");
00162 
00163   ROS_INFO("Saved update log file %s, read with 'numpy.loadtxt(\"%s\")'",full_file_name_.c_str(),filename_.c_str());
00164 }
00165 
00166 } /* namespace smoothers */
00167 } /* namespace stomp_moveit */


stomp_moveit
Author(s): Jorge Nicho
autogenerated on Sat Jun 8 2019 19:24:01