modeExecutor.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 
00018 #include <modeExecutor.h>
00019 #include <ros/ros.h>
00020 
00021 ModeExecutor::ModeExecutor(IColorO* colorO)
00022 : _stopRequested(false), default_priority(0)
00023 {
00024         _colorO = colorO;
00025         _colorO->signalColorSet()->connect(boost::bind(&ModeExecutor::onColorSetReceived, this, _1));
00026 }
00027 
00028 ModeExecutor::~ModeExecutor()
00029 {
00030 }
00031 
00032 uint64_t ModeExecutor::execute(cob_light::LightMode requestedMode)
00033 {
00034         boost::shared_ptr<Mode> mode = ModeFactory::create(requestedMode, _colorO);
00035         // check if mode was correctly created
00036         if(mode)
00037                 return execute(mode);
00038         else
00039                 return 0;
00040 }
00041 
00042 uint64_t ModeExecutor::execute(boost::shared_ptr<Mode> mode)
00043 {
00044         uint64_t u_id;
00045 
00046         // check if modes allready executing
00047         if(_mapActiveModes.size() > 0)
00048         {
00049                 // check if mode with lower prio is running
00050                 if(_mapActiveModes.begin()->first < mode->getPriority())
00051                 {
00052                         ROS_DEBUG("Pause mode: %i with prio %i",
00053                                 ModeFactory::type(_mapActiveModes.begin()->second.get()), _mapActiveModes.begin()->second->getPriority());
00054                         _mapActiveModes.begin()->second->pause();
00055                 }
00056                 else
00057                 {
00058                         std::map<int, boost::shared_ptr<Mode>, std::greater<int> >::iterator itr;
00059                         itr = _mapActiveModes.find(mode->getPriority());
00060                         if(itr != _mapActiveModes.end())
00061                         {
00062                                 ROS_DEBUG("Stopping mode: %i with prio %i",
00063                                         ModeFactory::type(itr->second.get()), itr->second->getPriority());
00064                                 itr->second->stop();
00065                                 _mapActiveModes.erase(itr);
00066                         }
00067                 }
00068         }
00069         mode->signalColorReady()->connect(boost::bind(&IColorO::setColor, _colorO, _1));
00070         mode->signalColorsReady()->connect(boost::bind(&IColorO::setColorMulti, _colorO, _1));
00071         mode->signalModeFinished()->connect(boost::bind(&ModeExecutor::onModeFinishedReceived, this, _1));
00072         mode->setActualColor(_activeColor);
00073         ROS_DEBUG("Attaching Mode %i with prio: %i freq: %f timeout: %f pulses: %i ",
00074                 ModeFactory::type(mode.get()), mode->getPriority(), mode->getFrequency(), mode->getTimeout(), mode->getPulses());
00075         _mapActiveModes.insert(std::pair<int, boost::shared_ptr<Mode> >(mode->getPriority(), mode));
00076 
00077         if(!_mapActiveModes.begin()->second->isRunning())
00078         {
00079                 ROS_DEBUG("Executing Mode %i with prio: %i freq: %f timeout: %f pulses: %i ",
00080                         ModeFactory::type(mode.get()), mode->getPriority(), mode->getFrequency(), mode->getTimeout(), mode->getPulses());
00081                 _mapActiveModes.begin()->second->start();
00082         }
00083         Mode* ptr = mode.get();
00084         u_id = reinterpret_cast<uint64_t>( ptr );
00085         return u_id;
00086 }
00087 
00088 void ModeExecutor::pause()
00089 {
00090         if(_mapActiveModes.size() > 0)
00091         {
00092                 _mapActiveModes.begin()->second->pause();
00093         }
00094 }
00095 
00096 void ModeExecutor::resume()
00097 {
00098         if(_mapActiveModes.size() > 0 && !_mapActiveModes.begin()->second->isRunning())
00099                 _mapActiveModes.begin()->second->start();
00100 }
00101 
00102 void ModeExecutor::stop()
00103 {
00104         if(_mapActiveModes.size() > 0)
00105         {
00106                 std::map<int, boost::shared_ptr<Mode>, std::greater<int> >::iterator itr;
00107                 for(itr = _mapActiveModes.begin(); itr != _mapActiveModes.end(); itr++)
00108                 {
00109                         itr->second->stop();
00110                 }
00111                 _mapActiveModes.clear();
00112         }
00113 }
00114 
00115 bool ModeExecutor::stop(uint64_t uId)
00116 {
00117         bool ret = false;
00118         if(_mapActiveModes.size() > 0)
00119         {
00120                 std::map<int, boost::shared_ptr<Mode>, std::greater<int> >::iterator itr;
00121                 for(itr = _mapActiveModes.begin(); itr != _mapActiveModes.end(); itr++)
00122                 {
00123                         uint64_t uid = reinterpret_cast<uint64_t>(itr->second.get());
00124                         if(uid == uId)
00125                         {
00126                                 ROS_DEBUG("Stopping mode: %i with prio %i",
00127                                         ModeFactory::type(itr->second.get()), itr->second->getPriority());
00128                                 itr->second->stop();
00129                                 _mapActiveModes.erase(itr);
00130 
00131                                 if(_mapActiveModes.size() > 0)
00132                                 {
00133                                         if(!_mapActiveModes.begin()->second->isRunning())
00134                                         {
00135                                                 ROS_DEBUG("Resume mode: %i with prio %i",
00136                                                         ModeFactory::type(_mapActiveModes.begin()->second.get()), _mapActiveModes.begin()->second->getPriority());
00137                                                 _mapActiveModes.begin()->second->start();
00138                                         }
00139                                 }
00140                                 ret = true;
00141                                 break;
00142                         }
00143                 }
00144         }
00145         return ret;
00146 }
00147 void ModeExecutor::onModeFinishedReceived(int prio)
00148 {
00149         //check if finished mode is the current active
00150         if(_mapActiveModes.begin()->first == prio)
00151         {
00152                 //erase mode from map and exec mode with next lower prio
00153                 _mapActiveModes.erase(prio);
00154                 if(_mapActiveModes.size() > 0)
00155                 {
00156                         ROS_DEBUG("Resume mode: %i with prio %i",
00157                                 ModeFactory::type(_mapActiveModes.begin()->second.get()), _mapActiveModes.begin()->second->getPriority());
00158                         _mapActiveModes.begin()->second->start();
00159                 }
00160         }
00161         //finished mode is not the current executing one (this should never happen)
00162         else
00163         {
00164                 ROS_WARN("Mode finished which should't be executed");
00165                 _mapActiveModes.erase(prio);
00166         }
00167 }
00168 
00169 void ModeExecutor::onColorSetReceived(color::rgba color)
00170 {
00171   _activeColor = color;
00172 }
00173 
00174 int ModeExecutor::getExecutingMode()
00175 {
00176         if(_mapActiveModes.size() > 0)
00177                 return ModeFactory::type(_mapActiveModes.begin()->second.get());
00178         else
00179                 return ModeFactory::type(NULL);
00180 }
00181 
00182 int ModeExecutor::getExecutingPriority()
00183 {
00184         if(_mapActiveModes.size()>0)
00185                 return _mapActiveModes.begin()->second->getPriority();
00186         else
00187                 return default_priority;
00188 }
00189 
00190 uint64_t ModeExecutor::getExecutingUId()
00191 {
00192         if(_mapActiveModes.size()>0)
00193                 return reinterpret_cast<uint64_t>(_mapActiveModes.begin()->second.get());
00194         else
00195                 return 0;
00196 }
00197 
00198 void ModeExecutor::setDefaultPriority(int priority)
00199 {
00200         default_priority = priority;
00201 }


cob_light
Author(s): Benjamin Maidel
autogenerated on Sat Jun 8 2019 21:02:07