$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 #ifdef ROS 00032 #include "ros_conversion.h" 00033 #include "trajectory_job.h" 00034 #include "string.h" 00035 #include "simple_message/joint_traj_pt.h" 00036 #include "simple_message/shared_types.h" 00037 #include "simple_message/log_wrapper.h" 00038 #endif 00039 00040 #ifdef MOTOPLUS 00041 #include "ros_conversion.h" 00042 #include "trajectory_job.h" 00043 #include "string.h" 00044 #include "joint_traj_pt.h" 00045 #include "shared_types.h" 00046 #include "log_wrapper.h" 00047 #endif 00048 00049 using namespace industrial::shared_types; 00050 using namespace industrial::joint_traj; 00051 using namespace industrial::joint_traj_pt; 00052 using namespace industrial::joint_data; 00053 using namespace motoman::ros_conversion; 00054 00055 namespace motoman 00056 { 00057 namespace trajectory_job 00058 { 00059 00068 // A character is reserved for NULL (not sure it is needed) 00069 00070 #define SAFE_STRCAT(dest, dest_max_size, src) \ 00071 do \ 00072 { \ 00073 if (strlen(src) < dest_max_size - strlen(dest) - 1) \ 00074 { \ 00075 strcat(dest, src); \ 00076 } \ 00077 else \ 00078 { \ 00079 LOG_ERROR("STRCAT failed to append %s to %s", src, dest); \ 00080 return false; \ 00081 } \ 00082 } while (0) 00083 00089 #define APPEND_LINEFEED(dest, dest_max_size) SAFE_STRCAT(dest, dest_max_size, "\r\n"); 00090 00096 #define APPEND_LINE(dest, dest_max_size, src) \ 00097 do \ 00098 { \ 00099 SAFE_STRCAT(dest, dest_max_size, src); \ 00100 APPEND_LINEFEED(dest, dest_max_size); \ 00101 } while (0) 00102 00103 TrajectoryJob::TrajectoryJob(void) 00104 { 00105 } 00106 TrajectoryJob::~TrajectoryJob(void) 00107 { 00108 } 00109 00110 bool TrajectoryJob::init(const char* name, JointTraj joint_traj) 00111 { 00112 bool rtn = false; 00113 if( strlen(name) <= NAME_BUFFER_SIZE_ ) 00114 { 00115 strcpy(this->name_, name); 00116 this->trajectory_.copyFrom(joint_traj); 00117 rtn = true; 00118 } 00119 else 00120 { 00121 LOG_ERROR("Failed to initialize trajectory job, name size %d too large", 00122 NAME_BUFFER_SIZE_); 00123 rtn = false; 00124 } 00125 return rtn; 00126 00127 } 00128 00129 bool TrajectoryJob::toJobString(char* str_buffer, size_t buffer_size) 00130 { 00131 bool rtn = false; 00132 if (0 < buffer_size) 00133 { 00134 00135 // Header begin 00136 // ---------------------------------------------------------------------------- 00137 strcpy(str_buffer, ""); 00138 APPEND_LINE(str_buffer, buffer_size, "/JOB"); 00139 00140 sprintf(this->line_buffer_, "//NAME %s", this->name_); 00141 APPEND_LINE(str_buffer, buffer_size, this->line_buffer_); 00142 00143 APPEND_LINE(str_buffer, buffer_size, "//POS"); 00144 00145 sprintf(this->line_buffer_, "///NPOS %d,0,0,0,0,0", this->trajectory_.size()); 00146 APPEND_LINE(str_buffer, buffer_size, this->line_buffer_); 00147 00148 APPEND_LINE(str_buffer, buffer_size, "///TOOL 0"); 00149 APPEND_LINE(str_buffer, buffer_size, "///POSTYPE PULSE"); 00150 APPEND_LINE(str_buffer, buffer_size, "///PULSE"); 00151 00152 // Point declaration and initialization 00153 // ---------------------------------------------------------------------------- 00154 JointTrajPt pt; 00155 JointData rosData; 00156 JointData mpData; 00157 00158 for(int i = 0; i < this->trajectory_.size(); i++) 00159 { 00160 sprintf(this->line_buffer_, "C%05d=", i); 00161 SAFE_STRCAT(str_buffer, buffer_size, this->line_buffer_); 00162 this->trajectory_.getPoint(i, pt); 00163 pt.getJointPosition(rosData); 00164 00165 // Converting to a motoplus joint (i.e. the correct order and units 00166 toMpJoint(rosData, mpData); 00167 for(int j = 0; j < mpData.getMaxNumJoints(); j++) 00168 { 00169 // Don't append comma to last position, instead line-feed. 00170 if (j < (mpData.getMaxNumJoints() - 1)) 00171 { 00172 sprintf(this->line_buffer_, "%d,", (int)mpData.getJoint(j)); 00173 SAFE_STRCAT(str_buffer, buffer_size, this->line_buffer_); 00174 } 00175 else 00176 { 00177 sprintf(this->line_buffer_, "%d", (int)mpData.getJoint(j)); 00178 APPEND_LINE(str_buffer, buffer_size, this->line_buffer_); 00179 } 00180 } 00181 } 00182 00183 // Header end 00184 // ---------------------------------------------------------------------------- 00185 APPEND_LINE(str_buffer, buffer_size, "//INST"); 00186 APPEND_LINE(str_buffer, buffer_size, "///DATE 1979/10/01 00:00"); 00187 APPEND_LINE(str_buffer, buffer_size, "///ATTR SC,RW"); 00188 APPEND_LINE(str_buffer, buffer_size, "///GROUP1 RB1"); 00189 00190 // Program 00191 APPEND_LINE(str_buffer, buffer_size, "NOP"); 00192 for(int i = 0; i < this->trajectory_.size(); i++) 00193 { 00194 this->trajectory_.getPoint(i, pt); 00195 sprintf(this->line_buffer_, "MOVJ C%05d VJ=%.2f", i, pt.getVelocity()); 00196 APPEND_LINE(str_buffer, buffer_size, this->line_buffer_); 00197 } 00198 APPEND_LINE(str_buffer, buffer_size, "END"); 00199 00200 rtn = true; 00201 } 00202 else 00203 { 00204 rtn = false; 00205 LOG_ERROR("Failed to generate job string"); 00206 } 00207 return rtn; 00208 } 00209 00210 00211 } 00212 } 00213