00001 #include "robodyn_mechanisms/JointControlManagerFactory.h"
00002
00014 JointControlManagerPtr JointControlManagerFactory::generate(const std::string& mechanism, const JointControlCommonInterface::IoFunctions& io, double timeLimit, NodeRegisterManagerPtr nodeRegisterManager)
00015 {
00016 JointControlManagerPtr newManager;
00017 std::string registerFile;
00018 std::string jointType;
00019
00020 registerFile = io.getRegisterFile(mechanism);
00021
00022 if (registerFile.empty())
00023 {
00024 NasaCommonLogging::Logger::log("gov.nasa.robonet.JointControlManagerFactory", log4cpp::Priority::ERROR, "No control file specified for mechanism " + mechanism);
00025 newManager.reset();
00026 return newManager;
00027 }
00028
00029 jointType = Private::getPropertyFromFile(registerFile, "NodeType");
00030
00031
00032 if (jointType == "SeriesElastic")
00033 {
00034 nodeRegisterManager->addNode(mechanism, registerFile);
00035 newManager = boost::make_shared<JointControlManagerSeriesElastic>(mechanism, io, timeLimit, jointType, nodeRegisterManager);
00036 }
00037 else if (jointType == "Rigid")
00038 {
00039 nodeRegisterManager->addNode(mechanism, registerFile);
00041 newManager = boost::make_shared<JointControlManagerSeriesElastic>(mechanism, io, timeLimit, jointType, nodeRegisterManager);
00042 }
00043 else if (jointType == "Gripper")
00044 {
00045 nodeRegisterManager->addNode(mechanism, registerFile);
00046 newManager = boost::make_shared<JointControlManagerGripper>(mechanism, io, timeLimit, jointType, nodeRegisterManager);
00047 }
00048
00049
00050 else if (jointType == "Wrist")
00051 {
00052 newManager = boost::make_shared<JointControlManagerWrist>(mechanism, io, timeLimit, jointType);
00053 }
00054 else if (jointType == "Thumb")
00055 {
00056 newManager = boost::make_shared<JointControlManagerFinger>(mechanism, io, timeLimit, jointType);
00057 }
00058 else if (jointType == "PrimaryFinger")
00059 {
00060 newManager = boost::make_shared<JointControlManagerFinger>(mechanism, io, timeLimit, jointType);
00061 }
00062 else if (jointType == "SecondaryFingers")
00063 {
00064 newManager = boost::make_shared<JointControlManagerFinger>(mechanism, io, timeLimit, jointType);
00065 }
00066 else
00067 {
00068 std::stringstream err;
00069 err << "Unsupported joint type [" << jointType << "] found in ControlFile";
00070 NasaCommonLogging::Logger::log("gov.nasa.robonet.JointControlManagerFactory", log4cpp::Priority::ERROR, err.str());
00071 throw std::runtime_error(err.str());
00072 }
00073
00074 return newManager;
00075 }
00076
00085 std::string JointControlManagerFactory::Private::getPropertyFromFile(const std::string& filename, const std::string& property)
00086 {
00087 TiXmlDocument file(filename.c_str());
00088 bool loadOkay = file.LoadFile();
00089
00090 if (!loadOkay)
00091 {
00092 std::stringstream err;
00093 err << "Failed to load file [" << filename << "]";
00094 NasaCommonLogging::Logger::log("gov.nasa.robonet.JointControlManagerFactory", log4cpp::Priority::FATAL, err.str());
00095 throw std::runtime_error(err.str());
00096 }
00097
00098 TiXmlHandle doc(&file);
00099 NasaCommonLogging::Logger::log("gov.nasa.robonet.JointControlManagerFactory", log4cpp::Priority::INFO, "Register file [" + filename + "] successfully loaded.");
00100 NasaCommonLogging::Logger::log("gov.nasa.robonet.JointControlManagerFactory", log4cpp::Priority::INFO, "XML being parsed for [RegisterData]");
00101
00102
00103 if (!(doc.FirstChildElement("RegisterData").ToElement()))
00104 {
00105 NasaCommonLogging::Logger::log("gov.nasa.robonet.JointControlManagerFactory", log4cpp::Priority::ERROR, "The file " + filename + " has no element named [RegisterData]");
00106 MissingXMLElementException missingXMLElementException;
00107 throw missingXMLElementException;
00108 }
00109
00110
00111 std::stringstream ss;
00112 TiXmlHandle registersElement(doc.FirstChildElement("RegisterData").FirstChildElement("Properties"));
00113
00114 if (registersElement.ToElement())
00115 {
00116
00117 if (!(registersElement.FirstChildElement(property).ToElement()))
00118 {
00119 NasaCommonLogging::Logger::log("gov.nasa.robonet.JointControlManagerFactory", log4cpp::Priority::ERROR, "The file " + filename + " has no property named [" + property + "]");
00120 MissingXMLElementException missingXMLElementException;
00121 throw missingXMLElementException;
00122 }
00123
00124 ss << registersElement.FirstChildElement(property).ToElement()->GetText();
00125 return ss.str();
00126 }
00127 else
00128 {
00129 NasaCommonLogging::Logger::log("gov.nasa.robonet.JointControlManagerFactory", log4cpp::Priority::ERROR, "The file " + filename + " has no element named [Properties]");
00130 MissingXMLElementException missingXMLElementException;
00131 throw missingXMLElementException;
00132 }
00133 }