robotiq_3f_gripper_can_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 
29 #include <ros/ros.h>
30 
31 // Used to convert seconds elapsed to nanoseconds
32 static const double BILLION = 1000000000.0;
33 
35 {
36 public:
38  const ros::NodeHandle& nh,
40  : nh_(nh), name_("generic_hw_control_loop"), hardware_interface_(hardware_interface)
41  {
42  ROS_DEBUG("creating loop");
43 
46  ROS_DEBUG("created controller manager");
47 
49  ros::NodeHandle rpsnh(nh, name_);
50 
51  loop_hz_ = rpsnh.param<double>("loop_hz", 30);
52  cycle_time_error_threshold_ = rpsnh.param<double>("cycle_time_error_threshold", 0.1);
53 
55  clock_gettime(CLOCK_MONOTONIC, &last_time_);
56 
59  non_realtime_loop_ = nh_.createTimer(desired_update_freq_, &GenericHWLoop::update, this);
60  ROS_DEBUG("created timer");
61  }
62 
68  void update(const ros::TimerEvent& e)
69  {
71  clock_gettime(CLOCK_MONOTONIC, &current_time_);
73  + (current_time_.tv_nsec - last_time_.tv_nsec) / BILLION);
75  ROS_DEBUG_STREAM_THROTTLE_NAMED(1, "GenericHWLoop","Sampled update loop with elapsed time " << elapsed_time_.toSec());
76 
78  const double cycle_time_error = (elapsed_time_ - desired_update_freq_).toSec();
79  if (cycle_time_error > cycle_time_error_threshold_)
80  {
81  ROS_WARN_STREAM_NAMED(name_, "Cycle time exceeded error threshold by: "
82  << cycle_time_error << ", cycle time: " << elapsed_time_
83  << ", threshold: " << cycle_time_error_threshold_);
84  }
85 
88 
91 
94  }
95 
96 protected:
97  // Startup and shutdown of the internal node inside a roscpp program
99 
100  // Name of this class
101  std::string name_;
102 
103  // Settings
106 
107  // Timing
110  double loop_hz_;
111  struct timespec last_time_;
112  struct timespec current_time_;
113 
121 
124 };
125 
126 int main(int argc, char** argv)
127 {
128  ros::init(argc, argv, "robotiq_3f_gripper_node");
129  ros::NodeHandle nh;
130  ros::NodeHandle pnh("~");
131 
132  // NOTE: We run the ROS loop in a separate thread as external calls such
133  // as service callbacks to load controllers can block the (main) control loop
134  ros::AsyncSpinner spinner(2);
135  spinner.start();
136 
137  std::string can_device;
138  nh.param<std::string>("can_device", can_device, "can0");
139 
141 
142  driver_.reset(new can::ThreadedSocketCANInterface());
143  ROS_INFO_STREAM("Initializing device "<<can_device<<"..");
144  while(!driver_->init(can_device, false))
145  {
146  ROS_INFO_STREAM("waiting..");
147  if(!ros::ok())
148  {
149  driver_->shutdown();
150  return 0;
151  }
152  ros::Duration(0.1).sleep();
153  }
154 
155  ROS_INFO_STREAM("device initialized");
156 
157  int can_id;
158  nh.param<int>("can_id", can_id, 0x0B);
159 
160  // Create the hw client layer
162  (new robotiq_3f_gripper_control::Robotiq3FGripperCanClient((unsigned int)can_id, driver_));
163  can_client->init(pnh);
164 
165  // Create the hw api layer
168 
169  // Create the hardware interface layer
172 
173  ROS_DEBUG("created hw interface");
174 
175  // Register interfaces
176  hardware_interface::JointStateInterface joint_state_interface;
177  hardware_interface::PositionJointInterface position_joint_interface;
178  hw_interface->configure(joint_state_interface, position_joint_interface);
179  hw_interface->registerInterface(&joint_state_interface);
180  hw_interface->registerInterface(&position_joint_interface);
181 
182  ROS_DEBUG("registered control interfaces");
183 
184  // Start the control loop
185  GenericHWLoop control_loop(pnh, hw_interface);
186  ROS_INFO("started");
187 
188  // Wait until shutdown signal recieved
190 
191  return 0;
192 }
int main(int argc, char **argv)
#define ROS_DEBUG_STREAM_THROTTLE_NAMED(rate, name, args)
bool sleep() const
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.
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
ROSCPP_DECL bool ok()
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
static const double BILLION
void update(const ros::TimerEvent &e)
Timer event Note: we do not use the TimerEvent time difference because it does NOT guarantee that the...
#define ROS_INFO_STREAM(args)
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