TeleKybCore.cpp
Go to the documentation of this file.
00001 /*
00002  * TeleKybCore.cpp
00003  *
00004  *  Created on: Nov 15, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include <telekyb_interface/TeleKybCore.hpp>
00009 
00010 #include <telekyb_srvs/StringOutput.h>
00011 
00012 #include <boost/lexical_cast.hpp>
00013 
00014 #ifdef __APPLE__
00015 #define WAITTIME_TELEKYBSYSTEM 10.0
00016 #else
00017 #define WAITTIME_TELEKYBSYSTEM 5.0
00018 #endif
00019 
00020 namespace TELEKYB_INTERFACE_NAMESPACE {
00021 
00022 TeleKybCore::TeleKybCore(int robotID_, const std::string& mainHandleNamespace)
00023         : robotID(robotID_), mainNodeHandle(mainHandleNamespace), optionController(NULL), behaviorController(NULL)
00024 {
00025         // only created if call getTeleKybCore is successful.
00026         createOptionController();
00027 
00028         // Every Controller needs the OptionController!
00029         createBehaviorController();
00030 
00031 }
00032 
00033 TeleKybCore::~TeleKybCore()
00034 {
00035         if (optionController) { delete optionController; }
00036         if (behaviorController) { delete behaviorController; }
00037 }
00038 
00039 OptionController* TeleKybCore::getOptionController() const
00040 {
00041         return optionController;
00042 }
00043 
00044 BehaviorController* TeleKybCore::getBehaviorController() const
00045 {
00046         return behaviorController;
00047 }
00048 
00049 
00050 void TeleKybCore::createOptionController()
00051 {
00052         ros::ServiceClient client = mainNodeHandle.serviceClient<telekyb_srvs::StringOutput>(OPTION_GETOPTIONNODEHANDLE);
00053         telekyb_srvs::StringOutput soService;
00054         if (client.call(soService)) {
00055                 optionController = new OptionController(soService.response.output);
00056         } else {
00057                 ROS_ERROR_STREAM("Unable to create OptionController. Failed to call: " << client.getService());
00058                 ROS_FATAL("This is fatal!");
00059                 ros::shutdown();
00060         }
00061 }
00062 
00063 void TeleKybCore::createBehaviorController()
00064 {
00065         ros::ServiceClient client = mainNodeHandle.serviceClient<telekyb_srvs::StringOutput>(BEHAVIOR_GETBEHAVIORNODEHANDLE);
00066         telekyb_srvs::StringOutput soService;
00067         if (client.call(soService)) {
00068                 behaviorController = new BehaviorController(soService.response.output, optionController);
00069         } else {
00070                 ROS_ERROR_STREAM("Unable to create BehaviorController. Failed to call: " << client.getService());
00071         }
00072 }
00073 
00074 bool TeleKybCore::isOk() const
00075 {
00076         if (!behaviorController) {
00077                 return false;
00078         }
00079 
00080         if (!optionController) {
00081                 return false;
00082         }
00083 
00084 
00085         // everything seems ok.
00086         return true;
00087 }
00088 
00089 TeleKybCore* TeleKybCore::getTeleKybCore(int robotID_)
00090 {
00091         TeleKybCore* core = NULL;
00092 
00093         ros::NodeHandle robotIDNodeHandle( std::string(TELEKYB_BASENAME) + "/" + boost::lexical_cast<std::string>(robotID_));
00094         ros::ServiceClient client = robotIDNodeHandle.serviceClient<telekyb_srvs::StringOutput>(TELEKYB_SYSTEM_GETMAINNODEHANDLE);
00095 
00096         telekyb_srvs::StringOutput soService;
00097 
00098         // wait for it. (starting with roslaunch)
00099         if (! client.waitForExistence(ros::Duration(WAITTIME_TELEKYBSYSTEM)) ) {
00100                 ROS_ERROR_STREAM("Unable to create TeleKybCore " <<  robotID_ << ". Service not available. Service: " << client.getService());
00101                 return NULL;
00102         }
00103 
00104         if (client.call(soService)) {
00105                 core = new TeleKybCore(robotID_, soService.response.output);
00106         } else {
00107                 ROS_ERROR_STREAM("Unable to create TeleKybCore " <<  robotID_ << ". Failed to call: " << client.getService());
00108         }
00109 
00110         // check that core is ok
00111         if (core && ! core->isOk()) {
00112                 delete core;
00113                 core = NULL;
00114                 ROS_ERROR_STREAM("Unable to create TeleKybCore " <<  robotID_ << ". System reported initialization problem");
00115         }
00116 
00117         return core;
00118 }
00119 
00120 bool TeleKybCore::getTeleKybCoreMainNodeHandle(int robotID_, ros::NodeHandle& nodeHandle_, double waitTime_)
00121 {
00122         ros::NodeHandle robotIDNodeHandle( std::string(TELEKYB_BASENAME) + "/" + boost::lexical_cast<std::string>(robotID_));
00123         ros::ServiceClient client = robotIDNodeHandle.serviceClient<telekyb_srvs::StringOutput>(TELEKYB_SYSTEM_GETMAINNODEHANDLE);
00124         telekyb_srvs::StringOutput soService;
00125 
00126         if (waitTime_ > 0.0 && ! client.waitForExistence(ros::Duration(waitTime_)) ) {
00127                 ROS_ERROR_STREAM("Unable to find TeleKybCore " <<  robotID_ << ". Service not available. Service: " << client.getService());
00128                 return NULL;
00129         }
00130 
00131         if (client.call(soService)) {
00132                 nodeHandle_ = ros::NodeHandle(soService.response.output);
00133         } else {
00134                 ROS_ERROR_STREAM("Unable to find TeleKybCore " <<  robotID_ << ". Failed to call: " << client.getService());
00135                 return false;
00136         }
00137 
00138         return true;
00139 }
00140 
00141 }
00142 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


telekyb_interface
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:12:47