franka_combinable_hw.h
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
3 #pragma once
4 
5 #include <atomic>
6 #include <functional>
7 #include <memory>
8 #include <string>
9 #include <thread>
10 
11 #include <franka/exception.h>
12 #include <franka/robot_state.h>
13 
15 #include <franka_hw/franka_hw.h>
16 #include <franka_hw/services.h>
17 #include <franka_msgs/ErrorRecoveryAction.h>
18 #include <ros/ros.h>
19 
20 namespace franka_hw {
21 
30 class FrankaCombinableHW : public FrankaHW {
31  public:
36 
45  void initROSInterfaces(ros::NodeHandle& robot_hw_nh) override;
46 
58  void control(const std::function<bool(const ros::Time&, const ros::Duration&)>&
59  ros_callback = // NOLINT (google-default-arguments)
60  [](const ros::Time&, const ros::Duration&) {
61  return true;
62  }) const override; // NOLINT (google-default-arguments)
63 
72  bool checkForConflict(const std::list<hardware_interface::ControllerInfo>& info) const override;
73 
80  void read(const ros::Time& /*time*/, const ros::Duration& /*period*/) override;
81 
88  void write(const ros::Time& /*time*/, const ros::Duration& period) override;
89 
96  std::string getArmID() const noexcept;
97 
102  void triggerError();
103 
109  bool hasError() const noexcept;
110 
114  void resetError();
115 
121  bool controllerNeedsReset() const noexcept;
122 
123  private:
124  template <typename T>
125  T libfrankaUpdateCallback(const T& command,
126  const franka::RobotState& robot_state,
127  franka::Duration time_step) {
128  if (commandHasNaN(command)) {
129  std::string error_message = "FrankaCombinableHW: Got NaN value in command!";
130  ROS_FATAL("%s", error_message.c_str());
131  throw std::invalid_argument(error_message);
132  }
134  {
135  std::lock_guard<std::mutex> state_lock(libfranka_state_mutex_);
136  robot_state_libfranka_ = robot_state;
137  }
138 
139  std::lock_guard<std::mutex> command_lock(libfranka_cmd_mutex_);
140  T current_cmd = command;
141  if (has_error_ || !controller_active_) {
142  return franka::MotionFinished(current_cmd);
143  }
144  return current_cmd;
145  }
146 
147  void publishErrorState(bool error);
148 
150 
151  void initRobot() override;
152 
153  bool setRunFunction(const ControlMode& requested_control_mode,
154  bool limit_rate,
155  double cutoff_frequency,
156  franka::ControllerMode internal_controller) override;
157 
158  void controlLoop();
159 
160  std::unique_ptr<std::thread> control_loop_thread_;
162  std::unique_ptr<actionlib::SimpleActionServer<franka_msgs::ErrorRecoveryAction>>
164  std::atomic_bool has_error_{false};
166  std::atomic_bool error_recovered_{false};
167  std::atomic_bool controller_needs_reset_{false};
168 };
169 
170 } // namespace franka_hw
#define ROS_FATAL(...)
std::unique_ptr< actionlib::SimpleActionServer< franka_msgs::ErrorRecoveryAction > > recovery_action_server_
void initRobot() override
Uses the robot_ip_ to connect to the robot via libfranka and loads the libfranka model.
std::unique_ptr< std::thread > control_loop_thread_
void triggerError()
Triggers a stop of the controlLoop.
This class serves as container that gathers all possible service interfaces to a libfranka robot inst...
Definition: services.h:55
A hardware class for a Panda robot based on the ros_control framework.
std::string getArmID() const noexcept
Getter method for the arm_id which is used to distinguish between multiple instances of FrankaCombina...
void initROSInterfaces(ros::NodeHandle &robot_hw_nh) override
Initializes the class in terms of ros_control interfaces.
virtual void checkJointLimits()
Checks the proximity of each joint to its joint position limits and prints a warning whenever a joint...
Definition: franka_hw.cpp:300
T libfrankaUpdateCallback(const T &command, const franka::RobotState &robot_state, franka::Duration time_step)
bool setRunFunction(const ControlMode &requested_control_mode, bool limit_rate, double cutoff_frequency, franka::ControllerMode internal_controller) override
Configures the run function which is used as libfranka control callback based on the requested contro...
bool hasError() const noexcept
Getter for the error flag of the class.
void control(const std::function< bool(const ros::Time &, const ros::Duration &)> &ros_callback=[](const ros::Time &, const ros::Duration &){return true;}) const override
Runs the currently active controller in a realtime loop.
void read(const ros::Time &, const ros::Duration &) override
Reads data from the franka robot.
std::mutex libfranka_cmd_mutex_
Definition: franka_hw.h:482
franka::RobotState robot_state_libfranka_
Definition: franka_hw.h:479
void resetError()
Recovers the libfranka robot, resets the error flag and publishes the error state.
void setupServicesAndActionServers(ros::NodeHandle &node_handle)
std::atomic_bool controller_active_
Definition: franka_hw.h:507
FrankaCombinableHW()
Creates an instance of FrankaCombinableHW.
void write(const ros::Time &, const ros::Duration &period) override
Writes data to the franka robot.
bool checkForConflict(const std::list< hardware_interface::ControllerInfo > &info) const override
Checks whether a requested controller can be run, based on the resources and interfaces it claims...
This class wraps the functionality of libfranka for controlling Panda robots into the ros_control fra...
Definition: franka_hw.h:38
bool controllerNeedsReset() const noexcept
Returns whether the controller needs to be reset e.g.
Torques MotionFinished(Torques command) noexcept
std::mutex libfranka_state_mutex_
Definition: franka_hw.h:477
static bool commandHasNaN(const franka::Torques &command)
Checks a command for NaN values.
Definition: franka_hw.cpp:553


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