- Timestamp:
- 08/29/12 16:52:52 (12 years ago)
- Location:
- trunk/extremum_seeking
- Files:
-
- 5 added
- 14 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/extremum_seeking/esc_approx/include/esc_approx/approx_esc_1d.h
r5 r13 37 37 std::vector<double> monitor(); 38 38 std::vector<std::string> monitorNames(); 39 void reset(); 39 40 40 41 }; -
trunk/extremum_seeking/esc_approx/include/esc_approx/approx_esc_2d.h
r5 r13 40 40 std::vector<double> monitor(); 41 41 std::vector<std::string> monitorNames(); 42 void reset(); 42 43 43 44 }; -
trunk/extremum_seeking/esc_approx/src/approx_esc_1d.cpp
r5 r13 35 35 k_grad_ = k_grad; 36 36 init_vel_ = init_vel; 37 sample_ = 0;38 37 sampling_ = sampling; 39 ptr_ = 0;40 38 states_.resize(data_size); 41 39 obj_vals_.resize(data_size); 40 sample_ = 0; 41 ptr_ = 0; 42 42 initialized_ = true; 43 43 } … … 111 111 return std::vector<std::string>(); 112 112 } 113 114 void ApproxESC1D::reset(){ 115 sample_ = 0; 116 ptr_ = 0; 117 } -
trunk/extremum_seeking/esc_approx/src/approx_esc_2d.cpp
r5 r13 33 33 k_grad_ = k_grad; 34 34 init_vel_ = init_vel; 35 sampling_ = sampling; 35 36 sample_ = 0; 36 sampling_ = sampling;37 37 ptr_ = 0; 38 states_ = Eigen::MatrixXf::Zero(2,data_size );38 states_ = Eigen::MatrixXf::Zero(2,data_size_); 39 39 obj_vals_ = Eigen::VectorXf::Zero(data_size_); 40 40 state_curr_.resize(2); … … 133 133 return std::vector<std::string>(); 134 134 } 135 136 void ApproxESC2D::reset(){ 137 sample_ = 0; 138 ptr_ = 0; 139 states_ = Eigen::MatrixXf::Zero(2,data_size_); 140 obj_vals_ = Eigen::VectorXf::Zero(data_size_); 141 } -
trunk/extremum_seeking/esc_common/include/esc_common/esc.h
r5 r13 15 15 #include <string> 16 16 #define PI 3.141592654 17 18 /// Superclass for extremum seeking control algorithms. 17 19 class ESC 18 20 { 19 public: 20 21 enum inputType { inputStateValue, inputValue }; 22 enum outputType { outputVelocity, outputPosition }; 21 public: 22 /// Controller input type 23 enum inputType 24 { 25 inputStateValue, ///< State-value input. 26 inputValue ///< Value input. 27 }; 28 29 /// Controller output type. 30 enum outputType 31 { 32 outputVelocity, ///< Velocity reference output. 33 outputPosition ///< Position reference output. 34 }; 23 35 24 36 public: 25 37 virtual ~ESC() { } 26 38 27 virtual std::vector<std::string> monitorNames() { return std::vector<std::string>(); } 28 virtual std::vector<double> monitor() { return std::vector<double>(); } 39 /// Get internal monitor variable names. 40 virtual std::vector<std::string> monitorNames() { return std::vector<std::string>(); } 41 42 /// Get internal monitor variables. 43 virtual std::vector<double> monitor() { return std::vector<double>(); } 29 44 30 virtual inputType getInputType() = 0; 31 virtual outputType getOutputType() = 0; 45 /// Get controller input type. 46 virtual inputType getInputType() = 0; 47 48 /// Get controller output type. 49 virtual outputType getOutputType() = 0; 32 50 33 virtual std::vector<double> step(std::vector<double> state, double obj_val) 34 { 35 return step(obj_val); 36 } 51 /// Control step function for value-input control algorithms. 52 virtual std::vector<double> step(std::vector<double> state, double obj_val) 53 { 54 return step(obj_val); 55 } 37 56 38 virtual std::vector<double> step(double obj_val) 39 { 40 return std::vector<double>(); 41 } 57 /// Control step function for state-input control algorithms. 58 virtual std::vector<double> step(double obj_val) 59 { 60 return std::vector<double>(); 61 } 62 63 /// Reset control algorithm to initial conditions. 64 virtual void reset() = 0; 42 65 }; 43 66 44 45 67 #endif /* ESC_H_ */ -
trunk/extremum_seeking/esc_nn/include/esc_nn/nn_esc_1d.h
r5 r13 47 47 double minPeakDetect(double e_minus); 48 48 double aSwitch(double e); 49 void reset(); 49 50 50 51 }; -
trunk/extremum_seeking/esc_nn/include/esc_nn/nn_esc_2d.h
r5 r13 25 25 26 26 protected: 27 double A_,M_,ddelta1_,ddelta2_,ddelta3_, A,delta_,B_, mpd_init_, w_switch_old_, a_switch1_old_, a_switch2_old_, a_switch3_old_,yr_,period_;27 double A_,M_,ddelta1_,ddelta2_,ddelta3_,delta_,B_, mpd_init_, w_switch_old_, a_switch1_old_, a_switch2_old_, a_switch3_old_,yr_,period_; 28 28 double min_peak_,w_switch_; 29 29 std::vector<double> vel_ref_; … … 42 42 std::vector<double> monitor(); 43 43 std::vector<std::string> monitorNames(); 44 void reset(); 44 45 protected: 45 double wSwitch(double e _minus);46 double minPeakDetect(double e _minus);46 double wSwitch(double e); 47 double minPeakDetect(double e); 47 48 double aSwitch1(double e); 48 49 double aSwitch2(double e); -
trunk/extremum_seeking/esc_nn/src/nn_esc_1d.cpp
r5 r13 134 134 135 135 } 136 137 void NNESC1D::reset(){ 138 w_switch_old_ = 0; 139 a_switch_old_ = A; 140 yr_ = 0; 141 min_peak_ = 0; 142 vel_ref_ = 0; 143 w_switch_ = 0; 144 min_peak_detect_init_ = false; 145 } -
trunk/extremum_seeking/esc_nn/src/nn_esc_2d.cpp
r5 r13 20 20 ddelta1_ = 0; 21 21 ddelta2_ = 0; 22 ddelta 2_ = 0;22 ddelta3_ = 0; 23 23 delta_ = 0; 24 24 B_ = 0; … … 51 51 monitor_vals.push_back(min_peak_); 52 52 monitor_vals.push_back(w_switch_); 53 monitor_vals.push_back(yr_+ddelta1_); 54 monitor_vals.push_back(yr_+ddelta2_); 55 monitor_vals.push_back(yr_+ddelta3_); 56 monitor_vals.push_back(yr_+delta_); 53 57 54 58 return monitor_vals; … … 61 65 monitor_names.push_back("minimum peak detector output"); 62 66 monitor_names.push_back("w switch value"); 67 monitor_names.push_back("threshold value 1"); 68 monitor_names.push_back("threshold value 2"); 69 monitor_names.push_back("threshold value 3"); 70 monitor_names.push_back("threshold value 4"); 63 71 64 72 return monitor_names; … … 75 83 ddelta1_ = ddelta1; 76 84 ddelta2_ = ddelta2; 77 ddelta 2_ = ddelta3;85 ddelta3_ = ddelta3; 78 86 delta_ = delta; 79 87 period_ = period; 80 88 w_switch_old_ = 0; 81 89 a_switch1_old_ = A_; 82 a_switch2_old_ = A_;90 a_switch2_old_ = 0; 83 91 a_switch3_old_ = 0; 84 92 vel_ref_.resize(2); … … 99 107 100 108 if(!min_peak_detect_init_){ 101 yr_ = obj_val; 109 min_peak_ = obj_val; 110 yr_ = min_peak_; 102 111 min_peak_detect_init_ = true; 103 112 } 104 113 105 114 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); 115 double s[3]; 116 s[0] = aSwitch1(e); 117 s[1] = aSwitch2(e); 118 s[2] = aSwitch3(e); 119 120 vel_ref_[1] = s[0]+s[1]; 121 vel_ref_[0] = s[1]+s[2]; 122 123 min_peak_ = minPeakDetect(e); 124 w_switch_ = wSwitch(e); 110 125 yr_ = yr_ + (w_switch_+min_peak_)*period_; 126 111 127 return vel_ref_; 112 128 } 113 double NNESC2D::wSwitch(double e _minus){114 if(e _minus<-delta_){129 double NNESC2D::wSwitch(double e){ 130 if(e>delta_){ 115 131 w_switch_old_ = 0; 116 132 return 0; 117 133 } 118 else if(e _minus>delta_){134 else if(e<-delta_){ 119 135 w_switch_old_ = B_; 120 136 return B_; … … 125 141 } 126 142 127 double NNESC2D::minPeakDetect(double e _minus){128 if(e _minus>0)143 double NNESC2D::minPeakDetect(double e){ 144 if(e<=0) 129 145 return 0; 130 146 else … … 146 162 147 163 double NNESC2D::aSwitch2(double e){ 164 148 165 if( e < -ddelta2_ ){ 149 a_switch2_old_ = 0;150 return 0;151 }152 else if(e>ddelta2_){153 166 a_switch2_old_ = A_; 154 167 return A_; 168 169 } 170 else if(e>ddelta2_){ 171 a_switch2_old_ = 0; 172 return 0; 155 173 } 156 174 else … … 170 188 return a_switch3_old_; 171 189 } 190 191 void NNESC2D::reset(){ 192 w_switch_old_ = 0; 193 a_switch1_old_ = A_; 194 a_switch2_old_ = 0; 195 a_switch3_old_ = 0; 196 vel_ref_.resize(2); 197 vel_ref_[0] = 0; 198 vel_ref_[1] = 0; 199 yr_ = 0; 200 min_peak_ = 0; 201 w_switch_ = 0; 202 min_peak_detect_init_ = false; 203 initialized_ = true; 204 } -
trunk/extremum_seeking/esc_sm/include/esc_sm/sm_esc_1d.h
r5 r13 36 36 std::vector<double> monitor(); 37 37 std::vector<std::string> monitorNames(); 38 void reset(); 38 39 protected: 39 40 int sign(double value); -
trunk/extremum_seeking/esc_sm/src/sm_esc_1d.cpp
r5 r13 87 87 } 88 88 89 void SMESC1D::reset(){ 90 driving_input_ = 0; 91 driving_input_init_ = false; 92 } -
trunk/extremum_seeking/esc_test/include/esc_test/esc_test.h
r11 r13 11 11 12 12 ESCSystem *system_; 13 std::vector<float> vel_, pos_; 13 14 14 15 protected: -
trunk/extremum_seeking/esc_test/include/esc_test/function.h
r11 r13 9 9 }; 10 10 11 class Gauss : public ESCFunction11 class Gauss1D : public ESCFunction 12 12 { 13 13 private: 14 float a_, b_, c_ ;14 float a_, b_, c_, d_; 15 15 16 16 public: 17 Gauss (float a=1, float b=0, float c=1) : a_(a), b_(b), c_(c) { }17 Gauss1D(float a=0, float b=1, float c=0, float d=1) : a_(a), b_(b), c_(c), d_(d) { } 18 18 19 19 std::vector<float> init() … … 29 29 throw std::runtime_error("invalid state size"); 30 30 31 return a_ *std::exp(-(state[0]-b_)*(state[0]-b_)/(2*c_*c_));31 return a_ + b_*std::exp(-(state[0]-c_)*(state[0]-c_)/(2*d_*d_)); 32 32 } 33 33 }; 34 35 class Gauss2D : public ESCFunction 36 { 37 private: 38 float a_, b_; 39 std::vector<float> c_, d_; 40 41 public: 42 Gauss2D(float a, float b, std::vector<float> c, std::vector<float> d) 43 { 44 if (c.empty()) 45 { 46 c_.push_back(0); 47 c_.push_back(0); 48 } 49 else 50 c_ = c; 51 52 if (d.empty()) 53 { 54 d_.push_back(1); 55 d_.push_back(1); 56 } 57 else 58 d_ = d; 59 } 60 61 std::vector<float> init() 62 { 63 std::vector<float> state; 64 state.push_back(0); 65 state.push_back(0); 66 return state; 67 } 68 69 float value(const std::vector<float> &state) const 70 { 71 if (state.size() != 1) 72 throw std::runtime_error("invalid state size"); 73 74 double exponent = (state[0]-c_[0])*(state[0]-c_[0])/(2*d_[0]*d_[0]) + 75 (state[1]-c_[1])*(state[1]-c_[1])/(2*d_[1]*d_[1]); 76 77 return a_ + b_*std::exp(-exponent); 78 } 79 }; 80 34 81 35 82 class ESCSystem -
trunk/extremum_seeking/esc_test/src/esc_test.cpp
r11 r13 7 7 void ESCTest::velocityCallback(const std_msgs::Float32MultiArray::ConstPtr &msg) 8 8 { 9 system_->step(msg->data);10 p ublish();9 vel_ = msg->data; 10 pos_.clear(); 11 11 } 12 12 13 13 void ESCTest::positionCallback(const std_msgs::Float32MultiArray::ConstPtr &msg) 14 14 { 15 system_->set(msg->data);16 publish();15 pos_ = msg->data; 16 vel_.clear(); 17 17 } 18 18 … … 39 39 vel_sub_ = nh_.subscribe("vel_ref", 1, &ESCTest::velocityCallback, this); 40 40 pos_sub_ = nh_.subscribe("pos_ref", 1, &ESCTest::positionCallback, this); 41 42 publish();43 41 } 44 42 … … 47 45 ROS_INFO("Spinning"); 48 46 49 ros::spin(); 47 ros::Rate loop_rate(100); 48 while (nh_.ok()) 49 { 50 if (!vel_.empty()) 51 system_->step(vel_); 52 else if (!pos_.empty()) 53 system_->set(pos_); 54 publish(); 55 56 loop_rate.sleep(); 57 ros::spinOnce(); 58 } 50 59 } 51 60 … … 54 63 ros::init(argc, argv, "esc_test"); 55 64 65 ESCFunction *function; 66 67 if (argc > 1 && strcmp(argv[1], "-2") == 0) 68 { 69 std::vector<float> mean, sigma; 70 mean.push_back(0.5); 71 mean.push_back(0.2); 72 sigma.push_back(0.15); 73 sigma.push_back(0.15); 74 function = new Gauss2D(2, -1, mean, sigma); 75 } 76 else 77 function = new Gauss1D(2.5, -1.5, 0.4, 0.27/sqrt(2.0)); 78 56 79 ESCTest test; 57 80 58 test.init(new ESCSystem( new Gauss(-1, 2, 1)));81 test.init(new ESCSystem(function)); 59 82 test.spin(); 60 83
Note: See TracChangeset
for help on using the changeset viewer.