joint_trajectory_downloader.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Southwest Research Institute
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *      * Redistributions of source code must retain the above copyright
00011  *      notice, this list of conditions and the following disclaimer.
00012  *      * Redistributions in binary form must reproduce the above copyright
00013  *      notice, this list of conditions and the following disclaimer in the
00014  *      documentation and/or other materials provided with the distribution.
00015  *      * Neither the name of the Southwest Research Institute, nor the names
00016  *      of its contributors may be used to endorse or promote products derived
00017  *      from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029  * POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include <dx100/joint_trajectory_downloader.h>
00033 #include "simple_message/joint_traj_pt.h"
00034 #include "simple_message/messages/joint_traj_pt_message.h"
00035 #include "simple_message/smpl_msg_connection.h"
00036 #include <dx100/utils.h>
00037 
00038 using namespace industrial::smpl_msg_connection;
00039 using namespace industrial::joint_data;
00040 using namespace industrial::joint_traj_pt;
00041 using namespace industrial::joint_traj_pt_message;
00042 using namespace industrial::simple_message;
00043 using namespace motoman::utils;
00044 
00045 
00046 namespace motoman
00047 {
00048 namespace joint_trajectory_downloader
00049 {
00050 JointTrajectoryDownloader::JointTrajectoryDownloader()
00051 {
00052 }
00053 
00054 JointTrajectoryDownloader::JointTrajectoryDownloader(ros::NodeHandle &n,
00055                 SmplMsgConnection* robotConnecton) : node_(n)
00056 {
00057         ROS_INFO("Constructor joint trajectory downloader node");
00058 
00059         this->sub_joint_trajectory_ = this->node_.subscribe("command",
00060                         0, &JointTrajectoryDownloader::jointTrajectoryCB, this);
00061         this->robot_ = robotConnecton;
00062   this->robot_->makeConnect();
00063         ROS_INFO("Joint trajectory downloader node initialized");
00064 }
00065 
00066 JointTrajectoryDownloader::~JointTrajectoryDownloader()
00067 {  
00068         trajectoryStop();
00069         this->sub_joint_trajectory_.shutdown();
00070 }
00071 
00072 void JointTrajectoryDownloader::jointTrajectoryCB(
00073                 const trajectory_msgs::JointTrajectoryConstPtr &msg)
00074 {
00075         ROS_INFO("Receiving joint trajectory message");
00076 
00077         if (!checkTrajectory(msg))
00078         {
00079                 ROS_ERROR("Joint trajectory check failed, trajectory not downloaded");
00080                 return;
00081         }
00082   
00083         std::vector<double> joint_velocity_limits;
00084   
00085         if (!getVelocityLimits("robot_description", msg, joint_velocity_limits))
00086         {
00087                 ROS_ERROR("Failed to get joint velocity limits");
00088                 return;
00089         }
00090 
00091   if (!this->robot_->isConnected())
00092   {
00093     ROS_WARN("Attempting robot reconnection");
00094     this->robot_->makeConnect();
00095   }
00096   
00097   ROS_INFO("Sending trajectory points, size: %d", msg->points.size());
00098 
00099         for (int i = 0; i < msg->points.size(); i++)
00100         {
00101                 ROS_INFO("Sending joints trajectory point[%d]", i);
00102 
00103                 JointTrajPt jPt;
00104                 JointTrajPtMessage jMsg;
00105                 SimpleMessage topic;
00106 
00107                 // Performing a manual copy of the joint velocities in order to send them
00108     // to the utility function.  Passing the pt data members doesn't seem to
00109     // work.
00110     std::vector<double> joint_velocities(0.0);
00111     double velocity =0 ;
00112     joint_velocities.resize(msg->joint_names.size(), 0.0);
00113     for (int j = 0; j < joint_velocities.size(); j++)
00114     {
00115       joint_velocities[j] = msg->points[i].velocities[j];
00116     }
00117     ROS_DEBUG("Joint velocities copied");
00118     velocity = toMotomanVelocity(joint_velocity_limits, joint_velocities);
00119 
00120     jPt.setVelocity(velocity);
00121 
00122                 // The first and last sequence values must be given a special sequence
00123                 // value
00124                 if (0 == i)
00125                 {
00126                         ROS_DEBUG("First trajectory point, setting special sequence value");
00127                         jPt.setSequence(SpecialSeqValues::START_TRAJECTORY_DOWNLOAD);
00128                 }
00129                 else if (msg->points.size() - 1 == i)
00130                 {
00131                         ROS_DEBUG("Last trajectory point, setting special sequence value");
00132                         jPt.setSequence(SpecialSeqValues::END_TRAJECTORY);
00133                 }
00134                 else
00135                 {
00136                         jPt.setSequence(i);
00137                 }
00138 
00139                 // Copy position data to local variable
00140                 JointData data;
00141                 for (int j = 0; j < msg->joint_names.size(); j++)
00142                 {
00143                         data.setJoint(j, msg->points[i].positions[j]);
00144                 }
00145 
00146                 // Initialize joint trajectory message
00147                 jPt.setJointPosition(data);
00148 
00149                 jMsg.init(jPt);
00150                 jMsg.toTopic(topic);
00151 
00152                 ROS_DEBUG("Sending joint trajectory point");
00153                 if (this->robot_->sendMsg(topic))
00154                 {
00155                         ROS_INFO("Point[%d] sent to controller", i);
00156                 }
00157                 else
00158                 {
00159                         ROS_WARN("Failed sent joint point, skipping point");
00160                 }
00161         }
00162 }
00163 
00164 void JointTrajectoryDownloader::trajectoryStop()
00165 {
00166   JointTrajPtMessage jMsg;
00167   SimpleMessage msg;
00168   SimpleMessage reply;
00169 
00170   ROS_INFO("Joint trajectory downloader: entering stopping state");
00171   jMsg.point_.setSequence(SpecialSeqValues::STOP_TRAJECTORY);
00172   jMsg.toRequest(msg);
00173   ROS_DEBUG("Sending stop command");
00174   this->robot_->sendAndReceiveMsg(msg, reply);
00175   ROS_DEBUG("Stop command sent");
00176 }
00177 
00178 } //joint_trajectory_handler
00179 } //motoman
00180 


dx100
Author(s): Shaun Edwards (Southwest Research Institute)
autogenerated on Thu Jan 2 2014 11:29:36