runtime_core.cpp
Go to the documentation of this file.
00001 
00023 #include "micros_swarm/runtime_core.h"
00024 
00025 namespace micros_swarm{
00026     RuntimeCore::RuntimeCore():ci_loader_("micros_swarm", "micros_swarm::CommInterface") {}
00027     RuntimeCore::~RuntimeCore() {}
00028     
00029     void RuntimeCore::spin_msg_queue()
00030     {
00031         for(;;) {
00032             std::vector<std::vector<uint8_t> > msg_vec;
00033             msg_queue_manager_->spinAllOutMsgQueueOnce(msg_vec);
00034             for(int i = 0; i < msg_vec.size(); i++) {
00035                 communicator_->broadcast(msg_vec[i]);
00036             }
00037         }
00038     }
00039     
00040     void RuntimeCore::barrier_check(const ros::TimerEvent&)
00041     {
00042         int barrier_size = rth_->getBarrierSize();
00043         if(barrier_size >= total_robot_numbers_-1) {
00044             std::cout<<"robot "<<rth_->getRobotID()<<" daemon node start."<<std::endl;
00045             barrier_timer_.stop();
00046         }
00047                 
00048         //barrier
00049         int robot_id = rth_->getRobotID();
00050     
00051         std::string syn="SYN";
00052         gsdf_msgs::BarrierSyn bs;
00053         bs.s = syn;
00054         std::vector<uint8_t> bs_vec = serialize_ros(bs);
00055     
00056         gsdf_msgs::CommPacket p;
00057         p.header.source = robot_id;
00058         p.header.type = micros_swarm::BARRIER_SYN;
00059         p.header.data_len = bs_vec.size();
00060         p.header.version = 1;
00061         p.header.checksum = 0;
00062         p.content.buf = bs_vec;
00063         std::vector<uint8_t> msg_data = serialize_ros(p);
00064         msg_queue_manager_->getOutMsgQueue("barrier")->push(msg_data);
00065     }
00066     
00067     void RuntimeCore::publish_robot_base(const ros::TimerEvent&)
00068     {
00069         int robot_id = rth_->getRobotID();
00070         const Base& l = rth_->getRobotBase();
00071         gsdf_msgs::RobotBase rb;
00072         rb.id = robot_id;
00073         rb.px = l.x;
00074         rb.py = l.y;
00075         rb.pz = l.z;
00076         rb.vx = l.vx;
00077         rb.vy = l.vy;
00078         rb.vz = l.vz;
00079         rb.theta = 0;
00080         rb.valid = l.valid;
00081         std::vector<uint8_t> rb_vec = serialize_ros(rb);
00082                       
00083         gsdf_msgs::CommPacket p;
00084         p.header.source = robot_id;
00085         p.header.type = SINGLE_ROBOT_BROADCAST_BASE;
00086         p.header.data_len = rb_vec.size();
00087         p.header.version = 1;
00088         p.header.checksum = 0;
00089         p.content.buf = rb_vec;
00090         std::vector<uint8_t> msg_data = serialize_ros(p);
00091         msg_queue_manager_->getOutMsgQueue("base")->push(msg_data);
00092     }
00093     
00094     void RuntimeCore::publish_swarm_list(const ros::TimerEvent&)
00095     {
00096         int robot_id = rth_->getRobotID();
00097         std::vector<int> swarm_list;
00098         swarm_list.clear();
00099         rth_->getSwarmList(swarm_list);
00100         
00101         gsdf_msgs::SwarmList sl;
00102         sl.robot_id = robot_id;
00103         sl.swarm_list = swarm_list;
00104         std::vector<uint8_t> sl_vec = serialize_ros(sl);
00105                       
00106         gsdf_msgs::CommPacket p;
00107         p.header.source = robot_id;
00108         p.header.type = SINGLE_ROBOT_SWARM_LIST;
00109         p.header.data_len = sl_vec.size();
00110         p.header.version = 1;
00111         p.header.checksum = 0;
00112         p.content.buf = sl_vec;
00113         std::vector<uint8_t> msg_data = serialize_ros(p);
00114         msg_queue_manager_->getOutMsgQueue("swarm")->push(msg_data);
00115     }
00116     
00117     void RuntimeCore::setParameters()
00118     {
00119         bool param_ok = node_handle_.getParam("/publish_robot_id_duration", publish_robot_base_duration_);
00120         if(!param_ok) {
00121             std::cout<<"could not get parameter publish_robot_id_duration! use the default value."<<std::endl;
00122             publish_robot_base_duration_ = 0.1;
00123         }
00124         else {
00125             std::cout<<"publish_robot_id_duration: "<<publish_robot_base_duration_<<std::endl;
00126         }
00127         
00128         param_ok = node_handle_.getParam("/publish_swarm_list_duration", publish_swarm_list_duration_);
00129         if(!param_ok) {
00130             std::cout<<"could not get parameter publish_swarm_list_duration! use the default value."<<std::endl;
00131             publish_swarm_list_duration_ = 5.0;
00132         }
00133         else {
00134             std::cout<<"publish_swarm_list_duration: "<<publish_swarm_list_duration_<<std::endl;
00135         }
00136         
00137         param_ok = node_handle_.getParam("/default_neighbor_distance", default_neighbor_distance_);
00138         if(!param_ok) {
00139             std::cout<<"could not get parameter default_neighbor_distance! use the default value."<<std::endl;
00140             default_neighbor_distance_ = 50;
00141         }
00142         else {
00143             std::cout<<"default_neighbor_distance: "<<default_neighbor_distance_<<std::endl;
00144         }
00145         
00146         param_ok = node_handle_.getParam("/total_robot_numbers", total_robot_numbers_);
00147         if(!param_ok) {
00148             std::cout<<"could not get parameter total_robot_numbers! use the default value."<<std::endl;
00149             total_robot_numbers_ = 1;
00150         }
00151         else {
00152             std::cout<<"total_robot_numbers: "<<total_robot_numbers_<<std::endl;
00153         }
00154 
00155         param_ok = node_handle_.getParam("/comm_type", comm_type_);
00156         if(!param_ok) {
00157             std::cout<<"could not get parameter comm_type, use the default ros_comm."<<std::endl;
00158             comm_type_ = "ros_comm/ROSComm";
00159         }
00160         else {
00161             std::cout<<"comm_type: "<<comm_type_<<std::endl;
00162         }
00163 
00164         ros::NodeHandle private_nh("~");
00165         private_nh.param("robot_id", robot_id_, 0);
00166         std::cout<<"robot_id: "<<robot_id_<<std::endl;
00167         private_nh.param("worker_num", worker_num_, 4);
00168         std::cout<<"worker_num: "<<worker_num_<<std::endl;
00169     }
00170     
00171     void RuntimeCore::initialize()
00172     {
00173         //srand(time(NULL) + robot_id_);
00174         srand(time(NULL) + (int)getpid());
00175         setParameters();
00176         //construct runtime platform
00177         rth_ = Singleton<RuntimeHandle>::getSingleton();
00178         rth_->setRobotID(robot_id_);
00179         rth_->setNeighborDistance(default_neighbor_distance_);
00180         //construct packet parser
00181         parser_ = Singleton<PacketParser>::getSingleton();
00182         //construct communicator
00183         communicator_ = ci_loader_.createInstance(comm_type_);
00184         communicator_->init(comm_type_, *parser_);
00185         communicator_->receive();
00186         //construct message queue manager
00187         msg_queue_manager_ = Singleton<MsgQueueManager>::getSingleton();
00188         msg_queue_manager_->createOutMsgQueue("base", 1000);
00189         msg_queue_manager_->createOutMsgQueue("swarm", 1000);
00190         msg_queue_manager_->createOutMsgQueue("vstig", 50000);
00191         msg_queue_manager_->createOutMsgQueue("bb", 10000);
00192         msg_queue_manager_->createOutMsgQueue("nc", 10000);
00193         msg_queue_manager_->createOutMsgQueue("barrier", 10000);
00194         msg_queue_manager_->createOutMsgQueue("scds_pso", 10000);
00195         //construct app manager
00196         app_manager_ = Singleton<AppManager>::getSingleton(worker_num_);
00197         //construct timers
00198         publish_robot_base_timer_ = node_handle_.createTimer(ros::Duration(publish_robot_base_duration_), &RuntimeCore::publish_robot_base, this);
00199         publish_swarm_list_timer_ = node_handle_.createTimer(ros::Duration(publish_swarm_list_duration_), &RuntimeCore::publish_swarm_list, this);
00200         //barrier_timer_=node_handle_.createTimer(ros::Duration(1), &RuntimeCore::barrier_check, this);
00201         spin_thread_ = new boost::thread(&RuntimeCore::spin_msg_queue, this);
00202         std::cout<<"robot "<<rth_->getRobotID()<<" daemon node start."<<std::endl;
00203     }
00204 
00205     void RuntimeCore::shutdown()
00206     {
00207         spin_thread_->interrupt();
00208         spin_thread_->join();
00209         delete spin_thread_;
00210 
00211         app_manager_->stop();
00212         Singleton<AppManager>::deleteSingleton();
00213         app_manager_.reset();
00214         Singleton<CommInterface>::deleteSingleton();
00215         communicator_.reset();
00216         ci_loader_.unloadLibraryForClass(comm_type_);
00217         Singleton<PacketParser>::deleteSingleton();
00218         parser_.reset();
00219         Singleton<RuntimeHandle>::deleteSingleton();
00220         rth_.reset();
00221     }
00222 };


micros_swarm
Author(s):
autogenerated on Thu Jun 6 2019 18:52:14