demo_not_working.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002 *
00003 * Software License Agreement (BSD License)
00004 *
00005 *  Copyright (c) 2010, ISR University of Coimbra.
00006 *  All rights reserved.
00007 *
00008 *  Redistribution and use in source and binary forms, with or without
00009 *  modification, are permitted provided that the following conditions
00010 *  are met:
00011 *
00012 *   * Redistributions of source code must retain the above copyright
00013 *     notice, this list of conditions and the following disclaimer.
00014 *   * Redistributions in binary form must reproduce the above
00015 *     copyright notice, this list of conditions and the following
00016 *     disclaimer in the documentation and/or other materials provided
00017 *     with the distribution.
00018 *   * Neither the name of the ISR University of Coimbra nor the names of its
00019 *     contributors may be used to endorse or promote products derived
00020 *     from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033 *  POSSIBILITY OF SUCH DAMAGE.
00034 *
00035 * Author: Gonçalo Cabrita and Pedro Sousa on 17/11/2010
00036 *********************************************************************/
00037 #include <vector>
00038 #include <string>
00039 
00040 #include <ros/ros.h>
00041 #include <tf/tf.h>
00042 #include <move_base_msgs/MoveBaseAction.h>
00043 #include <actionlib/client/simple_action_client.h>
00044 #include <geometry_msgs/Pose2D.h>
00045 #include <lse_sensor_msgs/Nostril.h>
00046 #include <roomba_500_series/Battery.h>
00047 #include <roomba_500_series/GoDockAction.h>
00048 #include <geometry_msgs/Twist.h>
00049 
00050 typedef actionlib::SimpleActionClient<move_base_msgs::MoveBaseAction> MoveBaseClient;
00051 
00052 bool nextGoal;
00053 bool needToBackUp;
00054 
00055 roomba_500_series::Battery battery;
00056 
00057 void goalDoneCallback(const actionlib::SimpleClientGoalState &state, const move_base_msgs::MoveBaseResultConstPtr &result)
00058 {
00059         ROS_INFO("Goal is done!");
00060         if(state.state_ == actionlib::SimpleClientGoalState::SUCCEEDED) nextGoal = true;
00061         else if(state.state_ == actionlib::SimpleClientGoalState::ABORTED) needToBackUp = true;
00062 }
00063 
00064 void goalActiveCallback()
00065 {
00066         //ROS_INFO("Goal active! Hurray!");
00067 }
00068 
00069 void goalFeedbackCallback(const move_base_msgs::MoveBaseFeedbackConstPtr &feedback)
00070 {
00071         //ROS_INFO("Getting feedback! How cool is that?");
00072 }
00073 
00074 void noseCallback(const lse_sensor_msgs::Nostril::ConstPtr& nose)
00075 {
00076         
00077 }
00078 
00079 void batteryCallback(const roomba_500_series::Battery::ConstPtr& batt)
00080 {
00081         battery.level = batt->level;
00082         battery.dock = batt->dock;
00083 }
00084 
00085 int main(int argc, char** argv)
00086 {
00087         ros::init(argc, argv, "fctuc_open_day_demo");
00088 
00089         ROS_INFO("Welcome to FCTUC Open Day Demo!");
00090         
00091         ros::NodeHandle n;
00092         
00093         //ros::Publisher cmd_vel_pub  = n.advertise<geometry_msgs::Twist>("/cmd_vel", 50);
00094         
00095         ros::Subscriber nose_sub = n.subscribe("/nose", 20, noseCallback);
00096         ros::Subscriber batt_sub = n.subscribe("/battery", 20, batteryCallback);
00097         
00098         /*actionlib::SimpleActionClient<roomba_500_series::GoDockAction> gd("godock", true);
00099         ROS_INFO("Waiting for the godock action server to come online... Connecting the roomba would be a good starting point!");
00100         gd.waitForServer();
00101         ROS_INFO("Found the godock action server! Moving on...");*/
00102         
00103         std::string tf_prefix;
00104         n.param<std::string>("tf_prefix", tf_prefix, "");
00105         
00106         std::string file_path;
00107         n.param<std::string>("demo/file_name", file_path, "demo.goal");
00108         
00109         FILE * goalsFile;
00110         goalsFile = fopen(file_path.c_str(), "r");
00111         if(goalsFile == NULL)
00112         {
00113                 ROS_INFO("Cannot open file %s", file_path.c_str());
00114                 ROS_BREAK();    
00115         }
00116         
00117         std::vector<geometry_msgs::Pose2D> targets;
00118         std::vector<geometry_msgs::Pose2D>::iterator target;
00119         
00120         /*geometry_msgs::Pose2D myDock;
00121         myDock.x = 1.0;
00122         myDock.y = 1.0;
00123         myDock.theta = 0.0;*/
00124         
00125         geometry_msgs::Pose2D pose;
00126         // Might come in handy... some day :P
00127         float z, qx, qy, qz, qw;
00128         while(!feof(goalsFile))
00129         {
00130                 fscanf(goalsFile, "Frame:/map, Position(%f, %f, %f), Orientation(%f, %f, %f, %f) = Angle: %lf\n", &(pose.x), &(pose.y), &z, &qx, &qy, &qz, &qw, &(pose.theta));
00131                 targets.push_back(pose);
00132         }
00133         
00134         // Tell the action client that we want to spin a thread by default
00135         MoveBaseClient ac("move_base", true);
00136         // Wait for the action server to come up
00137         ROS_INFO("Waiting for the move_base action server to come online... Forgot to launch move_base now did we?");
00138         while(!ac.waitForServer(ros::Duration(5.0))){;}
00139         ROS_INFO("Found the move_base action server! Moving on...");
00140 
00141         battery.level = 100.0;
00142         battery.dock = false;
00143         needToBackUp = false;
00144         nextGoal = false;
00145         bool sendGoal = true;
00146         bool docking = false;
00147         int backUpCounter = 0;
00148         target = targets.begin();
00149         ROS_INFO("Lets have some fun...");
00150         
00151         ros::Rate r(10);
00152         while(ros::ok())
00153         {       
00154                 // If we are trapped or we are recharged back up a lil bit
00155                 /*if(needToBackUp)// || (battery.level>80.0 && battery.dock))
00156                 {
00157                         needToBackUp = true;
00158                         sendGoal = false;
00159                         nextGoal = false;
00160                         if(backUpCounter==0)
00161                         {
00162                                 ROS_INFO("The wall is too close! I need to do some backing up...");
00163                                 // Move the robot back...
00164                                 geometry_msgs::Twist cmd_vel;
00165                                 cmd_vel.linear.x = -0.1;
00166                                 cmd_vel.angular.z = 0.0;
00167                                 cmd_vel_pub.publish(cmd_vel);
00168                         }
00169                         
00170                         if(backUpCounter==50)
00171                         {
00172                                 // Turn the robot around...
00173                                 geometry_msgs::Twist cmd_vel;
00174                                 cmd_vel.linear.x = 0.0;
00175                                 cmd_vel.angular.z = 1.0;
00176                                 cmd_vel_pub.publish(cmd_vel);
00177                         }
00178                         
00179                         if(backUpCounter==80)u
00180                         {
00181                                 // Stop the robot...
00182                                 geometry_msgs::Twist cmd_vel;
00183                                 cmd_vel.linear.x = 0.0;
00184                                 cmd_vel.angular.z = 0.0;
00185                                 cmd_vel_pub.publish(cmd_vel);
00186                                 
00187                                 ROS_INFO("Done backing up, now on with my life!");
00188                                 
00189                                 // Reset variables
00190                                 backUpCounter = 0;
00191                                 sendGoal = true;
00192                                 needToBackUp = false;
00193                         }
00194                         
00195                         backUpCounter++;
00196                 }*/
00197         
00198                 // Send the next goal!
00199                 if(nextGoal)
00200                 {
00201                         // Run the docking proceadure!
00202                         if(docking)
00203                         {
00204                                 ROS_INFO("Lets see if I can bullseye the dock...");
00205                                 
00206                                 /*ac.cancelAllGoals();
00207                         
00208                                 roomba_500_series::GoDockGoal goal;
00209                                 goal.timeout = ros::Duration(30.0);
00210                                 gd.sendGoal(goal);
00211                         
00212                                 gd.waitForResult(ros::Duration(30.0));
00213 
00214                                 if(!battery.dock) ROS_ERROR("I could not dock! No I'm going to run out of battery and fade away! Help!!!");
00215                                 else ROS_INFO("Got it! And now for some tasty battery juice!");*/
00216                                 docking = false;
00217                         }
00218                         else
00219                         {
00220                                 target++;
00221                                 if(target==targets.end()) target=targets.begin();
00222                         }
00223                         sendGoal = true;
00224                         nextGoal = false;
00225                 }
00226         
00227                 // Send a goal to the action client
00228                 if(sendGoal || (battery.level<50.0 && !battery.dock && !docking))
00229                 {
00230                         std::string frame_name = tf_prefix;
00231                         move_base_msgs::MoveBaseGoal goal;
00232                 
00233                         // Send a goal to the robot
00234                         goal.target_pose.header.frame_id = frame_name.append("/map");
00235                         goal.target_pose.header.stamp = ros::Time::now();
00236                 
00237                         // If we need to recharge
00238                         if(battery.level<50.0 && !battery.dock)
00239                         {
00240                                 /*ROS_INFO("Going for docking station... Battery is at %.0f%%", battery.level);
00241                                 goal.target_pose.pose.position.x = myDock.x;
00242                                 goal.target_pose.pose.position.y = myDock.y;
00243                                 goal.target_pose.pose.orientation = tf::createQuaternionMsgFromYaw(myDock.theta);
00244                                 docking = true;*/
00245                         }
00246                         else
00247                         {
00248                                 ROS_INFO("Sending next goal... How long do we have to keep this up?");
00249                                 goal.target_pose.pose.position.x = target->x;
00250                                 goal.target_pose.pose.position.y = target->y;
00251                                 goal.target_pose.pose.orientation = tf::createQuaternionMsgFromYaw(target->theta);
00252                         }
00253                         
00254                         sendGoal = false;
00255                         ac.sendGoal(goal, boost::bind(&goalDoneCallback, _1, _2), boost::bind(&goalActiveCallback), boost::bind(&goalFeedbackCallback, _1));
00256                 }
00257                 
00258                 // If you smell something do interactive stuff!
00259                 //if()
00260                 
00261                 ros::spinOnce();
00262                 r.sleep();
00263         }
00264 
00265         ROS_INFO("Exiting FCTUC Open Day Demo...");
00266 
00267         return 0;
00268 }


fctuc_open_day
Author(s): Gonçalo Cabrita and Pedro Sousa
autogenerated on Mon Jan 6 2014 11:28:35