SVHWrapper.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 //----------------------------------------------------------------------
30 //----------------------------------------------------------------------
31 
32 #include "SVHWrapper.h"
33 #include "DynamicParameterClass.h"
34 #include "ROSLogHandler.h"
35 
36 
37 #include <std_msgs/MultiArrayDimension.h>
38 
40  : m_priv_nh(nh)
41  , m_channels_enabled(false)
42 
43 {
44  bool autostart;
45 
46  int reset_timeout;
47  std::vector<bool> disable_flags(driver_svh::SVH_DIMENSION, false);
48  // Config that contains the log stream configuration without the file names
49 
50  sensor_msgs::JointState joint_msg;
51 
52  float max_force;
53 
54  m_priv_nh.param<bool>("autostart", autostart, false);
55  m_priv_nh.param<std::string>("serial_device", m_serial_device_name, "/dev/ttyUSB0");
56  // Note : Wrong values (like numerics) in the launch file will lead to a "true" value here
57  m_priv_nh.getParam("disable_flags", disable_flags);
58  m_priv_nh.param<int>("reset_timeout", reset_timeout, 5);
59  m_priv_nh.param<std::string>("name_prefix", m_name_prefix, "left_hand");
60  m_priv_nh.param<int>("connect_retry_count", m_connect_retry_count, 3);
61  m_priv_nh.param<float>("maximal_force", max_force, 0.8);
62  m_priv_nh.param<int>("use_major_version", m_firmware_major_version, 0);
63  m_priv_nh.param<int>("use_minor_version", m_firmware_minor_version, 0);
64 
65  // Tell the user what we are using
66  ROS_INFO("Name prefix for this Hand was set to: %s", m_name_prefix.c_str());
67 
69  ROS_INFO("Forced Handversion %d.%d", m_firmware_major_version, m_firmware_minor_version);
70 
71 
73 
74 
75  for (size_t i = 0; i < 9; ++i)
76  {
77  if (disable_flags[i])
78  {
79  ROS_WARN_STREAM("svh_controller disabling channel nr " << i);
80  }
81  }
82 
83  // Init the actual driver hook
84  m_finger_manager.reset(new driver_svh::SVHFingerManager(disable_flags, reset_timeout));
85 
86  // Connect and start the reset so that the hand is ready for use
87  // try if SVH is connected
88  connect();
89  if (autostart)
90  {
91  if (m_finger_manager->resetChannel(driver_svh::SVH_ALL))
92  {
93  ROS_INFO("Driver was autostarted! Input can now be sent. Have a safe and productive day!");
94  m_channels_enabled = true;
95  }
96  else
97  {
98  ROS_ERROR("Tried to reset the fingers by autostart: Not succeeded!");
99  }
100  }
101  else
102  {
103  ROS_INFO("SVH Driver Ready, you will need to connect and reset the fingers before you can use "
104  "the hand.");
105  }
106 
107  // set the maximal force / current value from the parameters
108  m_finger_manager->setMaxForce(max_force);
109 
110  // Subscribe connect topic (Empty)
112 
113  // Subscribe enable channel topic (Int8)
114  enable_sub = m_priv_nh.subscribe("enable_channel", 1, &SVHWrapper::enableChannelCallback, this);
115 
116  // services
118  m_priv_nh.advertiseService("home_reset_offset_all", &SVHWrapper::homeAllNodes, this);
120  m_priv_nh.advertiseService("home_reset_offset_by_id", &SVHWrapper::homeNodesChannelIds, this);
121 
123  m_priv_nh.advertiseService("set_all_force_limits", &SVHWrapper::setAllForceLimits, this);
125  m_priv_nh.advertiseService("set_force_limit_by_id", &SVHWrapper::setForceLimitById, this);
126 
128  m_priv_nh,
130  std::bind(&SVHWrapper::setRosControlEnable, this, std::placeholders::_1),
131  std::bind(
132  &SVHWrapper::initControllerParameters, this, std::placeholders::_1, std::placeholders::_2),
133  "diagnostics_to_protocol"));
134 }
135 
137 {
138  m_channels_enabled = enable;
139 }
140 
141 void SVHWrapper::initControllerParameters(const uint16_t firmware_major_version,
142  const uint16_t firmware_minor_version)
143 {
144  // Parameters that depend on the hardware version of the hand
145  XmlRpc::XmlRpcValue dynamic_parameters;
146 
147  // read hand parameter (operation data) from parameter server
148  std::string parameters_name = "VERSIONS_PARAMETERS";
149  try
150  {
151  if (!m_priv_nh.getParam(parameters_name, dynamic_parameters))
152  {
153  ROS_FATAL_STREAM("Could not find controller_parameters under "
154  << m_priv_nh.resolveName(parameters_name));
155  exit(-1);
156  }
157  }
158  catch (ros::InvalidNameException e)
159  {
160  ROS_FATAL_STREAM("Illegal parameter name: " << parameters_name);
161  exit(-1);
162  }
163 
164  try
165  {
166  // Loading hand parameters
167  DynamicParameter dyn_parameters(
168  firmware_major_version, firmware_minor_version, dynamic_parameters);
169 
170  for (size_t channel = 0; channel < driver_svh::SVH_DIMENSION; ++channel)
171  {
172  // Only update the values in case actually have some. Otherwise the driver will use internal
173  // defaults. Overwriting them with zeros would be counter productive
174  if (dyn_parameters.getSettings().current_settings_given[channel])
175  {
176  m_finger_manager->setCurrentSettings(
177  static_cast<driver_svh::SVHChannel>(channel),
178  driver_svh::SVHCurrentSettings(dyn_parameters.getSettings().current_settings[channel]));
179  }
180  if (dyn_parameters.getSettings().position_settings_given[channel])
181  {
182  m_finger_manager->setPositionSettings(
183  static_cast<driver_svh::SVHChannel>(channel),
185  }
186  if (dyn_parameters.getSettings().home_settings_given[channel])
187  {
188  m_finger_manager->setHomeSettings(
189  static_cast<driver_svh::SVHChannel>(channel),
190  driver_svh::SVHHomeSettings(dyn_parameters.getSettings().home_settings[channel]));
191  }
192  }
193  }
194  catch (ros::InvalidNameException e)
195  {
196  ROS_ERROR("Parameter Error! While reading the controller settings. Will use default settings");
197  }
198 }
199 
200 
202 {
203  m_finger_manager->disconnect();
204  m_svh_diagnostics.reset();
205 }
206 
208 {
209  m_channels_enabled = false;
210 
211  if (m_finger_manager->isConnected())
212  {
213  m_finger_manager->disconnect();
214  }
215 
216  // Receives current Firmware Version
217  // because some parameters depend on the version
219  {
220  // reads out current Firmware Version
221  driver_svh::SVHFirmwareInfo version_info =
223  ROS_INFO("Current Handversion %d.%d", version_info.version_major, version_info.version_minor);
226  }
227 
228  // Was firmware info given by the hand?
230  {
231  ROS_ERROR("Could not get Version Info from SCHUNK five finger hand with serial device %s, and "
232  "retry count %i",
233  m_serial_device_name.c_str(),
235  return false;
236  }
237 
238  // read out the operation data with the connected hand version
239  initControllerParameters(static_cast<uint16_t>(m_firmware_major_version),
240  static_cast<uint16_t>(m_firmware_minor_version));
241 
242  // try connect, channels are not enabled -> have to be resetted/homed
244  {
245  ROS_ERROR(
246  "Could not connect to SCHUNK five finger hand with serial device %s, and retry count %i",
247  m_serial_device_name.c_str(),
249  return false;
250  }
251 
252  return true;
253 }
254 
255 // Callback function for connecting to SCHUNK five finger hand
256 void SVHWrapper::connectCallback(const std_msgs::Empty&)
257 {
258  ROS_INFO("trying to connect");
259  connect();
260 }
261 
262 // Callback function to enable channels of SCHUNK five finger hand
263 void SVHWrapper::enableChannelCallback(const std_msgs::Int8ConstPtr& channel)
264 {
265  m_finger_manager->enableChannel(static_cast<driver_svh::SVHChannel>(channel->data));
266 }
267 
268 bool SVHWrapper::homeAllNodes(schunk_svh_msgs::HomeAll::Request& req,
269  schunk_svh_msgs::HomeAll::Response& resp)
270 {
271  // disable flag to stop ros-control-loop
272  m_channels_enabled = false;
273 
274  resp.success = m_finger_manager->resetChannel(driver_svh::SVH_ALL);
275 
276  // enable flag to start ros-control-loop if successfully resetted
277  if (resp.success == true)
278  {
279  ROS_INFO("successfully resetted");
280  m_channels_enabled = true;
281  }
282 
283  return resp.success;
284 }
285 
286 bool SVHWrapper::homeNodesChannelIds(schunk_svh_msgs::HomeWithChannels::Request& req,
287  schunk_svh_msgs::HomeWithChannels::Response& resp)
288 {
289  // is ros-control-loop enabled ?
290  bool channels_enabled_before;
291 
292  if (m_channels_enabled)
293  {
294  // disable flag to stop ros-control-loop
295  m_channels_enabled = false;
296  channels_enabled_before = true;
297  }
298  else
299  {
300  // not all channels resetted before, so ros-control-loop won't be enabled after
301  channels_enabled_before = false;
302  ROS_WARN_STREAM("After resetting asked channel the ros controll loop will not be enabled");
303  }
304 
305 
306  for (std::vector<uint8_t>::iterator it = req.channel_ids.begin(); it != req.channel_ids.end();
307  ++it)
308  {
309  m_finger_manager->resetChannel(static_cast<driver_svh::SVHChannel>(*it));
310  }
311 
312  if (channels_enabled_before || m_finger_manager->isHomed(driver_svh::SVH_ALL))
313  {
314  // enable flag to stop ros-control-loop
315  m_channels_enabled = true;
316  }
317 
318  resp.success = true;
319  return resp.success;
320 }
321 
322 
323 bool SVHWrapper::setAllForceLimits(schunk_svh_msgs::SetAllChannelForceLimits::Request& req,
324  schunk_svh_msgs::SetAllChannelForceLimits::Response& res)
325 {
326  for (size_t channel = 0; channel < driver_svh::SVH_DIMENSION; ++channel)
327  {
328  res.force_limit[channel] = setChannelForceLimit(channel, req.force_limit[channel]);
329  }
330  return true;
331 }
332 
333 bool SVHWrapper::setForceLimitById(schunk_svh_msgs::SetChannelForceLimit::Request& req,
334  schunk_svh_msgs::SetChannelForceLimit::Response& res)
335 {
336  res.force_limit = setChannelForceLimit(req.channel_id, req.force_limit);
337  return true;
338 }
339 
340 
341 float SVHWrapper::setChannelForceLimit(size_t channel, float force_limit)
342 {
343  // no force setting if ros-control-loop disabled -> no impact on diagnostic test
344  if (m_channels_enabled)
345  {
346  return m_finger_manager->setForceLimit(static_cast<driver_svh::SVHChannel>(channel),
347  force_limit);
348  }
349  else
350  {
351  return false;
352  }
353 }
SVHWrapper::m_setAllForceLimits_srv
ros::ServiceServer m_setAllForceLimits_srv
Definition: SVHWrapper.h:133
SVHWrapper::m_svh_diagnostics
std::shared_ptr< SVHDiagnostics > m_svh_diagnostics
Handle to the diagnostics test class creating a test protocol with the web gui package.
Definition: SVHWrapper.h:88
SVHWrapper::m_firmware_major_version
int m_firmware_major_version
Definition: SVHWrapper.h:122
SVHWrapper.h
DynamicParameter::Settings::home_settings_given
std::vector< bool > home_settings_given
Definition: DynamicParameterClass.h:87
SVHDiagnostics
Definition: SVHDiagnostics.h:70
SVHWrapper::m_channels_enabled
bool m_channels_enabled
m_channels_enabled enables the ros-control-loop in the hw interface
Definition: SVHWrapper.h:126
ros::NodeHandle::getParam
bool getParam(const std::string &key, bool &b) const
ROS_FATAL_STREAM
#define ROS_FATAL_STREAM(args)
SVHWrapper::enable_sub
ros::Subscriber enable_sub
Definition: SVHWrapper.h:129
SVHWrapper::enableChannelCallback
void enableChannelCallback(const std_msgs::Int8ConstPtr &channel)
Callback function to enable channels of SCHUNK five finger hand.
Definition: SVHWrapper.cpp:263
driver_svh::SVH_ALL
SVH_ALL
SVHWrapper::m_priv_nh
ros::NodeHandle m_priv_nh
private node handle
Definition: SVHWrapper.h:82
ros::NodeHandle::advertiseService
ServiceServer advertiseService(AdvertiseServiceOptions &ops)
SVHWrapper::m_home_service_joint_names
ros::ServiceServer m_home_service_joint_names
Definition: SVHWrapper.h:132
driver_svh::SVH_DIMENSION
SVH_DIMENSION
DynamicParameter::Settings::position_settings_given
std::vector< bool > position_settings_given
Definition: DynamicParameterClass.h:81
DynamicParameter::Settings::home_settings
std::vector< std::vector< float > > home_settings
Definition: DynamicParameterClass.h:86
SVHWrapper::m_finger_manager
std::shared_ptr< driver_svh::SVHFingerManager > m_finger_manager
Handle to the SVH finger manager for library access.
Definition: SVHWrapper.h:85
SVHWrapper::m_home_service_all
ros::ServiceServer m_home_service_all
Definition: SVHWrapper.h:131
SVHWrapper::connectCallback
void connectCallback(const std_msgs::Empty &)
Callback function for connecting to SCHUNK five finger hand.
Definition: SVHWrapper.cpp:256
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
SVHWrapper::homeNodesChannelIds
bool homeNodesChannelIds(schunk_svh_msgs::HomeWithChannelsRequest &req, schunk_svh_msgs::HomeWithChannelsResponse &resp)
Definition: SVHWrapper.cpp:286
SVHWrapper::m_name_prefix
std::string m_name_prefix
Definition: SVHWrapper.h:118
ros::NodeHandle::resolveName
std::string resolveName(const std::string &name, bool remap=true) const
ROS_WARN_STREAM
#define ROS_WARN_STREAM(args)
SVHWrapper::setChannelForceLimit
float setChannelForceLimit(size_t channel, float force_limit)
Definition: SVHWrapper.cpp:341
ROSLogHandler.h
SVHWrapper::setAllForceLimits
bool setAllForceLimits(schunk_svh_msgs::SetAllChannelForceLimits::Request &req, schunk_svh_msgs::SetAllChannelForceLimits::Response &res)
Definition: SVHWrapper.cpp:323
SVHWrapper::homeAllNodes
bool homeAllNodes(schunk_svh_msgs::HomeAllRequest &req, schunk_svh_msgs::HomeAllResponse &resp)
Definition: SVHWrapper.cpp:268
driver_svh::SVHCurrentSettings
ROS_ERROR
#define ROS_ERROR(...)
SVHWrapper::setForceLimitById
bool setForceLimitById(schunk_svh_msgs::SetChannelForceLimit::Request &req, schunk_svh_msgs::SetChannelForceLimit::Response &res)
Definition: SVHWrapper.cpp:333
driver_svh::SVHFirmwareInfo
SVHWrapper::setRosControlEnable
void setRosControlEnable(bool enable)
function to set the ros-control-loop enabling flag from the diagnostics class
Definition: SVHWrapper.cpp:136
SVHWrapper::initControllerParameters
void initControllerParameters(const uint16_t manual_major_version, const uint16_t manual_minor_version)
Definition: SVHWrapper.cpp:141
driver_svh::SVHHomeSettings
ros::NodeHandle::subscribe
Subscriber subscribe(const std::string &topic, uint32_t queue_size, const boost::function< void(C)> &callback, const VoidConstPtr &tracked_object=VoidConstPtr(), const TransportHints &transport_hints=TransportHints())
DynamicParameter
Class to read parameter file and set the correct hardware parameters.
Definition: DynamicParameterClass.h:49
DynamicParameter::getSettings
const Settings & getSettings() const
Definition: DynamicParameterClass.h:93
driver_svh::SVHChannel
SVHChannel
SVHWrapper::~SVHWrapper
~SVHWrapper()
Definition: SVHWrapper.cpp:201
SVHWrapper::m_serial_device_name
std::string m_serial_device_name
Serial device to use for communication with hardware.
Definition: SVHWrapper.h:91
driver_svh::SVHFirmwareInfo::version_minor
uint16_t version_minor
DynamicParameterClass.h
driver_svh::SVHFingerManager
driver_svh::setupROSLogHandler
void setupROSLogHandler(LogLevel level=LogLevel::INFO)
Definition: ROSLogHandler.cpp:83
ros::InvalidNameException
ros::NodeHandle::param
T param(const std::string &param_name, const T &default_val) const
driver_svh::SVHPositionSettings
driver_svh::SVHFirmwareInfo::version_major
uint16_t version_major
DynamicParameter::Settings::current_settings
std::vector< std::vector< float > > current_settings
Definition: DynamicParameterClass.h:83
SVHWrapper::connect
bool connect()
load parameters and try connecting
Definition: SVHWrapper.cpp:207
SVHWrapper::connect_sub
ros::Subscriber connect_sub
Definition: SVHWrapper.h:128
SVHWrapper::m_firmware_minor_version
int m_firmware_minor_version
Definition: SVHWrapper.h:123
SVHWrapper::SVHWrapper
SVHWrapper(const ros::NodeHandle &nh)
Definition: SVHWrapper.cpp:39
ROS_INFO
#define ROS_INFO(...)
SVHWrapper::m_setForceLimitById_srv
ros::ServiceServer m_setForceLimitById_srv
Definition: SVHWrapper.h:134
SVHWrapper::m_connect_retry_count
int m_connect_retry_count
Definition: SVHWrapper.h:114
XmlRpc::XmlRpcValue
ros::NodeHandle


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