$search
00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2011, Yaskawa America, Inc. 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 Yaskawa America, Inc., 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 "p_var_q.h" 00033 #include "joint_data.h" 00034 #include "joint_motion_handler.h" 00035 #include "ros_conversion.h" 00036 #include "log_wrapper.h" 00037 #include "mp_wrapper.h" 00038 00039 using motoman::joint_motion_handler; 00040 using motoman::ros_conversion; 00041 using industrial::joint_data; 00042 00043 00044 namespace motoman 00045 { 00046 namespace p_var_q 00047 { 00048 00049 00050 PVarQ::PVarQ() 00051 { 00052 00053 // Set up point variable 00054 pointData_.usType = MP_RESTYPE_VAR_ROBOT; 00055 00056 // TODO: Should check constants such as Q_SIZE, MOTION_POINTER & BUFFER_POINTER for 00057 // consitency 00058 00059 // Checking class constants for consistancy 00060 STATIC_ASSERT(QSIZE_ > PT_LOOK_AHEAD_, QSIZE_NOT_GREATER_THAN_LOOK_AHEAD); 00061 STATIC_ASSERT(MOTION_POINTER_ > QSIZE_, MOTION_PONTER_NOT_GREATER_THAN_QSIZE); 00062 STATIC_ASSERT(BUFFER_POINTER_ > QSIZE_, BUFFER_PONTER_NOT_GREATER_THAN_QSIZE); 00063 STATIC_ASSERT(MIN_BUF_START_POINTER_ > QSIZE_, MIN_BUFF_START_PONTER_NOT_GREATER_THAN_QSIZE); 00064 00065 } 00066 00067 PVarQ::~PVarQ(void) 00068 { 00069 } 00070 00071 00072 void PVarQ::init(industrial::joint_data::JointData & point, double velocity_percent) 00073 { 00074 // Reseting the position buffer. This should make it eaiser to catch any bugs on the 00075 // motoman side. 00076 industrial::joint_data::JointData empty; 00077 for(int i = 0; i < this->posVarQueueSize(); i++) 00078 { 00079 this->setPosition(i, empty, 0); 00080 } 00081 00082 // Seed the intial point - this is required because upon startup, the indexes are zero and 00083 // therefore the intial point never gets set. 00084 setPosition(0, point, this->TEMP_getVelocityPercent()); 00085 00086 // Set the minium buffer size 00087 motoman::mp_wrapper::setInteger(MIN_BUF_START_POINTER_, PT_LOOK_AHEAD_); 00088 } 00089 00090 void PVarQ::addPoint(industrial::joint_data::JointData & joints, double velocity_percent) 00091 { 00092 00093 // Wait until buffer is not full 00094 00095 LOG_DEBUG("Checking for buffer full"); 00096 while(this->bufferFull()) { 00097 mpTaskDelay(this->BUFFER_POLL_TICK_DELAY_); 00098 }; 00099 LOG_DEBUG("Buffer ready to accept new points"); 00100 00101 setNextPosition(joints, this->TEMP_getVelocityPercent()); 00102 incBufferIndex(); 00103 00104 } 00105 00106 int PVarQ::bufferSize() 00107 { 00108 int motionIdx = this->getMotionIndex(); 00109 int bufferIdx = this->getBufferIndex(); 00110 int rtn = 0; 00111 00112 if (motionIdx > bufferIdx) 00113 { 00114 LOG_ERROR("Motion index: %d is greater than Buffer index: %d, returning empty buffer size", motionIdx, bufferIdx); 00115 rtn = 0; 00116 } 00117 else 00118 { 00119 rtn = bufferIdx - motionIdx; 00120 } 00121 00122 return rtn; 00123 } 00124 00125 00126 00127 int PVarQ::getMotionPosIndex() 00128 { 00129 return (this->getMotionIndex() % this->posVarQueueSize()); 00130 } 00131 00132 int PVarQ::getBufferPosIndex() 00133 { 00134 return (this->getBufferIndex() % this->posVarQueueSize()); 00135 } 00136 00137 int PVarQ::getNextBufferPosIndex() 00138 { 00139 return ((this->getBufferIndex() + 1) % this->posVarQueueSize()); 00140 } 00141 00142 bool PVarQ::bufferFull() 00143 { 00144 bool rtn = false; 00145 int bufferSize = this->bufferSize(); 00146 int maxBufferSize = this->maxBufferSize(); 00147 if (bufferSize >= maxBufferSize) 00148 { 00149 rtn = true; 00150 } 00151 return rtn; 00152 } 00153 00154 bool PVarQ::bufferEmpty() 00155 { 00156 bool rtn = false; 00157 if (this->bufferSize() <= 0) 00158 { 00159 LOG_DEBUG("Buffer is empty"); 00160 rtn = true; 00161 } 00162 return rtn; 00163 } 00164 00165 void PVarQ::incBufferIndex() 00166 { 00167 int bufferIdx = this->getBufferIndex(); 00168 00169 LOG_DEBUG("Incrementing buffer index from %d to %d", bufferIdx, bufferIdx + 1); 00170 motoman::mp_wrapper::setInteger(BUFFER_POINTER_, bufferIdx + 1); 00171 } 00172 00173 00174 void PVarQ::setNextPosition(industrial::joint_data::JointData & point, double velocity_percent) 00175 { 00176 setPosition(this->getNextBufferPosIndex(), point, velocity_percent); 00177 } 00178 00179 00180 void PVarQ::setPosition(int index, industrial::joint_data::JointData & point, 00181 double velocity_percent) 00182 { 00183 const double VELOCITY_CONVERSION = 100.0; 00184 int convertedVelocity = 0; 00185 00186 LOG_DEBUG("Setting joint position, index: %d", index); 00187 motoman::mp_wrapper::toMpPosVarData(index, point, this->pointData_); 00188 00189 while (mpPutPosVarData ( &(this->pointData_), 1 ) == ERROR) { 00190 LOG_ERROR("Failed set position variable, index: %d, retrying...", index); 00191 mpTaskDelay(this->VAR_POLL_TICK_DELAY_); 00192 }; 00193 00194 convertedVelocity = (int)(velocity_percent * VELOCITY_CONVERSION); 00195 LOG_DEBUG("Converting percent velocity: %g to motoman integer value: %d", 00196 velocity_percent, convertedVelocity); 00197 LOG_DEBUG("Setting velocity, index: %d, value: %d", index, convertedVelocity); 00198 motoman::mp_wrapper::setInteger(index, convertedVelocity); 00199 } 00200 00201 00202 00203 }//namespace p_var_q 00204 }//namespace motoman