runtime_core.cpp
Go to the documentation of this file.
1 
24 
25 namespace micros_swarm{
26  RuntimeCore::RuntimeCore():ci_loader_("micros_swarm", "micros_swarm::CommInterface") {}
28 
30  {
31  for(;;) {
32  std::vector<std::vector<uint8_t> > msg_vec;
33  msg_queue_manager_->spinAllOutMsgQueueOnce(msg_vec);
34  for(int i = 0; i < msg_vec.size(); i++) {
35  communicator_->broadcast(msg_vec[i]);
36  }
37  }
38  }
39 
41  {
42  int barrier_size = rth_->getBarrierSize();
43  if(barrier_size >= total_robot_numbers_-1) {
44  std::cout<<"robot "<<rth_->getRobotID()<<" daemon node start."<<std::endl;
46  }
47 
48  //barrier
49  int robot_id = rth_->getRobotID();
50 
51  std::string syn="SYN";
52  gsdf_msgs::BarrierSyn bs;
53  bs.s = syn;
54  std::vector<uint8_t> bs_vec = serialize_ros(bs);
55 
56  gsdf_msgs::CommPacket p;
57  p.header.source = robot_id;
58  p.header.type = micros_swarm::BARRIER_SYN;
59  p.header.data_len = bs_vec.size();
60  p.header.version = 1;
61  p.header.checksum = 0;
62  p.content.buf = bs_vec;
63  std::vector<uint8_t> msg_data = serialize_ros(p);
64  msg_queue_manager_->getOutMsgQueue("barrier")->push(msg_data);
65  }
66 
68  {
69  int robot_id = rth_->getRobotID();
70  const Base& l = rth_->getRobotBase();
71  gsdf_msgs::RobotBase rb;
72  rb.id = robot_id;
73  rb.px = l.x;
74  rb.py = l.y;
75  rb.pz = l.z;
76  rb.vx = l.vx;
77  rb.vy = l.vy;
78  rb.vz = l.vz;
79  rb.theta = 0;
80  rb.valid = l.valid;
81  std::vector<uint8_t> rb_vec = serialize_ros(rb);
82 
83  gsdf_msgs::CommPacket p;
84  p.header.source = robot_id;
85  p.header.type = SINGLE_ROBOT_BROADCAST_BASE;
86  p.header.data_len = rb_vec.size();
87  p.header.version = 1;
88  p.header.checksum = 0;
89  p.content.buf = rb_vec;
90  std::vector<uint8_t> msg_data = serialize_ros(p);
91  msg_queue_manager_->getOutMsgQueue("base")->push(msg_data);
92  }
93 
95  {
96  int robot_id = rth_->getRobotID();
97  std::vector<int> swarm_list;
98  swarm_list.clear();
99  rth_->getSwarmList(swarm_list);
100 
101  gsdf_msgs::SwarmList sl;
102  sl.robot_id = robot_id;
103  sl.swarm_list = swarm_list;
104  std::vector<uint8_t> sl_vec = serialize_ros(sl);
105 
106  gsdf_msgs::CommPacket p;
107  p.header.source = robot_id;
108  p.header.type = SINGLE_ROBOT_SWARM_LIST;
109  p.header.data_len = sl_vec.size();
110  p.header.version = 1;
111  p.header.checksum = 0;
112  p.content.buf = sl_vec;
113  std::vector<uint8_t> msg_data = serialize_ros(p);
114  msg_queue_manager_->getOutMsgQueue("swarm")->push(msg_data);
115  }
116 
118  {
119  bool param_ok = node_handle_.getParam("/publish_robot_id_duration", publish_robot_base_duration_);
120  if(!param_ok) {
121  std::cout<<"could not get parameter publish_robot_id_duration! use the default value."<<std::endl;
123  }
124  else {
125  std::cout<<"publish_robot_id_duration: "<<publish_robot_base_duration_<<std::endl;
126  }
127 
128  param_ok = node_handle_.getParam("/publish_swarm_list_duration", publish_swarm_list_duration_);
129  if(!param_ok) {
130  std::cout<<"could not get parameter publish_swarm_list_duration! use the default value."<<std::endl;
132  }
133  else {
134  std::cout<<"publish_swarm_list_duration: "<<publish_swarm_list_duration_<<std::endl;
135  }
136 
137  param_ok = node_handle_.getParam("/default_neighbor_distance", default_neighbor_distance_);
138  if(!param_ok) {
139  std::cout<<"could not get parameter default_neighbor_distance! use the default value."<<std::endl;
141  }
142  else {
143  std::cout<<"default_neighbor_distance: "<<default_neighbor_distance_<<std::endl;
144  }
145 
146  param_ok = node_handle_.getParam("/total_robot_numbers", total_robot_numbers_);
147  if(!param_ok) {
148  std::cout<<"could not get parameter total_robot_numbers! use the default value."<<std::endl;
150  }
151  else {
152  std::cout<<"total_robot_numbers: "<<total_robot_numbers_<<std::endl;
153  }
154 
155  param_ok = node_handle_.getParam("/comm_type", comm_type_);
156  if(!param_ok) {
157  std::cout<<"could not get parameter comm_type, use the default ros_comm."<<std::endl;
158  comm_type_ = "ros_comm/ROSComm";
159  }
160  else {
161  std::cout<<"comm_type: "<<comm_type_<<std::endl;
162  }
163 
164  ros::NodeHandle private_nh("~");
165  private_nh.param("robot_id", robot_id_, 0);
166  std::cout<<"robot_id: "<<robot_id_<<std::endl;
167  private_nh.param("worker_num", worker_num_, 4);
168  std::cout<<"worker_num: "<<worker_num_<<std::endl;
169  }
170 
172  {
173  //srand(time(NULL) + robot_id_);
174  srand(time(NULL) + (int)getpid());
175  setParameters();
176  //construct runtime platform
178  rth_->setRobotID(robot_id_);
179  rth_->setNeighborDistance(default_neighbor_distance_);
180  //construct packet parser
182  //construct communicator
183  communicator_ = ci_loader_.createInstance(comm_type_);
185  communicator_->receive();
186  //construct message queue manager
188  msg_queue_manager_->createOutMsgQueue("base", 1000);
189  msg_queue_manager_->createOutMsgQueue("swarm", 1000);
190  msg_queue_manager_->createOutMsgQueue("vstig", 50000);
191  msg_queue_manager_->createOutMsgQueue("bb", 10000);
192  msg_queue_manager_->createOutMsgQueue("nc", 10000);
193  msg_queue_manager_->createOutMsgQueue("barrier", 10000);
194  msg_queue_manager_->createOutMsgQueue("scds_pso", 10000);
195  //construct app manager
197  //construct timers
200  //barrier_timer_=node_handle_.createTimer(ros::Duration(1), &RuntimeCore::barrier_check, this);
201  spin_thread_ = new boost::thread(&RuntimeCore::spin_msg_queue, this);
202  std::cout<<"robot "<<rth_->getRobotID()<<" daemon node start."<<std::endl;
203  }
204 
206  {
207  spin_thread_->interrupt();
208  spin_thread_->join();
209  delete spin_thread_;
210 
211  app_manager_->stop();
213  app_manager_.reset();
215  communicator_.reset();
216  ci_loader_.unloadLibraryForClass(comm_type_);
218  parser_.reset();
220  rth_.reset();
221  }
222 };
pluginlib::ClassLoader< micros_swarm::CommInterface > ci_loader_
Definition: runtime_core.h:49
void barrier_check(const ros::TimerEvent &)
void stop()
boost::thread * spin_thread_
Definition: runtime_core.h:66
boost::shared_ptr< RuntimeHandle > rth_
Definition: runtime_core.h:46
ros::Timer publish_swarm_list_timer_
Definition: runtime_core.h:55
std::vector< uint8_t > serialize_ros(T t)
Definition: serialize.h:39
bool param(const std::string &param_name, T &param_val, const T &default_val) const
ros::NodeHandle node_handle_
Definition: runtime_core.h:45
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
boost::shared_ptr< CommInterface > communicator_
Definition: runtime_core.h:47
ros::Timer publish_robot_base_timer_
Definition: runtime_core.h:54
static boost::shared_ptr< T > getSingleton()
Definition: singleton.h:70
boost::shared_ptr< micros_swarm::MsgQueueManager > msg_queue_manager_
Definition: runtime_core.h:50
boost::shared_ptr< micros_swarm::PacketParser > parser_
Definition: runtime_core.h:51
static void deleteSingleton()
Definition: singleton.h:61
bool getParam(const std::string &key, std::string &s) const
void publish_robot_base(const ros::TimerEvent &)
void publish_swarm_list(const ros::TimerEvent &)
boost::shared_ptr< AppManager > app_manager_
Definition: runtime_core.h:52


micros_swarm
Author(s):
autogenerated on Mon Jun 10 2019 14:02:06