jaco_arm.cpp
Go to the documentation of this file.
00001 //============================================================================
00002 // Name        : jaco_arm.cpp
00003 // Author      : WPI, Clearpath Robotics
00004 // Version     : 0.5
00005 // Copyright   : BSD
00006 // Description : A ROS driver for controlling the Kinova Jaco robotic manipulator arm
00007 //============================================================================
00008 
00009 #include "jaco_driver/jaco_arm.h"
00010 
00011 namespace jaco
00012 {
00013 
00014 JacoArm::JacoArm(JacoComm &arm_comm, ros::NodeHandle &nh) : arm(arm_comm)
00015 {
00016         std::string joint_velocity_topic, joint_angles_topic, cartesian_velocity_topic,
00017                 tool_position_topic, set_finger_position_topic, finger_position_topic, joint_state_topic,
00018                 set_joint_angle_topic;
00019 
00020         nh.param<std::string>("joint_velocity_topic", joint_velocity_topic, "joint_velocity");
00021         nh.param<std::string>("joint_angles_topic", joint_angles_topic, "joint_angles");
00022         nh.param<std::string>("cartesian_velocity_topic", cartesian_velocity_topic, "cartesian_velocity");
00023         nh.param<std::string>("tool_position_topic", tool_position_topic, "tool_position");
00024         nh.param<std::string>("set_finger_position_topic", set_finger_position_topic, "set_finger_position");
00025         nh.param<std::string>("finger_position_topic", finger_position_topic, "finger_position");
00026         nh.param<std::string>("joint_state_topic", joint_state_topic, "joint_state");
00027 
00028         //Print out received topics
00029         ROS_DEBUG("Got Joint Velocity Topic Name: <%s>", joint_velocity_topic.c_str());
00030         ROS_DEBUG("Got Joint Angles Topic Name: <%s>", joint_angles_topic.c_str());
00031         ROS_DEBUG("Got Cartesian Velocity Topic Name: <%s>", cartesian_velocity_topic.c_str());
00032         ROS_DEBUG("Got Tool Position Topic Name: <%s>", tool_position_topic.c_str());
00033         ROS_DEBUG("Got Set Finger Position Topic Name: <%s>", set_finger_position_topic.c_str());
00034         ROS_DEBUG("Got Finger Position Topic Name: <%s>", finger_position_topic.c_str());
00035         ROS_DEBUG("Got Joint State Topic Name: <%s>", joint_state_topic.c_str());
00036 
00037         ROS_INFO("Starting Up Jaco Arm Controller...");
00038 
00039         previous_state = 0;
00040 
00041         /* Set up Services */
00042         stop_service = nh.advertiseService("stop", &JacoArm::StopSRV, this);
00043         start_service = nh.advertiseService("start", &JacoArm::StartSRV, this);
00044         homing_service = nh.advertiseService("home_arm", &JacoArm::HomeArmSRV, this);
00045 
00046         /* Set Default Configuration */
00047 
00048         // API->RestoreFactoryDefault(); // uncomment comment ONLY if you want to lose your settings on each launch.
00049 
00050         ClientConfigurations configuration;
00051         arm.GetConfig(configuration);
00052         arm.PrintConfig(configuration);
00053 
00054         last_update_time = ros::Time::now();
00055         update_time = ros::Duration(5.0);
00056 
00057         /* Storing arm in home position */
00058 
00059         /* Set up Publishers */
00060         JointAngles_pub = nh.advertise<jaco_driver::JointAngles>(joint_angles_topic, 2);
00061         JointState_pub = nh.advertise<sensor_msgs::JointState>(joint_state_topic, 2);
00062         ToolPosition_pub = nh.advertise<geometry_msgs::PoseStamped>(tool_position_topic, 2);
00063         FingerPosition_pub = nh.advertise<jaco_driver::FingerPosition>(finger_position_topic, 2);
00064 
00065         /* Set up Subscribers*/
00066         JointVelocity_sub = nh.subscribe(joint_velocity_topic, 1, &JacoArm::VelocityMSG, this);
00067         CartesianVelocity_sub = nh.subscribe(cartesian_velocity_topic, 1, &JacoArm::CartesianVelocityMSG, this);
00068 
00069         status_timer = nh.createTimer(ros::Duration(0.05), &JacoArm::StatusTimer, this);
00070 
00071         joint_vel_timer = nh.createTimer(ros::Duration(0.01), &JacoArm::JointVelTimer, this);
00072         joint_vel_timer.stop();
00073         joint_vel_timer_flag = false;
00074         cartesian_vel_timer = nh.createTimer(ros::Duration(0.01), &JacoArm::CartesianVelTimer, this);
00075         cartesian_vel_timer.stop();
00076         cartesian_vel_timer_flag = false;
00077 
00078         ROS_INFO("The Arm is ready to use.");
00079 
00080         TrajectoryPoint Jaco_Velocity;
00081 
00082         memset(&Jaco_Velocity, 0, sizeof(Jaco_Velocity)); //zero structure
00083 
00084         arm.SetCartesianVelocities(Jaco_Velocity.Position.CartesianPosition);
00085 }
00086 
00087 JacoArm::~JacoArm()
00088 {
00089 }
00090 
00091 bool JacoArm::HomeArmSRV(jaco_driver::HomeArm::Request &req, jaco_driver::HomeArm::Response &res)
00092 {
00093         arm.HomeArm();
00094         res.homearm_result = "JACO ARM HAS BEEN RETURNED HOME";
00095 
00096         return true;
00097 }
00098 
00099 void JacoArm::VelocityMSG(const jaco_driver::JointVelocityConstPtr& joint_vel)
00100 {
00101         if (!arm.Stopped())
00102         {
00103                 joint_velocities.Actuator1 = joint_vel->Velocity_J1;
00104                 joint_velocities.Actuator2 = joint_vel->Velocity_J2;
00105                 joint_velocities.Actuator3 = joint_vel->Velocity_J3;
00106                 joint_velocities.Actuator4 = joint_vel->Velocity_J4;
00107                 joint_velocities.Actuator5 = joint_vel->Velocity_J5;
00108                 joint_velocities.Actuator6 = joint_vel->Velocity_J6;
00109                 last_joint_update = ros::Time().now();
00110 
00111                 if (joint_vel_timer_flag == false)
00112                 {
00113                         joint_vel_timer.start();
00114                         joint_vel_timer_flag = true;
00115                 }
00116         }
00117 }
00118 
00125 bool JacoArm::StopSRV(jaco_driver::Stop::Request &req, jaco_driver::Stop::Response &res)
00126 {
00127         arm.Stop();
00128         res.stop_result = "JACO ARM HAS BEEN STOPPED";
00129         ROS_DEBUG("JACO ARM STOP REQUEST");
00130 
00131         return true;
00132 }
00133 
00139 bool JacoArm::StartSRV(jaco_driver::Start::Request &req, jaco_driver::Start::Response &res)
00140 {
00141         arm.Start();
00142         res.start_result = "JACO ARM CONTROL HAS BEEN ENABLED";
00143         ROS_DEBUG("JACO ARM START REQUEST");
00144 
00145         return true;
00146 }
00147 
00148 
00149 void JacoArm::CartesianVelocityMSG(const geometry_msgs::TwistStampedConstPtr& cartesian_vel)
00150 {
00151         if (!arm.Stopped())
00152         {
00153                 cartesian_velocities.X = cartesian_vel->twist.linear.x;
00154                 cartesian_velocities.Y = cartesian_vel->twist.linear.y;
00155                 cartesian_velocities.Z = cartesian_vel->twist.linear.z;
00156                 cartesian_velocities.ThetaX = cartesian_vel->twist.angular.x;
00157                 cartesian_velocities.ThetaY = cartesian_vel->twist.angular.y;
00158                 cartesian_velocities.ThetaZ = cartesian_vel->twist.angular.z;
00159 
00160                 last_cartesian_update = ros::Time().now();
00161 
00162                 if (cartesian_vel_timer_flag == false)
00163                 {
00164                         cartesian_vel_timer.start();
00165                         cartesian_vel_timer_flag = true;
00166                 }
00167         }
00168 }
00169 
00170 void JacoArm::CartesianVelTimer(const ros::TimerEvent&)
00171 {
00172         arm.SetCartesianVelocities(cartesian_velocities);
00173 
00174         if ((ros::Time().now().toSec() - last_cartesian_update.toSec()) > 1)
00175         {
00176                 cartesian_vel_timer.stop();
00177                 cartesian_vel_timer_flag = false;
00178         }
00179 }
00180 
00181 void JacoArm::JointVelTimer(const ros::TimerEvent&)
00182 {
00183         arm.SetVelocities(joint_velocities);
00184 
00185         if ((ros::Time().now().toSec() - last_joint_update.toSec()) > 1)
00186         {
00187                 joint_vel_timer.stop();
00188                 joint_vel_timer_flag = false;
00189         }
00190 }
00191 
00198 void JacoArm::GoHome(void)
00199 {
00200 /*
00201         AngularInfo joint_home;
00202 
00203         joint_home.Actuator1 = 176.0;
00204         joint_home.Actuator2 = 111.0;
00205         joint_home.Actuator3 = 107.0;
00206         joint_home.Actuator4 = 459.0;
00207         joint_home.Actuator5 = 102.0;
00208         joint_home.Actuator6 = 106.0;
00209 
00210         SetAngles(joint_home, 10); //send joints to home position
00211 
00212         API->SetCartesianControl();
00213 */
00214 }
00215 
00227 void JacoArm::BroadCastAngles(void)
00228 {
00229         // Populate an array of joint names.  arm_0_joint is the base, arm_5_joint is the wrist.
00230         const char* nameArgs[] = {"arm_0_joint", "arm_1_joint", "arm_2_joint", "arm_3_joint", "arm_4_joint", "arm_5_joint"};
00231         std::vector<std::string> JointName(nameArgs, nameArgs+6);
00232 
00233         jaco_driver::JointAngles current_angles;
00234 
00235         sensor_msgs::JointState joint_state;
00236         joint_state.name = JointName;
00237 
00238         // Define array sizes for the joint_state topic
00239         joint_state.position.resize(6);
00240         joint_state.velocity.resize(6);
00241         joint_state.effort.resize(6);
00242 
00243         //Query arm for current joint angles
00244         JacoAngles arm_angles;
00245         arm.GetAngles(arm_angles);
00246         jaco_driver::JointAngles ros_angles = arm_angles.Angles();
00247 
00248         // Transform from Kinova DH algorithm to physical angles in radians, then place into vector array
00249         joint_state.position[0] = ros_angles.Angle_J1;
00250         joint_state.position[1] = ros_angles.Angle_J2;
00251         joint_state.position[2] = ros_angles.Angle_J3;
00252         joint_state.position[3] = ros_angles.Angle_J4;
00253         joint_state.position[4] = ros_angles.Angle_J5;
00254         joint_state.position[5] = ros_angles.Angle_J6;
00255 
00256         //Publish the joint state messages
00257         ros_angles.Angle_J1 = arm_angles.Actuator1;
00258         ros_angles.Angle_J2 = arm_angles.Actuator2;
00259         ros_angles.Angle_J3 = arm_angles.Actuator3;
00260         ros_angles.Angle_J4 = arm_angles.Actuator4;
00261         ros_angles.Angle_J5 = arm_angles.Actuator5;
00262         ros_angles.Angle_J6 = arm_angles.Actuator6;
00263         JointAngles_pub.publish(ros_angles); // Publishes the raw joint angles in a custom message.
00264 
00265         JointState_pub.publish(joint_state);     // Publishes the transformed angles in a standard sensor_msgs format.
00266 }
00267 
00271 void JacoArm::BroadCastPosition(void)
00272 {
00273         JacoPose pose;
00274         geometry_msgs::PoseStamped current_position;
00275 
00276         arm.GetPosition(pose);
00277         current_position.pose = pose.Pose();
00278 
00279         ToolPosition_pub.publish(current_position);
00280 }
00281 
00285 void JacoArm::BroadCastFingerPosition(void)
00286 {
00287 
00288 
00289         FingerAngles fingers;
00290         jaco_driver::FingerPosition finger_position;
00291 
00292         arm.GetFingers(fingers);
00293 
00294         FingerPosition_pub.publish(fingers.Fingers());
00295 }
00296 
00297 
00298 void JacoArm::StatusTimer(const ros::TimerEvent&)
00299 {
00300         BroadCastAngles();
00301         BroadCastPosition();
00302         BroadCastFingerPosition();
00303 }
00304 
00305 }


jaco_driver
Author(s): Jeff Schmidt (Clearpath), Alex Bencz (Clearpath), Matt DeDonato (WPI)
autogenerated on Mon Jan 6 2014 11:23:43