lawnmower_on_demand.cpp
Go to the documentation of this file.
00001 /*
00002  * lawnmower_on_demand.cpp
00003  *
00004  *  Created on: Jun 20, 2014
00005  *      Author: Filip Mandic
00006  */
00007 
00008 /*********************************************************************
00009 * Software License Agreement (BSD License)
00010 *
00011 *  Copyright (c) 2014, LABUST, UNIZG-FER
00012 *  All rights reserved.
00013 *
00014 *  Redistribution and use in source and binary forms, with or without
00015 *  modification, are permitted provided that the following conditions
00016 *  are met:
00017 *
00018 *   * Redistributions of source code must retain the above copyright
00019 *     notice, this list of conditions and the following disclaimer.
00020 *   * Redistributions in binary form must reproduce the above
00021 *     copyright notice, this list of conditions and the following
00022 *     disclaimer in the documentation and/or other materials provided
00023 *     with the distribution.
00024 *   * Neither the name of the LABUST nor the names of its
00025 *     contributors may be used to endorse or promote products derived
00026 *     from this software without specific prior written permission.
00027 *
00028 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00029 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00030 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00031 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00032 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00033 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00034 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00035 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00036 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00037 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00038 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00039 *  POSSIBILITY OF SUCH DAMAGE.
00040 *********************************************************************/
00041 
00042 //#ifndef EVENTEVALUATION_HPP_
00043 //#define EVENTEVALUATION_HPP_
00044 //
00045 #include <labust_mission/labustMission.hpp>
00046 #include <sensor_msgs/Joy.h>
00048 //
00049 namespace labust {
00050         namespace data {
00051 
00052                 class DataManager{
00053 
00054                 public:
00055 
00056                         /*****************************************************************
00057                          ***  Class functions
00058                          ****************************************************************/
00059 
00060                         DataManager();
00061 
00062                         void updateData(const auv_msgs::NavSts::ConstPtr& data);
00063 
00064                         /*********************************************************************
00065                          ***  Class variables
00066                          ********************************************************************/
00067 
00068                         //ros::Subscriber subExternalEvents;
00069                         //ros::Subscriber subStateHatAbs;
00070 
00071                         /* State Hat variables */
00072                         enum {u=0, v, w, r, x, y, z, psi, x_var, y_var, z_var, psi_var, alt, stateHatNum};
00073                         std::vector<double> stateHatVar;
00074 
00075                         /* Events data variables */
00076                         std::vector<double> eventsVar;
00077 
00078                         /* Mission specific variables */ // Svki specificni projekt ima klasu koja extenda DataManager klasu
00079                         std::vector<double> missionVar;
00080                 };
00081 
00082                 DataManager::DataManager(){
00083 
00084                         ros::NodeHandle nh;
00085                         //subExternalEvents= nh.subscribe<misc_msgs::ExternalEvent>("externalEvent",1, &EventEvaluation::onReceiveExternalEvent, this);
00086                         //subStateHatAbs= nh.subscribe<auv_msgs::NavSts>("stateHatAbs",1, &DataManager::onStateHat, this);
00087 
00088 
00089                         //externalEventContainer.resize(5); // 5 eksternih evenata
00090                         stateHatVar.resize(stateHatNum);
00091                 }
00092 
00093                 void DataManager::updateData(const auv_msgs::NavSts::ConstPtr& data){
00094 
00095                         stateHatVar[u] = data->body_velocity.x;
00096                         stateHatVar[v] = data->body_velocity.y;
00097                         stateHatVar[w] = data->body_velocity.z;
00098                         stateHatVar[r] = data->orientation_rate.yaw;
00099 
00100                         stateHatVar[x] = data->position.north;
00101                         stateHatVar[y] = data->position.east;
00102                         stateHatVar[z] = data->position.depth;
00103                         stateHatVar[psi] = data->orientation.yaw;
00104 
00105                         stateHatVar[x_var] = data->position_variance.north;
00106                         stateHatVar[y_var] = data->position_variance.east;
00107                         stateHatVar[z_var] = data->position_variance.depth;
00108                         stateHatVar[psi_var] = data->orientation_variance.yaw;
00109 
00110                         stateHatVar[alt] = data->altitude;
00111 
00112                 }
00113         }
00114 }
00115 
00116 //using namespace labust::data;
00117 
00118 class LOD : public labust::data::DataManager{
00119 
00120 public:
00121 
00122         ros::Subscriber subStateHatAbs, subRequestLawn, subMissionOffset, subJoy;
00123         ros::Publisher pubEvent;
00124 
00125         ros::Timer timer;
00126 
00127         bool requestLawnFlag;
00128 
00129         int counter;
00130 
00131         auv_msgs::NED offset;
00132 
00133         //enum {u=0, v, w, r, x, y, z, psi, x_var, y_var, z_var, psi_var, alt, stateHatNum};
00134 
00135         double startLawnX, startLawnY;
00136 
00137 
00138         LOD():startLawnX(0.0),startLawnY(0.0), requestLawnFlag(false), counter(0){
00139 
00140                 ros::NodeHandle nh;
00141 
00142                 /* Subscribers */
00143                 subStateHatAbs = nh.subscribe<auv_msgs::NavSts>("stateHatAbs",5, &LOD::onStateHat, this);
00144                 subRequestLawn = nh.subscribe<std_msgs::Bool>("requestLawn",1, &LOD::onRequestLawn, this);
00145                 subMissionOffset = nh.subscribe<auv_msgs::NED>("missionOffset",1, &LOD::onMissionOffset, this);
00146                 subJoy = nh.subscribe<sensor_msgs::Joy>("joy", 1, &LOD::onJoy,this);
00147 
00148 
00149 
00150                 /* Publishers */
00151                 pubEvent = nh.advertise<misc_msgs::ExternalEvent>("externalEvent",3);
00152 
00153                 //missionLoop();
00154 
00155 
00156         }
00157 
00158         void onStateHat(const auv_msgs::NavSts::ConstPtr& data){
00159 
00160                 updateData(data);
00161                 //ROS_ERROR("UPDATE");
00162                 ros::NodeHandle nh;
00163                 if(requestLawnFlag == true){
00164                         timer = nh.createTimer(ros::Duration(2.0), &LOD::onTimeout, this, true);
00165                         requestLawnFlag = false;
00166                 }
00167 
00168         }
00169 
00170         void onTimeout(const ros::TimerEvent& timer){
00171 
00172                 //ROS_ERROR("Timeout");
00173                 //mainEventQueue->riseEvent("/PRIMITIVE_FINISHED");
00174                 //checkEventFlag = false;
00175                 misc_msgs::ExternalEvent sendEvent;
00176 
00177                 sendEvent.id = 1;
00178                 sendEvent.value = 0;
00179                 pubEvent.publish(sendEvent);
00180 
00181 
00182         }
00183 
00184         void onRequestLawn(const std_msgs::Bool::ConstPtr& req){
00185 
00186                 if(req->data){
00187                         //requestLawnFlag = true;
00188 
00189                         misc_msgs::ExternalEvent sendEvent;
00190                         sendEvent.id = 2;
00191                         sendEvent.value = startLawnX = stateHatVar[x] + offset.north;
00192                         pubEvent.publish(sendEvent);
00193 
00194                         sendEvent.id = 3;
00195                         sendEvent.value = startLawnY = stateHatVar[y] + offset.east;
00196                         pubEvent.publish(sendEvent);
00197 
00198                         sendEvent.id = 1;
00199                         sendEvent.value = 1;
00200                         pubEvent.publish(sendEvent);
00201 
00202                         requestLawnFlag = true;
00203                 }
00204         }
00205 
00206         void onMissionOffset(const auv_msgs::NED::ConstPtr& data){
00207 
00208                 offset.north = data->north;
00209                 offset.east = data->east;
00210         }
00211 
00212         void onJoy(const sensor_msgs::Joy::ConstPtr& data){
00213 
00214 
00215 
00216                 if(data->buttons[1]){
00217 
00218                         ROS_ERROR("pritisak");
00219 
00220                         misc_msgs::ExternalEvent sendEvent;
00221                         sendEvent.id = 2;
00222                         sendEvent.value = startLawnX = stateHatVar[x] + offset.north;
00223                         pubEvent.publish(sendEvent);
00224 
00225                         sendEvent.id = 3;
00226                         sendEvent.value = startLawnY = stateHatVar[y] + offset.east;
00227                         pubEvent.publish(sendEvent);
00228 
00229                         sendEvent.id = 1;
00230                         sendEvent.value = 1;
00231                         pubEvent.publish(sendEvent);
00232 
00233                         requestLawnFlag = true;
00234                 }
00235         }
00236 
00237 
00238 };
00239 
00240 
00241 int main(int argc, char** argv){
00242 
00243         ros::init(argc, argv, "LOD");
00244         ros::NodeHandle nh;
00245 
00246         LOD lawnmowerOnDemand;
00247         //lawnmowerOnDemand.missionLoop();
00248 
00249         ros::spin();
00250         return 0;
00251 }
00252 
00253 
00254 


labust_mission
Author(s): Filip Mandic
autogenerated on Fri Aug 28 2015 11:23:04