Go to the documentation of this file.00001
00023 #include "testscdspso/testscdspso.h"
00024
00025 #define PI 3.1415926
00026
00027
00028 PLUGINLIB_EXPORT_CLASS(testscdspso::TestSCDSPSO, micros_swarm::Application)
00029
00030 namespace testscdspso{
00031
00032 TestSCDSPSO::TestSCDSPSO() {}
00033
00034 TestSCDSPSO::~TestSCDSPSO() {}
00035
00036 void TestSCDSPSO::init() {}
00037
00038 void TestSCDSPSO::stop() {}
00039
00040 void TestSCDSPSO::loop(const ros::TimerEvent&)
00041 {
00042 static int i=0;
00043 if(i<50)
00044 i++;
00045 micros_swarm::SCDSPSODataTuple data;
00046 data.val = get_id() + i;
00047 tuple.put("test_scds_pso", data);
00048
00049 micros_swarm::SCDSPSODataTuple best = tuple.get("test_scds_pso");
00050 std::cout<<"robot: "<<get_id()<<", best: "<<best.val<<std::endl;
00051 }
00052
00053 void TestSCDSPSO::baseCallback(const nav_msgs::Odometry& lmsg)
00054 {
00055 float x=lmsg.pose.pose.position.x;
00056 float y=lmsg.pose.pose.position.y;
00057
00058 float vx=lmsg.twist.twist.linear.x;
00059 float vy=lmsg.twist.twist.linear.y;
00060
00061 micros_swarm::Base l(x, y, 0, vx, vy, 0, 1);
00062 set_base(l);
00063 }
00064
00065 float TestSCDSPSO::fitness(const std::vector<float>& vec)
00066 {
00067 float x = vec[0];
00068 float y = vec[1];
00069 float fitness = -(20+x*x+y*y-10*cos(2*PI*x)-10*cos(2*PI*y));
00070 return fitness;
00071 }
00072
00073 void TestSCDSPSO::start()
00074 {
00075 agent.set_param(1, 1.49445, 1.49445);
00076 agent.set_dim(2);
00077 agent.set_fitness(boost::bind(&TestSCDSPSO::fitness, this, _1));
00078
00079 std::vector<float> max_pos;
00080 max_pos.resize(2);
00081 max_pos[0] = 5.12;
00082 max_pos[1] = 5.12;
00083 agent.set_max_pos(max_pos);
00084
00085 std::vector<float> min_pos;
00086 min_pos.resize(2);
00087 min_pos[0] = -5.12;
00088 min_pos[1] = -5.12;
00089 agent.set_min_pos(min_pos);
00090
00091 std::vector<float> max_vel;
00092 max_vel.resize(2);
00093 max_vel[0] = 1;
00094 max_vel[1] = 1;
00095 agent.set_max_vel(max_vel);
00096
00097 std::vector<float> min_vel;
00098 min_vel.resize(2);
00099 min_vel[0] = -1;
00100 min_vel[1] = -1;
00101 agent.set_min_vel(min_vel);
00102
00103 agent.rand_init();
00104 agent.start(1500);
00105 }
00106 };
00107