Go to the documentation of this file.00001
00047 #include <kinova/KinovaTypes.h>
00048
00049 #include "jaco_driver/jaco_angles_action.h"
00050
00051 #include "jaco_driver/jaco_types.h"
00052
00053
00054 namespace jaco
00055 {
00056
00057 JacoAnglesActionServer::JacoAnglesActionServer(JacoComm &arm_comm, const ros::NodeHandle &nh)
00058 : arm_comm_(arm_comm),
00059 node_handle_(nh, "joint_angles"),
00060 action_server_(node_handle_, "arm_joint_angles",
00061 boost::bind(&JacoAnglesActionServer::actionCallback, this, _1), false)
00062 {
00063 double tolerance;
00064 node_handle_.param<double>("stall_interval_seconds", stall_interval_seconds_, 0.5);
00065 node_handle_.param<double>("stall_threshold", stall_threshold_, 1.0);
00066 node_handle_.param<double>("rate_hz", rate_hz_, 10.0);
00067 node_handle_.param<double>("tolerance", tolerance, 2.0);
00068 tolerance_ = (float)tolerance;
00069
00070 action_server_.start();
00071 }
00072
00073
00074 JacoAnglesActionServer::~JacoAnglesActionServer()
00075 {
00076 }
00077
00078
00079 void JacoAnglesActionServer::actionCallback(const jaco_msgs::ArmJointAnglesGoalConstPtr &goal)
00080 {
00081 jaco_msgs::ArmJointAnglesFeedback feedback;
00082 jaco_msgs::ArmJointAnglesResult result;
00083 JacoAngles current_joint_angles;
00084 ros::Time current_time = ros::Time::now();
00085
00086 try
00087 {
00088 arm_comm_.getJointAngles(current_joint_angles);
00089
00090 if (arm_comm_.isStopped())
00091 {
00092 ROS_INFO("Could not complete joint angle action because the arm is 'stopped'.");
00093 result.angles = current_joint_angles.constructAnglesMsg();
00094 action_server_.setAborted(result);
00095 return;
00096 }
00097
00098 last_nonstall_time_ = current_time;
00099 last_nonstall_angles_ = current_joint_angles;
00100
00101 JacoAngles target(goal->angles);
00102 arm_comm_.setJointAngles(target);
00103
00104
00105
00106
00107 while (true)
00108 {
00109 ros::spinOnce();
00110
00111 if (action_server_.isPreemptRequested() || !ros::ok())
00112 {
00113 result.angles = current_joint_angles.constructAnglesMsg();
00114 arm_comm_.stopAPI();
00115 arm_comm_.startAPI();
00116 action_server_.setPreempted(result);
00117 return;
00118 }
00119 else if (arm_comm_.isStopped())
00120 {
00121 result.angles = current_joint_angles.constructAnglesMsg();
00122 action_server_.setAborted(result);
00123 return;
00124 }
00125
00126 arm_comm_.getJointAngles(current_joint_angles);
00127 current_time = ros::Time::now();
00128 feedback.angles = current_joint_angles.constructAnglesMsg();
00129 action_server_.publishFeedback(feedback);
00130
00131 if (target.isCloseToOther(current_joint_angles, tolerance_))
00132 {
00133
00134 result.angles = current_joint_angles.constructAnglesMsg();
00135 action_server_.setSucceeded(result);
00136 return;
00137 }
00138 else if (!last_nonstall_angles_.isCloseToOther(current_joint_angles, stall_threshold_))
00139 {
00140
00141 last_nonstall_time_ = current_time;
00142 last_nonstall_angles_ = current_joint_angles;
00143 }
00144 else if ((current_time - last_nonstall_time_).toSec() > stall_interval_seconds_)
00145 {
00146
00147 result.angles = current_joint_angles.constructAnglesMsg();
00148 arm_comm_.stopAPI();
00149 arm_comm_.startAPI();
00150 action_server_.setPreempted(result);
00151 return;
00152 }
00153
00154 ros::Rate(rate_hz_).sleep();
00155 }
00156 }
00157 catch(const std::exception& e)
00158 {
00159 result.angles = current_joint_angles.constructAnglesMsg();
00160 ROS_ERROR_STREAM(e.what());
00161 action_server_.setAborted(result);
00162 }
00163 }
00164
00165 }