course_keeping_turns.cpp
Go to the documentation of this file.
00001 /*
00002  * course_keeping_turns.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#include <sensor_msgs/Joy.h>
00012 *
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   seq: 230
00030 *  PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00031 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00032 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00033 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00034 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00035 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00036 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00037 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00038 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00039 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00040 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00041 *  POSSIBILITY OF SUCH DAMAGE.
00042 *********************************************************************/
00043 
00044 //#ifndef EVENTEVALUATION_HPP_
00045 //#define EVENTEVALUATION_HPP_
00046 //
00047 #include <labust_mission/labustMission.hpp>
00049 #include <sensor_msgs/Joy.h>
00050 
00051 //
00052 namespace labust {
00053         namespace data {
00054 
00055                 class DataManager{
00056 
00057                 public:
00058 
00059                         /*****************************************************************
00060                          ***  Class functions
00061                          ****************************************************************/
00062 
00063                         DataManager();
00064 
00065                         void updateData(const auv_msgs::NavSts::ConstPtr& data);
00066 
00067                         /*********************************************************************
00068                          ***  Class variables
00069                          ********************************************************************/
00070 
00071                         //ros::Subscriber subExternalEvents;
00072                         //ros::Subscriber subStateHatAbs;
00073 
00074                         /* State Hat variables */
00075                         enum {u=0, v, w, r, x, y, z, psi, x_var, y_var, z_var, psi_var, alt, stateHatNum};
00076                         std::vector<double> stateHatVar;
00077 
00078                         /* Events data variables */
00079                         std::vector<double> eventsVar;
00080 
00081                         /* Mission specific variables */ // Svki specificni projekt ima klasu koja extenda DataManager klasu
00082                         std::vector<double> missionVar;
00083                 };
00084 
00085                 DataManager::DataManager(){
00086 
00087                         ros::NodeHandle nh;
00088                         //subExternalEvents= nh.subscribe<misc_msgs::ExternalEvent>("externalEvent",1, &EventEvaluation::onReceiveExternalEvent, this);
00089                         //subStateHatAbs= nh.subscribe<auv_msgs::NavSts>("stateHatAbs",1, &DataManager::onStateHat, this);
00090 
00091 
00092                         //externalEventContainer.resize(5); // 5 eksternih evenata
00093                         stateHatVar.resize(stateHatNum);
00094                 }
00095 
00096                 void DataManager::updateData(const auv_msgs::NavSts::ConstPtr& data){
00097 
00098                         stateHatVar[u] = data->body_velocity.x;
00099                         stateHatVar[v] = data->body_velocity.y;
00100                         stateHatVar[w] = data->body_velocity.z;
00101                         stateHatVar[r] = data->orientation_rate.yaw;
00102 
00103                         stateHatVar[x] = data->position.north;
00104                         stateHatVar[y] = data->position.east;
00105                         stateHatVar[z] = data->position.depth;
00106                         stateHatVar[psi] = data->orientation.yaw;
00107 
00108                         stateHatVar[x_var] = data->position_variance.north;
00109                         stateHatVar[y_var] = data->position_variance.east;
00110                         stateHatVar[z_var] = data->position_variance.depth;
00111                         stateHatVar[psi_var] = data->orientation_variance.yaw;
00112 
00113                         stateHatVar[alt] = data->altitude;
00114 
00115                 }
00116         }
00117 }
00118 
00119 
00120 class CKT : public labust::data::DataManager{
00121 
00122 public:
00123 
00124         ros::Subscriber subStateHatAbs, subRequestLawn, subMissionOffset, subJoy;
00125         ros::Publisher pubEvent;
00126 
00127         ros::Timer timer;
00128 
00129         bool requestTurn;
00130 
00131         double course;
00132 
00133 
00134 
00135         auv_msgs::NED offset;
00136 
00137         //enum {u=0, v, w, r, x, y, z, psi, x_var, y_var, z_var, psi_var, alt, stateHatNum};
00138 
00139         double startLawnX, startLawnY;
00140 
00141 
00142         CKT():startLawnX(0.0),startLawnY(0.0), requestTurn(false), course(0){
00143 
00144                 ros::NodeHandle nh;
00145 
00146                 /* Subscribers */
00147                 subStateHatAbs = nh.subscribe<auv_msgs::NavSts>("stateHatAbsSlow",1, &CKT::onStateHat, this);
00148                 //subRequestLawn = nh.subscribe<std_msgs::Bool>("requestLawn",1, &CKT::onRequestLawn, this);
00149                 //subMissionOffset = nh.subscribe<auv_msgs::NED>("missionOffset",1, &CKT::onMissionOffset, this);
00150                 subJoy = nh.subscribe<sensor_msgs::Joy>("joy", 1, &CKT::onJoy,this);
00151 
00152 
00153 
00154                 /* Publishers */
00155                 pubEvent = nh.advertise<misc_msgs::ExternalEvent>("externalEvent",1);
00156 
00157                 //missionLoop();
00158 
00159 
00160         }
00161 
00162         void onStateHat(const auv_msgs::NavSts::ConstPtr& data){
00163 
00164                 updateData(data);
00165                 //ROS_ERROR("UPDATE");
00166                 ros::NodeHandle nh;
00167 
00168                 misc_msgs::ExternalEvent sendEvent;
00169 
00170 
00171                 if(requestTurn == true){
00172 
00173                         //misc_msgs::ExternalEvent sendEvent;
00174 
00175 
00176                         timer = nh.createTimer(ros::Duration(0.5), &CKT::onTimeout, this, true);
00177                         requestTurn = false;
00178 
00179                         ROS_ERROR("KURS: %f", course*180/M_PI);
00180                 }
00181 
00182 
00183 
00184         }
00185 
00186 
00187         void onTimeout(const ros::TimerEvent& timer){
00188 
00189                 //ROS_ERROR("Timeout");
00190                 //mainEventQueue->riseEvent("/PRIMITIVE_FINISHED");
00191                 //checkEventFlag = false;
00192                 misc_msgs::ExternalEvent sendEvent;
00193 
00194                 sendEvent.id = 2;
00195                 sendEvent.value = 0;
00196                 pubEvent.publish(sendEvent);
00197 
00198 
00199 
00200 
00201 
00202 //              sendEvent.id = 2;
00203 //              sendEvent.value = 0;
00204 //              pubEvent.publish(sendEvent);
00205 
00206 
00207 
00208         }
00209 
00210 
00211         void onJoy(const sensor_msgs::Joy::ConstPtr& data){
00212 
00213                 misc_msgs::ExternalEvent sendEvent;
00214                         if(data->buttons[2]){
00215 
00216                                 ROS_ERROR("lijevo");
00217 
00218                                 course -= M_PI/2;
00219 
00220                                 if(course>M_PI){
00221                                         course -= 2*M_PI;
00222                                 }else if(course<-M_PI){
00223                                         course += 2*M_PI;
00224                                 }
00225 
00226 
00227                                         sendEvent.id = 5;
00228                                         sendEvent.value = course;
00229                                         pubEvent.publish(sendEvent);
00230 
00231                                 requestTurn = true;
00232                         } else if(data->buttons[3]){
00233 
00234                                 ROS_ERROR("desno");
00235 
00236                                 course += M_PI/2;
00237 
00238                                 if(course>M_PI){
00239                                         course -= 2*M_PI;
00240                                 }else if(course<-M_PI){
00241                                         course += 2*M_PI;
00242                                 }
00243 
00244 
00245                                                                 sendEvent.id = 5;
00246                                                                 sendEvent.value = course;
00247                                                                 pubEvent.publish(sendEvent);
00248                                 requestTurn = true;
00249 
00250                         }
00251 
00252                         if(requestTurn){
00253                                 sendEvent.id = 2;
00254                                 sendEvent.value = 1;
00255                                 pubEvent.publish(sendEvent);
00256                         }
00257                 }
00258 
00259 
00260 };
00261 
00262 
00263 
00264 
00265 
00266 
00267 
00268 
00269 
00270 
00271 int main(int argc, char** argv){
00272 
00273         ros::init(argc, argv, "LOD");
00274         ros::NodeHandle nh;
00275 
00276         CKT courseKeepingTurns;
00277         //lawnmowerOnDemand.missionLoop();
00278 
00279         ros::spin();
00280         return 0;
00281 }
00282 
00283 
00284 
00285 
00286 
00287 
00288 


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