TrajectoryModuleContainer.cpp
Go to the documentation of this file.
00001 /*
00002  * TrajectoryModuleContainer.cpp
00003  *
00004  *  Created on: Dec 13, 2011
00005  *      Author: mriedel
00006  */
00007 
00008 #include <tk_trajprocessor/TrajectoryModuleContainer.hpp>
00009 
00010 #include <boost/foreach.hpp>
00011 
00012 namespace TELEKYB_NAMESPACE {
00013 
00014 TrajectoryModuleContainer::TrajectoryModuleContainer()
00015         : trajectoryModuleLoader("tk_trajprocessor", std::string( TELEKYB_NAMESPACE_STRING ) + "::TrajectoryModule")
00016 {
00017         // TODO Auto-generated constructor stub
00018 
00019 }
00020 
00021 TrajectoryModuleContainer::~TrajectoryModuleContainer()
00022 {
00023         // delete all Behaviors that are still loaded
00024         BOOST_FOREACH(TrajectoryModule *tm, trajectoryModuleInstances) {
00025                 unLoadTrajectoryModule(tm);
00026         }
00027 }
00028 
00029 
00030 void TrajectoryModuleContainer::insertPriorityLists(TrajectoryModule* tm)
00031 {
00032         ROS_INFO_STREAM("Adding Trajectory Module " << tm->getName() << " to " << tm->getType().str());
00033         switch (tm->getType().index()) {
00034                 case TrajModulePosType::Position:
00035                         insertPriorityElementIntoList(tm, positionPriorityList);
00036                         break;
00037                 case TrajModulePosType::Velocity:
00038                         insertPriorityElementIntoList(tm, velocityPriorityList);
00039                         break;
00040                 case TrajModulePosType::Acceleration:
00041                         insertPriorityElementIntoList(tm, accelerationPriorityList);
00042                         break;
00043                 case TrajModulePosType::All:
00044                         // Insert into all!
00045                         insertPriorityElementIntoList(tm, positionPriorityList);
00046                         insertPriorityElementIntoList(tm, velocityPriorityList);
00047                         insertPriorityElementIntoList(tm, accelerationPriorityList);
00048                         insertPriorityElementIntoList(tm, otherPriorityList);
00049                         break;
00050                 default:
00051                         break;
00052         }
00053 }
00054 
00055 void TrajectoryModuleContainer::removePriorityLists(TrajectoryModule* tm)
00056 {
00057         switch (tm->getType().index()) {
00058                 case TrajModulePosType::Position:
00059                         positionPriorityList.remove(tm);
00060                         break;
00061                 case TrajModulePosType::Velocity:
00062                         velocityPriorityList.remove(tm);
00063                         break;
00064                 case TrajModulePosType::Acceleration:
00065                         accelerationPriorityList.remove(tm);
00066                         break;
00067                 case TrajModulePosType::All:
00068                         // Insert into all!
00069                         positionPriorityList.remove(tm);
00070                         velocityPriorityList.remove(tm);
00071                         accelerationPriorityList.remove(tm);
00072                         otherPriorityList.remove(tm);
00073                         break;
00074                 default:
00075                         break;
00076         }
00077 
00078 }
00079 
00080 void TrajectoryModuleContainer::insertPriorityElementIntoList(TrajectoryModule* tm, std::list<TrajectoryModule*>& list)
00081 {
00082         //ROS_INFO_STREAM("Adding " << tm->getName());
00083         std::list<TrajectoryModule*>::iterator it = list.begin();
00084         for (; it != list.end(); it++) {
00085                 if (tm->getPriority() <= (*it)->getPriority()) {
00086                         // WARN if same Prio!!!
00087                         if (tm->getPriority() == (*it)->getPriority()) {
00088                                 ROS_WARN_STREAM("TrajectoryModule " << tm->getName()
00089                                                 << " and " << (*it)->getName() << "have equal Priority (" << tm->getPriority() << " != 0)");
00090                         }
00091 
00092                         // insert
00093                         list.insert(it, tm);
00094                         break;
00095                 }
00096         }
00097 
00098         // end?
00099         if (it == list.end()) {
00100                 // either empty, or priority was highest so far
00101                 list.push_back(tm);
00102         }
00103 
00104 }
00105 
00106 // should be const, but getDeclaredClasses is declared wrongly.
00107 void TrajectoryModuleContainer::getAvailableTrajectoryModules(std::vector<std::string>& trajectoryModuleClassNames)
00108 {
00109         trajectoryModuleClassNames = trajectoryModuleLoader.getDeclaredClasses();
00110 }
00111 
00112 // returns 0 if fails
00113 TrajectoryModule* TrajectoryModuleContainer::loadTrajectoryModule(const std::string& trajectoryModuleClassName)
00114 {
00115         TrajectoryModule *tm = NULL;
00116         try {
00117                 tm = trajectoryModuleLoader.createClassInstance(trajectoryModuleClassName);
00118 
00119                 // inform behavior about its name.
00120                 //b->setName(behaviorClassName);
00121                 // initialize
00122                 tm->initialize();
00123 
00124                 // add local
00125                 trajectoryModuleInstances.insert(tm);
00126                 insertPriorityLists(tm);
00127 
00128                 // success
00129                 //ROS_INFO_STREAM("Successfully loaded Behavior: " << behaviorClassName << ", Instance: " << (long)b);
00130         } catch (pluginlib::PluginlibException &e) {
00131                 ROS_ERROR_STREAM("TrajectoryModule Plugin " << trajectoryModuleClassName << " failed to load. Message: " << e.what());
00132         }
00133 
00134         return tm;
00135 }
00136 
00137 void TrajectoryModuleContainer::unLoadTrajectoryModule(TrajectoryModule* tm)
00138 {
00139         ROS_INFO_STREAM("Unloading Trajectory Module: " << tm->getName());
00140         if(trajectoryModuleInstances.erase(tm)) {
00141                 // remove from Priolist
00142                 removePriorityLists(tm);
00143                 // Set Inactive
00144                 tm->setInactive();
00145                 // Inform behavior
00146                 tm->destroy();
00147                 // delete
00148                 delete tm;
00149         }
00150 }
00151 
00152 void TrajectoryModuleContainer::activateAllTrajectoryModules()
00153 {
00154         BOOST_FOREACH(TrajectoryModule *tm, trajectoryModuleInstances) {
00155                 if (!tm->isActive()) tm->setActive();
00156         }
00157 }
00158 
00159 void TrajectoryModuleContainer::deactivateAllTrajectoryModules()
00160 {
00161         BOOST_FOREACH(TrajectoryModule *tm, trajectoryModuleInstances) {
00162                 if (tm->isActive()) tm->setInactive();
00163         }
00164 }
00165 
00166 // Steps
00167 void TrajectoryModuleContainer::trajectoryStepPosition(const TKState& currentState, TKTrajectory& trajInput)
00168 {
00169         // working  through list
00170         std::list<TrajectoryModule*>::iterator it;
00171         for (it = positionPriorityList.begin(); it != positionPriorityList.end(); it++) {
00172                 //ROS_ERROR("Calling TM %s\n", (*it)->getName().c_str());
00173                 if ( !(*it)->trajectoryStep(currentState, trajInput) ) {
00174                         ROS_ERROR("TM Module %s canceled further list execution\n", (*it)->getName().c_str());
00175                         // Trajectory Module told us to not check the later conditions
00176                         break;
00177                 }
00178         }
00179 }
00180 void TrajectoryModuleContainer::trajectoryStepVelocity(const TKState& currentState, TKTrajectory& trajInput)
00181 {
00182         //ROS_INFO("Cycling VM List:");
00183         std::list<TrajectoryModule*>::iterator it;
00184         for (it = velocityPriorityList.begin(); it != velocityPriorityList.end(); it++) {
00185                 //ROS_INFO_STREAM("Current List: " << (*it)->getName());
00186                 if ( !(*it)->trajectoryStep(currentState, trajInput) ) {
00187                         ROS_ERROR("TM Module %s canceled further list execution\n", (*it)->getName().c_str());
00188                         // Trajectory Module told us to not check the later conditions
00189                         break;
00190                 }
00191         }
00192 }
00193 void TrajectoryModuleContainer::trajectoryStepAcceleration(const TKState& currentState, TKTrajectory& trajInput)
00194 {
00195         std::list<TrajectoryModule*>::iterator it;
00196         for (it = accelerationPriorityList.begin(); it != accelerationPriorityList.end(); it++) {
00197                 if ( !(*it)->trajectoryStep(currentState, trajInput) ) {
00198                         ROS_ERROR("TM Module %s canceled further list execution\n", (*it)->getName().c_str());
00199                         // Trajectory Module told us to not check the later conditions
00200                         break;
00201                 }
00202         }
00203 }
00204 void TrajectoryModuleContainer::trajectoryStepOther(const TKState& currentState, TKTrajectory& trajInput)
00205 {
00206         std::list<TrajectoryModule*>::iterator it;
00207         for (it = otherPriorityList.begin(); it != otherPriorityList.end(); it++) {
00208                 if ( !(*it)->trajectoryStep(currentState, trajInput) ) {
00209                         ROS_ERROR("TM Module %s canceled further list execution\n", (*it)->getName().c_str());
00210                         // Trajectory Module told us to not check the later conditions
00211                         break;
00212                 }
00213         }
00214 }
00215 
00216 } /* namespace telekyb */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Defines


tk_trajprocessor
Author(s): Martin Riedel
autogenerated on Mon Nov 11 2013 11:13:29