Go to the documentation of this file.00001 #include "robot_instance/CoeffMapLoader.h"
00002
00003 using namespace std;
00004 using namespace log4cpp;
00005
00006
00015 void CoeffMapLoader::loadElements(RobotInstancePtr instance, CoeffMapPtr motorCoeffMap, CoeffMapPtr brainstemCoeffMap)
00016 {
00017 FileMap::const_iterator itr;
00018
00019 if (not motorCoeffMap)
00020 {
00021 motorCoeffMap.reset();
00022 }
00023
00024 motorCoeffMap->clear();
00025
00026 if (not brainstemCoeffMap)
00027 {
00028 brainstemCoeffMap.reset();
00029 }
00030
00031 brainstemCoeffMap->clear();
00032
00034 for (itr = instance->mechanismClassFileMap.begin(); itr != instance->mechanismClassFileMap.end(); ++itr)
00035 {
00036 const std::string& filename = instance->configurationBasePath + RobotInstance::COEFF_BASE_PATH + RobotInstance::COEFF_CLASS_PATH + (*itr).second;
00037
00038 try
00039 {
00040 Private::loadElementsFromFile(filename, (*itr).first, motorCoeffMap, brainstemCoeffMap);
00041 }
00042 catch (std::exception& e)
00043 {
00044 NasaCommonLogging::Logger::log("gov.nasa.configuration.CoeffMapLoader", log4cpp::Priority::NOTICE, "Skipping non-safety coeff file [" + filename + "]");
00045 }
00046 }
00047
00049 for (itr = instance->mechanismJointFileMap.begin(); itr != instance->mechanismJointFileMap.end(); ++itr)
00050 {
00051 const std::string& filename = instance->configurationBasePath + RobotInstance::COEFF_BASE_PATH + RobotInstance::COEFF_JOINT_PATH + instance->robotInstance + "/" + (*itr).second;
00052
00053 try
00054 {
00055 Private::loadElementsFromFile(filename, (*itr).first, motorCoeffMap, brainstemCoeffMap);
00056 }
00057 catch (std::exception& e)
00058 {
00059 NasaCommonLogging::Logger::log("gov.nasa.configuration.CoeffMapLoader", log4cpp::Priority::NOTICE, "Skipping non-safety coeff file [" + filename + "]");
00060 }
00061 }
00062
00064 for (itr = instance->mechanismClassSafetyFileMap.begin(); itr != instance->mechanismClassSafetyFileMap.end(); ++itr)
00065 {
00066 try
00067 {
00068 Private::loadElementsFromFile(instance->configurationSafetyBasePath + RobotInstance::COEFF_BASE_PATH + RobotInstance::COEFF_CLASS_SAFETY_PATH + (*itr).second, (*itr).first, motorCoeffMap, brainstemCoeffMap);
00069 }
00070 catch (std::exception& e)
00071 {
00072 NasaCommonLogging::Logger::log("gov.nasa.configuration.CoeffMapLoader", log4cpp::Priority::FATAL, e.what());
00073 throw runtime_error(e.what());
00074 }
00075 }
00076
00078 for (itr = instance->mechanismJointSafetyFileMap.begin(); itr != instance->mechanismJointSafetyFileMap.end(); ++itr)
00079 {
00080 try
00081 {
00082 Private::loadElementsFromFile(instance->configurationSafetyBasePath + RobotInstance::COEFF_BASE_PATH + RobotInstance::COEFF_JOINT_SAFETY_PATH + instance->robotInstance + "/" + (*itr).second, (*itr).first, motorCoeffMap, brainstemCoeffMap);
00083 }
00084 catch (std::exception& e)
00085 {
00086 NasaCommonLogging::Logger::log("gov.nasa.configuration.CoeffMapLoader", log4cpp::Priority::FATAL, e.what());
00087 throw runtime_error(e.what());
00088 }
00089 }
00090 }
00091
00092
00101 void CoeffMapLoader::Private::loadElementsFromFile(const string& filename, const string& namePrefix, CoeffMapPtr motorCoeffMap, CoeffMapPtr brainstemCoeffMap)
00102 {
00103 TiXmlDocument file(filename.c_str());
00104 bool loadOkay = file.LoadFile();
00105
00106 if (!loadOkay)
00107 {
00108 stringstream err;
00109 err << "Failed to load file [" << filename << "]";
00110
00111 throw runtime_error(err.str());
00112 }
00113
00114 TiXmlHandle doc(&file);
00115 NasaCommonLogging::Logger::log("gov.nasa.configuration.CoeffMapLoader", Priority::INFO, "CoeffFile [" + filename + "] successfully loaded.");
00116
00117 string elementString;
00118
00119
00120 if (!(doc.FirstChildElement("CoeffData").ToElement()))
00121 {
00122 stringstream err;
00123 err << "The file " << filename << " has no element named [CoeffData]";
00124
00125 throw runtime_error(err.str());
00126 }
00127
00128 TiXmlHandle currentCoeffElement = 0;
00129
00130
00131 TiXmlHandle motorCoeffsElement(doc.FirstChildElement("CoeffData").FirstChildElement("MotorCoeffs"));
00132
00133 if (motorCoeffsElement.ToElement())
00134 {
00135
00136 currentCoeffElement = motorCoeffsElement.FirstChild("Coeff");
00137
00138 while (currentCoeffElement.ToElement())
00139 {
00140 elementString.clear();
00141 elementString << *(currentCoeffElement.ToElement());
00142 elementString.append("\n");
00143
00144 CoeffMap::value_type newCoeffMapMember(CoeffFactory::fromXml(elementString, namePrefix));
00145
00146 if (motorCoeffMap->find(newCoeffMapMember.first) == motorCoeffMap->end())
00147 {
00148 motorCoeffMap->insert(newCoeffMapMember);
00149 }
00150 else
00151 {
00152 NasaCommonLogging::Logger::log("gov.nasa.configuration.CoeffMapLoader", Priority::WARN, "motorCoeffMap already contains element with key = " + newCoeffMapMember.first);
00153 }
00154
00155 currentCoeffElement = currentCoeffElement.ToElement()->NextSiblingElement("Coeff");
00156 }
00157 }
00158
00159
00160 TiXmlHandle brainstemCoeffsElement(doc.FirstChildElement("CoeffData").FirstChildElement("BrainstemCoeffs"));
00161
00162 if (brainstemCoeffsElement.ToElement())
00163 {
00164
00165 currentCoeffElement = brainstemCoeffsElement.FirstChild("Coeff");
00166
00167 while (currentCoeffElement.ToElement())
00168 {
00169 elementString.clear();
00170 elementString << *(currentCoeffElement.ToElement());
00171 elementString.append("\n");
00172
00173 CoeffMap::value_type newCoeffMapMember(CoeffFactory::fromXml(elementString, namePrefix));
00174
00175 if (brainstemCoeffMap->find(newCoeffMapMember.first) == brainstemCoeffMap->end())
00176 {
00177 brainstemCoeffMap->insert(newCoeffMapMember);
00178 }
00179 else
00180 {
00181 NasaCommonLogging::Logger::log("gov.nasa.configuration.CoeffMapLoader", Priority::WARN, "brainstemCoeffMap already contains element with key = " + newCoeffMapMember.first);
00182 }
00183
00184 currentCoeffElement = currentCoeffElement.ToElement()->NextSiblingElement("Coeff");
00185 }
00186 }
00187 }