GoDock.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 on 11/10/2010
00036 *********************************************************************/
00037 #include "roomba_500_series/GoDock.h"
00038 
00039 GoDockAction::GoDockAction(std::string name) : as_(nh_, name, boost::bind(&GoDockAction::goalCallback, this, _1)) , action_name_(name)
00040 {
00041         ir_sub_ = nh_.subscribe("/ir_character", 1, &GoDockAction::irCallback, this);
00042         bat_sub_ = nh_.subscribe("/battery", 1, &GoDockAction::batteryCallback, this);
00043 
00044         cmd_vel_pub_ = nh_.advertise<geometry_msgs::Twist>("/cmd_vel", 1);
00045 }
00046 
00047 GoDockAction::~GoDockAction()
00048 {
00049         
00050 }
00051 
00052 void GoDockAction::goalCallback(const roomba_500_series::GoDockGoalConstPtr & goal)
00053 {
00054         ros::Rate r(10);
00055         ROS_INFO("GoDock -- Initiating docking proceadure...");
00056 
00057         int no_signal = 0;
00058         ros::Time start_time = ros::Time::now();
00059         while(true)
00060         {
00061                 // No active
00062                 if(!as_.isActive()) return;
00063                 // Failed
00064                 if(ir_character_.omni == 0 && ir_character_.left == 0 && ir_character_.right == 0) no_signal++;
00065                 else no_signal = 0;
00066                 if(no_signal == 10)
00067                 {
00068                         as_.setAborted();
00069                         ROS_INFO("GoDock - Aborting...");
00070                         sendCmdVel(0.0, 0.0);
00071                         return;
00072                 }
00073                 // Succeeded
00074                 if(dock_)
00075                 {
00076                         as_.setSucceeded();
00077                         ROS_INFO("GoDock -- Charging!");
00078                         sendCmdVel(0.0, 0.0);
00079                         return;
00080                 }
00081                 
00082                 // TODO: Finetune the docking proceadure!!!
00083                 if((ir_character_.left == RED_BUOY_GREEN_BUOY && ir_character_.right == RED_BUOY_GREEN_BUOY) || (ir_character_.left == RED_BUOY_GREEN_BUOY_FORCE_FIELD && ir_character_.right == RED_BUOY_GREEN_BUOY_FORCE_FIELD))
00084                 {
00085                         sendCmdVel(0.05, 0.0);
00086                 }
00087                 else if(ir_character_.left == RED_BUOY || ir_character_.left == RED_BUOY_FORCE_FIELD)
00088                 {
00089                         sendCmdVel(0.05, 0.1);
00090                 }
00091                 else if(ir_character_.right == GREEN_BUOY || ir_character_.right == GREEN_BUOY_FORCE_FIELD)
00092                 {
00093                         sendCmdVel(0.05, -0.1);
00094                 }
00095 
00096                 r.sleep();
00097         }       
00098 }
00099 
00100 void GoDockAction::irCallback(const roomba_500_series::IRCharacterConstPtr & ir)
00101 {
00102         ir_character_.omni = ir->omni;
00103         ir_character_.left = ir->left;
00104         ir_character_.right = ir->right;
00105 }
00106 
00107 void GoDockAction::batteryCallback(const roomba_500_series::BatteryConstPtr & bat)
00108 {
00109         dock_ = bat->dock;
00110 }
00111 
00112 void GoDockAction::sendCmdVel(float linear, float angular)
00113 {
00114         geometry_msgs::Twist cmd_vel;
00115         cmd_vel.linear.x = linear;
00116         cmd_vel.angular.z = angular;
00117         cmd_vel_pub_.publish(cmd_vel);
00118 }
00119 
00120 // EOF


roomba_500_series
Author(s): Gonçalo Cabrita
autogenerated on Mon Jan 6 2014 11:26:40