JointControlActualFinger.cpp
Go to the documentation of this file.
00001 
00005 #include "robodyn_mechanisms/JointControlActualFinger.h"
00006 /***************************************************************************/
00015 JointControlActualFinger::JointControlActualFinger(const std::string& mechanism, IoFunctions ioFunctions)
00016     : JointControlActualInterface(mechanism, ioFunctions), logCategory("gov.nasa.JointControlActualFinger")
00017 {
00018     if (io.hasLiveCoeff.empty()    or
00019             io.getLiveCoeff.empty()    or
00020             io.setLiveCoeff.empty())
00021     {
00022         std::stringstream err;
00023         err << "Constructor requires 'io.hasLiveCoeff', 'io.getLiveCoeff', 'io.setLiveCoeff' be non-empty.";
00024         NasaCommonLogging::Logger::log(logCategory, log4cpp::Priority::FATAL, err.str());
00025         throw std::invalid_argument(err.str());
00026     }
00027 
00028     setParameters();
00029 }
00030 
00031 JointControlActualFinger::~JointControlActualFinger()
00032 {
00033     // Nothing
00034 }
00035 
00036 void JointControlActualFinger::setParameters()
00037 {
00038     std::string parameterFile = io.getControlFile(mechanism);
00039 
00041     TiXmlDocument file(parameterFile.c_str());
00042     bool          loadOkay = file.LoadFile();
00043 
00044     if (!loadOkay)
00045     {
00046         std::stringstream err;
00047         err << "Failed to load file [" << parameterFile << "]";
00048         NasaCommonLogging::Logger::log(logCategory, log4cpp::Priority::FATAL, err.str());
00049         throw std::runtime_error(err.str());
00050     }
00051 
00052     TiXmlHandle doc(&file);
00053     NasaCommonLogging::Logger::log(logCategory, log4cpp::Priority::INFO, "File [" + parameterFile + "] successfully loaded.");
00054 
00055     // Print the XML file's contents
00056     //cout << "Successfully loaded file: " << parameterFile << endl;
00057     //TiXmlPrinter printer;
00058     //printer.SetIndent("\t");
00059     //file.Accept(&printer);
00060     //cout << printer.Str() << endl;
00061 
00062     // Check for ApiMap
00063     TiXmlHandle parametersElement(doc.FirstChildElement("ApiMap"));
00064 
00065     if (parametersElement.ToElement())
00066     {
00067         // Live coeffs names
00068 //        IsCalibratedLiveCoeffName          = StringUtilities::makeFullyQualifiedRoboDynElement(mechanism, ApiMap::getXmlElementValue(parametersElement, "IsCalibratedLiveCoeff"));
00069         ControlModeLiveCoeffName           = StringUtilities::makeFullyQualifiedRoboDynElement(mechanism, ApiMap::getXmlElementValue(parametersElement, "ControlModeLiveCoeff"));
00070         CommandModeLiveCoeffName           = StringUtilities::makeFullyQualifiedRoboDynElement(mechanism, ApiMap::getXmlElementValue(parametersElement, "CommandModeLiveCoeff"));
00071         CalibrationModeLiveCoeffName       = StringUtilities::makeFullyQualifiedRoboDynElement(mechanism, ApiMap::getXmlElementValue(parametersElement, "CalibrationModeLiveCoeff"));
00072         CoeffStateLiveCoeffName            = StringUtilities::makeFullyQualifiedRoboDynElement(mechanism, ApiMap::getXmlElementValue(parametersElement, "CoeffStateLiveCoeff"));
00073         ClearFaultStateLiveCoeffName       = StringUtilities::makeFullyQualifiedRoboDynElement(mechanism, ApiMap::getXmlElementValue(parametersElement, "ClearFaultStateLiveCoeff"));
00074     }
00075     else
00076     {
00077         std::stringstream err;
00078         err << "The file " << parameterFile << " has no element named [ApiMap]";
00079         NasaCommonLogging::Logger::log(logCategory, log4cpp::Priority::ERROR, err.str());
00080         throw std::runtime_error(err.str());
00081     }
00082 
00083     // Set up expected values @bootup
00084     io.setLiveCoeff(ControlModeLiveCoeffName,     r2_msgs::JointControlMode::BOOTLOADER);
00085     io.setLiveCoeff(CommandModeLiveCoeffName,     r2_msgs::JointControlCommandMode::MULTILOOPSMOOTH);
00086     io.setLiveCoeff(CalibrationModeLiveCoeffName, r2_msgs::JointControlCalibrationMode::UNCALIBRATED);
00087     io.setLiveCoeff(CoeffStateLiveCoeffName,      r2_msgs::JointControlCoeffState::LOADED);
00088     io.setLiveCoeff(ClearFaultStateLiveCoeffName, r2_msgs::JointControlClearFaultMode::DISABLE);
00089 }
00090 
00091 /***************************************************************************/
00097 void JointControlActualFinger::getStates(r2_msgs::JointControlData& actualStates)
00098 {
00099     updateCommandModeState(); // Must come before updateControlModeState()
00100     updateControlModeState();
00101     updateCalibrationModeState();
00102     updateClearFaultModeState();
00103     updateCoeffState();
00104     actualStates = states;
00105 }
00106 /***************************************************************************/
00111 void JointControlActualFinger::updateControlModeState(void)
00112 {
00113     // Verify existence of live coeffs
00114     if (not(io.hasLiveCoeff(ControlModeLiveCoeffName)))
00115     {
00116         NasaCommonLogging::Logger::getCategory(logCategory) << log4cpp::Priority::ERROR << mechanism << ": Missing live coeff: " << ControlModeLiveCoeffName;
00117         return;
00118     }
00119 
00120     states.controlMode.state = io.getLiveCoeff(ControlModeLiveCoeffName);
00121 
00122     // Check for valid DRIVE ( DRIVE cannot occur unless calibration is DISABLE )
00123     if ( (states.controlMode.state == r2_msgs::JointControlMode::DRIVE) &&
00124             (io.getLiveCoeff(CalibrationModeLiveCoeffName) != r2_msgs::JointControlCalibrationMode::DISABLE))
00125     {
00126         NasaCommonLogging::Logger::getCategory(logCategory) << log4cpp::Priority::ERROR << mechanism << ": Can not DRIVE until JointControlCalibrationMode is DISABLE";
00127         states.controlMode.state = r2_msgs::JointControlMode::PARK;
00128     }
00129 }
00130 /***************************************************************************/
00135 void JointControlActualFinger::updateCommandModeState(void)
00136 {
00137     // Verify existence of live coeffs
00138     if (not(io.hasLiveCoeff(CommandModeLiveCoeffName)))
00139     {
00140         NasaCommonLogging::Logger::getCategory(logCategory) << log4cpp::Priority::ERROR << mechanism << ": Missing live coeff: " << CommandModeLiveCoeffName;
00141         return;
00142     }
00143 
00144     states.commandMode.state = io.getLiveCoeff(CommandModeLiveCoeffName);
00145 }
00146 /***************************************************************************/
00151 void JointControlActualFinger::updateCalibrationModeState(void)
00152 {
00153     if (not(io.hasLiveCoeff(CalibrationModeLiveCoeffName)))
00154     {
00155         NasaCommonLogging::Logger::getCategory(logCategory) << log4cpp::Priority::ERROR << mechanism << ": Missing live coeff: " << CalibrationModeLiveCoeffName;
00156         return;
00157     }
00158 
00159     states.calibrationMode.state = io.getLiveCoeff(CalibrationModeLiveCoeffName);
00160 
00161 //    // Check for DISABLE
00162 //    if (io.getLiveCoeff(IsCalibratedLiveCoeffName) == 1)
00163 //    {
00164 //        if (states.calibrationMode.state != r2_msgs::JointControlCalibrationMode::DISABLE)
00165 //        {
00166 //            NasaCommonLogging::Logger::getCategory("gov.nasa.JointControlActualFsmFinger") << log4cpp::Priority::DEBUG << "Transition to JointControlCalibrationMode::DISABLE on joint: " << mechanism;
00167 //            states.calibrationMode.state = r2_msgs::JointControlCalibrationMode::DISABLE;
00168 //        }
00169 //        return;
00170 //    }
00171 
00172 //    // If not DISABLE then ENABLE
00173 //    if (states.calibrationMode.state != r2_msgs::JointControlCalibrationMode::ENABLE)
00174 //    {
00175 //        NasaCommonLogging::Logger::getCategory("gov.nasa.JointControlActualFsmFinger") << log4cpp::Priority::DEBUG << "Transition to JointControlCalibrationMode::ENABLE on joint: " << mechanism;
00176 //        states.calibrationMode.state = r2_msgs::JointControlCalibrationMode::ENABLE;
00177 //    }
00178 }
00179 /***************************************************************************/
00184 void JointControlActualFinger::updateClearFaultModeState(void)
00185 {
00186     if (not(io.hasLiveCoeff(ClearFaultStateLiveCoeffName)))
00187     {
00188         NasaCommonLogging::Logger::getCategory(logCategory) << log4cpp::Priority::ERROR << mechanism << ": Missing live coeff: " << ClearFaultStateLiveCoeffName;
00189         return;
00190     }
00191 
00192     states.clearFaultMode.state = io.getLiveCoeff(ClearFaultStateLiveCoeffName);
00193 }
00194 /***************************************************************************/
00199 void JointControlActualFinger::updateCoeffState(void)
00200 {
00201     if (not(io.hasLiveCoeff(CoeffStateLiveCoeffName)))
00202     {
00203         NasaCommonLogging::Logger::getCategory(logCategory) << log4cpp::Priority::ERROR << mechanism << ": Missing live coeff: " << CoeffStateLiveCoeffName;
00204         return;
00205     }
00206 
00207     states.coeffState.state = io.getLiveCoeff(CoeffStateLiveCoeffName);
00208 }
00209 
00210 void JointControlActualFinger::getFaults(diagnostic_msgs::DiagnosticStatus& faultStatus)
00211 {
00212     faultStatus.name = mechanism;
00213     faultStatus.hardware_id = mechanism;
00214     faultStatus.level = diagnostic_msgs::DiagnosticStatus::OK;
00215     faultStatus.message = "OK";
00216 
00217     if (states.controlMode.state == r2_msgs::JointControlMode::FAULTED)
00218     {
00219         faultStatus.level = diagnostic_msgs::DiagnosticStatus::ERROR;
00220         faultStatus.message = "FAULT state detected on " + mechanism + ": FAULT";
00221     }
00222 
00223 }


robodyn_mechanisms
Author(s):
autogenerated on Thu Jun 6 2019 21:22:48