robotiq_3f_gripper_ethercat_node.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016, Toyota Research Institute. All rights reserved.
2 
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 
6 // 1. Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 
9 // 2. Redistributions in binary form must reproduce the above copyright
10 // notice, this list of conditions and the following disclaimer in the
11 // documentation and/or other materials provided with the distribution.
12 
13 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 // POSSIBILITY OF SUCH DAMAGE.
24 
30 #include <ros/ros.h>
31 
32 // Used to convert seconds elapsed to nanoseconds
33 static const double BILLION = 1000000000.0;
34 
35 class GenericHWLoop
36 {
37 public:
39  const ros::NodeHandle& nh,
41  : nh_(nh), name_("generic_hw_control_loop"), hardware_interface_(hardware_interface)
42  {
43  ROS_DEBUG("creating loop");
44 
47  ROS_DEBUG("created controller manager");
48 
50  ros::NodeHandle rpsnh(nh, name_);
51 
52  loop_hz_ = rpsnh.param<double>("loop_hz", 30);
53  cycle_time_error_threshold_ = rpsnh.param<double>("cycle_time_error_threshold", 0.1);
54 
56  clock_gettime(CLOCK_MONOTONIC, &last_time_);
57 
60  non_realtime_loop_ = nh_.createTimer(desired_update_freq_, &GenericHWLoop::update, this);
61  ROS_DEBUG("created timer");
62  }
63 
69  void update(const ros::TimerEvent& e)
70  {
72  clock_gettime(CLOCK_MONOTONIC, &current_time_);
74  + (current_time_.tv_nsec - last_time_.tv_nsec) / BILLION);
76  ROS_DEBUG_STREAM_THROTTLE_NAMED(1, "GenericHWLoop","Sampled update loop with elapsed time " << elapsed_time_.toSec());
77 
79  const double cycle_time_error = (elapsed_time_ - desired_update_freq_).toSec();
80  if (cycle_time_error > cycle_time_error_threshold_)
81  {
82  ROS_WARN_STREAM_NAMED(name_, "Cycle time exceeded error threshold by: "
83  << cycle_time_error << ", cycle time: " << elapsed_time_
84  << ", threshold: " << cycle_time_error_threshold_);
85  }
86 
89 
92 
95  }
96 
97 protected:
98  // Startup and shutdown of the internal node inside a roscpp program
100 
101  // Name of this class
102  std::string name_;
103 
104  // Settings
107 
108  // Timing
111  double loop_hz_;
112  struct timespec last_time_;
113  struct timespec current_time_;
114 
122 
125 };
126 
127 int main(int argc, char** argv)
128 {
129 
132  ros::init(argc, argv, "robotiq_3f_gripper_node");
133  ros::NodeHandle nh;
134  ros::NodeHandle pnh("~");
135 
136  // NOTE: We run the ROS loop in a separate thread as external calls such
137  // as service callbacks to load controllers can block the (main) control loop
138  ros::AsyncSpinner spinner(2);
139  spinner.start();
140 
141  // Parameter names
142  std::string ifname;
143  int slave_no;
144  bool activate;
145 
146  nh.param<std::string>("ifname", ifname, "eth0");
147  nh.param<int>("slave_number", slave_no, 1);
148  nh.param<bool>("activate", activate, true);
149 
150  // Start ethercat manager
151  EtherCatManager manager(ifname);
152 
153  // Create the hw client layer
156  ethercat_client->init(pnh);
157 
158  // Create the hw api layer
161 
162  // Create the hardware interface layer
165 
166  ROS_DEBUG("created hw interface");
167 
168  // Register interfaces
169  hardware_interface::JointStateInterface joint_state_interface;
170  hardware_interface::PositionJointInterface position_joint_interface;
171  hw_interface->configure(joint_state_interface, position_joint_interface);
172  hw_interface->registerInterface(&joint_state_interface);
173  hw_interface->registerInterface(&position_joint_interface);
174 
175  ROS_DEBUG("registered control interfaces");
176 
177  // Start the control loop
178  GenericHWLoop control_loop(pnh, hw_interface);
179  ROS_INFO("started");
180 
181  // Wait until shutdown signal recieved
183 
184  return 0;
185 }
186 
#define ROS_DEBUG_STREAM_THROTTLE_NAMED(rate, name, args)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
boost::shared_ptr< controller_manager::ControllerManager > controller_manager_
ROS Controller Manager and Runner.
int main(int argc, char **argv)
GenericHWLoop(const ros::NodeHandle &nh, boost::shared_ptr< robotiq_3f_gripper_control::Robotiq3FGripperHWInterface > hardware_interface)
#define ROS_INFO(...)
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
void update(const ros::TimerEvent &e)
Timer event Note: we do not use the TimerEvent time difference because it does NOT guarantee that the...
static const double BILLION
static Time now()
This class provides a client for the EtherCAT manager object that can translate robot input/output me...
boost::shared_ptr< robotiq_3f_gripper_control::Robotiq3FGripperHWInterface > hardware_interface_
Abstract Hardware Interface for your robot.
ROSCPP_DECL void waitForShutdown()
#define ROS_WARN_STREAM_NAMED(name, args)
#define ROS_DEBUG(...)


robotiq_3f_gripper_control
Author(s): Nicolas Lauzier (Robotiq inc.), Allison Thackston
autogenerated on Tue Jun 1 2021 02:29:58