00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00026 #include <malloc.h>
00027 #include <assert.h>
00028 #include <math.h>
00029 #include "Jacobian.h"
00030 #include "DH_Link.h"
00031 #include "math\Matrixmxn.h"
00032 #include "math\VectorN.h"
00033 #include "math\Vector4.h"
00034
00035 #define PI 3.1415926535897932384
00036 #define END_EFF_DOF 2
00037
00038 CJacobian::CJacobian(RobotBase& robot)
00039 {
00040 m_pRobot = &robot;
00041 m_nDof = robot.DOF();
00042 m_configCurrent.SetNumDOF(m_nDof);
00043 m_bOrientation = false;
00044 m_bPosition = true;
00045 m_nJoint = m_nDof;
00046 m_pMatrix = new Matrixmxn(3, m_nDof);
00047 m_pJointMatrix = new Matrix4x4[m_nDof];
00048 m_pJointOrig = new Vector4[m_nDof];
00049 m_pJointZ = new Vector4[m_nDof];
00050 }
00051
00052 CJacobian::~CJacobian()
00053 {
00054 delete (m_pMatrix);
00055 delete [] m_pJointMatrix;
00056 delete [] m_pJointOrig;
00057 delete [] m_pJointZ;
00058 }
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 bool CJacobian::SetInterestPoint(int joint, Frame& endFrame, bool position, bool orient, int baseJoint)
00089 {
00090 int m_nDof = m_pRobot->DOF();
00091
00092
00093
00094
00095
00096
00097 if ((m_bPosition != position)||(m_bOrientation != orient) || (m_nJoint != joint) || (m_nBaseJoint!= baseJoint) )
00098 {
00099 m_frObserve = endFrame;
00100 m_nJoint = joint;
00101 m_bPosition = position;
00102 m_bOrientation = orient;
00103 m_nBaseJoint = baseJoint;
00104 delete m_pMatrix;
00105 if (orient && position)
00106 {
00107 m_pMatrix = new Matrixmxn(6, m_nDof);
00108 }
00109 else if (orient || position)
00110 {
00111 m_pMatrix = new Matrixmxn(3, m_nDof);
00112 }
00113 else
00114 {
00115 assert(false);
00116 return false;
00117 }
00118 }
00119
00120 if (!Calculate())
00121 {
00122 return false;
00123 }
00124
00125 return true;
00126 }
00127
00128
00129 bool CJacobian::GetPhyVelocity(VectorN& phyVel, VectorN& jointVel)
00130 {
00131 assert(m_pMatrix->GetRows() == phyVel.Length());
00132 phyVel = (*m_pMatrix) * jointVel;
00133 return true;
00134 }
00135
00136
00137 bool CJacobian::GetJointVelocity(VectorN& jointVel, VectorN& phyVel)
00138 {
00139 Matrixmxn inv;
00140 m_pMatrix->Inverse(inv);
00141 jointVel = inv*phyVel;
00142 return true;
00143 }
00144
00145 bool CJacobian::SetConfiguration(Configuration& conf)
00146 {
00147 m_configCurrent = conf;
00148 if (UpdateJoints())
00149 return true;
00150 else
00151 return false;
00152 }
00153
00154 Matrixmxn* CJacobian::GetMatrix()
00155 {
00156 return m_pMatrix;
00157 }
00158
00159 bool CJacobian::UpdateJoints()
00160 {
00161 std::list< LinkBase*> robotLinks = m_pRobot->GetAllLinks();
00162 std::list< LinkBase* >::iterator link = robotLinks.begin();
00163
00164 Frame frame;
00165 for (int i = 0; i < m_nDof; i++, link++)
00166 {
00167 ((DH_Link *)(*link))->SetJointVariable(m_configCurrent[i]);
00168 frame *= ((DH_Link *)(*link))->GetFrame();
00169 m_pJointMatrix[i] = frame;
00170
00171 m_pJointOrig[i] = frame.GetTranslationVector();
00172 m_pJointZ[i][0] = frame(0, 2);
00173 m_pJointZ[i][1] = frame(1, 2);
00174 m_pJointZ[i][2] = frame(2, 2);
00175 }
00176 return true;
00177 }
00178
00179 bool CJacobian::Calculate()
00180 {
00181 assert(m_nDof >= m_nJoint);
00182
00183 Vector4 pEndZ;
00184 Vector4 pEndOrig;
00185
00186 Frame frame = m_pJointMatrix[m_nJoint];
00187 frame *= m_frObserve;
00188
00189 pEndOrig = frame.GetTranslationVector();
00190 pEndZ[0] = frame(0, 2);
00191 pEndZ[1] = frame(1, 2);
00192 pEndZ[2] = frame(2, 2);
00193
00194
00195 for (int i = 0; i < m_nBaseJoint; i++)
00196 {
00197 int nRow=3;
00198 if ((m_bOrientation) && (m_bPosition))
00199 nRow = 6;
00200 for (int j = 0; j< nRow; j++)
00201 {
00202 m_pMatrix->SetValues(j, i, (double)0);
00203 }
00204 }
00205
00206
00207 std::list< LinkBase*> robotLinks = m_pRobot->GetAllLinks();
00208 std::list< LinkBase* >::iterator link = robotLinks.begin();
00209 for (i = m_nBaseJoint; i <= m_nJoint; i++)
00210 {
00211 int jointType = ((DH_Link*)(*link))->GetJointType();
00212 assert((jointType == DH_THETA) || (jointType == DH_D));
00213
00214 Vector4 pIPTool;
00215 Vector4 linearColumn;
00216 Vector4 angularColumn;
00217
00218
00219 if (m_bPosition)
00220 {
00221 linearColumn = m_pJointZ[i];
00222 if (jointType == DH_THETA)
00223 {
00224 pIPTool = pEndOrig - m_pJointOrig[i];
00225 linearColumn = linearColumn.Cross(pIPTool);
00226 }
00227
00228 for (int j = 0; j < 3; j++)
00229 {
00230 m_pMatrix->SetValues(j, i, linearColumn[j]);
00231 }
00232 }
00233
00234
00235 if (m_bOrientation)
00236 {
00237 angularColumn *= 0;
00238 if (jointType == DH_THETA)
00239 {
00240 angularColumn = m_pJointZ[i];
00241 }
00242 int offset = (m_bPosition)?3:0;
00243 for (int j = 0 ; j < 3; j++)
00244 {
00245 m_pMatrix->SetValues(offset+j, i, angularColumn[j]);
00246 }
00247 }
00248 link++;
00249 }
00250
00251 for (i = m_nJoint+1; i < m_nDof; i++)
00252 {
00253 int nRow=3;
00254 if ((m_bOrientation) && (m_bPosition))
00255 nRow = 6;
00256 for (int j = 0; j< nRow; j++)
00257 {
00258 m_pMatrix->SetValues(j, i, (double)0);
00259 }
00260 }
00261
00262 return true;
00263 }
00264
00265 Frame CJacobian::GetJointFrame(int nJoint) const
00266 {
00267 Frame lastJoint;
00268 if (nJoint > 0)
00269 {
00270 lastJoint = m_pJointMatrix[nJoint-1].Inverse();
00271 }
00272 lastJoint *= m_pJointMatrix[nJoint];
00273 return lastJoint;
00274 }
00275
00276 Frame CJacobian::GetJointWorldFrame(int nJoint) const
00277 {
00278 return m_pJointMatrix[nJoint];
00279 }
00280