pose_based_vs_alg.cpp
Go to the documentation of this file.
00001 #include "pose_based_vs_alg.h"
00002 
00003 PoseBasedVsAlgorithm::PoseBasedVsAlgorithm(void)
00004 {
00005 }
00006 
00007 PoseBasedVsAlgorithm::~PoseBasedVsAlgorithm(void)
00008 {
00009 }
00010 
00011 void PoseBasedVsAlgorithm::config_update(Config& new_cfg, uint32_t level)
00012 {
00013   this->lock();
00014 
00015   // save the current configuration
00016   this->config_=new_cfg;
00017   
00018   this->unlock();
00019 }
00020 
00021 // PoseBasedVsAlgorithm Public API
00022 void PoseBasedVsAlgorithm::pose_vs(const std::vector<double>& current_pose,const std::vector<double>& desired_pose,const std::vector<double>& kp,const std::vector<double>& kd,const std::vector<double>& ki, const double& i_lim, const double& dt,std::vector<double>& cam_vel)
00023 {
00024         this->current_pose_=current_pose;
00025   this->desired_pose_=desired_pose;
00026   this->kp_=kp;
00027   this->kd_=kd;
00028   this->ki_=ki;
00029   this->i_lim_=i_lim;
00030   this->dt_=dt;
00031     
00032   //pose error computation
00033   double error;
00034 
00035   //Initializations
00036   this->p_error_last_.resize(6,0.0);
00037   this->p_error_.resize(6,0.0);
00038   this->d_error_.resize(6,0.0);
00039   this->i_error_.resize(6,0.0);
00040   this->kp_.resize(6,0.0);
00041   this->kd_.resize(6,0.0);
00042   this->ki_.resize(6,0.0);
00043     
00044   for (unsigned int i = 0; i < this->current_pose_.size(); ++i)
00045   {
00046         //compute the error     
00047         error = this->desired_pose_[i] - this->current_pose_[i];
00048         //compute velocities with the PID
00049         cam_vel.at(i)=updatePid(error, this->dt_, i);  
00050   }
00051 }
00052 
00053 
00054 double PoseBasedVsAlgorithm::updatePid(double error, double dt, double i)
00055 {
00056 
00057   double p_term, d_term, i_term;  //Proportional, derivative and integral terms.
00058   double i_max = this->i_lim_;   //Maximum allowable integral term.
00059   double i_min = -(this->i_lim_);   //Minimum allowable integral term.
00060   this->p_error_.at(i) = error; //this is pError = pState-pTarget
00061         
00062   if (dt == 0.0 || std::isnan(error) || std::isinf(error))
00063         return 0.0;
00064         // Calculate proportional contribution to command
00065   p_term = this->kp_[i] * p_error_[i];
00066 
00067         // Calculate the integral error
00068   this->i_error_.at(i) = this->i_error_[i] + dt * this->p_error_[i];
00069 
00070         //Calculate integral contribution to command
00071   i_term = this->ki_[i] * this->i_error_[i];
00072 
00073         // Limit i_term so that the limit is meaningful in the output
00074         if (i_term > i_max)
00075         {
00076         i_term = i_max;
00077         this->i_error_.at(i)=i_term/this->ki_[i];
00078         }
00079         else if (i_term < i_min)
00080   {
00081         i_term = i_min;
00082         this->i_error_.at(i)=i_term/this->ki_[i];
00083         }
00084         // Calculate the derivative error
00085   if (dt != 0.0)
00086   {
00087         this->d_error_.at(i) = (this->p_error_[i] - this->p_error_last_[i]) / dt;
00088         this->p_error_last_.at(i) = this->p_error_[i];
00089   }
00090   // Calculate derivative contribution to command
00091   d_term = this->kd_[i] * this->d_error_[i];
00092   this->cmd_ = -p_term - i_term - d_term;
00093         return cmd_;
00094 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Friends


iri_pose_based_vs
Author(s): asantamaria
autogenerated on Wed Jul 24 2013 10:27:12