map_navigation.cpp
Go to the documentation of this file.
00001 /*
00002  *  Gaitech Educational Portal
00003  *
00004  * Copyright (c) 2016
00005  * All rights reserved.
00006  *
00007  * License Type: GNU GPL
00008  *
00009  *   This program is free software: you can redistribute it and/or modify
00010  *   it under the terms of the GNU General Public License as published by
00011  *   the Free Software Foundation, either version 3 of the License, or
00012  *   (at your option) any later version.
00013  *   This program is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  *   You should have received a copy of the GNU General Public License
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020  *
00021  *   Program: Map-Based Navigation
00022  *
00023  */
00024 
00025 #include <ros/ros.h>
00026 #include <move_base_msgs/MoveBaseAction.h>
00027 #include <actionlib/client/simple_action_client.h>
00028 #include "sound_play/sound_play.h"
00029 
00030 std::string path_to_sounds;
00031 
00033 bool moveToGoal(double xGoal, double yGoal);
00034 char choose();
00035 
00037 double xCafe = 15.50;
00038 double yCafe = 10.20;
00039 double xOffice1 = 27.70 ;
00040 double yOffice1 = 13.50;
00041 double xOffice2 = 30.44 ;
00042 double yOffice2 = 13.50;
00043 double xOffice3 = 35.20 ;
00044 double yOffice3 = 13.50;
00045 
00046 bool goalReached = false;
00047 
00048 int main(int argc, char** argv){
00049         ros::init(argc, argv, "map_navigation_node");
00050         ros::NodeHandle n;
00051         sound_play::SoundClient sc;
00052         ros::spinOnce();
00053         path_to_sounds = "/home/ros/catkin_ws/src/gaitech_edu/src/sounds/";
00054         //sc.playWave(path_to_sounds+"short_buzzer.wav");
00055         //tell the action client that we want to spin a thread by default
00056 
00057         char choice = 'q';
00058         do{
00059                 choice =choose();
00060                 if (choice == '0'){
00061                         goalReached = moveToGoal(xCafe, yCafe);
00062                 }else if (choice == '1'){
00063                         goalReached = moveToGoal(xOffice1, yOffice1);
00064                 }else if (choice == '2'){
00065                         goalReached = moveToGoal(xOffice2, yOffice2);
00066                 }else if (choice == '3'){
00067                         goalReached = moveToGoal(xOffice3, yOffice3);
00068                 }
00069                 if (choice!='q'){
00070                         if (goalReached){
00071                                 ROS_INFO("Congratulations!");
00072                                 ros::spinOnce();
00073                                 sc.playWave(path_to_sounds+"ship_bell.wav");
00074                                 ros::spinOnce();
00075 
00076                         }else{
00077                                 ROS_INFO("Hard Luck!");
00078                                 sc.playWave(path_to_sounds+"short_buzzer.wav");
00079                         }
00080                 }
00081         }while(choice !='q');
00082         return 0;
00083 }
00084 
00085 bool moveToGoal(double xGoal, double yGoal){
00086 
00087         //define a client for to send goal requests to the move_base server through a SimpleActionClient
00088         actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> ac("move_base", true);
00089 
00090         //wait for the action server to come up
00091         while(!ac.waitForServer(ros::Duration(5.0))){
00092                 ROS_INFO("Waiting for the move_base action server to come up");
00093         }
00094 
00095         move_base_msgs::MoveBaseGoal goal;
00096 
00097         //set up the frame parameters
00098         goal.target_pose.header.frame_id = "map";
00099         goal.target_pose.header.stamp = ros::Time::now();
00100 
00101         /* moving towards the goal*/
00102 
00103         goal.target_pose.pose.position.x =  xGoal;
00104         goal.target_pose.pose.position.y =  yGoal;
00105         goal.target_pose.pose.position.z =  0.0;
00106         goal.target_pose.pose.orientation.x = 0.0;
00107         goal.target_pose.pose.orientation.y = 0.0;
00108         goal.target_pose.pose.orientation.z = 0.0;
00109         goal.target_pose.pose.orientation.w = 1.0;
00110 
00111         ROS_INFO("Sending goal location ...");
00112         ac.sendGoal(goal);
00113 
00114         ac.waitForResult();
00115 
00116         if(ac.getState() == actionlib::SimpleClientGoalState::SUCCEEDED){
00117                 ROS_INFO("You have reached the destination");
00118                 return true;
00119         }
00120         else{
00121                 ROS_INFO("The robot failed to reach the destination");
00122                 return false;
00123         }
00124 
00125 }
00126 
00127 char choose(){
00128         char choice='q';
00129         std::cout<<"|-------------------------------|"<<std::endl;
00130         std::cout<<"|PRESSE A KEY:"<<std::endl;
00131         std::cout<<"|'0': Cafe "<<std::endl;
00132         std::cout<<"|'1': Office 1 "<<std::endl;
00133         std::cout<<"|'2': Office 2 "<<std::endl;
00134         std::cout<<"|'3': Office 3 "<<std::endl;
00135         std::cout<<"|'q': Quit "<<std::endl;
00136         std::cout<<"|-------------------------------|"<<std::endl;
00137         std::cout<<"|WHERE TO GO?";
00138         std::cin>>choice;
00139 
00140         return choice;
00141 
00142 
00143 }
00144 
00145 


gapter
Author(s):
autogenerated on Thu Jun 6 2019 22:05:13