StateEstimatorController.cpp
Go to the documentation of this file.
00001 /*
00002  * StateEstimatorController.cpp
00003  *
00004  *  Created on: Nov 8, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include <tk_state/StateEstimatorController.hpp>
00009 
00010 #include <telekyb_base/ROS.hpp>
00011 
00012 namespace TELEKYB_NAMESPACE {
00013 
00014 // Init static class Loader
00015 //pluginlib::ClassLoader<StateEstimator> StateEstimatorController::seLoader(
00016 //              "tk_state", std::string( TELEKYB_NAMESPACE_STRING ) + "::StateEstimator" );
00017 
00018 StateEstimatorController::StateEstimatorController()
00019         : seLoader( "tk_state", std::string( TELEKYB_NAMESPACE_STRING ) + "::StateEstimator" ),
00020           activeStateEstimator( NULL ),
00021           recvFirstState( false ),
00022           nodeHandle( ROSModule::Instance().getMainNodeHandle(), TELEKYB_SENSOR_NODESUFFIX )
00023 {
00024         tkStatePublisher = nodeHandle.advertise<telekyb_msgs::TKState>(options.tPublisherTopic->getValue(), 1);
00025         transformStampedPub = nodeHandle.advertise<geometry_msgs::TransformStamped>(options.tTransformStampedTopic->getValue(), 10);
00026 
00027 }
00028 
00029 StateEstimatorController::~StateEstimatorController()
00030 {
00031         if (activeStateEstimator) {
00032                 activeStateEstimator->willBecomeInActive();
00033                 activeStateEstimator->destroy();
00034                 delete activeStateEstimator;
00035         }
00036 }
00037 
00038 void StateEstimatorController::initialize()
00039 {
00040         try {
00041                 activeStateEstimator = seLoader.createClassInstance(options.tPluginLookupName->getValue());
00042                 // Currently RunTime Switch is not supported. This has to be changed then.
00043                 activeStateEstimator->initialize();
00044                 activeStateEstimator->willBecomeActive();
00045 
00046         } catch (pluginlib::PluginlibException& e) {
00047                 ROS_FATAL("StateEstimator Plugin %s failed to load: %s", options.tPluginLookupName->getValue().c_str(), e.what());
00048                 //ROS_BREAK();
00049                 ros::shutdown();
00050         }
00051 }
00052 
00053 void StateEstimatorController::publishTransform(const TKState& tStateMsg)
00054 {
00055         tf::Transform transform;
00056         transform.setOrigin( tf::Vector3(tStateMsg.position(0),tStateMsg.position(1),tStateMsg.position(2)));
00057         transform.setRotation( tf::Quaternion(
00058                         tStateMsg.orientation.x(),
00059                         tStateMsg.orientation.y(),
00060                         tStateMsg.orientation.z(),
00061                         tStateMsg.orientation.w()));
00062 
00063         tfBroadCaster.sendTransform(
00064                         tf::StampedTransform(
00065                                         transform,
00066                                         tStateMsg.time.toRosTime(),
00067                                         options.tTransformParentID->getValue(),
00068                                         activeStateEstimator->getName()) // activeStateEstimator->getName()
00069         );
00070 }
00071 
00072 void StateEstimatorController::publishTransformStamped(const TKState& tStateMsg)
00073 {
00074         geometry_msgs::TransformStamped msg;
00075         msg.header.frame_id = activeStateEstimator->getName();
00076         msg.header.stamp = tStateMsg.time.toRosTime();
00077         msg.child_frame_id = options.tTransformParentID->getValue();
00078 
00079         msg.transform.translation.x = tStateMsg.position(0);
00080         msg.transform.translation.y = tStateMsg.position(1);
00081         msg.transform.translation.z = tStateMsg.position(2);
00082 
00083         msg.transform.rotation.w = tStateMsg.orientation.w();
00084         msg.transform.rotation.x = tStateMsg.orientation.x();
00085         msg.transform.rotation.y = tStateMsg.orientation.y();
00086         msg.transform.rotation.z = tStateMsg.orientation.z();
00087 
00088         transformStampedPub.publish(msg);
00089 }
00090 
00091 void StateEstimatorController::activeStateCallBack(const TKState& tStateMsg)
00092 {
00093         //ROS_INFO("activeStateCallBack called!");
00094         // high Priority. Publish
00095         telekyb_msgs::TKStatePtr tmStateMsg(new telekyb_msgs::TKState);
00096         tStateMsg.toTKStateMsg(*tmStateMsg);
00097         tkStatePublisher.publish(tmStateMsg);
00098 
00099         // lower prio. Save State
00100         boost::mutex::scoped_lock(lastStateMutex);
00101         lastState = tStateMsg;
00102 
00103         // Ok this is always written. Maybe there is a nice way to improve this.
00104         recvFirstState = true;
00105 
00106         if (options.tPublishRosTransform->getValue()) {
00107                 publishTransform(tStateMsg);
00108         }
00109 
00110         if (options.tPublishRosTransformStamped->getValue()) {
00111                 publishTransformStamped(tStateMsg);
00112         }
00113 }
00114 
00115 const StateEstimator* StateEstimatorController::getActiveStateEstimator() const
00116 {
00117         return activeStateEstimator;
00118 }
00119 
00120 std::string StateEstimatorController::getSePublisherTopic() const
00121 {
00122         return tkStatePublisher.getTopic();
00123 }
00124 
00125 bool StateEstimatorController::waitForFirstState(Time timeout) const
00126 {
00127         Time rate(0.1); // 1/10 s
00128         while(ros::ok() && timeout.toDSec() > 0.0) {
00129                 //ROS_INFO_STREAM("Timeout: " << timeout.toString());
00130                 // check
00131                 if ( recvFirstState ) {
00132                         // success
00133                         return true;
00134                 }
00135 
00136                 timeout -= rate;
00137                 // sleep
00138                 rate.sleep();
00139         }
00140 
00141         // did not get first state in timeout
00142         return false;
00143 }
00144 
00145 TKState StateEstimatorController::getLastState() const
00146 {
00147         boost::mutex::scoped_lock(lastStateMutex);
00148         return lastState;
00149 }
00150 
00151 const ros::NodeHandle& StateEstimatorController::getSensorNodeHandle() const
00152 {
00153         return nodeHandle;
00154 }
00155 
00156 
00157 //---------------
00158 // Singleton Stuff
00159 StateEstimatorController* StateEstimatorController::instance = NULL;
00160 
00161 StateEstimatorController& StateEstimatorController::Instance() {
00162         if (!instance) {
00163                 instance = new StateEstimatorController();
00164                 instance->initialize();
00165         }
00166         return *instance;
00167 }
00168 
00169 const StateEstimatorController* StateEstimatorController::InstancePtr() {
00170         if (!instance) {
00171                 instance = new StateEstimatorController();
00172                 instance->initialize();
00173         }
00174 
00175         return instance;
00176 }
00177 
00178 bool StateEstimatorController::HasInstance()
00179 {
00180         return (instance != NULL);
00181 }
00182 
00183 void StateEstimatorController::ShutDownInstance() {
00184         if (instance) {
00185                 delete instance;
00186         }
00187 
00188         instance = NULL;
00189 }
00190 
00191 
00192 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


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