DynamicParameterClass.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of the SCHUNK SVH Driver suite.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2017 SCHUNK Mobile Greifsysteme GmbH, Lauffen/Neckar Germany
00012 // © Copyright 2017 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00013 //
00014 // -- END LICENSE BLOCK ------------------------------------------------
00015 
00016 //----------------------------------------------------------------------
00024 //----------------------------------------------------------------------
00025 
00026 
00027 
00028 #include <DynamicParameterClass.h>
00029 
00030 /*
00031  * The DynamicParameter class reads out the parameter file and searches for the proposed version!
00032  */
00033 
00034 DynamicParameter::DynamicParameter(const uint16_t major_version,
00035                                    const uint16_t minor_version,
00036                                    XmlRpc::XmlRpcValue& parameters)
00037 {
00038   // joint names used for parameter mapping from string to SVHChannel
00039   // currently hardcoded...
00040   m_name_to_enum[driver_svh::eSVH_THUMB_FLEXION]          = "THUMB_FLEXION";
00041   m_name_to_enum[driver_svh::eSVH_THUMB_OPPOSITION]       = "THUMB_OPPOSITION";
00042   m_name_to_enum[driver_svh::eSVH_INDEX_FINGER_DISTAL]    = "INDEX_FINGER_DISTAL";
00043   m_name_to_enum[driver_svh::eSVH_INDEX_FINGER_PROXIMAL]  = "INDEX_FINGER_PROXIMAL";
00044   m_name_to_enum[driver_svh::eSVH_MIDDLE_FINGER_DISTAL]   = "MIDDLE_FINGER_DISTAL";
00045   m_name_to_enum[driver_svh::eSVH_MIDDLE_FINGER_PROXIMAL] = "MIDDLE_FINGER_PROXIMAL";
00046   m_name_to_enum[driver_svh::eSVH_RING_FINGER]            = "RING_FINGER";
00047   m_name_to_enum[driver_svh::eSVH_PINKY]                  = "PINKY";
00048   m_name_to_enum[driver_svh::eSVH_FINGER_SPREAD]          = "FINGER_SPREAD";
00049 
00050   // Assertion to test whether the vector sizes are the same. Otherwise fail
00051   ROS_ASSERT(static_cast<driver_svh::SVHChannel>(m_name_to_enum.size()) ==
00052              driver_svh::eSVH_DIMENSION);
00053 
00054   try
00055   {
00056     parse_parameters(major_version, minor_version, parameters);
00057   }
00058   catch (XmlRpc::XmlRpcException& e)
00059   {
00060     ROS_ERROR_STREAM("parsing error: " << e.getMessage() << "! Error code: " << e.getCode());
00061     ROS_ERROR("ATTENTION: YOU HAVE AN INCORRECT PARAMETER FILE!!!");
00062     exit(0);
00063   }
00064 
00065 }
00066 
00067 /*
00068  *  This function converts a RpcValue Vector to a std::vector
00069  */
00070 
00071 bool DynamicParameter::xml_rpc_value_to_vector(XmlRpc::XmlRpcValue my_array,
00072                                                std::vector<float>& my_vector)
00073 {
00074   my_vector.clear();
00075 
00076   for (size_t i = 0; i < (unsigned) my_array.size(); ++i)
00077   {
00078     ROS_ASSERT(my_array[i].getType() == XmlRpc::XmlRpcValue::TypeDouble ||
00079                my_array[i].getType() == XmlRpc::XmlRpcValue::TypeInt);
00080 
00081     if (my_array[i].getType() == XmlRpc::XmlRpcValue::TypeDouble)
00082     {
00083       my_vector.push_back(static_cast<double>(my_array[i]));
00084     }
00085     else if (my_array[i].getType() == XmlRpc::XmlRpcValue::TypeInt)
00086     {
00087       int value = my_array[i];
00088       my_vector.push_back(static_cast<double>(value));
00089     }
00090   }
00091 
00092   return true;
00093 }
00094 
00095 /*
00096  * This function parses the parameter file and set the corresponding major and minor version
00097  */
00098 
00099 void DynamicParameter::parse_parameters(const uint16_t major_version_target,
00100                                 const uint16_t minor_version_target,
00101                                 XmlRpc::XmlRpcValue& parameters)
00102 {
00103   ROS_INFO("Trying to search controller parameters for version: %d.%d", major_version_target, minor_version_target);
00104 
00105   m_settings.major_version = 0;
00106   m_settings.minor_version = 0;
00107 
00108   if ( 0 == major_version_target || 5 < major_version_target )
00109   {
00110     ROS_ERROR("Could not establish connection to Schunk SVH. Please check connections and power source!");
00111     return;
00112   }
00113 
00114   if (0 < parameters.size())
00115   {
00116     ROS_DEBUG("There exist %d different parameter versions", parameters.size());
00117     for (size_t i = 0; i < (unsigned) parameters.size(); ++i)
00118     {
00119       XmlRpc::XmlRpcValue parameter_set_yaml = parameters[i]["parameter_set"];
00120 
00121       // the following parameters are for this version
00122       uint16_t major_version_read = int(parameter_set_yaml["major_version"]);
00123       uint16_t minor_version_read = int(parameter_set_yaml["minor_version"]);
00124       if (m_settings.major_version > major_version_read)
00125       {
00126         ROS_DEBUG("Skipping version %d.%d as better matching version was already found",
00127                   major_version_read,
00128                   minor_version_read
00129         );
00130         continue;
00131       }
00132 
00133       bool is_correct_version    = major_version_read == major_version_target
00134                                 && minor_version_read == minor_version_target;
00135       bool is_same_major_version = major_version_read == major_version_target
00136                                 && minor_version_read <= minor_version_target;
00137       bool is_default_state      = major_version_read == 0
00138                                 && minor_version_read == 0;
00139 
00140       if (is_correct_version || is_same_major_version || is_default_state)
00141       {
00142         ROS_DEBUG("major version: %d minor version: %d", major_version_read, minor_version_read);
00143 
00144         for (std::map<driver_svh::SVHChannel, std::string>::iterator it = m_name_to_enum.begin();
00145               it != m_name_to_enum.end();
00146               ++it)
00147         {
00148           m_settings.major_version = major_version_read;
00149           m_settings.minor_version = minor_version_read;
00150           if (parameter_set_yaml.hasMember(it->second))
00151           {
00152             ROS_DEBUG("Parameter Name: %s", it->second.c_str());
00153 
00154             m_settings.position_settings_given[it->first] = xml_rpc_value_to_vector(
00155               parameter_set_yaml[it->second]["position_controller"], m_settings.position_settings[it->first]);
00156             m_settings.current_settings_given[it->first] = xml_rpc_value_to_vector(
00157               parameter_set_yaml[it->second]["current_controller"], m_settings.current_settings[it->first]);
00158             m_settings.home_settings_given[it->first] = xml_rpc_value_to_vector(
00159               parameter_set_yaml[it->second]["home_settings"], m_settings.home_settings[it->first]);
00160           }
00161           else
00162           {
00163             std::stringstream error_stream;
00164             error_stream << "Could not find parameters for channel " << it->second;
00165             throw(XmlRpc::XmlRpcException(error_stream.str(), -1));
00166           }
00167         }
00168 
00169         // First Reading of parameters
00170         if (is_default_state)
00171         {
00172           is_default_state = false;
00173         }
00174         else if (is_correct_version)
00175         {
00176           ROS_INFO("Did find correct version");
00177           return;
00178         }
00179         else
00180         {
00181           ROS_DEBUG("Did find same major version");
00182         }
00183       }
00184     }
00185   }
00186   ROS_WARN_STREAM("DID NOT FIND EXACT VERSION: " << major_version_target << "."
00187                                                  << minor_version_target
00188                                                  << " Falling back to: "
00189                                                  << m_settings.major_version
00190                                                  << "."
00191                                                  << m_settings.minor_version);
00192 }


schunk_svh_driver
Author(s): Georg Heppner
autogenerated on Thu Jun 6 2019 18:29:08