DynamicParameterClass.cpp
Go to the documentation of this file.
1 //
3 // © Copyright 2022 SCHUNK Mobile Greifsysteme GmbH, Lauffen/Neckar Germany
4 // © Copyright 2022 FZI Forschungszentrum Informatik, Karlsruhe, Germany
5 //
6 // This file is part of the Schunk SVH Driver.
7 //
8 // The Schunk SVH Driver is free software: you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or (at your
11 // option) any later version.
12 //
13 // The Schunk SVH Driver is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 // Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along with
19 // the Schunk SVH Driver. If not, see <https://www.gnu.org/licenses/>.
20 //
22 
23 //----------------------------------------------------------------------
31 //----------------------------------------------------------------------
32 
33 
34 #include <DynamicParameterClass.h>
35 
36 /*
37  * The DynamicParameter class reads out the parameter file and searches for the proposed version!
38  */
39 
40 DynamicParameter::DynamicParameter(const uint16_t major_version,
41  const uint16_t minor_version,
42  XmlRpc::XmlRpcValue& parameters)
43 {
44  // joint names used for parameter mapping from string to SVHChannel
45  // currently hardcoded...
48  m_name_to_enum[driver_svh::SVH_INDEX_FINGER_DISTAL] = "INDEX_FINGER_DISTAL";
49  m_name_to_enum[driver_svh::SVH_INDEX_FINGER_PROXIMAL] = "INDEX_FINGER_PROXIMAL";
50  m_name_to_enum[driver_svh::SVH_MIDDLE_FINGER_DISTAL] = "MIDDLE_FINGER_DISTAL";
51  m_name_to_enum[driver_svh::SVH_MIDDLE_FINGER_PROXIMAL] = "MIDDLE_FINGER_PROXIMAL";
55 
56  // Assertion to test whether the vector sizes are the same. Otherwise fail
57  ROS_ASSERT(static_cast<driver_svh::SVHChannel>(m_name_to_enum.size()) ==
59 
60  try
61  {
62  parse_parameters(major_version, minor_version, parameters);
63  }
64  catch (XmlRpc::XmlRpcException& e)
65  {
66  ROS_ERROR_STREAM("parsing error: " << e.getMessage() << "! Error code: " << e.getCode());
67  ROS_ERROR("ATTENTION: YOU HAVE AN INCORRECT PARAMETER FILE!!!");
68  exit(0);
69  }
70 }
71 
72 /*
73  * This function converts a RpcValue Vector to a std::vector
74  */
75 
77  std::vector<float>& my_vector)
78 {
79  my_vector.clear();
80 
81  for (size_t i = 0; i < (unsigned)my_array.size(); ++i)
82  {
83  ROS_ASSERT(my_array[i].getType() == XmlRpc::XmlRpcValue::TypeDouble ||
84  my_array[i].getType() == XmlRpc::XmlRpcValue::TypeInt);
85 
86  if (my_array[i].getType() == XmlRpc::XmlRpcValue::TypeDouble)
87  {
88  my_vector.push_back(static_cast<double>(my_array[i]));
89  }
90  else if (my_array[i].getType() == XmlRpc::XmlRpcValue::TypeInt)
91  {
92  int value = my_array[i];
93  my_vector.push_back(static_cast<double>(value));
94  }
95  }
96 
97  return true;
98 }
99 
100 /*
101  * This function parses the parameter file and set the corresponding major and minor version
102  */
103 
104 void DynamicParameter::parse_parameters(const uint16_t major_version_target,
105  const uint16_t minor_version_target,
106  XmlRpc::XmlRpcValue& parameters)
107 {
108  ROS_INFO("Trying to search controller parameters for version: %d.%d",
109  major_version_target,
110  minor_version_target);
111 
114 
115  if (0 == major_version_target || 5 < major_version_target)
116  {
117  ROS_ERROR(
118  "Could not establish connection to Schunk SVH. Please check connections and power source!");
119  return;
120  }
121 
122  if (0 < parameters.size())
123  {
124  ROS_DEBUG("There exist %d different parameter versions", parameters.size());
125  for (size_t i = 0; i < (unsigned)parameters.size(); ++i)
126  {
127  XmlRpc::XmlRpcValue parameter_set_yaml = parameters[i]["parameter_set"];
128 
129  // the following parameters are for this version
130  uint16_t major_version_read = int(parameter_set_yaml["major_version"]);
131  uint16_t minor_version_read = int(parameter_set_yaml["minor_version"]);
132  if (m_settings.major_version > major_version_read)
133  {
134  ROS_DEBUG("Skipping version %d.%d as better matching version was already found",
135  major_version_read,
136  minor_version_read);
137  continue;
138  }
139 
140  bool is_correct_version =
141  major_version_read == major_version_target && minor_version_read == minor_version_target;
142  bool is_same_major_version =
143  major_version_read == major_version_target && minor_version_read <= minor_version_target;
144  bool is_default_state = major_version_read == 0 && minor_version_read == 0;
145 
146  if (is_correct_version || is_same_major_version || is_default_state)
147  {
148  ROS_DEBUG("major version: %d minor version: %d", major_version_read, minor_version_read);
149 
150  for (std::map<driver_svh::SVHChannel, std::string>::iterator it = m_name_to_enum.begin();
151  it != m_name_to_enum.end();
152  ++it)
153  {
154  m_settings.major_version = major_version_read;
155  m_settings.minor_version = minor_version_read;
156  if (parameter_set_yaml.hasMember(it->second))
157  {
158  ROS_DEBUG("Parameter Name: %s", it->second.c_str());
159 
161  xml_rpc_value_to_vector(parameter_set_yaml[it->second]["position_controller"],
162  m_settings.position_settings[it->first]);
164  xml_rpc_value_to_vector(parameter_set_yaml[it->second]["current_controller"],
165  m_settings.current_settings[it->first]);
167  parameter_set_yaml[it->second]["home_settings"], m_settings.home_settings[it->first]);
168  }
169  else
170  {
171  std::stringstream error_stream;
172  error_stream << "Could not find parameters for channel " << it->second;
173  throw(XmlRpc::XmlRpcException(error_stream.str(), -1));
174  }
175  }
176 
177  // First Reading of parameters
178  if (is_default_state)
179  {
180  is_default_state = false;
181  }
182  else if (is_correct_version)
183  {
184  ROS_INFO("Did find correct version");
185  return;
186  }
187  else
188  {
189  ROS_DEBUG("Did find same major version");
190  }
191  }
192  }
193  }
194  ROS_WARN_STREAM("DID NOT FIND EXACT VERSION: " << major_version_target << "."
195  << minor_version_target
196  << " Falling back to: " << m_settings.major_version
197  << "." << m_settings.minor_version);
198 }
XmlRpc::XmlRpcValue::size
int size() const
DynamicParameter::Settings::home_settings_given
std::vector< bool > home_settings_given
Definition: DynamicParameterClass.h:87
ROS_ERROR_STREAM
#define ROS_ERROR_STREAM(args)
XmlRpc::XmlRpcException::getMessage
const std::string & getMessage() const
DynamicParameter::m_settings
Settings m_settings
Stores the settings received by the firmware.
Definition: DynamicParameterClass.h:115
XmlRpc::XmlRpcValue::TypeInt
TypeInt
ROS_DEBUG
#define ROS_DEBUG(...)
driver_svh::SVH_FINGER_SPREAD
SVH_FINGER_SPREAD
driver_svh::SVH_DIMENSION
SVH_DIMENSION
DynamicParameter::Settings::position_settings_given
std::vector< bool > position_settings_given
Definition: DynamicParameterClass.h:81
driver_svh::SVH_MIDDLE_FINGER_PROXIMAL
SVH_MIDDLE_FINGER_PROXIMAL
DynamicParameter::parse_parameters
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.
Definition: DynamicParameterClass.cpp:104
DynamicParameter::Settings::home_settings
std::vector< std::vector< float > > home_settings
Definition: DynamicParameterClass.h:86
DynamicParameter::Settings::current_settings_given
std::vector< bool > current_settings_given
Definition: DynamicParameterClass.h:84
DynamicParameter::Settings::position_settings
std::vector< std::vector< float > > position_settings
Definition: DynamicParameterClass.h:80
driver_svh::SVH_PINKY
SVH_PINKY
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
XmlRpc::XmlRpcValue::TypeDouble
TypeDouble
DynamicParameter::xml_rpc_value_to_vector
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...
Definition: DynamicParameterClass.cpp:76
ROS_ERROR
#define ROS_ERROR(...)
XmlRpc::XmlRpcException::getCode
int getCode() const
driver_svh::SVHChannel
SVHChannel
XmlRpc::XmlRpcException
driver_svh::SVH_RING_FINGER
SVH_RING_FINGER
XmlRpc::XmlRpcValue::hasMember
bool hasMember(const std::string &name) const
DynamicParameter::Settings::major_version
uint16_t major_version
Definition: DynamicParameterClass.h:89
DynamicParameterClass.h
driver_svh::SVH_THUMB_FLEXION
SVH_THUMB_FLEXION
DynamicParameter::Settings::minor_version
uint16_t minor_version
Definition: DynamicParameterClass.h:90
driver_svh::SVH_MIDDLE_FINGER_DISTAL
SVH_MIDDLE_FINGER_DISTAL
DynamicParameter::Settings::current_settings
std::vector< std::vector< float > > current_settings
Definition: DynamicParameterClass.h:83
driver_svh::SVH_THUMB_OPPOSITION
SVH_THUMB_OPPOSITION
DynamicParameter::m_name_to_enum
std::map< driver_svh::SVHChannel, std::string > m_name_to_enum
Stores an enum-string matching map.
Definition: DynamicParameterClass.h:118
driver_svh::SVH_INDEX_FINGER_DISTAL
SVH_INDEX_FINGER_DISTAL
ROS_INFO
#define ROS_INFO(...)
ROS_ASSERT
#define ROS_ASSERT(cond)
DynamicParameter::DynamicParameter
DynamicParameter(const uint16_t major_version, const uint16_t minor_version, XmlRpc::XmlRpcValue &parameters)
DynamicParameter constructs a new dynamic parameter handler.
Definition: DynamicParameterClass.cpp:40
XmlRpc::XmlRpcValue
driver_svh::SVH_INDEX_FINGER_PROXIMAL
SVH_INDEX_FINGER_PROXIMAL


schunk_svh_driver
Author(s): Georg Heppner , Felix Exner , Pascal Becker , Johannes Mangler
autogenerated on Sat Apr 15 2023 02:24:55