Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008 #include <telekyb_interface/BehaviorController.hpp>
00009
00010 #include <telekyb_srvs/StringArrayOutput.h>
00011 #include <telekyb_srvs/BehaviorInput.h>
00012 #include <telekyb_srvs/BehaviorOutput.h>
00013 #include <telekyb_srvs/BehaviorInputOutput.h>
00014
00015
00016 namespace TELEKYB_INTERFACE_NAMESPACE {
00017
00018 BehaviorController::BehaviorController(const std::string& behaviorHandleNamespace, OptionController* optionController_)
00019 : behaviorControllerNodeHandle(behaviorHandleNamespace), optionController(optionController_), listener(NULL)
00020 {
00021 ROS_INFO_STREAM("Created BehaviorController Nodehandle: " << behaviorHandleNamespace);
00022
00023
00024 activeBehavior = getActiveBehavior();
00025
00026
00027 activeBehaviorSub = behaviorControllerNodeHandle.subscribe(BEHAVIOR_BEHAVIORCHANGETOPIC, 10, &BehaviorController::activeBehaviorCallback ,this);
00028 }
00029
00030 BehaviorController::~BehaviorController()
00031 {
00032
00033 }
00034
00035 const ros::NodeHandle& BehaviorController::getNodeHandle() const
00036 {
00037 return behaviorControllerNodeHandle;
00038 }
00039
00040 OptionController* BehaviorController::getOptionController() const
00041 {
00042 return optionController;
00043 }
00044
00045
00046 Behavior BehaviorController::getSystemBehavior(const std::string& behaviorName)
00047 {
00048 ros::ServiceClient client = behaviorControllerNodeHandle.serviceClient<telekyb_srvs::BehaviorInputOutput>(BEHAVIOR_GETSYSTEMBEHAVIOR);
00049 telekyb_srvs::BehaviorInputOutput service;
00050 service.request.behaviorName = behaviorName;
00051 if (! client.call(service) ) {
00052 ROS_ERROR_STREAM("Failed to call: " << client.getService() << "! Returning NULL-Behavior.");
00053 return Behavior();
00054 }
00055
00056 return Behavior(service.response.behaviorID, service.response.behaviorName, this);
00057 }
00058
00059 bool BehaviorController::getAvailableBehaviors(std::vector<std::string>& behaviorNames)
00060 {
00061 ros::ServiceClient client = behaviorControllerNodeHandle.serviceClient<telekyb_srvs::StringArrayOutput>(BEHAVIOR_GETAVAILABLEBEHAVIORS);
00062 telekyb_srvs::StringArrayOutput service;
00063 if (! client.call(service) ) {
00064 ROS_ERROR_STREAM("Failed to call: " << client.getService());
00065 return false;
00066 }
00067
00068 behaviorNames = service.response.output;
00069 return true;
00070 }
00071
00072 Behavior BehaviorController::loadBehavior(const std::string& behaviorName)
00073 {
00074 ros::ServiceClient client = behaviorControllerNodeHandle.serviceClient<telekyb_srvs::BehaviorInputOutput>(BEHAVIOR_LOADBEHAVIOR);
00075 telekyb_srvs::BehaviorInputOutput service;
00076 service.request.behaviorName = behaviorName;
00077 if (! client.call(service) ) {
00078 ROS_ERROR_STREAM("Failed to call: " << client.getService() << "! Returning NULL-Behavior.");
00079 return Behavior();
00080 }
00081
00082 return Behavior(service.response.behaviorID, service.response.behaviorName, this);
00083 }
00084 bool BehaviorController::unloadBehavior(Behavior& behavior)
00085 {
00086 ros::ServiceClient client = behaviorControllerNodeHandle.serviceClient<telekyb_srvs::BehaviorInput>(BEHAVIOR_UNLOADBEHAVIOR);
00087 telekyb_srvs::BehaviorInput service;
00088 service.request.behaviorID = behavior.getBehaviorID();
00089 if (! client.call(service) ) {
00090 ROS_ERROR_STREAM("Failed to call: " << client.getService());
00091 return false;
00092 }
00093
00094
00095 behavior.setNull();
00096
00097 return true;
00098 }
00099
00100
00101 bool BehaviorController::switchBehavior(const Behavior& behavior)
00102 {
00103 ros::ServiceClient client = behaviorControllerNodeHandle.serviceClient<telekyb_srvs::BehaviorInput>(BEHAVIOR_SWITCHBEHAVIOR);
00104 telekyb_srvs::BehaviorInput service;
00105 service.request.behaviorID = behavior.getBehaviorID();
00106 if (! client.call(service) ) {
00107 ROS_ERROR_STREAM("Failed to call: " << client.getService());
00108 return false;
00109 }
00110 return true;
00111 }
00112
00113 Behavior BehaviorController::getActiveBehavior()
00114 {
00115 ros::ServiceClient client = behaviorControllerNodeHandle.serviceClient<telekyb_srvs::BehaviorOutput>(BEHAVIOR_GETACTIVEBEHAVIOR);
00116 telekyb_srvs::BehaviorOutput service;
00117 if (! client.call(service) ) {
00118 ROS_ERROR_STREAM("Failed to call: " << client.getService() << "! Returning NULL-Behavior.");
00119 return Behavior();
00120 }
00121
00122 return Behavior(service.response.behaviorID, service.response.behaviorName, this);
00123 }
00124
00125 Behavior* BehaviorController::getActiveBehaviorPointer()
00126 {
00127 return &activeBehavior;
00128 }
00129
00130 const Behavior& BehaviorController::getActiveBehaviorReference() const
00131 {
00132 return activeBehavior;
00133 }
00134
00135 void BehaviorController::activeBehaviorCallback(const telekyb_msgs::Behavior::ConstPtr& msg)
00136 {
00137 ROS_INFO_STREAM("Active Behavior was changed to: " << msg->behaviorName);
00138 activeBehavior.behaviorID = msg->behaviorID;
00139 activeBehavior.behaviorName = msg->behaviorName;
00140
00141 if (listener) {
00142 listener->activeBehaviorChanged(activeBehavior);
00143 }
00144 }
00145
00146 void BehaviorController::setActiveBehaviorListener(ActiveBehaviorListener* listener_)
00147 {
00148 listener = listener_;
00149 }
00150
00151 }