testvstig.cpp
Go to the documentation of this file.
1 
23 #include "testvstig/testvstig.h"
24 
25 // Register the application
27 
28 namespace testvstig{
29 
30  TestVstig::TestVstig() {}
31 
32  TestVstig::~TestVstig() {}
33 
34  void TestVstig::init() {}
35 
36  void TestVstig::stop() {}
37 
38  void TestVstig::loop_puts(const ros::TimerEvent&)
39  {
40  std::string robot_id_string = "robot_"+boost::lexical_cast<std::string>(get_id());
41  static int count = 1;
42  std_msgs::Int32 pval;
43  pval.data = get_id() + count;
44  vs.puts(robot_id_string, pval);
45  count++;
46  std::cout<<robot_id_string<<": "<<vs.size()<<std::endl;
47  //vs.print();
48  /*static bool flag = true;
49  if(flag) {
50  if(vs.size() == 60) {
51  std::cout<<get_id()<<" get all tuple"<<std::endl;
52  flag = false;
53  }
54  }*/
55  }
56 
57  void TestVstig::loop_gets(const ros::TimerEvent&)
58  {
59  //for(int j = 0; j < 5; j++) {
60  std::string robot_id_string = "robot_" + boost::lexical_cast<std::string>(get_id());
61  std_msgs::Int32 gval = vs.gets(robot_id_string);
62  std::cout << robot_id_string << ": " << vs.size() << ", " << gval.data << std::endl;
63  //}
64  }
65 
66  void TestVstig::loop_put(const ros::TimerEvent&)
67  {
68  std::string robot_id_string = "robot_"+boost::lexical_cast<std::string>(get_id());
69  static int count = 1;
70  std_msgs::Int32 pval;
71  pval.data = get_id() + count;
72  vs.put(robot_id_string, pval);
73  count++;
74  std::cout<<robot_id_string<<": "<<vs.size()<<std::endl;
75  //vs.print();
76  }
77 
78  void TestVstig::loop_get(const ros::TimerEvent&)
79  {
80  //for(int j = 0; j < 5; j++) {
81  std::string robot_id_string = "robot_" + boost::lexical_cast<std::string>(get_id());
82  std_msgs::Int32 gval = vs.get(robot_id_string);
83  std::cout << robot_id_string << ": " << vs.size() << ", " << gval.data << std::endl;
84  //}
85  }
86 
87  void TestVstig::not_loop_put(const ros::TimerEvent&)
88  {
89  static int count = 1;
90  std_msgs::Int32 pval;
91  pval.data = get_id() + count;
92 
93  std::string cur_string="robot_"+boost::lexical_cast<std::string>(get_id())+"/"+boost::lexical_cast<std::string>(count);
94  std::string front_string="robot_"+boost::lexical_cast<std::string>(get_id())+"/"+boost::lexical_cast<std::string>(count-1);
95  vs.put(cur_string, pval);
96  //vs.remove(front_string);
97 
98  std::cout<<cur_string<<": "<<vs.size()<<std::endl;
99  count++;
100  }
101 
102  void TestVstig::not_loop_get(const ros::TimerEvent&)
103  {
104  //for(int j = 0; j < 5; j++) {
105  int i = micros_swarm::random_int(0, 2047);
106  std::string cur_string = "tuple/" + boost::lexical_cast<std::string>(i);
107  std_msgs::Int32 gval = vs.get(cur_string);
108  std::cout << cur_string << ": " << vs.size() << ", " << gval.data << std::endl;
109  //}
110  }
111 
112  void TestVstig::baseCallback(const nav_msgs::Odometry& lmsg)
113  {
114  static int msg_count = 0;
115  float x=lmsg.pose.pose.position.x;
116  float y=lmsg.pose.pose.position.y;
117 
118  float vx=lmsg.twist.twist.linear.x;
119  float vy=lmsg.twist.twist.linear.y;
120 
121  micros_swarm::Base l(x, y, 0, vx, vy, 0, 1);
122  set_base(l);
123 
124  msg_count++;
125  if(msg_count >= 5) {
126  std::cout<<"shutdown sub"<<std::endl;
127  sub.shutdown();
128  }
129  //std::cout<<"<<<"<<x<<", "<<y<<">>>"<<std::endl;
130  }
131 
132  void TestVstig::start()
133  {
134  ros::NodeHandle nh;
135  sub = nh.subscribe("base_pose_ground_truth", 1000, &TestVstig::baseCallback, this, ros::TransportHints().udp());
136  //ros::Duration(1).sleep();
137  //test virtual stigmergy
139  std_msgs::Int32 val;
140  val.data = get_id();
141 
142  /*
143  * loop write and read
144  */
145  //SCDS puts
146  //std::string robot_id_string="robot_"+boost::lexical_cast<std::string>(get_id());
147  //vs.puts(robot_id_string, val);
148  //timer = nh.createTimer(ros::Duration(0.1), &TestVstig::loop_puts, this);
149 
150  //SCDS gets
151  /*std::string robot_id_string="robot_"+boost::lexical_cast<std::string>(get_id());
152  vs.put(robot_id_string, val);
153  timer = nh.createTimer(ros::Duration(0.1), &TestVstig::loop_gets, this);*/
154 
155  //Vstig put
156  /*std::string robot_id_string="robot_"+boost::lexical_cast<std::string>(get_id());
157  vs.put(robot_id_string, val);
158  timer = nh.createTimer(ros::Duration(0.1), &TestVstig::loop_put, this);*/
159 
160  //Vstig get
161  /*std::string robot_id_string="robot_"+boost::lexical_cast<std::string>(get_id());
162  vs.put(robot_id_string, val);
163  timer = nh.createTimer(ros::Duration(0.1), &TestVstig::loop_get, this);*/
164 
165  /*
166  * not loop write and read
167  */
168  //put
169  /*std::string cur_string="robot_"+boost::lexical_cast<std::string>(get_id())+"/"+boost::lexical_cast<std::string>(0);
170  vs.put(cur_string, val);
171  timer = nh.createTimer(ros::Duration(0.5), &TestVstig::not_loop_put, this);*/
172 
173  //get
174  for(int i = 0; i < 2048; i++){
175  std::string cur_string="tuple/"+boost::lexical_cast<std::string>(i);
176  std_msgs::Int32 pval;
177  pval.data = i;
178  vs.put(cur_string, pval);
179  }
180  ros::Duration(20).sleep();
181  timer = nh.createTimer(ros::Duration(0.1), &TestVstig::not_loop_get, this);
182  }
183 };
184 
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
bool sleep() const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
int random_int(int min, int max)
#define PLUGINLIB_EXPORT_CLASS(class_type, base_class_type)


testvstig
Author(s):
autogenerated on Mon Jun 10 2019 14:02:37