testvstig.cpp
Go to the documentation of this file.
00001 
00023 #include "testvstig/testvstig.h"
00024 
00025 // Register the application
00026 PLUGINLIB_EXPORT_CLASS(testvstig::TestVstig, micros_swarm::Application)
00027 
00028 namespace testvstig{
00029 
00030     TestVstig::TestVstig() {}
00031     
00032     TestVstig::~TestVstig() {}
00033 
00034     void TestVstig::init() {}
00035 
00036     void TestVstig::stop() {}
00037     
00038     void TestVstig::loop_puts(const ros::TimerEvent&)
00039     {
00040         std::string robot_id_string = "robot_"+boost::lexical_cast<std::string>(get_id());
00041         static int count = 1;
00042         std_msgs::Int32 pval;
00043         pval.data = get_id() + count;
00044         vs.puts(robot_id_string, pval);
00045         count++;
00046         std::cout<<robot_id_string<<": "<<vs.size()<<std::endl;
00047         //vs.print();
00048         /*static bool flag = true;
00049         if(flag) {
00050             if(vs.size() == 60) {
00051                 std::cout<<get_id()<<" get all tuple"<<std::endl;
00052                 flag = false;
00053             }
00054         }*/
00055     }
00056 
00057     void TestVstig::loop_gets(const ros::TimerEvent&)
00058     {
00059         //for(int j = 0; j < 5; j++) {
00060             std::string robot_id_string = "robot_" + boost::lexical_cast<std::string>(get_id());
00061             std_msgs::Int32 gval = vs.gets(robot_id_string);
00062             std::cout << robot_id_string << ": " << vs.size() << ", " << gval.data << std::endl;
00063         //}
00064     }
00065 
00066     void TestVstig::loop_put(const ros::TimerEvent&)
00067     {
00068         std::string robot_id_string = "robot_"+boost::lexical_cast<std::string>(get_id());
00069         static int count = 1;
00070         std_msgs::Int32 pval;
00071         pval.data = get_id() + count;
00072         vs.put(robot_id_string, pval);
00073         count++;
00074         std::cout<<robot_id_string<<": "<<vs.size()<<std::endl;
00075         //vs.print();
00076     }
00077 
00078     void TestVstig::loop_get(const ros::TimerEvent&)
00079     {
00080         //for(int j = 0; j < 5; j++) {
00081             std::string robot_id_string = "robot_" + boost::lexical_cast<std::string>(get_id());
00082             std_msgs::Int32 gval = vs.get(robot_id_string);
00083             std::cout << robot_id_string << ": " << vs.size() << ", " << gval.data << std::endl;
00084         //}
00085     }
00086 
00087     void TestVstig::not_loop_put(const ros::TimerEvent&)
00088     {
00089         static int count = 1;
00090         std_msgs::Int32 pval;
00091         pval.data = get_id() + count;
00092 
00093         std::string cur_string="robot_"+boost::lexical_cast<std::string>(get_id())+"/"+boost::lexical_cast<std::string>(count);
00094         std::string front_string="robot_"+boost::lexical_cast<std::string>(get_id())+"/"+boost::lexical_cast<std::string>(count-1);
00095         vs.put(cur_string, pval);
00096         //vs.remove(front_string);
00097 
00098         std::cout<<cur_string<<": "<<vs.size()<<std::endl;
00099         count++;
00100     }
00101 
00102     void TestVstig::not_loop_get(const ros::TimerEvent&)
00103     {
00104         //for(int j = 0; j < 5; j++) {
00105             int i = micros_swarm::random_int(0, 2047);
00106             std::string cur_string = "tuple/" + boost::lexical_cast<std::string>(i);
00107             std_msgs::Int32 gval = vs.get(cur_string);
00108             std::cout << cur_string << ": " << vs.size() << ", " << gval.data << std::endl;
00109         //}
00110     }
00111 
00112     void TestVstig::baseCallback(const nav_msgs::Odometry& lmsg)
00113     {
00114         static int msg_count = 0;
00115         float x=lmsg.pose.pose.position.x;
00116         float y=lmsg.pose.pose.position.y;
00117 
00118         float vx=lmsg.twist.twist.linear.x;
00119         float vy=lmsg.twist.twist.linear.y;
00120 
00121         micros_swarm::Base l(x, y, 0, vx, vy, 0, 1);
00122         set_base(l);
00123 
00124         msg_count++;
00125         if(msg_count >= 5) {
00126             std::cout<<"shutdown sub"<<std::endl;
00127             sub.shutdown();
00128         }
00129         //std::cout<<"<<<"<<x<<", "<<y<<">>>"<<std::endl;
00130     }
00131     
00132     void TestVstig::start()
00133     {
00134         ros::NodeHandle nh;
00135         sub = nh.subscribe("base_pose_ground_truth", 1000, &TestVstig::baseCallback, this, ros::TransportHints().udp());
00136         //ros::Duration(1).sleep();
00137         //test virtual stigmergy
00138         vs=micros_swarm::VirtualStigmergy<std_msgs::Int32>(1);
00139         std_msgs::Int32 val;
00140         val.data = get_id();
00141 
00142         /*
00143          * loop write and read
00144          */
00145         //SCDS puts
00146         //std::string robot_id_string="robot_"+boost::lexical_cast<std::string>(get_id());
00147         //vs.puts(robot_id_string, val);
00148         //timer = nh.createTimer(ros::Duration(0.1), &TestVstig::loop_puts, this);
00149 
00150         //SCDS gets
00151         /*std::string robot_id_string="robot_"+boost::lexical_cast<std::string>(get_id());
00152         vs.put(robot_id_string, val);
00153         timer = nh.createTimer(ros::Duration(0.1), &TestVstig::loop_gets, this);*/
00154 
00155         //Vstig put
00156         /*std::string robot_id_string="robot_"+boost::lexical_cast<std::string>(get_id());
00157         vs.put(robot_id_string, val);
00158         timer = nh.createTimer(ros::Duration(0.1), &TestVstig::loop_put, this);*/
00159 
00160         //Vstig get
00161         /*std::string robot_id_string="robot_"+boost::lexical_cast<std::string>(get_id());
00162         vs.put(robot_id_string, val);
00163         timer = nh.createTimer(ros::Duration(0.1), &TestVstig::loop_get, this);*/
00164 
00165         /*
00166          * not loop write and read
00167          */
00168         //put
00169         /*std::string cur_string="robot_"+boost::lexical_cast<std::string>(get_id())+"/"+boost::lexical_cast<std::string>(0);
00170         vs.put(cur_string, val);
00171         timer = nh.createTimer(ros::Duration(0.5), &TestVstig::not_loop_put, this);*/
00172 
00173         //get
00174         for(int i = 0; i < 2048; i++){
00175             std::string cur_string="tuple/"+boost::lexical_cast<std::string>(i);
00176             std_msgs::Int32 pval;
00177             pval.data = i;
00178             vs.put(cur_string, pval);
00179         }
00180         ros::Duration(20).sleep();
00181         timer = nh.createTimer(ros::Duration(0.1), &TestVstig::not_loop_get, this);
00182     }
00183 };
00184 


testvstig
Author(s):
autogenerated on Thu Jun 6 2019 18:52:46