srh_fake_joint_calibration_controller.cpp
Go to the documentation of this file.
1 
28 #include "ros/time.h"
30 #include <boost/algorithm/string.hpp>
31 #include <string>
32 #include <std_srvs/Empty.h>
33 
35 
36 using std::string;
37 
38 namespace controller
39 {
40  SrhFakeJointCalibrationController::SrhFakeJointCalibrationController()
41  : robot_(NULL),
42  last_publish_time_(0),
43  calibration_state_(IS_INITIALIZED),
44  actuator_(NULL),
45  joint_(NULL)
46  {
47  }
48 
49  bool SrhFakeJointCalibrationController::init(ros_ethercat_model::RobotStateInterface *robot, ros::NodeHandle &n)
50  {
51  ROS_ASSERT(robot);
52 
53  std::string robot_state_name;
54  node_.param<std::string>("robot_state_name", robot_state_name, "unique_robot_hw");
55 
56  try
57  {
58  robot_ = robot->getHandle(robot_state_name).getState();
59  }
61  {
62  ROS_ERROR_STREAM("Could not find robot state: " << robot_state_name << " Not loading the controller. " <<
63  e.what());
64  return false;
65  }
66 
67  node_ = n;
68 
69  // robot_id robot_id_, joint_prefix_, ns_
70  if (node_.getParam("robot_id", robot_id_)
71  && (!robot_id_.empty()))
72  {
73  joint_prefix_ = robot_id_ + "_";
74  ns_ = robot_id_ + "/";
75  }
76 
77 
78  // Joint
79  if (!node_.getParam("joint", joint_name_))
80  {
81  ROS_ERROR("No joint given (namespace: %s)", node_.getNamespace().c_str());
82  return false;
83  }
84  if (!(joint_ = robot_->getJointState(joint_prefix_ + joint_name_)))
85  {
86  ROS_ERROR("Could not find joint %s (namespace: %s)",
87  (joint_prefix_ + joint_name_).c_str(), node_.getNamespace().c_str());
88  return false;
89  }
90 
91  // Actuator
92  if (!node_.getParam("actuator", actuator_name_))
93  {
94  ROS_ERROR("No actuator given (namespace: %s)", node_.getNamespace().c_str());
95  return false;
96  }
97  if (!(actuator_ = robot_->getActuator(joint_prefix_ + actuator_name_)))
98  {
99  ROS_ERROR("Could not find actuator %s (namespace: %s)",
100  (joint_prefix_ + actuator_name_).c_str(), node_.getNamespace().c_str());
101  return false;
102  }
103 
104  // Transmission
105  string transmission_name;
106  if (!node_.getParam("transmission", transmission_name))
107  {
108  ROS_ERROR("No transmission given (namespace: %s)", node_.getNamespace().c_str());
109  return false;
110  }
111 
112  // "Calibrated" topic
113  pub_calibrated_.reset(
115 
116  return true;
117  }
118 
119 
121  {
124 
125  switch (calibration_state_)
126  {
127  case IS_INITIALIZED:
129  break;
130  case BEGINNING:
131  initialize_pids();
132  joint_->calibrated_ = true;
134  // We add the following line to delay for some time the first publish and allow the
135  // correct initialization of the subscribers in calibrate.py
136  last_publish_time_ = robot_->getTime();
137  break;
138  case CALIBRATED:
139  if (pub_calibrated_)
140  {
141  if (last_publish_time_ + ros::Duration(0.5) < robot_->getTime())
142  {
144  if (pub_calibrated_->trylock())
145  {
146  last_publish_time_ = robot_->getTime();
147  pub_calibrated_->unlockAndPublish();
148  }
149  }
150  }
151  break;
152  }
153  }
154 
156  {
157  // Reset the motor to make sure we have the proper 0 + correct PID settings
158  // trim any prefix in the actuator_name for low level driver to find it
159  std::string lowlevel_actuator_name = actuator_name_.substr(actuator_name_.size() - 4, 4);
160  ros::NodeHandle local_node("~");
161 
162  string service_name = ns_ + "reset_motor_" + boost::to_upper_copy(lowlevel_actuator_name);
163  ros::ServiceClient reset_service = local_node.serviceClient<std_srvs::Empty>(service_name.c_str());
164 
165  if (reset_service.waitForExistence(ros::Duration(2.0)))
166  {
167  std_srvs::Empty srv;
168  if (reset_service.call(srv))
169  {
170  return;
171  }
172  else
173  {
174  ROS_WARN("Reset failed: %s", service_name.c_str());
175  }
176  }
177  }
178 
179 } // namespace controller
180 
181 /* For the emacs weenies in the crowd.
182 Local Variables:
183  c-basic-offset: 2
184 End:
185 */
ServiceClient serviceClient(const std::string &service_name, bool persistent=false, const M_string &header_values=M_string())
bool call(MReq &req, MRes &res)
#define ROS_WARN(...)
virtual void update(const ros::Time &time, const ros::Duration &period)
bool waitForExistence(ros::Duration timeout=ros::Duration(-1))
boost::scoped_ptr< realtime_tools::RealtimePublisher< std_msgs::Empty > > pub_calibrated_
bool param(const std::string &param_name, T &param_val, const T &default_val) const
const std::string & getNamespace() const
virtual bool init(ros_ethercat_model::RobotStateInterface *robot, ros::NodeHandle &n)
bool getParam(const std::string &key, std::string &s) const
#define ROS_ERROR_STREAM(args)
#define ROS_ASSERT(cond)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)
#define ROS_ERROR(...)
A Fake joint calibration controller. Only loads the force pid settings from the parameter server...


sr_mechanism_controllers
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:58