00001
00027 #include <ros/ros.h>
00028
00029 #include <fstream>
00030 #include <boost/algorithm/string.hpp>
00031 #include <boost/algorithm/string/find_iterator.hpp>
00032 #include "sr_remappers/calibration_parser.h"
00033 #include <sstream>
00034
00035 using namespace std;
00036
00037 const std::string CalibrationParser::default_path = "/etc/robot/mappings/default_mapping";
00038
00039 CalibrationParser::CalibrationParser()
00040 {
00041 ROS_WARN("No calibration path was specified, using default path");
00042 init(default_path);
00043 }
00044
00045 CalibrationParser::CalibrationParser( std::string path )
00046 {
00047 init(path);
00048 }
00049
00050 int CalibrationParser::init( std::string path )
00051 {
00052
00053 calibration_matrix.reserve(25);
00054
00055 ifstream calibration_file;
00056 calibration_file.open(path.c_str());
00057
00058
00059 if( !calibration_file.is_open() )
00060 {
00061 ROS_ERROR("Couldn't open the file %s", path.c_str());
00062 return -1;
00063 }
00064
00065
00066 std::vector<std::vector<double> > tmp_matrix;
00067
00068 string line;
00069 while( !calibration_file.eof() )
00070 {
00071 getline(calibration_file, line);
00072
00073
00074 line = boost::algorithm::trim_copy(line);
00075
00076
00077 if( line.size() == 0 )
00078 continue;
00079
00080
00081 if( line[0] == '#' )
00082 continue;
00083
00084 std::vector<std::string> splitted_string;
00085 boost::split(splitted_string, line, boost::is_any_of("\t "));
00086
00087 std::vector<double> double_line(splitted_string.size());
00088 for( unsigned int index_col = 0; index_col < splitted_string.size(); ++index_col )
00089 double_line[index_col] = convertToDouble(splitted_string[index_col]);
00090
00091 calibration_matrix.push_back(double_line);
00092 }
00093 calibration_file.close();
00094
00095 stringstream ss;
00096 ss << "mapping matrix, from glove to hand" << endl;
00097 for( unsigned int line = 0; line < calibration_matrix.size(); ++line )
00098 {
00099 for( unsigned int col = 0; col < calibration_matrix[0].size(); ++col )
00100 {
00101 ss << calibration_matrix[line][col] << " ";
00102 }
00103 ss << endl;
00104 }
00105
00106 ROS_DEBUG("%s",ss.str().c_str());
00107 return 0;
00108 }
00109
00110 std::vector<double> CalibrationParser::get_remapped_vector( std::vector<double> input_vector )
00111 {
00112
00113 if( input_vector.size() != calibration_matrix.size() )
00114 {
00115 ROS_ERROR("The size of the given vector doesn't correspond to the mapping: received %d, wanted %d", input_vector.size(), calibration_matrix.size());
00116 return std::vector<double>(calibration_matrix[0].size());
00117 }
00118
00119 std::vector<double> result(calibration_matrix[0].size());
00120 double tmp_value;
00121
00122 for( unsigned int col = 0; col < calibration_matrix[0].size(); ++col )
00123 {
00124 tmp_value = 0.0;
00125 for( unsigned int index_vec = 0; index_vec < calibration_matrix.size(); ++index_vec )
00126 {
00127 tmp_value += (input_vector[index_vec] * calibration_matrix[index_vec][col]);
00128 }
00129 result[col] = tmp_value;
00130 }
00131
00132 return result;
00133 }