2016-08-30 21:44:11 +00:00
|
|
|
#include "IKTrajectoryHelper.h"
|
|
|
|
#include "BussIK/Node.h"
|
|
|
|
#include "BussIK/Tree.h"
|
|
|
|
#include "BussIK/Jacobian.h"
|
|
|
|
#include "BussIK/VectorRn.h"
|
2016-09-08 06:14:23 +00:00
|
|
|
#include "BussIK/MatrixRmn.h"
|
2016-08-30 21:44:11 +00:00
|
|
|
#include "Bullet3Common/b3AlignedObjectArray.h"
|
2016-09-08 22:15:58 +00:00
|
|
|
#include "BulletDynamics/Featherstone/btMultiBody.h"
|
2016-08-30 21:44:11 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
#define RADIAN(X) ((X)*RadiansToDegrees)
|
2016-08-30 21:44:11 +00:00
|
|
|
|
|
|
|
//use BussIK and Reflexxes to convert from Cartesian endeffector future target to
|
|
|
|
//joint space positions at each real-time (simulation) step
|
|
|
|
struct IKTrajectoryHelperInternalData
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
VectorR3 m_endEffectorTargetPosition;
|
|
|
|
VectorRn m_nullSpaceVelocity;
|
|
|
|
VectorRn m_dampingCoeff;
|
2016-08-30 21:44:11 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
b3AlignedObjectArray<Node*> m_ikNodes;
|
|
|
|
|
|
|
|
IKTrajectoryHelperInternalData()
|
|
|
|
{
|
|
|
|
m_endEffectorTargetPosition.SetZero();
|
|
|
|
m_nullSpaceVelocity.SetZero();
|
|
|
|
m_dampingCoeff.SetZero();
|
|
|
|
}
|
|
|
|
};
|
2016-08-30 21:44:11 +00:00
|
|
|
|
|
|
|
IKTrajectoryHelper::IKTrajectoryHelper()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
m_data = new IKTrajectoryHelperInternalData;
|
2016-08-30 21:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IKTrajectoryHelper::~IKTrajectoryHelper()
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
delete m_data;
|
2016-08-30 21:44:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IKTrajectoryHelper::computeIK(const double endEffectorTargetPosition[3],
|
2018-09-23 21:17:31 +00:00
|
|
|
const double endEffectorTargetOrientation[4],
|
|
|
|
const double endEffectorWorldPosition[3],
|
|
|
|
const double endEffectorWorldOrientation[4],
|
|
|
|
const double* q_current, int numQ, int endEffectorIndex,
|
|
|
|
double* q_new, int ikMethod, const double* linear_jacobian, const double* angular_jacobian, int jacobian_size, const double dampIk[6])
|
2016-08-30 21:44:11 +00:00
|
|
|
{
|
2020-01-11 20:43:27 +00:00
|
|
|
MatrixRmn AugMat;
|
2018-09-23 21:17:31 +00:00
|
|
|
bool useAngularPart = (ikMethod == IK2_VEL_DLS_WITH_ORIENTATION || ikMethod == IK2_VEL_DLS_WITH_ORIENTATION_NULLSPACE || ikMethod == IK2_VEL_SDLS_WITH_ORIENTATION) ? true : false;
|
|
|
|
|
2019-07-11 00:21:18 +00:00
|
|
|
Jacobian ikJacobian(useAngularPart, numQ, 1);
|
2018-09-23 21:17:31 +00:00
|
|
|
|
|
|
|
ikJacobian.Reset();
|
|
|
|
|
|
|
|
bool UseJacobianTargets1 = false;
|
|
|
|
|
|
|
|
if (UseJacobianTargets1)
|
|
|
|
{
|
|
|
|
ikJacobian.SetJtargetActive();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ikJacobian.SetJendActive();
|
|
|
|
}
|
|
|
|
VectorR3 targets;
|
|
|
|
targets.Set(endEffectorTargetPosition[0], endEffectorTargetPosition[1], endEffectorTargetPosition[2]);
|
|
|
|
ikJacobian.ComputeJacobian(&targets); // Set up Jacobian and deltaS vectors
|
|
|
|
|
|
|
|
// Set one end effector world position from Bullet
|
|
|
|
VectorRn deltaS(3);
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
deltaS.Set(i, dampIk[i] * (endEffectorTargetPosition[i] - endEffectorWorldPosition[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set one end effector world orientation from Bullet
|
|
|
|
VectorRn deltaR(3);
|
|
|
|
if (useAngularPart)
|
|
|
|
{
|
|
|
|
btQuaternion startQ(endEffectorWorldOrientation[0], endEffectorWorldOrientation[1], endEffectorWorldOrientation[2], endEffectorWorldOrientation[3]);
|
|
|
|
btQuaternion endQ(endEffectorTargetOrientation[0], endEffectorTargetOrientation[1], endEffectorTargetOrientation[2], endEffectorTargetOrientation[3]);
|
|
|
|
btQuaternion deltaQ = endQ * startQ.inverse();
|
|
|
|
float angle = deltaQ.getAngle();
|
|
|
|
btVector3 axis = deltaQ.getAxis();
|
|
|
|
if (angle > PI)
|
|
|
|
{
|
|
|
|
angle -= 2.0 * PI;
|
|
|
|
}
|
|
|
|
else if (angle < -PI)
|
|
|
|
{
|
|
|
|
angle += 2.0 * PI;
|
|
|
|
}
|
|
|
|
float angleDot = angle;
|
|
|
|
btVector3 angularVel = angleDot * axis.normalize();
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
deltaR.Set(i, dampIk[i + 3] * angularVel[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2016-09-22 20:27:09 +00:00
|
|
|
if (useAngularPart)
|
|
|
|
{
|
|
|
|
VectorRn deltaC(6);
|
2018-09-23 21:17:31 +00:00
|
|
|
MatrixRmn completeJacobian(6, numQ);
|
2016-09-22 20:27:09 +00:00
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
deltaC.Set(i, deltaS[i]);
|
|
|
|
deltaC.Set(i + 3, deltaR[i]);
|
2016-09-22 20:27:09 +00:00
|
|
|
for (int j = 0; j < numQ; ++j)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
completeJacobian.Set(i, j, linear_jacobian[i * numQ + j]);
|
|
|
|
completeJacobian.Set(i + 3, j, angular_jacobian[i * numQ + j]);
|
2016-09-22 20:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 17:47:07 +00:00
|
|
|
ikJacobian.SetDeltaS(deltaC);
|
|
|
|
ikJacobian.SetJendTrans(completeJacobian);
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
else
|
2016-09-22 20:27:09 +00:00
|
|
|
{
|
|
|
|
VectorRn deltaC(3);
|
2018-09-23 21:17:31 +00:00
|
|
|
MatrixRmn completeJacobian(3, numQ);
|
2016-09-22 20:27:09 +00:00
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
deltaC.Set(i, deltaS[i]);
|
2016-09-22 20:27:09 +00:00
|
|
|
for (int j = 0; j < numQ; ++j)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
completeJacobian.Set(i, j, linear_jacobian[i * numQ + j]);
|
2016-09-22 20:27:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 17:47:07 +00:00
|
|
|
ikJacobian.SetDeltaS(deltaC);
|
|
|
|
ikJacobian.SetJendTrans(completeJacobian);
|
2016-09-22 20:27:09 +00:00
|
|
|
}
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the change in theta values
|
|
|
|
switch (ikMethod)
|
|
|
|
{
|
|
|
|
case IK2_JACOB_TRANS:
|
|
|
|
ikJacobian.CalcDeltaThetasTranspose(); // Jacobian transpose method
|
|
|
|
break;
|
2016-09-25 21:24:28 +00:00
|
|
|
case IK2_DLS:
|
|
|
|
case IK2_VEL_DLS_WITH_ORIENTATION:
|
2017-10-19 21:00:53 +00:00
|
|
|
case IK2_VEL_DLS:
|
2018-09-23 21:17:31 +00:00
|
|
|
//ikJacobian.CalcDeltaThetasDLS(); // Damped least squares method
|
|
|
|
assert(m_data->m_dampingCoeff.GetLength() == numQ);
|
2020-01-11 20:43:27 +00:00
|
|
|
ikJacobian.CalcDeltaThetasDLS2(m_data->m_dampingCoeff, AugMat);
|
2018-09-23 21:17:31 +00:00
|
|
|
break;
|
|
|
|
case IK2_VEL_DLS_WITH_NULLSPACE:
|
|
|
|
case IK2_VEL_DLS_WITH_ORIENTATION_NULLSPACE:
|
|
|
|
assert(m_data->m_nullSpaceVelocity.GetLength() == numQ);
|
2020-01-11 20:43:27 +00:00
|
|
|
ikJacobian.CalcDeltaThetasDLSwithNullspace(m_data->m_nullSpaceVelocity, AugMat);
|
2018-09-23 21:17:31 +00:00
|
|
|
break;
|
|
|
|
case IK2_DLS_SVD:
|
|
|
|
ikJacobian.CalcDeltaThetasDLSwithSVD();
|
|
|
|
break;
|
|
|
|
case IK2_PURE_PSEUDO:
|
|
|
|
ikJacobian.CalcDeltaThetasPseudoinverse(); // Pure pseudoinverse method
|
|
|
|
break;
|
|
|
|
case IK2_SDLS:
|
2017-10-19 21:00:53 +00:00
|
|
|
case IK2_VEL_SDLS:
|
|
|
|
case IK2_VEL_SDLS_WITH_ORIENTATION:
|
2018-09-23 21:17:31 +00:00
|
|
|
ikJacobian.CalcDeltaThetasSDLS(); // Selectively damped least squares method
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ikJacobian.ZeroDeltaThetas();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use for velocity IK, update theta dot
|
|
|
|
//ikJacobian.UpdateThetaDot();
|
|
|
|
|
|
|
|
// Use for position IK, incrementally update theta
|
|
|
|
//ikJacobian.UpdateThetas();
|
|
|
|
|
|
|
|
// Apply the change in the theta values
|
|
|
|
//ikJacobian.UpdatedSClampValue(&targets);
|
|
|
|
|
|
|
|
for (int i = 0; i < numQ; i++)
|
|
|
|
{
|
|
|
|
// Use for velocity IK
|
|
|
|
q_new[i] = ikJacobian.dTheta[i] + q_current[i];
|
|
|
|
|
|
|
|
// Use for position IK
|
|
|
|
//q_new[i] = m_data->m_ikNodes[i]->GetTheta();
|
|
|
|
}
|
|
|
|
return true;
|
2016-09-08 22:15:58 +00:00
|
|
|
}
|
2016-09-30 05:45:31 +00:00
|
|
|
|
2019-07-11 00:21:18 +00:00
|
|
|
|
|
|
|
bool IKTrajectoryHelper::computeIK2(
|
|
|
|
const double* endEffectorTargetPositions,
|
|
|
|
const double* endEffectorCurrentPositions,
|
|
|
|
int numEndEffectors,
|
|
|
|
const double* q_current, int numQ,
|
|
|
|
double* q_new, int ikMethod, const double* linear_jacobians, const double dampIk[6])
|
|
|
|
{
|
2020-01-11 20:43:27 +00:00
|
|
|
MatrixRmn AugMat;
|
2019-07-11 00:21:18 +00:00
|
|
|
bool useAngularPart = false;//for now (ikMethod == IK2_VEL_DLS_WITH_ORIENTATION || ikMethod == IK2_VEL_DLS_WITH_ORIENTATION_NULLSPACE || ikMethod == IK2_VEL_SDLS_WITH_ORIENTATION) ? true : false;
|
|
|
|
|
|
|
|
Jacobian ikJacobian(useAngularPart, numQ, numEndEffectors);
|
|
|
|
|
|
|
|
ikJacobian.Reset();
|
|
|
|
|
|
|
|
bool UseJacobianTargets1 = false;
|
|
|
|
|
|
|
|
if (UseJacobianTargets1)
|
|
|
|
{
|
|
|
|
ikJacobian.SetJtargetActive();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ikJacobian.SetJendActive();
|
|
|
|
}
|
|
|
|
|
|
|
|
VectorRn deltaC(numEndEffectors *3);
|
|
|
|
MatrixRmn completeJacobian(numEndEffectors*3, numQ);
|
|
|
|
|
|
|
|
for (int ne = 0; ne < numEndEffectors; ne++)
|
|
|
|
{
|
|
|
|
VectorR3 targets;
|
|
|
|
targets.Set(endEffectorTargetPositions[ne*3+0], endEffectorTargetPositions[ne * 3 + 1], endEffectorTargetPositions[ne * 3 + 2]);
|
|
|
|
|
|
|
|
// Set one end effector world position from Bullet
|
|
|
|
VectorRn deltaS(3);
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
deltaS.Set(i, dampIk[i] * (endEffectorTargetPositions[ne*3+i] - endEffectorCurrentPositions[ne*3+i]));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i)
|
|
|
|
{
|
|
|
|
deltaC.Set(ne*3+i, deltaS[i]);
|
|
|
|
for (int j = 0; j < numQ; ++j)
|
|
|
|
{
|
2019-07-12 05:22:35 +00:00
|
|
|
completeJacobian.Set(ne * 3 + i, j, linear_jacobians[((ne*3+i) * numQ) + j]);
|
2019-07-11 00:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ikJacobian.SetDeltaS(deltaC);
|
|
|
|
ikJacobian.SetJendTrans(completeJacobian);
|
|
|
|
|
|
|
|
// Calculate the change in theta values
|
|
|
|
switch (ikMethod)
|
|
|
|
{
|
|
|
|
case IK2_JACOB_TRANS:
|
|
|
|
ikJacobian.CalcDeltaThetasTranspose(); // Jacobian transpose method
|
|
|
|
break;
|
|
|
|
case IK2_DLS:
|
|
|
|
case IK2_VEL_DLS_WITH_ORIENTATION:
|
|
|
|
case IK2_VEL_DLS:
|
|
|
|
//ikJacobian.CalcDeltaThetasDLS(); // Damped least squares method
|
|
|
|
assert(m_data->m_dampingCoeff.GetLength() == numQ);
|
2020-01-11 20:43:27 +00:00
|
|
|
ikJacobian.CalcDeltaThetasDLS2(m_data->m_dampingCoeff, AugMat);
|
2019-07-11 00:21:18 +00:00
|
|
|
break;
|
|
|
|
case IK2_VEL_DLS_WITH_NULLSPACE:
|
|
|
|
case IK2_VEL_DLS_WITH_ORIENTATION_NULLSPACE:
|
|
|
|
assert(m_data->m_nullSpaceVelocity.GetLength() == numQ);
|
2020-01-11 20:43:27 +00:00
|
|
|
ikJacobian.CalcDeltaThetasDLSwithNullspace(m_data->m_nullSpaceVelocity, AugMat);
|
2019-07-11 00:21:18 +00:00
|
|
|
break;
|
|
|
|
case IK2_DLS_SVD:
|
|
|
|
ikJacobian.CalcDeltaThetasDLSwithSVD();
|
|
|
|
break;
|
|
|
|
case IK2_PURE_PSEUDO:
|
|
|
|
ikJacobian.CalcDeltaThetasPseudoinverse(); // Pure pseudoinverse method
|
|
|
|
break;
|
|
|
|
case IK2_SDLS:
|
|
|
|
case IK2_VEL_SDLS:
|
|
|
|
case IK2_VEL_SDLS_WITH_ORIENTATION:
|
|
|
|
ikJacobian.CalcDeltaThetasSDLS(); // Selectively damped least squares method
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ikJacobian.ZeroDeltaThetas();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use for velocity IK, update theta dot
|
|
|
|
//ikJacobian.UpdateThetaDot();
|
|
|
|
|
|
|
|
// Use for position IK, incrementally update theta
|
|
|
|
//ikJacobian.UpdateThetas();
|
|
|
|
|
|
|
|
// Apply the change in the theta values
|
|
|
|
//ikJacobian.UpdatedSClampValue(&targets);
|
|
|
|
|
|
|
|
for (int i = 0; i < numQ; i++)
|
|
|
|
{
|
|
|
|
// Use for velocity IK
|
|
|
|
q_new[i] = ikJacobian.dTheta[i] + q_current[i];
|
|
|
|
|
|
|
|
// Use for position IK
|
|
|
|
//q_new[i] = m_data->m_ikNodes[i]->GetTheta();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-30 08:03:40 +00:00
|
|
|
bool IKTrajectoryHelper::computeNullspaceVel(int numQ, const double* q_current, const double* lower_limit, const double* upper_limit, const double* joint_range, const double* rest_pose)
|
2016-09-30 05:45:31 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
m_data->m_nullSpaceVelocity.SetLength(numQ);
|
|
|
|
m_data->m_nullSpaceVelocity.SetZero();
|
2017-09-27 21:14:57 +00:00
|
|
|
// TODO: Expose the coefficents of the null space term so that the user can choose to balance the null space task and the IK target task.
|
|
|
|
// Can also adaptively adjust the coefficients based on the residual of the null space velocity in the IK target task space.
|
2018-09-23 21:17:31 +00:00
|
|
|
double stayCloseToZeroGain = 0.001;
|
|
|
|
double stayAwayFromLimitsGain = 10.0;
|
|
|
|
|
|
|
|
// Stay close to zero
|
|
|
|
for (int i = 0; i < numQ; ++i)
|
|
|
|
{
|
|
|
|
m_data->m_nullSpaceVelocity[i] = stayCloseToZeroGain * (rest_pose[i] - q_current[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stay away from joint limits
|
|
|
|
for (int i = 0; i < numQ; ++i)
|
|
|
|
{
|
|
|
|
if (q_current[i] > upper_limit[i])
|
|
|
|
{
|
|
|
|
m_data->m_nullSpaceVelocity[i] += stayAwayFromLimitsGain * (upper_limit[i] - q_current[i]) / joint_range[i];
|
|
|
|
}
|
|
|
|
if (q_current[i] < lower_limit[i])
|
|
|
|
{
|
|
|
|
m_data->m_nullSpaceVelocity[i] += stayAwayFromLimitsGain * (lower_limit[i] - q_current[i]) / joint_range[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2016-11-04 17:47:07 +00:00
|
|
|
}
|
2017-02-01 06:58:37 +00:00
|
|
|
|
|
|
|
bool IKTrajectoryHelper::setDampingCoeff(int numQ, const double* coeff)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
m_data->m_dampingCoeff.SetLength(numQ);
|
|
|
|
m_data->m_dampingCoeff.SetZero();
|
|
|
|
for (int i = 0; i < numQ; ++i)
|
|
|
|
{
|
|
|
|
m_data->m_dampingCoeff[i] = coeff[i];
|
|
|
|
}
|
|
|
|
return true;
|
2017-02-01 06:58:37 +00:00
|
|
|
}
|