data_event_manager.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * data_event_manager.cpp
00003  *
00004  *  Created on: Jun 30, 2014
00005  *      Author: Filip Mandic
00006  *
00007  ********************************************************************/
00008 
00009 /*********************************************************************
00010 * Software License Agreement (BSD License)
00011 *
00012 *  Copyright (c) 2014, LABUST, UNIZG-FER
00013 *  All rights reserved.
00014 *
00015 *  Redistribution and use in source and binary forms, with or without
00016 *  modification, are permitted provided that the following conditions
00017 *  are met:
00018 *
00019 *   * Redistributions of source code must retain the above copyright
00020 *     notice, this list of conditions and the following disclaimer.
00021 *   * Redistributions in binary form must reproduce the above
00022 *     copyright notice, this list of conditions and the following
00023 *     disclaimer in the documentation and/or other materials provided
00024 *     with the distribution.
00025 *   * Neither the name of the LABUST nor the names of its
00026 *     contributors may be used to endorse or promote products derived
00027 *     from this software without specific prior written permission.
00028 *
00029 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00030 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00031 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00032 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00033 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00034 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00035 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00036 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00037 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00038 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00039 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00040 *  POSSIBILITY OF SUCH DAMAGE.
00041 *********************************************************************/
00042 
00043 #include<labust_mission/labustMission.hpp>
00044 #include<labust_mission/dataManager.hpp>
00045 #include<labust_mission/eventEvaluation.hpp>
00046 
00047 /*********************************************************************
00048  *** DataEventManager class definition
00049  ********************************************************************/
00050 
00051 class DataEventManager{
00052 
00053 public:
00054         DataEventManager():missionLoaded(false){
00055 
00056                 ros::NodeHandle nh;
00057 
00059                 subStateHatAbs = nh.subscribe<auv_msgs::NavSts>("stateHatAbs",1,&DataEventManager::onStateHat,this);
00060                 subMissionSetup = nh.subscribe<misc_msgs::MissionSetup>("missionSetup",1,&DataEventManager::onMissionSetup,this);
00061                 subExternalEvents= nh.subscribe<misc_msgs::ExternalEvent>("externalEvent",1, &DataEventManager::onExternalEvent, this);
00062                 subEventString = nh.subscribe<std_msgs::String>("eventString",1, &DataEventManager::onEventString, this);
00063 
00065                 pubDataEventsContainer = nh.advertise<misc_msgs::DataEventsContainer>("dataEventsContainer",1);
00066 
00068                 srvEvaluateExpression = nh.advertiseService("evaluate_expression", &DataEventManager::expressionEvaluationService,this);
00069         }
00070 
00072         void onStateHat(const auv_msgs::NavSts::ConstPtr& data){
00073 
00075                 DM.updateStateVar(data);
00076 
00077                 if(missionLoaded){
00079                         EE.updateSymbolTable(DM.getStateVar(),DM.getMissionVar(),DM.getMissionVarNames());
00080                         DM.updateEventsVar(EE.evaluateEvents(eventsContainer));
00081 
00083                         publishDataEvent(DM.getStateVar(), DM.getMissionVar(), DM.getEventsVar());
00084                 }
00085         }
00086 
00088         void publishDataEvent(vector<double> stateVar, vector<double> missionVar, vector<uint8_t> eventsVar){
00089 
00090                 misc_msgs::DataEventsContainer dataEventsContainer;
00091                 dataEventsContainer.stateVar.data  = stateVar;
00092                 dataEventsContainer.eventsVar = eventsVar;
00093                 dataEventsContainer.missionVar.data = missionVar;
00094 
00095                 pubDataEventsContainer.publish(dataEventsContainer);
00096         }
00097 
00099         void onExternalEvent(const misc_msgs::ExternalEvent::ConstPtr& data){
00100 
00101                 if((data->id > 0) && (data->id <= DM.getMissionVar().size()))
00102                         DM.updateMissionVar(data->id, data->value);
00103         }
00104 
00106         void onMissionSetup(const misc_msgs::MissionSetup::ConstPtr& data){
00107 
00108                 parseMissionParam(data->missionParams.c_str());
00109                 parseMissionEvents(data->missionEvents.c_str());
00110 
00112                 EE.initializeSymbolTable(DM.getStateVar(), DM.getMissionVar(), DM.getMissionVarNames());
00113 
00114                 ROS_ERROR("Mission successfully loaded");
00115                 missionLoaded = true;
00116         }
00117 
00118         void onEventString(const std_msgs::String::ConstPtr& msg){
00119 
00120                 ROS_INFO("EventString: %s",msg->data.c_str());
00121                 if(strcmp(msg->data.c_str(),"/STOP") == 0){
00122                         missionLoaded = false;
00123                         DM.reset();
00124                         EE.resetSymbolTable();
00125                 }
00126         }
00127 
00129         bool expressionEvaluationService(misc_msgs::EvaluateExpression::Request &req, misc_msgs::EvaluateExpression::Response &res){
00130 
00131                 res.result = EE.evaluateStringExpression(req.expression);
00132                 return true;
00133         }
00134 
00135         /*****************************************************************
00136          *** Helper functions
00137          ****************************************************************/
00138 
00140         void parseMissionParam(string missionParamString){
00141 
00142                 if(missionParamString.empty() == 0){
00143 
00144                         vector<string> tmp;
00145                         tmp = labust::utilities::split(missionParamString.c_str(), ':');
00146 
00147                         for(vector<string>::iterator it = tmp.begin(); it != tmp.end(); it=it+2){
00148 
00149                                 DM.setMissionVarNames((*(it)).c_str()); 
00150                                 DM.setMissionVar(atof((*(it+1)).c_str())); 
00151                         }
00152                 }
00153         }
00154 
00156         void parseMissionEvents(string missionEventsString){
00157 
00158                 if(missionEventsString.empty() == 0){
00159                         eventsContainer = labust::utilities::split(missionEventsString.c_str(), ':');
00160                         vector<uint8_t> tmp(eventsContainer.size(),0);
00161                         DM.updateEventsVar(tmp); 
00162                 }
00163         }
00164 
00165         /*****************************************************************
00166          *** Class variables
00167          ****************************************************************/
00168 
00169 private:
00170 
00172         labust::data::DataManager DM;
00173 
00175         labust::event::EventEvaluation EE;
00176 
00178         ros::Subscriber subStateHatAbs, subMissionSetup, subExternalEvents, subEventString;
00179 
00181         ros::Publisher pubDataEvent, pubDataEventsContainer;
00182 
00184         ros::ServiceServer srvEvaluateExpression;
00185 
00187         vector<string> eventsContainer;
00188 
00190         bool missionLoaded;
00191 };
00192 
00193 /*********************************************************************
00194  *** Main
00195  ********************************************************************/
00196 
00197 int main(int argc, char **argv){
00198 
00199         ros::init(argc, argv, "data_event_manager");
00200         ros::NodeHandle nh;
00201         DataEventManager DEM;
00202         ros::spin();
00203         return 0;
00204 }
00205 
00206 


labust_mission
Author(s): Filip Mandic
autogenerated on Fri Aug 28 2015 11:22:55