00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "jaco_driver/jaco_arm.h"
00010 #include <string>
00011 #include <vector>
00012
00013 #define PI 3.14159265359
00014
00015
00016 namespace jaco
00017 {
00018
00019 JacoArm::JacoArm(JacoComm &arm, const ros::NodeHandle &nodeHandle)
00020 : jaco_comm_(arm), node_handle_(nodeHandle)
00021 {
00022
00023 stop_service_ = node_handle_.advertiseService("in/stop", &JacoArm::stopServiceCallback, this);
00024 start_service_ = node_handle_.advertiseService("in/start", &JacoArm::startServiceCallback, this);
00025 homing_service_ = node_handle_.advertiseService("in/home_arm", &JacoArm::homeArmServiceCallback, this);
00026
00027
00028 joint_angles_publisher_ = node_handle_.advertise<jaco_msgs::JointAngles>("out/joint_angles", 2);
00029 joint_state_publisher_ = node_handle_.advertise<sensor_msgs::JointState>("out/joint_state", 2);
00030 tool_position_publisher_ = node_handle_.advertise<geometry_msgs::PoseStamped>("out/tool_position", 2);
00031 finger_position_publisher_ = node_handle_.advertise<jaco_msgs::FingerPosition>("out/finger_position", 2);
00032
00033
00034 joint_velocity_subscriber_ = node_handle_.subscribe("in/joint_velocity", 1,
00035 &JacoArm::jointVelocityCallback, this);
00036 cartesian_velocity_subscriber_ = node_handle_.subscribe("in/cartesian_velocity", 1,
00037 &JacoArm::cartesianVelocityCallback, this);
00038
00039 node_handle_.param<double>("status_interval_seconds", status_interval_seconds_, 0.1);
00040 node_handle_.param<double>("joint_angular_vel_timeout", joint_vel_timeout_seconds_, 0.25);
00041 node_handle_.param<double>("cartesian_vel_timeout", cartesian_vel_timeout_seconds_, 0.25);
00042 node_handle_.param<double>("joint_angular_vel_timeout", joint_vel_interval_seconds_, 0.1);
00043 node_handle_.param<double>("cartesian_vel_timeout", cartesian_vel_interval_seconds_, 0.1);
00044
00045 status_timer_ = node_handle_.createTimer(ros::Duration(status_interval_seconds_),
00046 &JacoArm::statusTimer, this);
00047
00048 joint_vel_timer_ = node_handle_.createTimer(ros::Duration(joint_vel_interval_seconds_),
00049 &JacoArm::jointVelocityTimer, this);
00050 joint_vel_timer_.stop();
00051 joint_vel_timer_flag_ = false;
00052
00053 cartesian_vel_timer_ = node_handle_.createTimer(ros::Duration(cartesian_vel_interval_seconds_),
00054 &JacoArm::cartesianVelocityTimer, this);
00055 cartesian_vel_timer_.stop();
00056 cartesian_vel_timer_flag_ = false;
00057
00058 ROS_INFO("The arm is ready to use.");
00059 }
00060
00061
00062 JacoArm::~JacoArm()
00063 {
00064 }
00065
00066
00067 bool JacoArm::homeArmServiceCallback(jaco_msgs::HomeArm::Request &req, jaco_msgs::HomeArm::Response &res)
00068 {
00069 jaco_comm_.homeArm();
00070 jaco_comm_.initFingers();
00071 res.homearm_result = "JACO ARM HAS BEEN RETURNED HOME";
00072 return true;
00073 }
00074
00075
00076 void JacoArm::jointVelocityCallback(const jaco_msgs::JointVelocityConstPtr& joint_vel)
00077 {
00078 if (!jaco_comm_.isStopped())
00079 {
00080 joint_velocities_.Actuator1 = joint_vel->joint1;
00081 joint_velocities_.Actuator2 = joint_vel->joint2;
00082 joint_velocities_.Actuator3 = joint_vel->joint3;
00083 joint_velocities_.Actuator4 = joint_vel->joint4;
00084 joint_velocities_.Actuator5 = joint_vel->joint5;
00085 joint_velocities_.Actuator6 = joint_vel->joint6;
00086 last_joint_vel_cmd_time_ = ros::Time().now();
00087
00088 if (joint_vel_timer_flag_ == false)
00089 {
00090 joint_vel_timer_.start();
00091 joint_vel_timer_flag_ = true;
00092 }
00093 }
00094 }
00095
00096
00103 bool JacoArm::stopServiceCallback(jaco_msgs::Stop::Request &req, jaco_msgs::Stop::Response &res)
00104 {
00105 jaco_comm_.stopAPI();
00106 res.stop_result = "Arm stopped";
00107 ROS_DEBUG("Arm stop requested");
00108 return true;
00109 }
00110
00111
00117 bool JacoArm::startServiceCallback(jaco_msgs::Start::Request &req, jaco_msgs::Start::Response &res)
00118 {
00119 jaco_comm_.startAPI();
00120 res.start_result = "Arm started";
00121 ROS_DEBUG("Arm start requested");
00122 return true;
00123 }
00124
00125
00126 void JacoArm::cartesianVelocityCallback(const geometry_msgs::TwistStampedConstPtr& cartesian_vel)
00127 {
00128 if (!jaco_comm_.isStopped())
00129 {
00130 cartesian_velocities_.X = cartesian_vel->twist.linear.x;
00131 cartesian_velocities_.Y = cartesian_vel->twist.linear.y;
00132 cartesian_velocities_.Z = cartesian_vel->twist.linear.z;
00133 cartesian_velocities_.ThetaX = cartesian_vel->twist.angular.x;
00134 cartesian_velocities_.ThetaY = cartesian_vel->twist.angular.y;
00135 cartesian_velocities_.ThetaZ = cartesian_vel->twist.angular.z;
00136
00137 last_cartesian_vel_cmd_time_ = ros::Time().now();
00138
00139 if (cartesian_vel_timer_flag_ == false)
00140 {
00141 cartesian_vel_timer_.start();
00142 cartesian_vel_timer_flag_ = true;
00143 }
00144 }
00145 }
00146
00147
00148 void JacoArm::cartesianVelocityTimer(const ros::TimerEvent&)
00149 {
00150 double elapsed_time_seconds = ros::Time().now().toSec() - last_cartesian_vel_cmd_time_.toSec();
00151
00152 if (elapsed_time_seconds > cartesian_vel_timeout_seconds_)
00153 {
00154 ROS_INFO("Cartesian vel timed out: %f", elapsed_time_seconds);
00155 cartesian_vel_timer_.stop();
00156 cartesian_vel_timer_flag_ = false;
00157 }
00158 else
00159 {
00160 ROS_INFO("Cart vel timer (%f): %f, %f, %f, %f, %f, %f", elapsed_time_seconds,
00161 cartesian_velocities_.X, cartesian_velocities_.Y, cartesian_velocities_.Z,
00162 cartesian_velocities_.ThetaX, cartesian_velocities_.ThetaY, cartesian_velocities_.ThetaZ);
00163 jaco_comm_.setCartesianVelocities(cartesian_velocities_);
00164 }
00165 }
00166
00167
00168 void JacoArm::jointVelocityTimer(const ros::TimerEvent&)
00169 {
00170 double elapsed_time_seconds = ros::Time().now().toSec() - last_joint_vel_cmd_time_.toSec();
00171
00172 if (elapsed_time_seconds > joint_vel_timeout_seconds_)
00173 {
00174 ROS_INFO("Joint vel timed out: %f", elapsed_time_seconds);
00175 joint_vel_timer_.stop();
00176 joint_vel_timer_flag_ = false;
00177 }
00178 else
00179 {
00180 ROS_INFO("Joint vel timer (%f): %f, %f, %f, %f, %f, %f", elapsed_time_seconds,
00181 joint_velocities_.Actuator1, joint_velocities_.Actuator2, joint_velocities_.Actuator3,
00182 joint_velocities_.Actuator4, joint_velocities_.Actuator5, joint_velocities_.Actuator6);
00183 jaco_comm_.setJointVelocities(joint_velocities_);
00184 }
00185 }
00186
00187
00199 void JacoArm::publishJointAngles(void)
00200 {
00201
00202 JacoAngles current_angles;
00203 jaco_comm_.getJointAngles(current_angles);
00204 jaco_msgs::JointAngles jaco_angles = current_angles.constructAnglesMsg();
00205
00206 jaco_angles.joint1 = current_angles.Actuator1;
00207 jaco_angles.joint2 = current_angles.Actuator2;
00208 jaco_angles.joint3 = current_angles.Actuator3;
00209 jaco_angles.joint4 = current_angles.Actuator4;
00210 jaco_angles.joint5 = current_angles.Actuator5;
00211 jaco_angles.joint6 = current_angles.Actuator6;
00212
00213 sensor_msgs::JointState joint_state;
00214 const char* nameArgs[] = {"jaco_joint_1", "jaco_joint_2", "jaco_joint_3", "jaco_joint_4", "jaco_joint_5", "jaco_joint_6"};
00215 std::vector<std::string> joint_names(nameArgs, nameArgs + 6);
00216 joint_state.name = joint_names;
00217
00218
00219 joint_state.position.resize(6);
00220
00221 joint_state.position[0] = (180- jaco_angles.joint1) * (PI / 180);
00222 joint_state.position[1] = (jaco_angles.joint2 - 270) * (PI / 180);
00223 joint_state.position[2] = (90-jaco_angles.joint3) * (PI / 180);
00224 joint_state.position[3] = (180-jaco_angles.joint4) * (PI / 180);
00225 joint_state.position[4] = (180-jaco_angles.joint5) * (PI / 180);
00226 joint_state.position[5] = (260-jaco_angles.joint6) * (PI / 180);
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242 joint_angles_publisher_.publish(jaco_angles);
00243 joint_state_publisher_.publish(joint_state);
00244 }
00245
00246
00250 void JacoArm::publishToolPosition(void)
00251 {
00252 JacoPose pose;
00253 geometry_msgs::PoseStamped current_position;
00254
00255 jaco_comm_.getCartesianPosition(pose);
00256 current_position.pose = pose.constructPoseMsg();
00257
00258 tool_position_publisher_.publish(current_position);
00259 }
00260
00261
00265 void JacoArm::publishFingerPosition(void)
00266 {
00267 FingerAngles fingers;
00268 jaco_comm_.getFingerPositions(fingers);
00269 finger_position_publisher_.publish(fingers.constructFingersMsg());
00270 }
00271
00272
00273 void JacoArm::statusTimer(const ros::TimerEvent&)
00274 {
00275 publishJointAngles();
00276 publishToolPosition();
00277 publishFingerPosition();
00278 }
00279
00280 }