BehaviorControllerInterface.cpp
Go to the documentation of this file.
00001 /*
00002  * BehaviorInterface.cpp
00003  *
00004  *  Created on: Nov 10, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include <tk_behavior/BehaviorControllerInterface.hpp>
00009 
00010 #include <tk_behavior/BehaviorController.hpp>
00011 
00012 #include <telekyb_msgs/Behavior.h>
00013 #include <telekyb_msgs/TKTrajectory.h>
00014 
00015 namespace TELEKYB_NAMESPACE {
00016 
00017 BehaviorControllerInterface::BehaviorControllerInterface(BehaviorController& behaviorController_)
00018         : behaviorController( behaviorController_ ), nodeHandle(behaviorController.getBehaviorNodeHandle())
00019 {
00020         // TODO Auto-generated constructor stub
00021         getAvailableBehaviorsClient = nodeHandle.advertiseService(
00022                         BEHAVIOR_GETAVAILABLEBEHAVIORS, &BehaviorControllerInterface::getAvailableBehaviorsCB , this);
00023         loadBehavior = nodeHandle.advertiseService(
00024                         BEHAVIOR_LOADBEHAVIOR, &BehaviorControllerInterface::loadBehaviorCB , this);
00025         unloadBehavior = nodeHandle.advertiseService(
00026                         BEHAVIOR_UNLOADBEHAVIOR, &BehaviorControllerInterface::unloadBehaviorCB , this);
00027         switchBehavior = nodeHandle.advertiseService(
00028                         BEHAVIOR_SWITCHBEHAVIOR, &BehaviorControllerInterface::switchBehaviorCB , this);
00029         getSystemBehavior = nodeHandle.advertiseService(
00030                         BEHAVIOR_GETSYSTEMBEHAVIOR, &BehaviorControllerInterface::getSystemBehaviorCB , this);
00031         getActiveBehavior = nodeHandle.advertiseService(
00032                         BEHAVIOR_GETACTIVEBEHAVIOR, &BehaviorControllerInterface::getActiveBehaviorCB , this);
00033         emergencyLand = nodeHandle.advertiseService(
00034                         BEHAVIOR_EMERGENCYLAND, &BehaviorControllerInterface::emergencyLandCB , this);
00035         normalBrake = nodeHandle.advertiseService(
00036                         BEHAVIOR_NORMALBRAKE, &BehaviorControllerInterface::normalBrakeCB , this);
00037 
00038         activeBehaviorPub = nodeHandle.advertise<telekyb_msgs::Behavior>(BEHAVIOR_BEHAVIORCHANGETOPIC, 1);
00039         trajectoryPub = nodeHandle.advertise<telekyb_msgs::TKTrajectory>(BEHAVIOR_BEHAVIORTRAJECTORY, 1);
00040 }
00041 
00042 BehaviorControllerInterface::~BehaviorControllerInterface() {
00043         // TODO Auto-generated destructor stub
00044 }
00045 
00046 bool BehaviorControllerInterface::getAvailableBehaviorsCB(
00047                 telekyb_srvs::StringArrayOutput::Request& request,
00048                 telekyb_srvs::StringArrayOutput::Response& response)
00049 {
00050         behaviorContainer.getAvailableBehaviors(response.output);
00051         return true;
00052 }
00053 
00054 bool BehaviorControllerInterface::loadBehaviorCB(
00055                 telekyb_srvs::BehaviorInputOutput::Request& request,
00056                 telekyb_srvs::BehaviorInputOutput::Response& response)
00057 {
00058         const Behavior *b = behaviorContainer.loadBehavior(request.behaviorName);
00059         response.behaviorID = (uint64_t)b;
00060         if ( b ) { response.behaviorName = b->getName(); } // this should be identical to input
00061         return b != NULL;
00062 }
00063 
00064 bool BehaviorControllerInterface::unloadBehaviorCB(
00065                 telekyb_srvs::BehaviorInput::Request& request,
00066                 telekyb_srvs::BehaviorInput::Response& response)
00067 {
00068         return behaviorContainer.unloadBehavior(Behavior::behaviorFromID(request.behaviorID));
00069 }
00070 
00071 bool BehaviorControllerInterface::switchBehaviorCB(
00072                 telekyb_srvs::BehaviorInput::Request& request,
00073                 telekyb_srvs::BehaviorInput::Response& response)
00074 {
00075         return behaviorController.switchToBehavior(Behavior::behaviorFromID(request.behaviorID));
00076 }
00077 
00078 bool BehaviorControllerInterface::getSystemBehaviorCB(
00079                 telekyb_srvs::BehaviorInputOutput::Request& request,
00080                 telekyb_srvs::BehaviorInputOutput::Response& response)
00081 {
00082         Behavior* b = behaviorController.getSystemBehaviorContainer().getBehaviorByName(request.behaviorName);
00083         // be can be NULL
00084         response.behaviorID = Behavior::behaviorToID(b);
00085         if ( b ) { response.behaviorName = b->getName(); }  // this should be identical to input
00086         return b != NULL;
00087 }
00088 
00089 bool BehaviorControllerInterface::getActiveBehaviorCB(
00090                 telekyb_srvs::BehaviorOutput::Request& request,
00091                 telekyb_srvs::BehaviorOutput::Response& response)
00092 {
00093         // b IS ALWAYS VALID!
00094         Behavior* b = behaviorController.getActiveBehavior();
00095         // b cannot be NULL
00096         response.behaviorID = b->getID();
00097         response.behaviorName = b->getName();
00098         return true;
00099 }
00100 
00101 bool BehaviorControllerInterface::emergencyLandCB(
00102                 std_srvs::Empty::Request& request,
00103                 std_srvs::Empty::Response& response)
00104 {
00105         return behaviorController.switchToEmergencyLand();
00106 }
00107 
00108 bool BehaviorControllerInterface::normalBrakeCB(
00109                 std_srvs::Empty::Request& request,
00110                 std_srvs::Empty::Response& response)
00111 {
00112         return behaviorController.switchToNormalBrake();
00113 }
00114 
00115 void BehaviorControllerInterface::publishActiveBehavior()
00116 {
00117         // b IS ALWAYS VALID!
00118         Behavior* b = behaviorController.getActiveBehavior();
00119         telekyb_msgs::Behavior msg;
00120         msg.header.stamp = ros::Time::now();
00121         msg.behaviorID = b->getID();
00122         msg.behaviorName = b->getName();
00123 
00124         activeBehaviorPub.publish(msg);
00125 }
00126 
00127 void BehaviorControllerInterface::publishTKTrajectory(const TKTrajectory& trajectory)
00128 {
00129         telekyb_msgs::TKTrajectory msg;
00130         msg.header.stamp = ros::Time::now();
00131         trajectory.toTKTrajMsg(msg);
00132 
00133         trajectoryPub.publish(msg);
00134 }
00135 
00136 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


tk_behavior
Author(s): Dr. Antonio Franchi and Martin Riedel
autogenerated on Mon Nov 11 2013 11:13:36