esc_classic.cpp
Go to the documentation of this file.
00001 /*
00002  * esc_classic.cpp
00003  *
00004  *  Created on: Dec 15, 2014
00005  *      Author: Filip Mandic
00006  */
00007 
00008 #include <labust/control/esc/EscClassic.hpp>
00009 
00010 #include <ros/ros.h>
00011 
00012 namespace labust{
00013         namespace control{
00014                 namespace esc{
00015 
00016                         typedef EscClassic Base;
00017 
00018                         Base::EscClassic(int ctrlNum, numericprecission Ts):EscPerturbationBase<double>(ctrlNum, Ts),
00019                                                                                 state_(vector::Zero(controlNum)){
00020 
00021 
00022                                 lpf_out_old_.resize(controlNum);
00023                                 signal_demodulated_old_.resize(controlNum);
00024                                 comp_old_.resize(controlNum);
00025                                 corr_signal_.resize(controlNum);
00026                                 control_ref_.resize(controlNum);
00027 
00028                                 sin_amp_.resize(controlNum);
00029                                 sin_freq_.resize(controlNum);
00030                                 gain_.resize(controlNum);
00031                                 control_.resize(controlNum);
00032                                 low_pass_pole_.resize(controlNum);
00033                                 comp_pole_.resize(controlNum);
00034                                 comp_zero_.resize(controlNum);
00035 
00036                                 lpf_out_old_.setZero();
00037                                 hpf_out_old_ = 0;
00038                                 control_ref_ = state_;
00039                                 lpf_out_old_.setZero();
00040                                 signal_demodulated_old_.setZero();
00041                                 comp_old_.setZero();
00042                                 corr_signal_.setZero();
00043 
00044                                 phase_shift_.resize(controlNum);
00045                                 phase_shift_[0] = 0;
00046                                 for (size_t i = 1; i<controlNum; i++){
00047                                         phase_shift_[i] = i*M_PI/((double)controlNum);
00048                                 }
00049                                 state_initialized_ = true;
00050 
00051                         }
00052 
00053                         Base::~EscClassic(){
00054 
00055                         }
00056 
00057                         void Base::initController(double sin_amp, double sin_freq, double corr_gain, double high_pass_pole, double low_pass_pole, double comp_zero, double comp_pole, double Ts){
00058                                 sin_amp_.setConstant(sin_amp);
00059                                 sin_freq_.setConstant(sin_freq);
00060                                 gain_.setConstant(corr_gain);
00061                                 high_pass_pole_ = high_pass_pole;
00062                                 low_pass_pole_.setConstant(low_pass_pole);
00063                                 comp_pole_.setConstant(comp_pole);
00064                                 comp_zero_.setConstant(comp_zero);
00065                                 Ts_ = Ts;
00066                                 obj_val_old_ = 0;
00067                                 cycle_count_ = 0;
00068                                 hpf_out_old_ = 0;
00069                                 //opt_dim_ = 0;
00070                                 state_initialized_ = false;
00071                                 old_vals_initialized_ = false;
00072                                 initialized_ = true;
00073                         }
00074                         Base::numericprecission Base::preFiltering(numericprecission cost_signal){
00075 
00076                                  // Check this limit
00077                                 numericprecission filtered_cost = (-(Ts_*high_pass_pole_-2)*pre_filter_output_old_+2*cost_signal-2*pre_filter_input_old_)/(2+high_pass_pole_*Ts_);
00078                                 ROS_ERROR("filtered_cost: %f", filtered_cost);
00079 
00080                                 filtered_cost = (abs(filtered_cost)> 0.5)?filtered_cost/abs(filtered_cost)*0.5:filtered_cost;
00081 
00082                                 ROS_ERROR("filtered_cost after limit: %f", filtered_cost);
00083 
00084                                 return filtered_cost;
00085                         }
00086 
00087                         Base::vector Base::gradientEstimation(numericprecission cost_signal_filtered, vector additional_input){
00088 
00089                                 vector signal_demodulated(controlNum);
00090 
00091                                 for (size_t i = 0; i<controlNum; i++){
00092 
00093                                         signal_demodulated(i)= cost_signal_filtered*sin_amp_(i)*std::sin(double(cycle_count_*Ts_*sin_freq_(i) + phase_shift_(i)));
00094                                 }
00095 
00096                                 return signal_demodulated;
00097                         }
00098 
00099                         Base::vector Base::postFiltering(vector estimated_gradient){
00100 
00101                                 vector lpf_out(controlNum);
00102                                 vector comp_out(controlNum);
00103 
00104                                 for (size_t i = 0; i<controlNum; i++){
00105 
00106                                         if(low_pass_pole_[i] == 0)
00107                                                 lpf_out[i] = estimated_gradient[i];
00108                                         else
00109                                                 lpf_out[i] = ((2.0-low_pass_pole_[i]*Ts_)*lpf_out_old_[i]+low_pass_pole_[i]*Ts_*estimated_gradient[i]+low_pass_pole_[i]*Ts_*estimated_gradient_old_[i])/(2.0+low_pass_pole_[i]*Ts_);
00110 
00111                                         if(comp_pole_[i] == 0)
00112                                                 comp_out[i] = lpf_out[i];
00113                                         else
00114                                                 comp_out[i]= ((2.0+Ts_*comp_zero_[i])*lpf_out[i]+(Ts_*comp_zero_[i]-2.0)*lpf_out_old_[i]-(Ts_*comp_pole_[i]-2.0)*comp_old_[i])/(2.0+Ts_*comp_pole_[i]);
00115 
00116                                         lpf_out_old_[i] = lpf_out[i];
00117                                         comp_old_[i] = comp_out[i];
00118                                 }
00119 
00120                                 return comp_out;
00121                         }
00122 
00123                         Base::vector Base::controllerGain(vector postFiltered){
00124 
00125                                 ROS_ERROR("GAIN:");
00126                                 ROS_ERROR_STREAM(gain_);
00127 
00128                                 control_ = gain_.cwiseProduct(postFiltered);
00129                                 return control_;
00130                         }
00131 
00132                         Base::vector Base::superimposePerturbation(Base::vector control){
00133 
00134                                 for (size_t i = 0; i<controlNum; i++){
00135                                         if(!old_vals_initialized_)
00136                                                 control_ref_[i] = sin_amp_[i]*std::sin(double(cycle_count_*Ts_*sin_freq_[i] + phase_shift_[i]+M_PI/2));
00137                                         else
00138                                                 control_ref_[i] = control[i]+sin_amp_[i]*std::sin(double(cycle_count_*Ts_*sin_freq_[i] + phase_shift_[i]+M_PI/2));
00139                                 }
00140 
00141                                 return control_ref_;
00142                         }
00143                 }
00144         }
00145 }
00146 
00147 
00148 
00149 
00150 


labust_control
Author(s): Gyula Nagy
autogenerated on Fri Aug 28 2015 11:22:42