Go to the documentation of this file.00001
00023 #include "micros_swarm/swarm.h"
00024
00025 namespace micros_swarm{
00026
00027 Swarm::Swarm()
00028 {
00029 swarm_id_ = -1;
00030 }
00031
00032 Swarm::Swarm(int swarm_id)
00033 {
00034 swarm_id_ = swarm_id;
00035 rth_ = Singleton<RuntimeHandle>::getSingleton();
00036 mqm_ = Singleton<MsgQueueManager>::getSingleton();
00037 rth_->insertOrUpdateSwarm(swarm_id_, 0);
00038 }
00039
00040 Swarm::Swarm(const Swarm& s)
00041 {
00042 rth_ = Singleton<RuntimeHandle>::getSingleton();
00043 mqm_ = Singleton<MsgQueueManager>::getSingleton();
00044 swarm_id_ = s.swarm_id_;
00045 }
00046
00047 Swarm& Swarm::operator=(const Swarm& s)
00048 {
00049 if(this == &s) {
00050 return *this;
00051 }
00052 rth_ = Singleton<RuntimeHandle>::getSingleton();
00053 mqm_ = Singleton<MsgQueueManager>::getSingleton();
00054 swarm_id_ = s.swarm_id_;
00055 return *this;
00056 }
00057
00058 Swarm::~Swarm()
00059 {
00060 rth_.reset();
00061 mqm_.reset();
00062 }
00063
00064 const int Swarm::id() const
00065 {
00066 return swarm_id_;
00067 }
00068
00069 const std::set<int> Swarm::members()
00070 {
00071 std::set<int> s;
00072 rth_->getSwarmMembers(swarm_id_, s);
00073
00074 return s;
00075 }
00076
00077 void Swarm::join()
00078 {
00079 int robot_id = rth_->getRobotID();
00080 rth_->insertOrUpdateSwarm(swarm_id_, 1);
00081
00082 gsdf_msgs::JoinSwarm js;
00083 js.robot_id = robot_id;
00084 js.swarm_id = swarm_id_;
00085 std::vector<uint8_t> js_vec = serialize_ros(js);
00086
00087 gsdf_msgs::CommPacket p;
00088 p.header.source = robot_id;
00089 p.header.type = SINGLE_ROBOT_JOIN_SWARM;
00090 p.header.data_len = js_vec.size();
00091 p.header.version = 1;
00092 p.header.checksum = 0;
00093 p.content.buf = js_vec;
00094 std::vector<uint8_t> msg_data = serialize_ros(p);
00095 mqm_->getOutMsgQueue("swarm")->push(msg_data);
00096 }
00097
00098 void Swarm::leave()
00099 {
00100 int robot_id = rth_->getRobotID();
00101 rth_->insertOrUpdateSwarm(swarm_id_, 0);
00102
00103 gsdf_msgs::LeaveSwarm ls;
00104 ls.robot_id = robot_id;
00105 ls.swarm_id = swarm_id_;
00106 std::vector<uint8_t> ls_vec = serialize_ros(ls);
00107
00108 gsdf_msgs::CommPacket p;
00109 p.header.source = robot_id;
00110 p.header.type = SINGLE_ROBOT_LEAVE_SWARM;
00111 p.header.data_len = ls_vec.size();
00112 p.header.version = 1;
00113 p.header.checksum = 0;
00114 p.content.buf = ls_vec;
00115 std::vector<uint8_t> msg_data = serialize_ros(p);
00116 mqm_->getOutMsgQueue("swarm")->push(msg_data);
00117 }
00118
00119 void Swarm::select(const boost::function<bool()>& bf)
00120 {
00121 if(bf()) {
00122 join();
00123 }
00124 else {
00125
00126 }
00127 }
00128
00129 void Swarm::unselect(const boost::function<bool()>& bf)
00130 {
00131 if(bf()) {
00132 leave();
00133 }
00134 else {
00135
00136 }
00137 }
00138
00139 const bool Swarm::in() const
00140 {
00141 if(rth_->getSwarmFlag(swarm_id_)) {
00142 return true;
00143 }
00144 return false;
00145 }
00146
00147
00148 void Swarm::execute(const boost::function<void()>& f)
00149 {
00150 if(in()) {
00151 f();
00152 }
00153 }
00154
00155 void Swarm::breakup()
00156 {
00157 if(in()) {
00158 leave();
00159 }
00160 rth_->deleteSwarm(swarm_id_);
00161 this->~Swarm();
00162 }
00163
00164 const Swarm Swarm::intersection(const Swarm& s, int new_swarm_id)
00165 {
00166 std::set<int> result;
00167 result.clear();
00168
00169 std::set<int> a;
00170 rth_->getSwarmMembers(swarm_id_, a);
00171 std::set<int> b;
00172 rth_->getSwarmMembers(s.id(), b);
00173
00174 std::set_intersection(a.begin(), a.end(), b.begin(), b.end(),
00175 std::insert_iterator<std::set<int> >(result, result.begin()));
00176
00177 Swarm result_swarm(new_swarm_id);
00178
00179 int robot_id=rth_->getRobotID();
00180
00181 std::set<int>::iterator it;
00182 it = result.find(robot_id);
00183 if(it != result.end()) {
00184 result_swarm.join();
00185 }
00186
00187 return result_swarm;
00188 }
00189
00190 const Swarm Swarm::swarm_union(const Swarm& s, int new_swarm_id)
00191 {
00192 std::set<int> result;
00193 result.clear();
00194
00195 std::set<int> a;
00196 rth_->getSwarmMembers(swarm_id_, a);
00197 std::set<int> b;
00198 rth_->getSwarmMembers(s.id(), b);
00199
00200 std::set_union(a.begin(), a.end(), b.begin(), b.end(),
00201 std::insert_iterator<std::set<int> >(result, result.begin()));
00202
00203 Swarm result_swarm(new_swarm_id);
00204
00205 int robot_id = rth_->getRobotID();
00206
00207 std::set<int>::iterator it;
00208 it = result.find(robot_id);
00209 if(it != result.end()) {
00210 result_swarm.join();
00211 }
00212
00213 return result_swarm;
00214 }
00215
00216 const Swarm Swarm::difference(const Swarm& s, int new_swarm_id)
00217 {
00218 std::set<int> result;
00219
00220 std::set<int> a;
00221 rth_->getSwarmMembers(swarm_id_, a);
00222 std::set<int> b;
00223 rth_->getSwarmMembers(s.id(), b);
00224
00225 std::set_difference(a.begin(), a.end(), b.begin(), b.end(),
00226 std::insert_iterator<std::set<int> >(result, result.begin()));
00227
00228 Swarm result_swarm(new_swarm_id);
00229
00230 int robot_id = rth_->getRobotID();
00231
00232 std::set<int>::iterator it;
00233 it = result.find(robot_id);
00234 if(it != result.end()) {
00235 result_swarm.join();
00236 }
00237
00238 return result_swarm;
00239 }
00240
00241 const Swarm Swarm::negation(int new_swarm_id)
00242 {
00243 Swarm result_swarm(new_swarm_id);
00244 std::set<int> a;
00245 rth_->getSwarmMembers(swarm_id_, a);
00246 int robot_id = rth_->getRobotID();
00247
00248 std::set<int>::iterator it;
00249 it = a.find(robot_id);
00250 if(it == a.end()) {
00251 result_swarm.join();
00252 }
00253
00254 return result_swarm;
00255 }
00256
00257 void Swarm::print() const
00258 {
00259 std::set<int> s;
00260 rth_->getSwarmMembers(swarm_id_, s);
00261
00262 int robot_id = rth_->getRobotID();
00263
00264 std::set<int>::iterator it;
00265 std::cout<<"swarm "<<swarm_id_<<" members: "<<std::endl;
00266 for(it = s.begin(); it != s.end(); it++) {
00267 std::cout<<*it<<", ";
00268 }
00269 std::cout<<std::endl;
00270 }
00271 };