nn_esc_1d.cpp
Go to the documentation of this file.
00001 /*
00002  * nn_esc_1d.cpp
00003  *
00004  *  Created on: Jul 26, 2012
00005  *      Author: Berk Calli
00006  *      Organization: Delft Biorobotics Lab., Delft University of Technology
00007  *              Contact info: b.calli@tudelft.nl, web: www.dbl.tudelft.nl
00008  *
00009  * Class for one dimensional neural network extremum seeking control
00010  *
00011  * * References:
00012  * - M. Teixeira and S. Zak, “Analog neural nonderivative optimizers,” IEEE Transactions on Neural Networks, vol. 9, pp. 629–638, 1998.
00013  * - B. Calli, W. Caarls, P. Jonker and M. Wisse, "Comparison of Extremum Seeking Control Algorithms for Robotic Applications", IROS 2012.
00014  */
00015 #include "esc_nn/nn_esc_1d.h"
00016 
00017 NNESC1D::NNESC1D(){
00018         M_ = 0;
00019         A_ = 0;
00020         ddelta_ = 0;
00021         delta_ = 0;
00022         B_ = 0;
00023         w_switch_old_ = 0;
00024         a_switch_old_ = 0;
00025         yr_ = 0;
00026         period_ = 0;
00027         min_peak_ = 0;
00028         vel_ref_ = 0;
00029         w_switch_ = 0;
00030         min_peak_detect_init_ = false;
00031         initialized_ = false;
00032 
00033 }
00034 
00035 ESC::inputType NNESC1D::getInputType(){
00036         return inputValue;
00037 }
00038 
00039 ESC::outputType NNESC1D::getOutputType(){
00040         return outputVelocity;
00041 }
00042 
00043 std::vector<double> NNESC1D::monitor(){
00044         std::vector<double> monitor_vals;
00045         monitor_vals.push_back(yr_);
00046         monitor_vals.push_back(min_peak_);
00047         monitor_vals.push_back(w_switch_);
00048 
00049         return monitor_vals;
00050 
00051 }
00052 
00053 std::vector<std::string> NNESC1D::monitorNames(){
00054         std::vector<std::string> monitor_names;
00055         monitor_names.push_back("driving input value");
00056         monitor_names.push_back("minimum peak detector output");
00057         monitor_names.push_back("w switch value");
00058 
00059         return monitor_names;
00060 }
00061 
00062 NNESC1D::NNESC1D(double A,double M, double B, double ddelta, double delta, double period, int stopping_cycle_number, double stoping_min_val){
00063         init(A, M, B, ddelta, delta, period,stopping_cycle_number,stoping_min_val);
00064 }
00065 
00066 void NNESC1D::init(double A, double M, double B, double ddelta, double delta, double period, int stopping_cycle_number, double stoping_min_val){
00067         A_ = A;
00068         M_ = M;
00069         B_ = B;
00070         ddelta_ = ddelta;
00071         delta_ = delta;
00072         period_ = period;
00073         reset();
00074         initialized_ = true;
00075         stopping_cycle_number_ = stopping_cycle_number;
00076         stoping_min_val_ = stoping_min_val;
00077 }
00078 
00079 std::vector<double> NNESC1D::step(double obj_val){
00080         if (!initialized_){
00081                 fprintf(stderr,"The neural network ESC (1D) is not initialized... It will not be executed. \n");
00082                 return std::vector<double>();
00083         }
00084 
00085         if(!min_peak_detect_init_){
00086                 yr_ = obj_val;
00087                 min_peak_detect_init_ = true;
00088                 obj_val_cycle_init_ = obj_val;
00089         }
00090 
00091         double e = yr_ - obj_val;
00092         vel_ref_ = aSwitch(e);
00093         min_peak_ = minPeakDetect(-e);
00094         w_switch_ = wSwitch(-e);
00095         yr_ = yr_ + (w_switch_+min_peak_)*period_;
00096 
00097         if(vel_ref_old_ != vel_ref_ && vel_ref_ == -A_){
00098                 if(obj_val_cycle_init_ - obj_val < stoping_min_val_)
00099                         nn_cycle_count_++;
00100                 else
00101                         nn_cycle_count_ = 0;
00102                 obj_val_cycle_init_ = obj_val;
00103         }
00104 
00105         vel_ref_old_ = vel_ref_;
00106 
00107         std::vector<double> output;
00108         output.push_back(vel_ref_);
00109         return output;
00110 }
00111 double NNESC1D::wSwitch(double e_minus){
00112         if(e_minus<-delta_){
00113                 w_switch_old_ = 0;
00114                 return 0;
00115         }
00116         else if(e_minus>delta_){
00117                 w_switch_old_ = B_;
00118                 return B_;
00119         }
00120         else
00121                 return w_switch_old_;
00122 
00123 }
00124 
00125 double NNESC1D::minPeakDetect(double e_minus){
00126         if(e_minus>0)
00127                 return 0;
00128         else
00129                 return -M_;
00130 }
00131 
00132 double NNESC1D::aSwitch(double e){
00133         if( e < -ddelta_ ){
00134                 a_switch_old_ = -A_;
00135                 return -A_;
00136         }
00137         else if(e>=ddelta_){
00138                 a_switch_old_ = A_;
00139                 return A_;
00140         }
00141         else
00142                 return a_switch_old_;
00143 
00144 }
00145 
00146 void NNESC1D::reset(){
00147         w_switch_old_ = 0;
00148         a_switch_old_ = A_;
00149         yr_ = 0;
00150         min_peak_ = 0;
00151         vel_ref_ = 0;
00152         w_switch_ = 0;
00153         nn_cycle_count_ = 0;
00154         vel_ref_old_ = 0;
00155         min_peak_detect_init_ = false;
00156 
00157 }
00158 
00159 
00160 bool NNESC1D::isStoppingConditionsMet(){
00161         if(stopping_cycle_number_ <= 0)
00162                 return false;
00163         else if(nn_cycle_count_ >= stopping_cycle_number_){
00164                 return true;
00165         }
00166         else
00167                 return false;
00168 }


esc_nn
Author(s): Berk Calli
autogenerated on Sun Jan 5 2014 11:07:07