StandardTrajectoryTracker.cpp
Go to the documentation of this file.
00001 /*
00002  * StandardTrajectoryTracker.cpp
00003  *
00004  *  Created on: Nov 8, 2011
00005  *      Author: mriedel
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 // Options
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         // Important first create Publisher, before receiving CallBacks
00059         tTcCommandsPub = commandNodeHandle.advertise<telekyb_msgs::TKCommands>(options.tCommandsTopic->getValue(),1);
00060 
00061         // CurrentState
00062         currentInput.setAcceleration( Acceleration3D(0.0, 0.0, GRAVITY) );
00063         currentInput.setYawRate(0.0);
00064 
00065         //std::string tkStateTopicName = StateEstimatorController::Instance().getSePublisherTopic();
00066         //tTcStateSub = nodeHandle.subscribe(tkStateTopicName,1,&StandardTrajectoryTracker::tkStateCB, this);
00067 
00068         try {
00069                 massEstimator = meLoader.createClassInstance(options.tPluginLookupName->getValue());
00070                 // Currently RunTime Switch is not supported. This has to be changed then.
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                 //ROS_BREAK();
00076                 ros::shutdown();
00077         }
00078 
00079         // Get Option
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         // fill currentMass with Initial Value!
00087         currentMass = massEstimator->getInitialMass();
00088 
00089         // sanity check
00090 //      TrajectoryTracker *tracker = new StandardTrajectoryTracker();
00091 //      delete tracker;
00092 }
00093 //      virtual void willBecomeActive() = 0;
00094 //      virtual void willBecomeInActive() = 0;
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         // new State Message. Triggers control step!
00115         //ROS_INFO("Received new TKState!");
00116         Vector3D rpyOrientation = state.getEulerRPY();
00117 
00118 
00119         // Position Control
00120         PosCtrlOutput pcOutput;
00121         // lock
00122         boost::mutex::scoped_lock currentInputLock(currentInputMutex);
00123         positionControl.run(currentInput, state, currentMass, pcOutput);
00124 
00125         // Yaw Control
00126         YawCtrlOutput ycOutput;
00127         yawControl.run(currentInput, state, ycOutput);
00128         // unlock
00129         currentInputLock.unlock();
00130 
00131 
00132 
00133         //ROS_INFO("Roll: %f, Pitch: %f, Thrust %f", pcOutput.comRoll, pcOutput.comPitch, pcOutput.comThrust);
00134 
00135         // Mass Estimation only when enabled.
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); // z
00142 
00143                 MassEstimOutput meOutput;
00144                 massEstimator->run(meInput, meOutput);
00145                 // update Mass.
00146                 currentMass = meOutput.estMass;
00147         }
00148 
00149 
00150         //positionControl.setMass(meOutput.estMass);
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         // mass
00161         cmdMsg.mass = currentMass;
00162 
00163 
00164         tTcCommandsPub.publish(cmdMsg);
00165 
00166 
00167 }
00168 
00169 
00170 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


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