franka_combined_hw.cpp
Go to the documentation of this file.
1 // Copyright (c) 2019 Franka Emika GmbH
2 // Use of this source code is governed by the Apache-2.0 license, see LICENSE
4 
5 #include <algorithm>
6 #include <memory>
7 
9 #include <ros/node_handle.h>
10 #include <ros/time.h>
11 
13 #include <franka_hw/franka_hw.h>
14 #include <franka_msgs/ErrorRecoveryAction.h>
15 
16 namespace franka_hw {
17 
19 
21  bool success = CombinedRobotHW::init(root_nh, robot_hw_nh);
22  // Error recovery server for all FrankaHWs
24  std::make_unique<actionlib::SimpleActionServer<franka_msgs::ErrorRecoveryAction>>(
25  robot_hw_nh, "error_recovery",
26  [&](const franka_msgs::ErrorRecoveryGoalConstPtr&) {
27  try {
28  is_recovering_ = true;
29  for (const auto& robot_hw : robot_hw_list_) {
30  auto* franka_combinable_hw_ptr =
31  dynamic_cast<franka_hw::FrankaCombinableHW*>(robot_hw.get());
32  if (franka_combinable_hw_ptr != nullptr) {
33  franka_combinable_hw_ptr->resetError();
34  } else {
35  ROS_ERROR(
36  "FrankaCombinedHW: dynamic_cast from RobotHW to FrankaCombinableHW failed.");
37  is_recovering_ = false;
39  franka_msgs::ErrorRecoveryResult(),
40  "dynamic_cast from RobotHW to FrankaCombinableHW failed");
41  return;
42  }
43  }
44  is_recovering_ = false;
45  combined_recovery_action_server_->setSucceeded();
46  } catch (const franka::Exception& ex) {
47  is_recovering_ = false;
48  combined_recovery_action_server_->setAborted(franka_msgs::ErrorRecoveryResult(),
49  ex.what());
50  }
51  },
52  false);
54  return success;
55 }
56 
57 void FrankaCombinedHW::read(const ros::Time& time, const ros::Duration& period) {
58  // Call the read method of the single RobotHW objects.
59  CombinedRobotHW::read(time, period);
60  handleError();
61 }
62 
64  // Check if any of the RobotHW object needs a controller reset
65  bool controller_reset = false;
66  for (const auto& robot_hw : robot_hw_list_) {
67  auto* franka_combinable_hw_ptr = dynamic_cast<franka_hw::FrankaCombinableHW*>(robot_hw.get());
68  if (franka_combinable_hw_ptr != nullptr) {
69  controller_reset = controller_reset || franka_combinable_hw_ptr->controllerNeedsReset();
70  } else {
71  ROS_ERROR("FrankaCombinedHW: dynamic_cast from RobotHW to FrankaCombinableHW failed.");
72  return false;
73  }
74  }
75  return controller_reset;
76 }
77 
79  // Trigger error state of all other RobotHW objects when one of them has a error.
80  if (hasError() && !is_recovering_) {
81  triggerError();
82  }
83 }
84 
86  bool has_error = false;
87  for (const auto& robot_hw : robot_hw_list_) {
88  auto* franka_combinable_hw_ptr = dynamic_cast<franka_hw::FrankaCombinableHW*>(robot_hw.get());
89  if (franka_combinable_hw_ptr != nullptr) {
90  has_error = has_error || franka_combinable_hw_ptr->hasError();
91  } else {
92  ROS_ERROR("FrankaCombinedHW: dynamic_cast from RobotHW to FrankaCombinableHW failed.");
93  return false;
94  }
95  }
96  return has_error;
97 }
98 
100  // Trigger error state of all RobotHW objects.
101  for (const auto& robot_hw : robot_hw_list_) {
102  auto* franka_combinable_hw_ptr = dynamic_cast<franka_hw::FrankaCombinableHW*>(robot_hw.get());
103  if (franka_combinable_hw_ptr != nullptr) {
104  franka_combinable_hw_ptr->triggerError();
105  } else {
106  ROS_ERROR("FrankaCombinedHW: dynamic_cast from RobotHW to FrankaCombinableHW failed.");
107  }
108  }
109 }
110 
111 } // namespace franka_hw
std::vector< hardware_interface::RobotHWSharedPtr > robot_hw_list_
void triggerError()
Triggers a stop of the controlLoop.
bool controllerNeedsReset()
Checks whether the controller needs to be reset.
A hardware class for a Panda robot based on the ros_control framework.
bool hasError() const noexcept
Getter for the error flag of the class.
bool init(ros::NodeHandle &root_nh, ros::NodeHandle &robot_hw_nh) override
The init function is called to initialize the CombinedFrankaHW from a non-realtime thread...
void resetError()
Recovers the libfranka robot, resets the error flag and publishes the error state.
FrankaCombinedHW()
Creates an instance of CombinedFrankaHW.
std::unique_ptr< actionlib::SimpleActionServer< franka_msgs::ErrorRecoveryAction > > combined_recovery_action_server_
bool controllerNeedsReset() const noexcept
Returns whether the controller needs to be reset e.g.
void read(const ros::Time &time, const ros::Duration &period) override
Reads data from the robot HW.
#define ROS_ERROR(...)


franka_hw
Author(s): Franka Emika GmbH
autogenerated on Fri Oct 23 2020 03:47:05