| 1 | /* |
|---|
| 2 | * nn_esc_2d.cpp |
|---|
| 3 | * |
|---|
| 4 | * Created on: Jul 31, 2012 |
|---|
| 5 | * Author: Berk Calli |
|---|
| 6 | * Organization: Delft Biorobotics Lab., Delft University of Technology |
|---|
| 7 | * Contact info: b.calli@tudelft.nl, web: www.dbl.tudelft.nl |
|---|
| 8 | * |
|---|
| 9 | * Class for two dimensional neural network extremum seeking control |
|---|
| 10 | * |
|---|
| 11 | * * References: |
|---|
| 12 | * - M. Teixeira and S. Zak, âAnalog neural nonderivative optimizers,â IEEE Transactions on Neural Networks, vol. 9, pp. 629â638, 1998. |
|---|
| 13 | * - B. Calli, W. Caarls, P. Jonker and M. Wisse, "Comparison of Extremum Seeking Control Algorithms for Robotic Applications", IROS 2012. |
|---|
| 14 | */ |
|---|
| 15 | #include "esc_nn/nn_esc_2d.h" |
|---|
| 16 | |
|---|
| 17 | NNESC2D::NNESC2D(){ |
|---|
| 18 | M_ = 0; |
|---|
| 19 | A_ = 0; |
|---|
| 20 | ddelta1_ = 0; |
|---|
| 21 | ddelta2_ = 0; |
|---|
| 22 | ddelta2_ = 0; |
|---|
| 23 | delta_ = 0; |
|---|
| 24 | B_ = 0; |
|---|
| 25 | w_switch_old_ = 0; |
|---|
| 26 | a_switch1_old_ = 0; |
|---|
| 27 | a_switch2_old_ = 0; |
|---|
| 28 | a_switch3_old_ = 0; |
|---|
| 29 | yr_ = 0; |
|---|
| 30 | period_ = 0; |
|---|
| 31 | min_peak_ = 0; |
|---|
| 32 | vel_ref_.resize(2); |
|---|
| 33 | vel_ref_[0] = 0; |
|---|
| 34 | vel_ref_[1] = 0; |
|---|
| 35 | w_switch_ = 0; |
|---|
| 36 | min_peak_detect_init_ = false; |
|---|
| 37 | initialized_ = false; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | ESC::inputType NNESC2D::getInputType(){ |
|---|
| 41 | return inputValue; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | ESC::outputType NNESC2D::getOutputType(){ |
|---|
| 45 | return outputVelocity; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | std::vector<double> NNESC2D::monitor(){ |
|---|
| 49 | std::vector<double> monitor_vals; |
|---|
| 50 | monitor_vals.push_back(yr_); |
|---|
| 51 | monitor_vals.push_back(min_peak_); |
|---|
| 52 | monitor_vals.push_back(w_switch_); |
|---|
| 53 | |
|---|
| 54 | return monitor_vals; |
|---|
| 55 | |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | std::vector<std::string> NNESC2D::monitorNames(){ |
|---|
| 59 | std::vector<std::string> monitor_names; |
|---|
| 60 | monitor_names.push_back("driving input value"); |
|---|
| 61 | monitor_names.push_back("minimum peak detector output"); |
|---|
| 62 | monitor_names.push_back("w switch value"); |
|---|
| 63 | |
|---|
| 64 | return monitor_names; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | NNESC2D::NNESC2D(double A,double M, double B, double ddelta1, double ddelta2, double ddelta3, double delta, double period){ |
|---|
| 68 | init(A, M, B, ddelta1, ddelta2, ddelta3, delta, period); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void NNESC2D::init(double A, double M, double B, double ddelta1, double ddelta2, double ddelta3, double delta, double period){ |
|---|
| 72 | A_ = A; |
|---|
| 73 | M_ = M; |
|---|
| 74 | B_ = B; |
|---|
| 75 | ddelta1_ = ddelta1; |
|---|
| 76 | ddelta2_ = ddelta2; |
|---|
| 77 | ddelta2_ = ddelta3; |
|---|
| 78 | delta_ = delta; |
|---|
| 79 | period_ = period; |
|---|
| 80 | w_switch_old_ = 0; |
|---|
| 81 | a_switch1_old_ = A_; |
|---|
| 82 | a_switch2_old_ = A_; |
|---|
| 83 | a_switch3_old_ = 0; |
|---|
| 84 | vel_ref_.resize(2); |
|---|
| 85 | vel_ref_[0] = 0; |
|---|
| 86 | vel_ref_[1] = 0; |
|---|
| 87 | yr_ = 0; |
|---|
| 88 | min_peak_ = 0; |
|---|
| 89 | w_switch_ = 0; |
|---|
| 90 | min_peak_detect_init_ = false; |
|---|
| 91 | initialized_ = true; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | std::vector<double> NNESC2D::step(double obj_val){ |
|---|
| 95 | if (!initialized_){ |
|---|
| 96 | fprintf(stderr,"The neural network ESC (1D) is not initialized... It will not be executed. \n"); |
|---|
| 97 | return std::vector<double>(); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | if(!min_peak_detect_init_){ |
|---|
| 101 | yr_ = obj_val; |
|---|
| 102 | min_peak_detect_init_ = true; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | double e = yr_ - obj_val; |
|---|
| 106 | vel_ref_[1] = aSwitch1(e)+aSwitch2(e); |
|---|
| 107 | vel_ref_[0] = aSwitch2(e)+aSwitch3(e); |
|---|
| 108 | min_peak_ = minPeakDetect(-e); |
|---|
| 109 | w_switch_ = wSwitch(-e); |
|---|
| 110 | yr_ = yr_ + (w_switch_+min_peak_)*period_; |
|---|
| 111 | return vel_ref_; |
|---|
| 112 | } |
|---|
| 113 | double NNESC2D::wSwitch(double e_minus){ |
|---|
| 114 | if(e_minus<-delta_){ |
|---|
| 115 | w_switch_old_ = 0; |
|---|
| 116 | return 0; |
|---|
| 117 | } |
|---|
| 118 | else if(e_minus>delta_){ |
|---|
| 119 | w_switch_old_ = B_; |
|---|
| 120 | return B_; |
|---|
| 121 | } |
|---|
| 122 | else |
|---|
| 123 | return w_switch_old_; |
|---|
| 124 | |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | double NNESC2D::minPeakDetect(double e_minus){ |
|---|
| 128 | if(e_minus>0) |
|---|
| 129 | return 0; |
|---|
| 130 | else |
|---|
| 131 | return -M_; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | double NNESC2D::aSwitch1(double e){ |
|---|
| 135 | if( e < -ddelta1_ ){ |
|---|
| 136 | a_switch1_old_ = -A_; |
|---|
| 137 | return -A_; |
|---|
| 138 | } |
|---|
| 139 | else if(e>ddelta1_){ |
|---|
| 140 | a_switch1_old_ = A_; |
|---|
| 141 | return A_; |
|---|
| 142 | } |
|---|
| 143 | else |
|---|
| 144 | return a_switch1_old_; |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | double NNESC2D::aSwitch2(double e){ |
|---|
| 148 | if( e < -ddelta2_ ){ |
|---|
| 149 | a_switch2_old_ = 0; |
|---|
| 150 | return 0; |
|---|
| 151 | } |
|---|
| 152 | else if(e>ddelta2_){ |
|---|
| 153 | a_switch2_old_ = A_; |
|---|
| 154 | return A_; |
|---|
| 155 | } |
|---|
| 156 | else |
|---|
| 157 | return a_switch2_old_; |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | double NNESC2D::aSwitch3(double e){ |
|---|
| 161 | if( e < -ddelta3_ ){ |
|---|
| 162 | a_switch3_old_ = -2*A_; |
|---|
| 163 | return -2*A_; |
|---|
| 164 | } |
|---|
| 165 | else if(e>ddelta3_){ |
|---|
| 166 | a_switch3_old_ = 0; |
|---|
| 167 | return 0; |
|---|
| 168 | } |
|---|
| 169 | else |
|---|
| 170 | return a_switch3_old_; |
|---|
| 171 | } |
|---|