Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 #include "StandardTrajectoryTracker.hpp"
00009 
00010 #include <telekyb_base/ROS.hpp>
00011 #include <telekyb_msgs/TKCommands.h>
00012 
00013 
00014 #include <tk_state/StateEstimatorController.hpp>
00015 
00016 #include <telekyb_defines/physic_defines.hpp>
00017 
00018 #include <pluginlib/class_list_macros.h>
00019 
00020 PLUGINLIB_DECLARE_CLASS(tk_trajctrl, StandardTrajectoryTracker, TELEKYB_NAMESPACE::StandardTrajectoryTracker, TELEKYB_NAMESPACE::TrajectoryTracker);
00021 
00022 namespace TELEKYB_NAMESPACE {
00023 
00024 
00025 
00026 StandardTrajectoryTrackerOptions::StandardTrajectoryTrackerOptions()
00027         : OptionContainer("StandardTrajectoryTracker")
00028 {
00029         tCommandsTopic = addOption<std::string>("tCommandsTopic","Topic for publishing telekyb_msgs::TKCommands",
00030                         "commands", false, true);
00031         tPluginLookupName = addOption<std::string>("tPluginLookupName",
00032                         "Specifies the Mass Estimation Plugin for the " + getOptionContainerNamespace(),
00033                         "tk_param_estimator/StandardMassEstimator", false, true);
00034 
00035 }
00036 
00037 
00038 StandardTrajectoryTracker::StandardTrajectoryTracker()
00039         : tDoMassEstimation( NULL ),
00040           meLoader( "tk_param_estimator", std::string( TELEKYB_NAMESPACE_STRING ) + "::MassEstimator" ),
00041           massEstimator( NULL ),
00042           nodeHandle( ROSModule::Instance().getMainNodeHandle() ),
00043           commandNodeHandle( nodeHandle, TELEKYB_COMMAND_NODESUFFIX )
00044 {
00045 
00046 }
00047 
00048 StandardTrajectoryTracker::~StandardTrajectoryTracker()
00049 {
00050         if (massEstimator) {
00051                 massEstimator->destroy();
00052                 delete massEstimator;
00053         }
00054 }
00055 
00056 void StandardTrajectoryTracker::initialize()
00057 {
00058         
00059         tTcCommandsPub = commandNodeHandle.advertise<telekyb_msgs::TKCommands>(options.tCommandsTopic->getValue(),1);
00060 
00061         
00062         currentInput.setAcceleration( Acceleration3D(0.0, 0.0, GRAVITY) );
00063         currentInput.setYawRate(0.0);
00064 
00065         
00066         
00067 
00068         try {
00069                 massEstimator = meLoader.createClassInstance(options.tPluginLookupName->getValue());
00070                 
00071                 massEstimator->initialize();
00072 
00073         } catch (pluginlib::PluginlibException& e) {
00074                 ROS_FATAL("Trajectory Tracker %s failed to load: %s", options.tPluginLookupName->getValue().c_str(), e.what());
00075                 
00076                 ros::shutdown();
00077         }
00078 
00079         
00080         tDoMassEstimation = OptionContainer::getGlobalOptionByName<bool>("TrajectoryController","tDoMassEstimation");
00081         if (!tDoMassEstimation) {
00082                 ROS_ERROR("Unable to get Option TrajectoryController/tDoMassEstimation. Quitting...");
00083                 ros::shutdown();
00084         }
00085 
00086         
00087         currentMass = massEstimator->getInitialMass();
00088 
00089         
00090 
00091 
00092 }
00093 
00094 
00095 void StandardTrajectoryTracker::destroy()
00096 {
00097 
00098 }
00099 
00100 std::string StandardTrajectoryTracker::getName() const
00101 {
00102         return "StandardTrajectoryTracker";
00103 }
00104 
00105 
00106 void StandardTrajectoryTracker::trajectoryCB(const TKTrajectory& trajectory)
00107 {
00108         boost::mutex::scoped_lock currentInputLock(currentInputMutex);
00109         currentInput = trajectory;
00110 }
00111 
00112 void StandardTrajectoryTracker::stateCB(const TKState& state)
00113 {
00114         
00115         
00116         Vector3D rpyOrientation = state.getEulerRPY();
00117 
00118 
00119         
00120         PosCtrlOutput pcOutput;
00121         
00122         boost::mutex::scoped_lock currentInputLock(currentInputMutex);
00123         positionControl.run(currentInput, state, currentMass, pcOutput);
00124 
00125         
00126         YawCtrlOutput ycOutput;
00127         yawControl.run(currentInput, state, ycOutput);
00128         
00129         currentInputLock.unlock();
00130 
00131 
00132 
00133         
00134 
00135         
00136         if (tDoMassEstimation->getValue()) {
00137                 MassEstimInput meInput;
00138                 meInput.roll = rpyOrientation(0);
00139                 meInput.pitch = rpyOrientation(1);
00140                 meInput.thrust = pcOutput.comThrust;
00141                 meInput.vertVel = state.linVelocity(2); 
00142 
00143                 MassEstimOutput meOutput;
00144                 massEstimator->run(meInput, meOutput);
00145                 
00146                 currentMass = meOutput.estMass;
00147         }
00148 
00149 
00150         
00151 
00152 
00153         telekyb_msgs::TKCommands cmdMsg;
00154 
00155         cmdMsg.header.stamp = ros::Time::now();
00156         cmdMsg.roll = pcOutput.comRoll;
00157         cmdMsg.pitch = pcOutput.comPitch;
00158         cmdMsg.yaw = ycOutput.comYaw;
00159         cmdMsg.thrust = pcOutput.comThrust;
00160         
00161         cmdMsg.mass = currentMass;
00162 
00163 
00164         tTcCommandsPub.publish(cmdMsg);
00165 
00166 
00167 }
00168 
00169 
00170 }