00001
00002
00003
00004
00005
00006
00007 #include <labust/control/esc/EscEKF.hpp>
00008
00009 #include <ros/ros.h>
00010
00011 namespace labust{
00012 namespace control{
00013 namespace esc{
00014
00015 typedef EscEkfGrad Base;
00016
00017 Base::EscEkfGrad(int ctrlNum, numericprecission Ts):EscPerturbationBase<double>(ctrlNum, Ts),
00018 state_(vector::Zero(controlNum)){
00019
00020 signal_demodulated_old_.resize(controlNum);
00021 control_ref_.resize(controlNum);
00022 sin_amp_.resize(controlNum);
00023 sin_freq_.resize(controlNum);
00024 gain_.resize(controlNum);
00025 control_.resize(controlNum);
00026
00027 control_ref_ = state_;
00028 signal_demodulated_old_.setZero();
00029
00030 A = matrix::Identity(3,3);
00031 L = matrix::Identity(3,3);
00032 H = matrix::Zero(3,3);
00033 M = matrix::Identity(3,3);
00034
00035 Pk_plu = 1.0e-3*matrix::Identity(3,3);
00036 xk_plu = vector::Zero(3);
00037
00038
00039
00040
00041 Eigen::Vector3d tmp;
00042
00043 tmp << 0.75, 0.75, 0.5;
00044 Q = tmp.asDiagonal();
00045
00046 tmp << 1, 1, 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(6);
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::~EscEkfGrad(){
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 input << additional_input(0), n1(0), n2(0), additional_input(1), n1(1), n2(1);
00089 yk << cost_signal_filtered, n1(2), n2(2);
00090
00091 n2 = n1;
00092 n1 << additional_input, cost_signal_filtered;
00093
00094 H << input(0), input(3), 1,
00095 input(1), input(4), 1,
00096 input(2), input(5), 1;
00097
00098 Pk_min = A*Pk_plu*A.transpose() + L*Q*L.transpose();
00099 xk_min = modelUpdate(xk_plu, input);
00100
00101 hk = outputUpdate(xk_min, input);
00102 matrix tmp = H*Pk_min*H.transpose()+M*R*M.transpose();
00103 Kk = (Pk_min*H.transpose())*tmp.inverse();
00104
00105 xk_plu = xk_min+Kk*(yk-hk);
00106 Pk_plu = (matrix::Identity(3,3)-Kk*H)*Pk_min;
00107 input_past = input;
00108
00109 signal_demodulated << xk_plu(0), xk_plu(1);
00110 return signal_demodulated;
00111 }
00112
00113
00114 Base::vector Base::controllerGain(vector postFiltered){
00115 control_ = gain_.cwiseProduct(postFiltered);
00116 ROS_ERROR("GAIN");
00117 ROS_ERROR_STREAM(gain_);
00118 ROS_ERROR("CONTROL");
00119 ROS_ERROR_STREAM(control_);
00120 return control_;
00121 }
00122
00123 Base::vector Base::superimposePerturbation(Base::vector control){
00124
00125 for (size_t i = 0; i<controlNum; i++){
00126 if(!old_vals_initialized_)
00127 control_ref_[i] = sin_amp_[i]*std::sin(double(cycle_count_*Ts_*sin_freq_[i] + phase_shift_[i]+M_PI/2));
00128 else
00129 control_ref_[i] = control[i]+sin_amp_[i]*std::sin(double(cycle_count_*Ts_*sin_freq_[i] + phase_shift_[i]+M_PI/2));
00130 }
00131
00132 return control_ref_;
00133 }
00134
00135 Base::vector Base::modelUpdate(vector state, vector input){
00136
00137 return A*state;
00138 }
00139
00140 Base::vector Base::outputUpdate(vector state, vector input){
00141
00142
00143 vector output(3);
00144 output << state(0)*input(0)+state(1)*input(3)+state(2),
00145 state(0)*input(1)+state(1)*input(4)+state(2),
00146 state(0)*input(2)+state(1)*input(5)+state(2);
00147 return output;
00148 }
00149 }
00150 }
00151 }
00152
00153
00154
00155
00156