DynamicParameterClass.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of the SCHUNK SVH Driver suite.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2017 SCHUNK Mobile Greifsysteme GmbH, Lauffen/Neckar Germany
12 // © Copyright 2017 FZI Forschungszentrum Informatik, Karlsruhe, Germany
13 //
14 // -- END LICENSE BLOCK ------------------------------------------------
15 
16 //----------------------------------------------------------------------
24 //----------------------------------------------------------------------
25 
26 
27 
28 #include <DynamicParameterClass.h>
29 
30 /*
31  * The DynamicParameter class reads out the parameter file and searches for the proposed version!
32  */
33 
35  const uint16_t minor_version,
36  XmlRpc::XmlRpcValue& parameters)
37 {
38  // joint names used for parameter mapping from string to SVHChannel
39  // currently hardcoded...
43  m_name_to_enum[driver_svh::eSVH_INDEX_FINGER_PROXIMAL] = "INDEX_FINGER_PROXIMAL";
44  m_name_to_enum[driver_svh::eSVH_MIDDLE_FINGER_DISTAL] = "MIDDLE_FINGER_DISTAL";
45  m_name_to_enum[driver_svh::eSVH_MIDDLE_FINGER_PROXIMAL] = "MIDDLE_FINGER_PROXIMAL";
49 
50  // Assertion to test whether the vector sizes are the same. Otherwise fail
51  ROS_ASSERT(static_cast<driver_svh::SVHChannel>(m_name_to_enum.size()) ==
53 
54  try
55  {
56  parse_parameters(major_version, minor_version, parameters);
57  }
58  catch (XmlRpc::XmlRpcException& e)
59  {
60  ROS_ERROR_STREAM("parsing error: " << e.getMessage() << "! Error code: " << e.getCode());
61  ROS_ERROR("ATTENTION: YOU HAVE AN INCORRECT PARAMETER FILE!!!");
62  exit(0);
63  }
64 
65 }
66 
67 /*
68  * This function converts a RpcValue Vector to a std::vector
69  */
70 
72  std::vector<float>& my_vector)
73 {
74  my_vector.clear();
75 
76  for (size_t i = 0; i < (unsigned) my_array.size(); ++i)
77  {
78  ROS_ASSERT(my_array[i].getType() == XmlRpc::XmlRpcValue::TypeDouble ||
79  my_array[i].getType() == XmlRpc::XmlRpcValue::TypeInt);
80 
81  if (my_array[i].getType() == XmlRpc::XmlRpcValue::TypeDouble)
82  {
83  my_vector.push_back(static_cast<double>(my_array[i]));
84  }
85  else if (my_array[i].getType() == XmlRpc::XmlRpcValue::TypeInt)
86  {
87  int value = my_array[i];
88  my_vector.push_back(static_cast<double>(value));
89  }
90  }
91 
92  return true;
93 }
94 
95 /*
96  * This function parses the parameter file and set the corresponding major and minor version
97  */
98 
99 void DynamicParameter::parse_parameters(const uint16_t major_version_target,
100  const uint16_t minor_version_target,
101  XmlRpc::XmlRpcValue& parameters)
102 {
103  ROS_INFO("Trying to search controller parameters for version: %d.%d", major_version_target, minor_version_target);
104 
107 
108  if ( 0 == major_version_target || 5 < major_version_target )
109  {
110  ROS_ERROR("Could not establish connection to Schunk SVH. Please check connections and power source!");
111  return;
112  }
113 
114  if (0 < parameters.size())
115  {
116  ROS_DEBUG("There exist %d different parameter versions", parameters.size());
117  for (size_t i = 0; i < (unsigned) parameters.size(); ++i)
118  {
119  XmlRpc::XmlRpcValue parameter_set_yaml = parameters[i]["parameter_set"];
120 
121  // the following parameters are for this version
122  uint16_t major_version_read = int(parameter_set_yaml["major_version"]);
123  uint16_t minor_version_read = int(parameter_set_yaml["minor_version"]);
124  if (m_settings.major_version > major_version_read)
125  {
126  ROS_DEBUG("Skipping version %d.%d as better matching version was already found",
127  major_version_read,
128  minor_version_read
129  );
130  continue;
131  }
132 
133  bool is_correct_version = major_version_read == major_version_target
134  && minor_version_read == minor_version_target;
135  bool is_same_major_version = major_version_read == major_version_target
136  && minor_version_read <= minor_version_target;
137  bool is_default_state = major_version_read == 0
138  && minor_version_read == 0;
139 
140  if (is_correct_version || is_same_major_version || is_default_state)
141  {
142  ROS_DEBUG("major version: %d minor version: %d", major_version_read, minor_version_read);
143 
144  for (std::map<driver_svh::SVHChannel, std::string>::iterator it = m_name_to_enum.begin();
145  it != m_name_to_enum.end();
146  ++it)
147  {
148  m_settings.major_version = major_version_read;
149  m_settings.minor_version = minor_version_read;
150  if (parameter_set_yaml.hasMember(it->second))
151  {
152  ROS_DEBUG("Parameter Name: %s", it->second.c_str());
153 
155  parameter_set_yaml[it->second]["position_controller"], m_settings.position_settings[it->first]);
157  parameter_set_yaml[it->second]["current_controller"], m_settings.current_settings[it->first]);
159  parameter_set_yaml[it->second]["home_settings"], m_settings.home_settings[it->first]);
160  }
161  else
162  {
163  std::stringstream error_stream;
164  error_stream << "Could not find parameters for channel " << it->second;
165  throw(XmlRpc::XmlRpcException(error_stream.str(), -1));
166  }
167  }
168 
169  // First Reading of parameters
170  if (is_default_state)
171  {
172  is_default_state = false;
173  }
174  else if (is_correct_version)
175  {
176  ROS_INFO("Did find correct version");
177  return;
178  }
179  else
180  {
181  ROS_DEBUG("Did find same major version");
182  }
183  }
184  }
185  }
186  ROS_WARN_STREAM("DID NOT FIND EXACT VERSION: " << major_version_target << "."
187  << minor_version_target
188  << " Falling back to: "
190  << "."
192 }
const std::string & getMessage() const
DynamicParameter(const uint16_t major_version, const uint16_t minor_version, XmlRpc::XmlRpcValue &parameters)
DynamicParameter constructs a new dynamic parameter handler.
int size() const
std::vector< bool > home_settings_given
Settings m_settings
Stores the settings received by the firmware.
std::map< driver_svh::SVHChannel, std::string > m_name_to_enum
Stores an enum-string matching map.
std::vector< bool > position_settings_given
std::vector< std::vector< float > > position_settings
std::vector< std::vector< float > > home_settings
std::vector< bool > current_settings_given
#define ROS_INFO(...)
bool xml_rpc_value_to_vector(XmlRpc::XmlRpcValue my_array, std::vector< float > &my_vector)
Converts the given XmlRpcValue array to a std:vector /param my_array XmlRpcValue array created by rea...
#define ROS_WARN_STREAM(args)
bool hasMember(const std::string &name) const
#define ROS_ERROR_STREAM(args)
#define ROS_ASSERT(cond)
unsigned short uint16_t
#define ROS_ERROR(...)
void parse_parameters(const uint16_t major_version, const uint16_t minor_version, XmlRpc::XmlRpcValue &parameters)
This method parses the given XmlRpcValue array and sets the corresponding parameters.
std::vector< std::vector< float > > current_settings
#define ROS_DEBUG(...)


schunk_svh_driver
Author(s): Georg Heppner
autogenerated on Mon Jun 10 2019 15:04:59