hekateros_as_calib.cpp
Go to the documentation of this file.
00001 
00002 //
00003 // Package:   RoadNarrows Robotics Hekateros Robotiic Manipulator ROS Package
00004 //
00005 // Link:      https://github.com/roadnarrows-robotics/hekateros
00006 //
00007 // ROS Node:  hekateros_control
00008 //
00009 // File:      hekateros_as_calib.cpp
00010 //
00026 /*
00027  * @EulaBegin@
00028  * 
00029  * Permission is hereby granted, without written agreement and without
00030  * license or royalty fees, to use, copy, modify, and distribute this
00031  * software and its documentation for any purpose, provided that
00032  * (1) The above copyright notice and the following two paragraphs
00033  * appear in all copies of the source code and (2) redistributions
00034  * including binaries reproduces these notices in the supporting
00035  * documentation.   Substantial modifications to this software may be
00036  * copyrighted by their authors and need not follow the licensing terms
00037  * described here, provided that the new terms are clearly indicated in
00038  * all files where they apply.
00039  * 
00040  * IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
00041  * OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
00042  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
00043  * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
00044  * EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
00045  * THE POSSIBILITY OF SUCH DAMAGE.
00046  * 
00047  * THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
00048  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00049  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
00050  * "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
00051  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
00052  * 
00053  * @EulaEnd@
00054  */
00056 
00057 //
00058 // System and Boost
00059 //
00060 #include <unistd.h>
00061 #include <boost/bind.hpp>
00062 
00063 //
00064 // ROS
00065 //
00066 #include "ros/ros.h"
00067 
00068 //
00069 // ROS generated core, industrial, and hekateros messages.
00070 //
00071 #include "std_msgs/String.h"
00072 #include "hekateros_control/HekOpState.h"
00073 #include "hekateros_control/HekJointStateExtended.h"
00074 
00075 //
00076 // ROS generated action servers.
00077 //
00078 #include "actionlib/server/simple_action_server.h"
00079 #include "hekateros_control/CalibrateAction.h"
00080 
00081 //
00082 // RoadNarrows embedded hekateros library.
00083 //
00084 #include "Hekateros/hekateros.h"
00085 #include "Hekateros/hekJoint.h"
00086 #include "Hekateros/hekRobot.h"
00087 
00088 //
00089 // Node headers.
00090 //
00091 #include "hekateros_control.h"
00092 #include "hekateros_as_calib.h"
00093 
00094 
00095 using namespace std;
00096 using namespace hekateros;
00097 using namespace hekateros_control;
00098 
00099 
00100 void ASCalibrate::execute_cb(const CalibrateGoalConstPtr &goal)
00101 {
00102   HekRobot           &robot(node_.getRobot());
00103   HekJointStatePoint  state;
00104   int                 rc;
00105 
00106   ROS_INFO("%s: Executing calibrate action - please standby.",
00107       action_name_.c_str());
00108 
00109   //
00110   // Start Hekateros to calibrate asynchronously (i.e. non-blocking).
00111   //
00112   rc = robot.calibrateAsync(goal->force_recalib? true: false);
00113 
00114   if( rc != HEK_OK )
00115   {
00116     ROS_ERROR("%s: Failed to initiate calibration.", action_name_.c_str());
00117     result_.op.calib_state = HekOpState::UNCALIBRATED;
00118     as_.setAborted(result_);
00119     return;
00120   }
00121 
00122   ros::Rate r(2);
00123 
00124   while( robot.getAsyncState() == HekAsyncTaskStateWorking )
00125   {
00126     //
00127     // Action was preempted.
00128     //
00129     if( as_.isPreemptRequested() || !ros::ok() )
00130     {
00131       ROS_INFO("%s: Execution preempted.", action_name_.c_str());
00132       result_.op.calib_state = HekOpState::UNCALIBRATED;
00133       as_.setPreempted(result_); // set the action state to preempted
00134       return;
00135     }
00136 
00137     //
00138     // Keep providing feedback during calibration.
00139     //
00140     else
00141     {
00142       robot.getJointState(state);
00143       node_.updateExtendedJointStateMsg(state, feedback_.joint);
00144       as_.publishFeedback(feedback_);
00145       r.sleep();
00146     }
00147   }
00148 
00149   // check outcome of asynchronous task
00150   rc = robot.getAsyncRc();
00151 
00152   // got calibrated
00153   if( (rc == HEK_OK) && robot.isCalibrated() )
00154   {
00155     ROS_INFO("%s: Calibration succeeded.", action_name_.c_str());
00156     result_.op.calib_state = HekOpState::CALIBRATED;
00157     as_.setSucceeded(result_);
00158   }
00159   // preempted
00160   else if( as_.isPreemptRequested() || !ros::ok() )
00161   {
00162     ROS_INFO("%s: Calibration preempted.", action_name_.c_str());
00163     result_.op.calib_state = HekOpState::UNCALIBRATED;
00164     as_.setPreempted(result_); // set the action state to preempted
00165   }
00166   // failed
00167   else
00168   {
00169     ROS_ERROR("Calibration failed with error code %d.", rc);
00170     result_.op.calib_state = HekOpState::UNCALIBRATED;
00171     as_.setAborted(result_);
00172   }
00173 }
00174 
00175 void ASCalibrate::preempt_cb()
00176 {
00177   ROS_INFO("%s: Preempt calibration.", action_name_.c_str());
00178   node_.getRobot().cancelAsyncTask();
00179 }


hekateros_control
Author(s): Robin Knight , Daniel Packard
autogenerated on Mon Oct 6 2014 00:36:42