00001 /* 00002 * BehaviorController.cpp 00003 * 00004 * Created on: Nov 3, 2011 00005 * Author: mriedel 00006 */ 00007 00008 #include <tk_behavior/BehaviorController.hpp> 00009 #include <telekyb_base/ROS.hpp> 00010 00011 #include <telekyb_defines/physic_defines.hpp> 00012 00013 // VERY TEMPORARY! 00014 #include <tk_trajprocessor/TrajectoryProcessorController.hpp> 00015 00016 namespace TELEKYB_NAMESPACE { 00017 00018 BehaviorController::BehaviorController() 00019 : behaviorNodeHandle( ROSModule::Instance().getMainNodeHandle(), TELEKYB_BEHAVIOR_NODESUFFIX ), 00020 activeBehavior( NULL ), 00021 trajProcCtrl( TrajectoryProcessorController::Instance() ) // TEMPORARY!!! 00022 { 00023 bcInterface = new BehaviorControllerInterface(*this); 00024 00025 00026 // Default lastInput for Ground. 00027 lastInput.setAcceleration( Acceleration3D(0.0, 0.0, GRAVITY) ); 00028 lastInput.setYawRate(0.0); 00029 } 00030 00031 //** This is like a Constructor. It's called by the Singleton creator DIRECTLY AFTER the actual constructor. **/ 00032 void BehaviorController::initialize() 00033 { 00034 systemBehaviorContainer = new SystemBehaviorContainer(); 00035 // Start in Ground Behavior 00036 activeBehavior = new ActiveBehaviorContainer( systemBehaviorContainer->getGround() ); 00037 00038 // tkStateTopicName is an absolute PATH! 00039 std::string tkStateTopicName = StateEstimatorController::Instance().getSePublisherTopic(); 00040 tStateSub = behaviorNodeHandle.subscribe(tkStateTopicName,1,&BehaviorController::tkStateCB, this); 00041 } 00042 00043 void BehaviorController::tkStateCB(const telekyb_msgs::TKState::ConstPtr& tkStateMsg) 00044 { 00045 TKState currentState(*tkStateMsg); 00046 00047 // This is where the magic happens 00048 activeBehavior->trajectoryStep(currentState, lastInput); 00049 00050 // send to ROS 00051 bcInterface->publishTKTrajectory(lastInput); 00052 // send to receiver. 00053 trajProcCtrl.trajInputStep(lastInput); 00054 } 00055 00056 BehaviorController::~BehaviorController() 00057 { 00058 delete bcInterface; 00059 delete activeBehavior; 00060 delete systemBehaviorContainer; 00061 } 00062 00063 00064 const ros::NodeHandle& BehaviorController::getBehaviorNodeHandle() const 00065 { 00066 return behaviorNodeHandle; 00067 } 00068 00069 Behavior* BehaviorController::getActiveBehavior() const 00070 { 00071 return activeBehavior->getActive(); 00072 } 00073 00074 bool BehaviorController::switchToBehavior(Behavior* newBehavior) 00075 { 00076 return activeBehavior->switchToBehavior(newBehavior); 00077 } 00078 00079 const SystemBehaviorContainer& BehaviorController::getSystemBehaviorContainer() const 00080 { 00081 return *systemBehaviorContainer; 00082 } 00083 00084 bool BehaviorController::switchToNormalBrake() 00085 { 00086 return activeBehavior->switchToBehavior(systemBehaviorContainer->getNormalBrake()); 00087 } 00088 00089 bool BehaviorController::switchToEmergencyLand() 00090 { 00091 return activeBehavior->switchToBehavior(systemBehaviorContainer->getEmergencyLand()); 00092 } 00093 00094 void BehaviorController::activeBehaviorChanged() 00095 { 00096 Behavior* newActive = activeBehavior->getActive(); 00097 00098 // Turn TrajProcCtrl if not in Air 00099 if (trajProcCtrl.isActive() && newActive->getType() != BehaviorType::Air) { 00100 trajProcCtrl.setInActive(); 00101 } 00102 if (!trajProcCtrl.isActive() && newActive->getType() == BehaviorType::Air) { 00103 trajProcCtrl.setActive(this); 00104 } 00105 00106 // tell the world about it 00107 bcInterface->publishActiveBehavior(); 00108 } 00109 00110 00111 00112 //--------------- 00113 // Singleton Stuff 00114 BehaviorController* BehaviorController::instance = NULL; 00115 00116 BehaviorController& BehaviorController::Instance() { 00117 if (!instance) { 00118 instance = new BehaviorController(); 00119 instance->initialize(); 00120 } 00121 return *instance; 00122 } 00123 00124 const BehaviorController* BehaviorController::InstancePtr() { 00125 if (!instance) { 00126 instance = new BehaviorController(); 00127 instance->initialize(); 00128 } 00129 00130 return instance; 00131 } 00132 00133 bool BehaviorController::HasInstance() 00134 { 00135 return (instance != NULL); 00136 } 00137 00138 void BehaviorController::ShutDownInstance() { 00139 if (instance) { 00140 delete instance; 00141 } 00142 00143 instance = NULL; 00144 } 00145 00146 00147 }