handle_robot.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_robot/handle_robot.h>
00023 
00024 namespace stdr_robot {
00025 
00030   HandleRobot::HandleRobot() 
00031     : _spawnRobotClient("stdr_server/spawn_robot", true)
00032     , _deleteRobotClient("stdr_server/delete_robot", true)
00033   {
00034   }
00035 
00041   stdr_msgs::RobotIndexedMsg HandleRobot::spawnNewRobot(
00042     const stdr_msgs::RobotMsg msg) 
00043   {
00044     
00045     stdr_msgs::SpawnRobotGoal goal;
00046     goal.description = msg;
00047       
00048     while (!_spawnRobotClient.waitForServer(ros::Duration(1)) && ros::ok()) {
00049       ROS_WARN("Could not find stdr_server/spawn_robot action.");
00050     }
00051     
00052     _spawnRobotClient.sendGoal(goal);
00053     
00054     bool success = _spawnRobotClient.waitForResult(ros::Duration(10));
00055     if (!success) {
00056       throw ConnectionException("Could not spawn robot...");
00057     }
00058     
00059     actionlib::SimpleClientGoalState state = _spawnRobotClient.getState();
00060     if(state.toString() == "ABORTED")
00061     {
00062       std::string msg = std::string("Could not spawn robot. ") + 
00063         _spawnRobotClient.getResult()->message;
00064       throw DoubleFrameIdException(msg);
00065     }
00066     
00067     ROS_INFO("New robot spawned successfully, with name %s.", 
00068       _spawnRobotClient.getResult()->indexedDescription.name.c_str());
00069     
00070     return _spawnRobotClient.getResult()->indexedDescription;
00071     
00072   }
00073 
00079   bool HandleRobot::deleteRobot(const std::string& name) 
00080   {
00081     
00082     stdr_msgs::DeleteRobotGoal goal;
00083     goal.name = name;
00084     
00085     while (!_deleteRobotClient.waitForServer(ros::Duration(1)) && ros::ok()) {
00086       ROS_WARN("Could not find stdr_server/delete_robot action.");
00087     }
00088     
00089     _deleteRobotClient.sendGoal(goal);
00090     
00091     bool success = _deleteRobotClient.waitForResult(ros::Duration(10));
00092     
00093     if (!success) {
00094       throw ConnectionException("Could not delete robot, connection error...");
00095     }
00096     
00097     return _deleteRobotClient.getResult()->success;
00098     
00099   }
00100 
00107   bool HandleRobot::moveRobot(
00108     const std::string& name, const geometry_msgs::Pose2D newPose) 
00109   {
00110     
00111     while (!ros::service::waitForService(name + "/replace", 
00112       ros::Duration(.1)) && ros::ok()) {
00113       ROS_WARN("Could not find %s/replace ...", name.c_str());
00114     }
00115     
00116     stdr_msgs::MoveRobot srv;
00117     srv.request.newPose = newPose;
00118     
00119     if (ros::service::call(name + "/replace", srv)) {
00120       return true;
00121     }
00122     
00123     return false;
00124   }
00125     
00126 } // end of namespace stdr_robot


stdr_robot
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Wed Sep 2 2015 03:36:25