Changeset 13 for trunk/extremum_seeking/esc_test
- Timestamp:
- 08/29/12 16:52:52 (12 years ago)
- Location:
- trunk/extremum_seeking/esc_test
- Files:
-
- 5 added
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
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.