$search
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 "trajectory_download_handler.h" 00033 #include "shared_types.h" 00034 #include "log_wrapper.h" 00035 #include "armadillo.h" 00036 #include "joint_traj_pt.h" 00037 #include "trajectory_job.h" 00038 #include "motoPlus.h" 00039 #include "smpl_msg_connection.h" 00040 00041 using namespace industrial::simple_message; 00042 using namespace industrial::shared_types; 00043 using namespace industrial::armadillo; 00044 using namespace industrial::joint_data; 00045 using namespace industrial::joint_traj_pt; 00046 using namespace industrial::joint_traj_pt_message; 00047 using namespace industrial::smpl_msg_connection; 00048 using namespace motoman::trajectory_job; 00049 using namespace motoman::controller; 00050 00051 namespace armadillo 00052 { 00053 namespace trajectory_download_handler 00054 { 00055 00056 00057 bool TrajectoryDownloadHandler::init(SmplMsgConnection* connection, Controller* ctrl) 00058 { 00059 this->ctrl_ = ctrl; 00060 return this->init(StandardMsgTypes::JOINT_TRAJ_PT, connection); 00061 } 00062 00063 bool TrajectoryDownloadHandler::internalCB(industrial::simple_message::SimpleMessage & in) 00064 { 00065 00066 bool rtn = false; 00067 SimpleMessage reply; 00068 JointTrajPtMessage jMsg; 00069 00070 jMsg.init(in); 00071 00072 switch( jMsg.point_.getSequence() ) 00073 { 00074 case SpecialSeqValues::START_TRAJECTORY_DOWNLOAD: 00075 startTrajectory(jMsg); 00076 rtn = true; 00077 break; 00078 00079 case SpecialSeqValues::END_TRAJECTORY: 00080 endTrajectory(jMsg); 00081 rtn = true; 00082 break; 00083 00084 case SpecialSeqValues::STOP_TRAJECTORY: 00085 LOG_DEBUG("Stoping trajectory"); 00086 this->ctrl_->stopMotionJob(JOB_NAME); 00087 rtn = true; 00088 break; 00089 00090 default: 00091 LOG_ERROR("Adding point: %d to trajectory", jMsg.point_.getSequence()); 00092 this->traj_.addPoint(jMsg.point_); 00093 break; 00094 } 00095 00096 // Send ack 00097 if (CommTypes::SERVICE_REQUEST == in.getCommType()) 00098 { 00099 if (rtn) 00100 { 00101 jMsg.toReply(reply, ReplyTypes::SUCCESS); 00102 } 00103 else 00104 { 00105 jMsg.toReply(reply, ReplyTypes::FAILURE); 00106 } 00107 00108 if(this->getConnection()->sendMsg(reply)) 00109 { 00110 LOG_INFO("Ack sent"); 00111 } 00112 else 00113 { 00114 LOG_ERROR("Failed to send joint ack"); 00115 } 00116 } 00117 00118 return rtn; 00119 } 00120 00121 void TrajectoryDownloadHandler::startTrajectory(JointTrajPtMessage & jMsg) 00122 { 00123 LOG_INFO("Trajectory download initialized, adding first point"); 00124 this->traj_.init(); 00125 this->traj_.addPoint(jMsg.point_); 00126 } 00127 00128 void TrajectoryDownloadHandler::endTrajectory(JointTrajPtMessage & jMsg) 00129 { 00130 00131 LOG_INFO("Trajecotry ended, starting job"); 00132 TrajectoryJob job; 00133 00134 // Add end point 00135 this->traj_.addPoint(jMsg.point_); 00136 00137 // Create, load, and execute motion 00138 if (job.init(JOB_NAME, this->traj_)) 00139 { 00140 if(job.toJobString(this->jobBuffer_, JOB_BUFFER_SIZE_)) 00141 { 00142 if(this->ctrl_->writeJob("", JOB_NAME)) 00143 { 00144 if(this->ctrl_->loadJob("", JOB_NAME)) 00145 { 00146 //this->ctrl_->startMotionJob(JOB_NAME); 00147 } 00148 else 00149 { 00150 LOG_ERROR("Failed to load job"); 00151 } 00152 } 00153 else 00154 { 00155 LOG_ERROR("Failed to write job"); 00156 } 00157 } 00158 else 00159 { 00160 LOG_ERROR("Failed to convert job to string"); 00161 } 00162 } 00163 else 00164 { 00165 LOG_ERROR("Failed to intialize job trajectory"); 00166 } 00167 } 00168 00169 }//namespace trajectory_download_handler 00170 }//namespace motoman 00171