Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 
00034 
00035 
00036 
00037 
00038 #include <ocl/Component.hpp>
00039 
00040 #include "controllers_manager.h"
00041 
00042 ControllersManager::ControllersManager(const std::string& name) :
00043   RTT::TaskContext(name, PreOperational), number_of_controllers_prop_("NumberOfControllers"), default_controller_prop_("DefaultController"), as(this, "ControllersManager",
00044                                                          boost::bind(&ControllersManager::goalCB, this, _1),
00045                                                          boost::bind(&ControllersManager::cancelCB, this, _1), true)
00046 {
00047   this->addProperty(number_of_controllers_prop_);
00048   this->addProperty(default_controller_prop_);
00049 }
00050 
00051 ControllersManager::~ControllersManager()
00052 {
00053 
00054 }
00055 
00056 bool ControllersManager::configureHook()
00057 {
00058   
00059   
00060   if ((number_of_controllers_ = number_of_controllers_prop_.get()) == 0)
00061   {
00062     return false;
00063   }
00064 
00065    RTT::Logger::log(RTT::Logger::Error) << "number of controllers " << number_of_controllers_
00066         << RTT::endlog();
00067 
00068   for (unsigned int i = 0; i < number_of_controllers_; i++)
00069   {
00070     controllers_names_.push_back(((RTT::Property<std::string>*)this->getProperty(std::string("controller_") + (char)(i + 48) + "_name"))->get());
00071     controllers_enable_port_.push_back(boost::shared_ptr<RTT::OutputPort<bool> > (new RTT::OutputPort<bool>));
00072     controllers_busy_port_.push_back(boost::shared_ptr<RTT::InputPort<bool> > (new RTT::InputPort<bool>));
00073     
00074     this->addPort(controllers_names_[i] + "Enable" , *controllers_enable_port_[i]);
00075     this->addPort(controllers_names_[i] + "Busy" , *controllers_busy_port_[i]);
00076   }
00077 
00078   return true;
00079 }
00080 
00081 bool ControllersManager::startHook()
00082 {
00083   std::string default_controller = default_controller_prop_.get();
00084   
00085   active_controller_ = -1;
00086   
00087   for(size_t i = 0; i < controllers_names_.size(); i++)
00088   {
00089     if(controllers_names_[i] == default_controller)
00090     {
00091       active_controller_ = i;
00092     }
00093   }
00094   
00095   if(active_controller_ == -1)
00096     return false;
00097     
00098   for(size_t i = 0; i < number_of_controllers_; i++)
00099   {
00100     if(i == active_controller_)
00101     {
00102       controllers_enable_port_[i]->write(true);
00103     } else 
00104     {
00105       controllers_enable_port_[i]->write(false);
00106     }
00107   }
00108   
00109   return true;
00110 }
00111 
00112 void ControllersManager::updateHook()
00113 {
00114   as.spinOnce();
00115   
00116   if(active_controller_ == next_controller_)
00117   {
00118     for(size_t i = 0; i < number_of_controllers_; i++)
00119     {
00120       if(i == active_controller_)
00121       {
00122         controllers_enable_port_[i]->write(true);
00123       } else 
00124       {
00125         controllers_enable_port_[i]->write(false);
00126       }
00127     }
00128   } else
00129   {
00130     for(size_t i = 0; i < number_of_controllers_; i++)
00131     { 
00132       controllers_enable_port_[i]->write(false);
00133     }
00134     
00135     bool busy = true;
00136     controllers_busy_port_[active_controller_]->read(busy);
00137     
00138     if(!busy)
00139     {
00140       active_controller_ = next_controller_;
00141       activeGoal.setSucceeded();
00142     }
00143   }
00144   
00145 }
00146 
00147 void ControllersManager::goalCB(GoalHandle gh)
00148 {
00149     int controller = -1;
00150     
00151     Goal g = gh.getGoal();
00152 
00153     activeGoal = gh;
00154  
00155     for(size_t i = 0; i < controllers_names_.size(); i++)
00156     {
00157       if(controllers_names_[i] == g->controller_id)
00158       {
00159         controller = i;
00160       }
00161     }
00162  
00163     if(controller == -1)
00164     {
00165       gh.setRejected();
00166     } else 
00167     {
00168       gh.setAccepted();
00169       next_controller_ = controller;
00170     }
00171 }
00172 
00173 void ControllersManager::cancelCB(GoalHandle gh)
00174 {
00175   goal_active = false;
00176 }
00177 
00178 ORO_CREATE_COMPONENT( ControllersManager )