trajectory_job.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 #ifdef ROS
00032 #include "dx100/ros_conversion.h"
00033 #include "dx100/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)
00111 {
00112 
00113   bool rtn = false;
00114 
00115   if( strlen(name) <= NAME_BUFFER_SIZE_ )
00116   {
00117     strcpy(this->name_, name);
00118     rtn = true;
00119   }
00120   else
00121   {
00122     LOG_ERROR("Failed to initialize trajectory job, name size %d too large",
00123               NAME_BUFFER_SIZE_);
00124     rtn = false;
00125   }
00126   return rtn;
00127 
00128 }
00129 
00130 bool TrajectoryJob::toJobString(JointTraj & trajectory, char* str_buffer, size_t buffer_size)
00131 {
00132   bool rtn = false;
00133   if (0 < buffer_size)
00134   {
00135 
00136     // Header begin
00137     // ----------------------------------------------------------------------------
00138     strcpy(str_buffer, "");
00139     APPEND_LINE(str_buffer, buffer_size, "/JOB");
00140 
00141     sprintf(this->line_buffer_, "//NAME %s", this->name_);
00142     APPEND_LINE(str_buffer, buffer_size, this->line_buffer_);
00143 
00144     APPEND_LINE(str_buffer, buffer_size, "//POS");
00145 
00146     sprintf(this->line_buffer_, "///NPOS %d,0,0,0,0,0", trajectory.size());
00147     APPEND_LINE(str_buffer, buffer_size, this->line_buffer_);
00148 
00149     APPEND_LINE(str_buffer, buffer_size, "///TOOL 0");
00150     APPEND_LINE(str_buffer, buffer_size, "///POSTYPE PULSE");
00151     APPEND_LINE(str_buffer, buffer_size, "///PULSE");
00152 
00153     // Point declaration and initialization
00154     // ----------------------------------------------------------------------------
00155     JointTrajPt pt;
00156     JointData rosData;
00157     JointData mpData;
00158 
00159     for(int i = 0; i < trajectory.size(); i++)
00160     {
00161       sprintf(this->line_buffer_, "C%05d=", i);
00162       SAFE_STRCAT(str_buffer, buffer_size, this->line_buffer_);
00163       trajectory.getPoint(i, pt);
00164       pt.getJointPosition(rosData);
00165       
00166       // Converting to a motoplus joint (i.e. the correct order and units
00167       toMpJoint(rosData, mpData);
00168       for(int j = 0; j < mpData.getMaxNumJoints(); j++)
00169       {
00170         // Don't append comma to last position, instead line-feed.
00171         if (j < (mpData.getMaxNumJoints() - 1))
00172         {
00173           sprintf(this->line_buffer_, "%d,", (int)mpData.getJoint(j));
00174           SAFE_STRCAT(str_buffer, buffer_size, this->line_buffer_);
00175         }
00176         else
00177         {
00178           sprintf(this->line_buffer_, "%d", (int)mpData.getJoint(j));
00179           APPEND_LINE(str_buffer, buffer_size, this->line_buffer_);
00180         }
00181       }
00182     }
00183 
00184     // Header end
00185     // ----------------------------------------------------------------------------
00186     APPEND_LINE(str_buffer, buffer_size, "//INST");
00187     APPEND_LINE(str_buffer, buffer_size, "///DATE 1979/10/01 00:00");
00188     APPEND_LINE(str_buffer, buffer_size, "///ATTR SC,RW");
00189     APPEND_LINE(str_buffer, buffer_size, "///GROUP1 RB1");
00190 
00191     // Program
00192     APPEND_LINE(str_buffer, buffer_size, "NOP");
00193     for(int i = 0; i < trajectory.size(); i++)
00194     {
00195       trajectory.getPoint(i, pt);
00196       sprintf(this->line_buffer_, "MOVJ C%05d VJ=%.2f", i, pt.getVelocity());
00197       APPEND_LINE(str_buffer, buffer_size, this->line_buffer_);
00198     }
00199     APPEND_LINE(str_buffer, buffer_size, "END");
00200 
00201     rtn = true;
00202   }
00203   else
00204   {
00205     rtn = false;
00206     LOG_ERROR("Failed to generate job string");
00207   }
00208   return rtn;
00209 
00210 }
00211 
00212 
00213 }
00214 }
00215 


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