esc_ekf_grad_model.cpp
Go to the documentation of this file.
00001 /*
00002  * esc_ekf_grad_model.cpp
00003  *
00004  *  Created on: Jan 13, 2015
00005  *      Author: Filip Mandic
00006  */
00007 
00008 #include <labust/control/esc/EscEKFModel.hpp>
00009 
00010 #include <ros/ros.h>
00011 
00012 namespace labust{
00013         namespace control{
00014                 namespace esc{
00015 
00016                         typedef EscEkfGradModel Base;
00017 
00018                         Base::EscEkfGradModel(int ctrlNum, numericprecission Ts):EscPerturbationBase<double>(ctrlNum, Ts),
00019                                                                                 state_(vector::Zero(controlNum)),
00020                                                                                 newCost(false){
00021 
00022                                 signal_demodulated_old_.resize(controlNum);
00023                                 control_ref_.resize(controlNum);
00024                                 sin_amp_.resize(controlNum);
00025                                 sin_freq_.resize(controlNum);
00026                                 gain_.resize(controlNum);
00027                                 control_.resize(controlNum);
00028 
00029                                 control_ref_ = state_;
00030                                 signal_demodulated_old_.setZero();
00031 
00032                                 A = matrix::Identity(3,3);
00033                                 L = matrix::Identity(3,3);
00034                                 H = matrix::Zero(3,3);
00035                                 M = matrix::Identity(3,3);
00036 
00037                                 Pk_plu = 1.0e-3*matrix::Identity(3,3);
00038                                 xk_plu = vector::Zero(3);
00039 
00040                                 //Qk = 1e-0*diag([0.75 0.75 0.5]); % Process noise vector OVO JE DOBRO 2
00041                                 //Rk = 1e-0*diag([1 1 1]); % Measurement noise vector
00042 
00043                                 Eigen::Vector3d tmp;
00044                                 tmp << 1, 1, 5;
00045                                 Q = tmp.asDiagonal();
00046                                 tmp << 0.1, 0.1, 0.1;
00047                                 R = tmp.asDiagonal();
00048 
00049                                 n1 = vector::Zero(3);
00050                                 n2 = vector::Zero(3);
00051                                 yk = vector::Zero(3);
00052                                 input = vector::Zero(8);
00053 
00054                                 phase_shift_.resize(controlNum);
00055                                 phase_shift_[0] = 0;
00056 
00057                                 for (size_t i = 1; i<controlNum; i++){
00058                                         phase_shift_[i] = i*M_PI/((double)controlNum);
00059                                 }
00060                                 state_initialized_ = true;
00061                         }
00062 
00063                         Base::~EscEkfGradModel(){
00064 
00065                         }
00066 
00067                         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, vector Q0, vector R0){
00068 
00069                                 sin_amp_.setConstant(sin_amp);
00070                                 sin_freq_.setConstant(sin_freq);
00071                                 gain_.setConstant(corr_gain);
00072                                 Ts_ = Ts;
00073                                 cycle_count_ = 0;
00074 
00075                                 Q = Q0.asDiagonal();
00076                                 R = R0.asDiagonal();
00077 
00078                                 state_initialized_ = false;
00079                                 old_vals_initialized_ = false;
00080                                 initialized_ = true;
00081                         }
00082 
00083 
00084                         Base::vector Base::gradientEstimation(numericprecission cost_signal_filtered, vector additional_input){
00085 
00086                                 vector signal_demodulated(controlNum);
00087 
00088                                 /*** Check if there is a new cost function value available ***/
00089                                 newCost = (cost_signal_filtered == pre_filter_output_old_)?false:true;
00090 
00091                                 input << additional_input(0), n1(0), n2(0), additional_input(1), n1(1), n2(1), additional_input(2), additional_input(3);
00092 
00093                                 if(newCost){
00094 
00095                                         yk << cost_signal_filtered, n1(2), n2(2);
00096 
00097                                         n2 = n1;
00098                                         n1 << additional_input.head(2), cost_signal_filtered;
00099 
00100                                         H << input(0), input(3), 1,
00101                                                  input(1), input(4), 1,
00102                                                  input(2), input(5), 1;
00103 
00104                                         ROS_ERROR("MJERENJE");
00105                                 }else{
00106 
00107                                         ROS_ERROR("NEMA MJERENJA");
00108 
00109                                         H << 0, 0, 0,
00110                                                  0, 0, 0,
00111                                                  0, 0, 0;
00112                                 }
00113 
00114                                 Pk_min = A*Pk_plu*A.transpose() + L*Q*L.transpose();
00115                                 xk_min =  modelUpdate(xk_plu, input);
00116 
00117                                 hk = outputUpdate(xk_min, input);
00118                                 matrix tmp = H*Pk_min*H.transpose()+M*R*M.transpose();
00119                                 Kk = (Pk_min*H.transpose())*tmp.inverse();
00120 
00121                                 xk_plu = xk_min+Kk*(yk-hk);
00122                                 Pk_plu = (matrix::Identity(3,3)-Kk*H)*Pk_min;
00123                                 input_past = input;
00124 
00125                                 signal_demodulated << xk_plu(0), xk_plu(1);
00126                                 return signal_demodulated;
00127 
00128                         }
00129 
00130 
00131                         Base::vector Base::controllerGain(vector postFiltered){
00132                                 control_ = gain_.cwiseProduct(postFiltered);
00133                                 ROS_ERROR("GAIN");
00134                                 ROS_ERROR_STREAM(gain_);
00135                                 ROS_ERROR("CONTROL");
00136                                 ROS_ERROR_STREAM(control_);
00137                                 return control_;
00138 
00139                         }
00140 
00141                         Base::vector Base::superimposePerturbation(Base::vector control){
00142 
00143                                 for (size_t i = 0; i<controlNum; i++){
00144                                         if(!old_vals_initialized_)
00145                                                 control_ref_[i] = sin_amp_[i]*std::sin(double(cycle_count_*Ts_*sin_freq_[i] + phase_shift_[i]+M_PI/2));
00146                                         else
00147                                                 control_ref_[i] = control[i]+sin_amp_[i]*std::sin(double(cycle_count_*Ts_*sin_freq_[i] + phase_shift_[i]+M_PI/2));
00148                                 }
00149 
00150                                 return control_ref_;
00151                         }
00152 
00153                         Base::vector Base::modelUpdate(vector state, vector input){
00154 
00155                                 vector model(3);
00156 
00157                                 model << state(0)+2*input(6)*Ts_, state(1)+2*input(7)*Ts_, state(2);
00158 
00159                                 return model;
00160                         }
00161 
00162                         Base::vector Base::outputUpdate(vector state, vector input){
00163 
00164                                 vector output(3);
00165                                 /*** outputUpdate = [u1k*dF1+u2k*dF2+d u1k1*dF1+u2k1*dF2+d u1k2*dF1+u2k2*dF2+d].'; ***/
00166                                 output << state(0)*input(0)+state(1)*input(3)+state(2),
00167                                                   state(0)*input(1)+state(1)*input(4)+state(2),
00168                                                   state(0)*input(2)+state(1)*input(5)+state(2);
00169 
00170                                 return output;
00171                         }
00172                 }
00173         }
00174 }
00175 
00176 
00177 
00178 
00179 
00180 
00181 
00182 
00183 
00184 


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