srh_joint_effort_controller.cpp
Go to the documentation of this file.
00001 
00029 #include "sr_mechanism_controllers/srh_joint_effort_controller.hpp"
00030 #include "angles/angles.h"
00031 #include "pluginlib/class_list_macros.h"
00032 #include <sstream>
00033 #include <math.h>
00034 #include "sr_utilities/sr_math_utils.hpp"
00035 
00036 #include <std_msgs/Float64.h>
00037 
00038 PLUGINLIB_EXPORT_CLASS(controller::SrhEffortJointController, controller_interface::ControllerBase)
00039 
00040 using namespace std;
00041 
00042 namespace controller
00043 {
00044 
00045 bool SrhEffortJointController::init(ros_ethercat_model::RobotState *robot, ros::NodeHandle &n)
00046 {
00047   ROS_ASSERT(robot);
00048   robot_ = robot;
00049   node_ = n;
00050 
00051   if (!node_.getParam("joint", joint_name_))
00052   {
00053     ROS_ERROR("No joint given (namespace: %s)", node_.getNamespace().c_str());
00054     return false;
00055   }
00056 
00057   controller_state_publisher_.reset(new realtime_tools::RealtimePublisher<control_msgs::JointControllerState>
00058                                     (node_, "state", 1));
00059 
00060   ROS_DEBUG(" --------- ");
00061   ROS_DEBUG_STREAM("Init: " << joint_name_);
00062 
00063   // joint 0s e.g. FFJ0
00064   has_j2 = is_joint_0();
00065   if (has_j2)
00066   {
00067     get_joints_states_1_2();
00068     if (!joint_state_)
00069     {
00070       ROS_ERROR("SrhEffortJointController could not find the first joint relevant to \"%s\"\n", joint_name_.c_str());
00071       return false;
00072     }
00073     if (!joint_state_2)
00074     {
00075       ROS_ERROR("SrhEffortJointController could not find the second joint relevant to \"%s\"\n", joint_name_.c_str());
00076       return false;
00077     }
00078   }
00079   else
00080   {
00081     joint_state_ = robot_->getJointState(joint_name_);
00082     if (!joint_state_)
00083     {
00084       ROS_ERROR("SrhEffortJointController could not find joint named \"%s\"\n", joint_name_.c_str());
00085       return false;
00086     }
00087   }
00088 
00089   friction_compensator.reset(new sr_friction_compensation::SrFrictionCompensator(joint_name_));
00090 
00091   serve_set_gains_ = node_.advertiseService("set_gains", &SrhEffortJointController::setGains, this);
00092   serve_reset_gains_ = node_.advertiseService("reset_gains", &SrhEffortJointController::resetGains, this);
00093 
00094   ROS_DEBUG_STREAM(" joint_state name: " << joint_state_->joint_->name);
00095   ROS_DEBUG_STREAM(" In Init: " << getJointName() << " This: " << this
00096                    << " joint_state: " << joint_state_);
00097 
00098   after_init();
00099   return true;
00100 }
00101 
00102 void SrhEffortJointController::starting(const ros::Time& time)
00103 {
00104   command_ = 0.0;
00105   read_parameters();
00106 }
00107 
00108 bool SrhEffortJointController::setGains(sr_robot_msgs::SetEffortControllerGains::Request &req,
00109                                         sr_robot_msgs::SetEffortControllerGains::Response &resp)
00110 {
00111   max_force_demand = req.max_force;
00112   friction_deadband = req.friction_deadband;
00113 
00114   //Setting the new parameters in the parameter server
00115   node_.setParam("max_force", max_force_demand);
00116   node_.setParam("friction_deadband", friction_deadband);
00117 
00118   return true;
00119 }
00120 
00121 bool SrhEffortJointController::resetGains(std_srvs::Empty::Request& req, std_srvs::Empty::Response& resp)
00122 {
00123   command_ = 0.0;
00124 
00125   read_parameters();
00126 
00127   if (has_j2)
00128     ROS_WARN_STREAM("Reseting controller gains: " << joint_state_->joint_->name << " and " << joint_state_2->joint_->name);
00129   else
00130     ROS_WARN_STREAM("Reseting controller gains: " << joint_state_->joint_->name);
00131 
00132   return true;
00133 }
00134 
00135 void SrhEffortJointController::getGains(double &p, double &i, double &d, double &i_max, double &i_min)
00136 {
00137 }
00138 
00139 void SrhEffortJointController::update(const ros::Time& time, const ros::Duration& period)
00140 {
00141   if (!has_j2 &&!joint_state_->calibrated_)
00142     return;
00143 
00144   ROS_ASSERT(robot_);
00145   ROS_ASSERT(joint_state_->joint_);
00146 
00147   if (!initialized_)
00148   {
00149     initialized_ = true;
00150     command_ = 0.0;
00151   }
00152 
00153   //The commanded effort is the error directly:
00154   // the PID loop for the force controller is running on the
00155   // motorboard.
00156   double commanded_effort = command_;
00157 
00158   //Clamps the effort
00159   commanded_effort = min(commanded_effort, (max_force_demand * max_force_factor_));
00160   commanded_effort = max(commanded_effort, -(max_force_demand * max_force_factor_));
00161 
00162   //Friction compensation
00163   if (has_j2)
00164     commanded_effort += friction_compensator->friction_compensation(joint_state_->position_ + joint_state_2->position_, joint_state_->velocity_ + joint_state_2->velocity_, int(commanded_effort), friction_deadband);
00165   else
00166     commanded_effort += friction_compensator->friction_compensation(joint_state_->position_, joint_state_->velocity_, int(commanded_effort), friction_deadband);
00167 
00168   joint_state_->commanded_effort_ = commanded_effort;
00169 
00170   if (loop_count_ % 10 == 0)
00171   {
00172     if (controller_state_publisher_ && controller_state_publisher_->trylock())
00173     {
00174       controller_state_publisher_->msg_.header.stamp = time;
00175       controller_state_publisher_->msg_.set_point = command_;
00176       controller_state_publisher_->msg_.process_value = joint_state_->effort_;
00177       //TODO: compute the derivative of the effort.
00178       controller_state_publisher_->msg_.process_value_dot = -1.0;
00179       controller_state_publisher_->msg_.error = commanded_effort - joint_state_->effort_;
00180       controller_state_publisher_->msg_.time_step = period.toSec();
00181       controller_state_publisher_->msg_.command = commanded_effort;
00182 
00183       double dummy;
00184       getGains(controller_state_publisher_->msg_.p,
00185                controller_state_publisher_->msg_.i,
00186                controller_state_publisher_->msg_.d,
00187                controller_state_publisher_->msg_.i_clamp,
00188                dummy);
00189       controller_state_publisher_->unlockAndPublish();
00190     }
00191   }
00192   loop_count_++;
00193 }
00194 
00195 void SrhEffortJointController::read_parameters()
00196 {
00197   node_.param<double>("max_force", max_force_demand, 1023.0);
00198   node_.param<int>("friction_deadband", friction_deadband, 5);
00199 }
00200 
00201 void SrhEffortJointController::setCommandCB(const std_msgs::Float64ConstPtr& msg)
00202 {
00203   command_ = msg->data;
00204 }
00205 }
00206 
00207 /* For the emacs weenies in the crowd.
00208 Local Variables:
00209    c-basic-offset: 2
00210 End:
00211  */


sr_mechanism_controllers
Author(s): Ugo Cupcic
autogenerated on Fri Aug 21 2015 12:26:14