stdr_server.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002    STDR Simulator - Simple Two DImensional Robot Simulator
00003    Copyright (C) 2013 STDR Simulator
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software Foundation,
00014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015    
00016    Authors : 
00017    * Manos Tsardoulias, etsardou@gmail.com
00018    * Aris Thallas, aris.thallas@gmail.com
00019    * Chris Zalidis, zalidis@gmail.com 
00020 ******************************************************************************/
00021 
00022 #include <stdr_server/stdr_server.h>
00023 
00024 namespace stdr_server {
00025   
00030   Server::Server(int argc, char** argv) 
00031     :_spawnRobotServer(_nh, "stdr_server/spawn_robot", 
00032       boost::bind(&Server::spawnRobotCallback, this, _1), false)
00033       
00034     ,_registerRobotServer(_nh, "stdr_server/register_robot", 
00035       boost::bind(&Server::registerRobotCallback, this, _1), false)
00036     
00037     ,_deleteRobotServer(_nh, "stdr_server/delete_robot", 
00038       boost::bind(&Server::deleteRobotCallback, this, _1), false)
00039     
00040     ,_id(0)
00041   {
00042   //~ _spawnRobotServer.registerGoalCallback( 
00043     //~ boost::bind(&Server::spawnRobotCallback, this) );
00044     
00045     if (argc > 2) {
00046       ROS_ERROR("%s", USAGE);
00047       exit(-1);
00048     }
00049     
00050     if (argc == 2) {
00051       std::string fname(argv[1]);
00052       _mapServer.reset(new MapServer(fname));
00053       
00055       activateActionServers();
00056     }
00057       
00058     _loadMapService = _nh.advertiseService(
00059       "/stdr_server/load_static_map", &Server::loadMapCallback, this);
00060     
00061     _loadExternalMapService = _nh.advertiseService(
00062       "/stdr_server/load_static_map_external", 
00063         &Server::loadExternalMapCallback, this);
00064     
00065     while (!ros::service::waitForService("robot_manager/load_nodelet", 
00066         ros::Duration(.1)) && ros::ok()) 
00067     {
00068       ROS_WARN("Trying to register to robot_manager/load_nodelet...");
00069     }
00070     
00071     _spawnRobotClient = _nh.serviceClient<nodelet::NodeletLoad>
00072       ("robot_manager/load_nodelet", true);
00073     
00074     while (!ros::service::waitForService("robot_manager/unload_nodelet", 
00075       ros::Duration(.1)) && ros::ok()) 
00076     {
00077       ROS_WARN("Trying to register to robot_manager/unload_nodelet...");
00078     }
00079     
00080     _unloadRobotClient = 
00081       _nh.serviceClient<nodelet::NodeletUnload>("robot_manager/unload_nodelet");
00082     
00083     _robotsPublisher = 
00084       _nh.advertise<stdr_msgs::RobotIndexedVectorMsg>
00085         ("stdr_server/active_robots", 10, true);
00086         
00088         
00089     _addRfidTagServiceServer = _nh.advertiseService("stdr_server/add_rfid_tag",
00090       &Server::addRfidTagCallback, this);
00091       
00092     _deleteRfidTagServiceServer = 
00093       _nh.advertiseService("stdr_server/delete_rfid_tag",
00094       &Server::deleteRfidTagCallback, this);
00095       
00096     _rfidTagVectorPublisher = _nh.advertise<stdr_msgs::RfidTagVector>(
00097       "stdr_server/rfid_list", 1, true);
00098       
00100         
00101     _addCO2SourceServiceServer = _nh.advertiseService(
00102       "stdr_server/add_co2_source",
00103       &Server::addCO2SourceCallback, this);
00104       
00105     _deleteCO2SourceServiceServer = 
00106       _nh.advertiseService("stdr_server/delete_co2_source",
00107       &Server::deleteCO2SourceCallback, this);
00108       
00109     _CO2SourceVectorPublisher = _nh.advertise<stdr_msgs::CO2SourceVector>(
00110       "stdr_server/co2_sources_list", 1, true);
00111       
00113         
00114     _addThermalSourceServiceServer = _nh.advertiseService(
00115       "stdr_server/add_thermal_source",
00116       &Server::addThermalSourceCallback, this);
00117       
00118     _deleteThermalSourceServiceServer = 
00119       _nh.advertiseService("stdr_server/delete_thermal_source",
00120       &Server::deleteThermalSourceCallback, this);
00121       
00122     _thermalSourceVectorPublisher = 
00123       _nh.advertise<stdr_msgs::ThermalSourceVector>(
00124       "stdr_server/thermal_sources_list", 1, true);
00125     
00127         
00128     _addSoundSourceServiceServer = _nh.advertiseService(
00129       "stdr_server/add_sound_source",
00130       &Server::addSoundSourceCallback, this);
00131       
00132     _deleteSoundSourceServiceServer = 
00133       _nh.advertiseService("stdr_server/delete_sound_source",
00134       &Server::deleteSoundSourceCallback, this);
00135       
00136     _soundSourceVectorPublisher = _nh.advertise<stdr_msgs::SoundSourceVector>(
00137       "stdr_server/sound_sources_list", 1, true);
00138   }
00139   
00143   bool Server::addRfidTagCallback(
00144     stdr_msgs::AddRfidTag::Request &req, 
00145     stdr_msgs::AddRfidTag::Response &res)
00146   {
00148     stdr_msgs::RfidTag new_rfid = req.newTag;
00149     if(_rfidTagMap.find(new_rfid.tag_id) != _rfidTagMap.end())
00150     { 
00151       res.success = false;
00152       res.message = "Duplicate rfid_id";
00153       return false;
00154     }
00155     
00157     _rfidTagMap.insert(std::pair<std::string, stdr_msgs::RfidTag>(
00158       new_rfid.tag_id, new_rfid));
00159     
00161     stdr_msgs::RfidTagVector rfidTagList;
00162     for(RfidTagMapIt it = _rfidTagMap.begin() ; it != _rfidTagMap.end() ; it++)
00163     {
00164       rfidTagList.rfid_tags.push_back(it->second);
00165     }
00166     _rfidTagVectorPublisher.publish(rfidTagList);
00167 
00169     res.success = true;
00170     return true;
00171   }
00172   
00176   bool Server::addCO2SourceCallback(
00177     stdr_msgs::AddCO2Source::Request &req, 
00178     stdr_msgs::AddCO2Source::Response &res)
00179   {
00181     stdr_msgs::CO2Source new_source = req.newSource;
00182     if(_CO2SourceMap.find(new_source.id) != _CO2SourceMap.end())
00183     { 
00184       res.success = false;
00185       res.message = "Duplicate CO2 is";
00186       return false;
00187     }
00188     
00190     _CO2SourceMap.insert(std::pair<std::string, stdr_msgs::CO2Source>(
00191       new_source.id, new_source));
00192     
00194     stdr_msgs::CO2SourceVector CO2SourceList;
00195     for(CO2SourceMapIt it = _CO2SourceMap.begin() 
00196       ; it != _CO2SourceMap.end() ; it++)
00197     {
00198       CO2SourceList.co2_sources.push_back(it->second);
00199     }
00200     _CO2SourceVectorPublisher.publish(CO2SourceList);
00201     
00203     res.success = true;
00204     return true;
00205   }
00206   
00210   bool Server::addThermalSourceCallback(
00211     stdr_msgs::AddThermalSource::Request &req, 
00212     stdr_msgs::AddThermalSource::Response &res)
00213   {
00215     stdr_msgs::ThermalSource new_source = req.newSource;
00216     if(_thermalSourceMap.find(new_source.id) != _thermalSourceMap.end())
00217     { 
00218       res.success = false;
00219       res.message = "Duplicate thermal source is";
00220       return false;
00221     }
00222     
00224     _thermalSourceMap.insert(std::pair<std::string, stdr_msgs::ThermalSource>(
00225       new_source.id, new_source));
00226     
00228     stdr_msgs::ThermalSourceVector thermalSourceList;
00229     for(ThermalSourceMapIt it = _thermalSourceMap.begin() 
00230       ; it != _thermalSourceMap.end() ; it++)
00231     {
00232       thermalSourceList.thermal_sources.push_back(it->second);
00233     }
00234     _thermalSourceVectorPublisher.publish(thermalSourceList);
00235     
00237     res.success = true;
00238     return true;
00239   }
00240   
00244   bool Server::addSoundSourceCallback(
00245     stdr_msgs::AddSoundSource::Request &req, 
00246     stdr_msgs::AddSoundSource::Response &res)
00247   {
00249     stdr_msgs::SoundSource new_source = req.newSource;
00250     if(_soundSourceMap.find(new_source.id) != _soundSourceMap.end())
00251     { 
00252       res.success = false;
00253       res.message = "Duplicate sound source is";
00254       return false;
00255     }
00256     
00258     _soundSourceMap.insert(std::pair<std::string, stdr_msgs::SoundSource>(
00259       new_source.id, new_source));
00260     
00262     stdr_msgs::SoundSourceVector soundSourceList;
00263     for(SoundSourceMapIt it = _soundSourceMap.begin() 
00264       ; it != _soundSourceMap.end() ; it++)
00265     {
00266       soundSourceList.sound_sources.push_back(it->second);
00267     }
00268     _soundSourceVectorPublisher.publish(soundSourceList);
00269     
00271     res.success = true;
00272     return true;
00273   }
00274   
00278   bool Server::deleteRfidTagCallback(
00279     stdr_msgs::DeleteRfidTag::Request &req, 
00280     stdr_msgs::DeleteRfidTag::Response &res)
00281   {
00282     
00283     std::string name = req.name;
00284     if(_rfidTagMap.find(name) != _rfidTagMap.end())
00285     {
00286       _rfidTagMap.erase(name);
00287       
00289       stdr_msgs::RfidTagVector rfidTagList;
00290       for(RfidTagMapIt it = _rfidTagMap.begin() ; 
00291         it != _rfidTagMap.end() ; it++)
00292       {
00293         rfidTagList.rfid_tags.push_back(it->second);
00294       }
00295       _rfidTagVectorPublisher.publish(rfidTagList);
00296     }
00297     else  
00298     {
00299       return false;
00300     }
00301     return true;
00302   }
00303   
00307   bool Server::deleteCO2SourceCallback(
00308     stdr_msgs::DeleteCO2Source::Request &req, 
00309     stdr_msgs::DeleteCO2Source::Response &res)
00310   {
00311     
00312     std::string name = req.name;
00313     if(_CO2SourceMap.find(name) != _CO2SourceMap.end())
00314     {
00315       _CO2SourceMap.erase(name);
00316       
00318       stdr_msgs::CO2SourceVector CO2SourceList;
00319       for(CO2SourceMapIt it = _CO2SourceMap.begin() ; 
00320         it != _CO2SourceMap.end() ; it++)
00321       {
00322         CO2SourceList.co2_sources.push_back(it->second);
00323       }
00324       _CO2SourceVectorPublisher.publish(CO2SourceList);
00325     }
00326     else  
00327     {
00328       return false;
00329     }
00330     return true;
00331   }
00332   
00336   bool Server::deleteThermalSourceCallback(
00337     stdr_msgs::DeleteThermalSource::Request &req, 
00338     stdr_msgs::DeleteThermalSource::Response &res)
00339   {
00340     
00341     std::string name = req.name;
00342     if(_thermalSourceMap.find(name) != _thermalSourceMap.end())
00343     {
00344       _thermalSourceMap.erase(name);
00345       
00347       stdr_msgs::ThermalSourceVector thermalSourceList;
00348       for(ThermalSourceMapIt it = _thermalSourceMap.begin() ; 
00349         it != _thermalSourceMap.end() ; it++)
00350       {
00351         thermalSourceList.thermal_sources.push_back(it->second);
00352       }
00353       _thermalSourceVectorPublisher.publish(thermalSourceList);
00354     }
00355     else  
00356     {
00357       return false;
00358     }
00359     return true;
00360   }
00361   
00365   bool Server::deleteSoundSourceCallback(
00366     stdr_msgs::DeleteSoundSource::Request &req, 
00367     stdr_msgs::DeleteSoundSource::Response &res)
00368   {
00369     
00370     std::string name = req.name;
00371     if(_soundSourceMap.find(name) != _soundSourceMap.end())
00372     {
00373       _soundSourceMap.erase(name);
00374       
00376       stdr_msgs::SoundSourceVector soundSourceList;
00377       for(SoundSourceMapIt it = _soundSourceMap.begin() ; 
00378         it != _soundSourceMap.end() ; it++)
00379       {
00380         soundSourceList.sound_sources.push_back(it->second);
00381       }
00382       _soundSourceVectorPublisher.publish(soundSourceList);
00383     }
00384     else  
00385     {
00386       return false;
00387     }
00388     return true;
00389   }
00390 
00397   bool Server::loadMapCallback(
00398     stdr_msgs::LoadMap::Request& req,
00399     stdr_msgs::LoadMap::Response& res) 
00400   {
00401     if (_mapServer) {
00402       ROS_WARN("Map already loaded!");
00403       return false;
00404     }
00405     _mapServer.reset(new MapServer(req.mapFile));
00407     activateActionServers();
00408 
00409     return true;        
00410   }
00411 
00418   bool Server::loadExternalMapCallback(
00419     stdr_msgs::LoadExternalMap::Request& req,
00420     stdr_msgs::LoadExternalMap::Response& res)
00421   {
00422     if (_mapServer) {
00423       ROS_WARN("Map already loaded!");
00424       return false;
00425     }
00426     _mapServer.reset(new MapServer(req.map));
00427     
00429     activateActionServers();
00430 
00431     return true;        
00432   }
00433 
00439   void Server::spawnRobotCallback(
00440     const stdr_msgs::SpawnRobotGoalConstPtr& goal) 
00441   {
00442     stdr_msgs::SpawnRobotResult result;
00443     
00444     std::string f_id;
00445     if(hasDublicateFrameIds(goal->description, f_id))
00446     {
00447       result.message = std::string("Double frame_id :") + f_id;
00448       _spawnRobotServer.setAborted(result);
00449       return;
00450     }
00451     
00452     if (addNewRobot(goal->description, &result)) {
00453       _spawnRobotServer.setSucceeded(result);
00454       
00456       stdr_msgs::RobotIndexedVectorMsg msg;
00457       for (RobotMap::iterator it = _robotMap.begin(); 
00458         it != _robotMap.end(); ++it) 
00459       {
00460         msg.robots.push_back( it->second );
00461       }
00462       
00463       _robotsPublisher.publish(msg);
00464       return;
00465     }
00466 
00467     _spawnRobotServer.setAborted();
00468   }
00469 
00475   void Server::deleteRobotCallback(
00476     const stdr_msgs::DeleteRobotGoalConstPtr&  goal) 
00477   {
00478     
00479     stdr_msgs::DeleteRobotResult result;
00480     
00481     if (deleteRobot(goal->name, &result)) {
00482       
00483       // publish to active_robots topic
00484       stdr_msgs::RobotIndexedVectorMsg msg;
00485       for (RobotMap::iterator it = _robotMap.begin(); 
00486         it != _robotMap.end(); ++it) 
00487       {
00488         msg.robots.push_back( it->second );
00489       }
00490       _robotsPublisher.publish(msg);
00491       _deleteRobotServer.setSucceeded(result);
00492       return;
00493     }
00494     
00495     _deleteRobotServer.setAborted(result);
00496   }
00497   
00503   void Server::registerRobotCallback(
00504     const stdr_msgs::RegisterRobotGoalConstPtr& goal) 
00505   {
00506     
00507     boost::unique_lock<boost::mutex> lock(_mut);
00508     stdr_msgs::RegisterRobotResult result;
00509     result.description = _robotMap[goal->name].robot;
00510     _registerRobotServer.setSucceeded(result);
00512     cond.notify_one();
00513   }
00514 
00519   void Server::activateActionServers(void) 
00520   {
00521     _spawnRobotServer.start();
00522     _registerRobotServer.start();
00523     _deleteRobotServer.start();
00524   }
00525 
00532   bool Server::addNewRobot(
00533     stdr_msgs::RobotMsg description, 
00534     stdr_msgs::SpawnRobotResult* result) 
00535   {
00536     
00537     stdr_msgs::RobotIndexedMsg namedRobot;
00538     
00539     namedRobot.robot = description;
00540     
00541     namedRobot.name = "robot" + boost::lexical_cast<std::string>(_id++);
00542     
00543     _robotMap.insert( std::make_pair(namedRobot.name, namedRobot) );
00544     
00545     nodelet::NodeletLoad srv;
00546     srv.request.name = namedRobot.name;
00547     srv.request.type = "stdr_robot/Robot";
00548     
00549     boost::unique_lock<boost::mutex> lock(_mut);
00550       
00551     if (_spawnRobotClient.call(srv)) {
00553       cond.wait(lock);
00554       
00555       result->indexedDescription = namedRobot;
00556       
00557       lock.unlock();
00558       return true;
00559     }
00560     
00561     lock.unlock();
00562     return false;
00563   }
00564 
00571   bool Server::deleteRobot(
00572     std::string name, 
00573     stdr_msgs::DeleteRobotResult* result) 
00574   {
00575     
00576     RobotMap::iterator it = _robotMap.find(name);
00577     
00578     if (it != _robotMap.end()) {
00579       
00580       nodelet::NodeletUnload srv;
00581       srv.request.name =  name;
00582       
00583       if (_unloadRobotClient.call(srv)) {
00584         
00585         if (srv.response.success) {
00586           _robotMap.erase(it);
00587         }
00588         
00589         result->success = srv.response.success;
00590         return srv.response.success;
00591       }
00592       
00593       result->success = false;
00594       return false;
00595     }
00596     
00597     ROS_WARN("Requested to delete robot, with name %s does not exist.", 
00598       name.c_str());
00599     
00600     result->success = false;
00601     return false;
00602   }
00603   
00604   bool Server::hasDublicateFrameIds(const stdr_msgs::RobotMsg& robot,
00605     std::string &f_id)
00606   {
00607     std::set<std::string> f_ids;
00608     for(unsigned int i = 0 ; i < robot.laserSensors.size() ; i++)
00609     {
00610       if(f_ids.find(robot.laserSensors[i].frame_id) == f_ids.end())
00611       {
00612         f_ids.insert(robot.laserSensors[i].frame_id);
00613       }
00614       else
00615       {
00616         f_id = robot.laserSensors[i].frame_id;
00617         return true;
00618       }
00619     }
00620     for(unsigned int i = 0 ; i < robot.sonarSensors.size() ; i++)
00621     {
00622       if(f_ids.find(robot.sonarSensors[i].frame_id) == f_ids.end())
00623       {
00624         f_ids.insert(robot.sonarSensors[i].frame_id);
00625       }
00626       else
00627       {
00628         f_id = robot.sonarSensors[i].frame_id;
00629         return true;
00630       }
00631     }
00632     for(unsigned int i = 0 ; i < robot.rfidSensors.size() ; i++)
00633     {
00634       if(f_ids.find(robot.rfidSensors[i].frame_id) == f_ids.end())
00635       {
00636         f_ids.insert(robot.rfidSensors[i].frame_id);
00637       }
00638       else
00639       {
00640         f_id = robot.rfidSensors[i].frame_id;
00641         return true;
00642       }
00643     }
00644     for(unsigned int i = 0 ; i < robot.co2Sensors.size() ; i++)
00645     {
00646       if(f_ids.find(robot.co2Sensors[i].frame_id) == f_ids.end())
00647       {
00648         f_ids.insert(robot.co2Sensors[i].frame_id);
00649       }
00650       else
00651       {
00652         f_id = robot.co2Sensors[i].frame_id;
00653         return true;
00654       }
00655     }
00656     for(unsigned int i = 0 ; i < robot.soundSensors.size() ; i++)
00657     {
00658       if(f_ids.find(robot.soundSensors[i].frame_id) == f_ids.end())
00659       {
00660         f_ids.insert(robot.soundSensors[i].frame_id);
00661       }
00662       else
00663       {
00664         f_id = robot.soundSensors[i].frame_id;
00665         return true;
00666       }
00667     }
00668     for(unsigned int i = 0 ; i < robot.thermalSensors.size() ; i++)
00669     {
00670       if(f_ids.find(robot.thermalSensors[i].frame_id) == f_ids.end())
00671       {
00672         f_ids.insert(robot.thermalSensors[i].frame_id);
00673       }
00674       else
00675       {
00676         f_id = robot.thermalSensors[i].frame_id;
00677         return true;
00678       }
00679     }
00680     return false;
00681   }
00682 
00683 } // end of namespace stdr_robot


stdr_server
Author(s): Chris Zalidis
autogenerated on Wed Sep 2 2015 03:36:33