arm_manip_action.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * \file
00003  *
00004  * $Id:$
00005  *
00006  * Copyright (C) Brno University of Technology
00007  *
00008  * This file is part of software developed by dcgm-robotics@FIT group.
00009  *
00010  * Author: Zdenek Materna (imaterna@fit.vutbr.cz)
00011  * Supervised by: Michal Spanel (spanel@fit.vutbr.cz)
00012  * Date: dd/mm/2012
00013  *
00014  * This file is free software: you can redistribute it and/or modify
00015  * it under the terms of the GNU Lesser General Public License as published by
00016  * the Free Software Foundation, either version 3 of the License, or
00017  * (at your option) any later version.
00018  *
00019  * This file is distributed in the hope that it will be useful,
00020  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00021  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022  * GNU Lesser General Public License for more details.
00023  *
00024  * You should have received a copy of the GNU Lesser General Public License
00025  * along with this file.  If not, see <http://www.gnu.org/licenses/>.
00026  */
00027 
00028 #include <srs_assisted_arm_navigation/assisted_arm_navigation/arm_manip_node.h>
00029 
00030 using namespace srs_assisted_arm_navigation;
00031 using namespace srs_assisted_arm_navigation_msgs;
00032 
00033 void ManualArmManipActionServer::executeCB(const ManualArmManipGoalConstPtr &goal) {
00034 
00035   ros::Rate r(1);
00036   ros::Rate r5(5);
00037   ArmNavStart srv_start;
00038 
00039   result_.result.collision = false;
00040   result_.result.success = false;
00041   result_.result.repeat = false;
00042   result_.result.failed = false;
00043   result_.result.timeout = false;
00044   result_.result.time_elapsed = ros::Duration(0);
00045 
00046   ROS_INFO("Executing ManualArmManipAction");
00047 
00048   start_time_ = ros::Time::now();
00049   timeout_ = ros::Time::now();
00050   state_ = S_NONE;
00051 
00052   while(!ros::service::exists(SRV_START,true)) {
00053 
00054     ROS_INFO("Waiting for arm_nav_start service");
00055     r5.sleep();
00056 
00057   };
00058 
00059   // move arm to pre-grasp position or away
00060   /*if (((goal->pregrasp == true) ^ (goal->away == true)) == 1) {
00061 
00062     if (goal->pregrasp) {
00063 
00064     srv_start.request.pregrasp = true;
00065     srv_start.request.object_name = goal->object_name;
00066 
00067     } else {
00068 
00069       srv_start.request.away = true;
00070 
00071     }*/
00072 
00073   srv_start.request.allow_repeat = goal->allow_repeat;
00074   srv_start.request.action = goal->action;
00075   srv_start.request.object_name = goal->object_name;
00076 
00078     ros::service::call(SRV_START,srv_start);
00079 
00080     inited_ = true;
00081 
00082     ROS_INFO("Actionserver: starting looping");
00083 
00084     // 1 Hz loop
00085     while(ros::ok()) {
00086 
00087       state_ = new_state_;
00088 
00089       // test for sleeping user
00090       /*if ( (state_==S_NONE) && ((ros::Time::now()-start_time_) > start_timeout_) && (start_timeout_.toSec() > 0.0) ) {
00091 
00092         ROS_ERROR("%s: Canceled. Start timeout reached.", action_name_.c_str());
00093 
00094         result_.result.timeout = true;
00095         result_.result.time_elapsed = ros::Time::now() - start_time_;
00096         server_.setAborted(result_.result,"User is probably sleeping.");
00097         break;
00098 
00099       }*/
00100 
00101       // another test for sleeping user
00102       /*if (state_!=S_NONE && ((ros::Time::now()-timeout_) > solve_timeout_) && (solve_timeout_.toSec() > 0.0)) {
00103 
00104          ROS_ERROR("%s: Canceled. Solve timeout reached.", action_name_.c_str());
00105 
00106          result_.result.timeout = true;
00107          result_.result.time_elapsed = ros::Time::now() - start_time_;
00108          server_.setAborted(result_.result,"User is probably sleeping.");
00109          break;
00110 
00111       }*/
00112 
00113       // cancel action...
00114       if (server_.isPreemptRequested() || !ros::ok()) {
00115 
00116         ROS_INFO("%s: Preempted", action_name_.c_str());
00117 
00118         // TODO: clean things...
00119 
00120         result_.result.time_elapsed = ros::Time::now() - start_time_;
00121         server_.setPreempted(result_.result);
00122 
00123         break;
00124 
00125       }
00126 
00127 
00128       if ((state_ != S_SUCCESS) && (state_ != S_FAILED) && (state_ != S_REPEAT)) {
00129 
00130         // send feedback
00131         setFeedbackFalse();
00132 
00133         if (state_ == S_NEW) feedback_.feedback.starting = true;
00134         if (state_ == S_PLAN) feedback_.feedback.planning = true;
00135         if (state_ == S_EXECUTE) feedback_.feedback.executing = true;
00136         if (state_ == S_RESET) feedback_.feedback.reset = true;
00137 
00138         server_.publishFeedback(feedback_.feedback);
00139 
00140       } else {
00141 
00142         result_.result.failed = false;
00143 
00144         // send result
00145         result_.result.time_elapsed = ros::Time::now() - start_time_;
00146 
00147         if (state_ == S_SUCCESS) {
00148 
00149           result_.result.success = true;
00150           server_.setSucceeded(result_.result);
00151 
00152         }
00153 
00154         if (state_ == S_FAILED) {
00155 
00156           ROS_INFO("S_FAILED");
00157           result_.result.failed = true;
00158           server_.setAborted(result_.result);
00159 
00160         }
00161 
00162         if (state_ == S_REPEAT) {
00163 
00164           ROS_INFO("S_REPEAT");
00165           result_.result.repeat = true;
00166           server_.setAborted(result_.result);
00167 
00168         }
00169 
00170 
00171         break;
00172 
00173       }
00174 
00175       r.sleep();
00176 
00177     } // loop
00178 
00179     inited_ = false;
00180     new_state_ = S_NONE;
00181 
00182  /* // undefined
00183   } else {
00184 
00185     ROS_INFO("Can't move both to pregrasp pos. and away!");
00186      server_.setAborted(result_.result,"Can't move both to pregrasp pos. and away!");
00187 
00188   }*/
00189 
00190 }
00191 
00192 
00193 void ManualArmManipActionServer::srv_set_state(unsigned int n) {
00194 
00195   if (inited_) {
00196     new_state_ = n;
00197     ROS_DEBUG("Setting internal state to %u",n);
00198     timeout_ = ros::Time::now();
00199   }
00200 
00201 }
00202 
00203 void ManualArmManipActionServer::setFeedbackFalse() {
00204 
00205   feedback_.feedback.starting = false;
00206   feedback_.feedback.executing = false;
00207   feedback_.feedback.planning = false;
00208   feedback_.feedback.reset = false;
00209 
00210 }


srs_assisted_arm_navigation
Author(s): Zdenek Materna (imaterna@fit.vutbr.cz), Michal Spanel (spanel@fit.vutbr.cz)
autogenerated on Mon Oct 6 2014 08:12:08