feat(ik): optimize pose solutions away from joint limits
Add configurable joint-limit clearance optimization after pose convergence and regression coverage. Enable the optimizer for the Xiaozhao arm with bounded iteration and joint steps.
This commit is contained in:
parent
a761f4e935
commit
49137cce0b
@ -78,6 +78,15 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
bool refreshJointLimitPolicy_();
|
bool refreshJointLimitPolicy_();
|
||||||
|
Eigen::Matrix<double, 6, 1> poseErrorAt_(const Eigen::VectorXd& q_full,
|
||||||
|
pinocchio::FrameIndex target_frame,
|
||||||
|
const pinocchio::SE3& base_target);
|
||||||
|
Eigen::Matrix<double, 6, 1> weightedPoseError_(Eigen::Matrix<double, 6, 1> error) const;
|
||||||
|
Eigen::MatrixXd poseJacobianAt_(const Eigen::VectorXd& q_full,
|
||||||
|
pinocchio::FrameIndex target_frame);
|
||||||
|
void improvePoseJointLimitDistance_(Eigen::VectorXd& q_full,
|
||||||
|
pinocchio::FrameIndex target_frame,
|
||||||
|
const pinocchio::SE3& base_target);
|
||||||
|
|
||||||
// 配置
|
// 配置
|
||||||
config::PinocchioQpIKConfig config_;
|
config::PinocchioQpIKConfig config_;
|
||||||
|
|||||||
@ -160,6 +160,15 @@ namespace cmvr {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool PinocchioQpIKSolver::init() {
|
bool PinocchioQpIKSolver::init() {
|
||||||
|
const auto& pose_avoidance = config_.pose_ik_joint_limit_avoidance();
|
||||||
|
if (pose_avoidance.enable() &&
|
||||||
|
((pose_avoidance.has_max_iters() && pose_avoidance.max_iters() <= 0) ||
|
||||||
|
(pose_avoidance.has_max_joint_step_rad() &&
|
||||||
|
(!std::isfinite(pose_avoidance.max_joint_step_rad()) || pose_avoidance.max_joint_step_rad() <= 0.0)))) {
|
||||||
|
CMVR_LOG(ERROR) << "[PinocchioQpIKSolver] invalid pose IK joint limit avoidance settings";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (!urdf_chain_cached_) {
|
if (!urdf_chain_cached_) {
|
||||||
if (urdf_path_.empty() ||
|
if (urdf_path_.empty() ||
|
||||||
chain_base_frame_name_.empty() ||
|
chain_base_frame_name_.empty() ||
|
||||||
@ -337,6 +346,157 @@ namespace cmvr {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Eigen::Matrix<double, 6, 1> PinocchioQpIKSolver::poseErrorAt_(
|
||||||
|
const VectorXd& q_full, const pinocchio::FrameIndex target_frame,
|
||||||
|
const pinocchio::SE3& base_target)
|
||||||
|
{
|
||||||
|
updateKinematics(q_full);
|
||||||
|
const pinocchio::SE3 world_target = base_frame_id_ == static_cast<pinocchio::FrameIndex>(-1)
|
||||||
|
? base_target : data_->oMf[base_frame_id_] * base_target;
|
||||||
|
return pinocchio::log6(data_->oMf[target_frame].inverse() * world_target).toVector();
|
||||||
|
}
|
||||||
|
|
||||||
|
Eigen::Matrix<double, 6, 1> PinocchioQpIKSolver::weightedPoseError_(
|
||||||
|
Eigen::Matrix<double, 6, 1> error) const
|
||||||
|
{
|
||||||
|
error.head<3>() *= 1.0 - w_posrot_;
|
||||||
|
error.tail<3>() *= w_posrot_;
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
MatrixXd PinocchioQpIKSolver::poseJacobianAt_(
|
||||||
|
const VectorXd& q_full, const pinocchio::FrameIndex target_frame)
|
||||||
|
{
|
||||||
|
Eigen::Matrix<double, 6, Eigen::Dynamic> jacobian =
|
||||||
|
Eigen::Matrix<double, 6, Eigen::Dynamic>::Zero(6, model().nv);
|
||||||
|
pinocchio::computeFrameJacobian(model(), *data_, q_full, target_frame,
|
||||||
|
pinocchio::ReferenceFrame::LOCAL, jacobian);
|
||||||
|
return extractChainJacobian(jacobian);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PinocchioQpIKSolver::improvePoseJointLimitDistance_(
|
||||||
|
VectorXd& q_full, const pinocchio::FrameIndex target_frame,
|
||||||
|
const pinocchio::SE3& base_target)
|
||||||
|
{
|
||||||
|
const auto& settings = config_.pose_ik_joint_limit_avoidance();
|
||||||
|
if (!settings.enable() || jointLimitsDisabled()) return;
|
||||||
|
|
||||||
|
const Eigen::Index dof = chain_q_dof_;
|
||||||
|
const auto& lower = joint_pos_lower_limits_;
|
||||||
|
const auto& upper = joint_pos_upper_limits_;
|
||||||
|
const VectorXd spans = upper - lower;
|
||||||
|
std::vector<Eigen::Index> bounded_joints;
|
||||||
|
for (Eigen::Index i = 0; i < dof; ++i) {
|
||||||
|
if (std::isfinite(spans[i]) && spans[i] > 0.0) bounded_joints.push_back(i);
|
||||||
|
}
|
||||||
|
if (bounded_joints.empty()) return;
|
||||||
|
|
||||||
|
const auto clearance = [&](const VectorXd& q) {
|
||||||
|
double distance_ratio = 0.5;
|
||||||
|
for (const auto i : bounded_joints) {
|
||||||
|
distance_ratio = std::min(distance_ratio,
|
||||||
|
std::min(q[i] - lower[i], upper[i] - q[i]) / spans[i]);
|
||||||
|
}
|
||||||
|
return distance_ratio;
|
||||||
|
};
|
||||||
|
|
||||||
|
constexpr Eigen::Index pose_dimension = 6;
|
||||||
|
const Eigen::Index limit_row = dof + 1 + pose_dimension;
|
||||||
|
const Eigen::Index rows = limit_row + 2 * static_cast<Eigen::Index>(bounded_joints.size());
|
||||||
|
MatrixXd constraints = MatrixXd::Zero(rows, dof + 1);
|
||||||
|
constraints.topRows(dof + 1).setIdentity();
|
||||||
|
VectorXd min_constraint = VectorXd::Zero(rows);
|
||||||
|
VectorXd max_constraint = VectorXd::Zero(rows);
|
||||||
|
max_constraint[dof] = 0.5;
|
||||||
|
|
||||||
|
// With rho in [0, 0.5], minimizing (rho - 0.5)^2 maximizes clearance.
|
||||||
|
MatrixXd cost = MatrixXd::Zero(1, dof + 1);
|
||||||
|
cost(0, dof) = 1.0;
|
||||||
|
QPSolver clearance_solver;
|
||||||
|
clearance_solver.Setup(dof + 1, rows, qp_time_limit_, 1e-8, 1e-8, true);
|
||||||
|
clearance_solver.SetCostFunction(cost, VectorXd::Constant(1, 0.5));
|
||||||
|
|
||||||
|
const double max_step = settings.has_max_joint_step_rad() ? settings.max_joint_step_rad() : 0.02;
|
||||||
|
const int max_iters = settings.has_max_iters() ? settings.max_iters() : 20;
|
||||||
|
const VectorXd step_limit = joint_vel_limits_.cwiseMin(qdd_max_global_).cwiseMin(max_step);
|
||||||
|
VectorXd best_q = q_full.segment(chain_q_start_, dof);
|
||||||
|
const double initial_clearance = clearance(best_q);
|
||||||
|
double best_clearance = initial_clearance;
|
||||||
|
constexpr double min_improvement = 1e-7;
|
||||||
|
|
||||||
|
for (int iteration = 0; iteration < max_iters && best_clearance < 0.5 - min_improvement; ++iteration) {
|
||||||
|
const VectorXd min_step = (-step_limit).cwiseMax(lower - best_q);
|
||||||
|
const VectorXd max_step_at_q = step_limit.cwiseMin(upper - best_q);
|
||||||
|
min_constraint.head(dof) = min_step;
|
||||||
|
max_constraint.head(dof) = max_step_at_q;
|
||||||
|
const MatrixXd jacobian = poseJacobianAt_(q_full, target_frame);
|
||||||
|
if (!jacobian.allFinite()) break;
|
||||||
|
constraints.block(dof + 1, 0, pose_dimension, dof) = jacobian;
|
||||||
|
|
||||||
|
Eigen::Index row = limit_row;
|
||||||
|
for (const auto i : bounded_joints) {
|
||||||
|
constraints(row, i) = 1.0;
|
||||||
|
constraints(row, dof) = -spans[i];
|
||||||
|
min_constraint[row] = lower[i] - best_q[i];
|
||||||
|
max_constraint[row++] = max_step_at_q[i];
|
||||||
|
constraints(row, i) = 1.0;
|
||||||
|
constraints(row, dof) = spans[i];
|
||||||
|
min_constraint[row] = min_step[i];
|
||||||
|
max_constraint[row++] = upper[i] - best_q[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
clearance_solver.SetConstraintsFunction(constraints, min_constraint, max_constraint);
|
||||||
|
VectorXd initial = VectorXd::Zero(dof + 1);
|
||||||
|
initial[dof] = best_clearance;
|
||||||
|
clearance_solver.SetPrimalVariable(initial);
|
||||||
|
VectorXd solution;
|
||||||
|
try {
|
||||||
|
solution = clearance_solver.Solve();
|
||||||
|
} catch (const QPSolverException&) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (solution.size() != dof + 1 || !solution.allFinite() ||
|
||||||
|
solution[dof] <= best_clearance + min_improvement) break;
|
||||||
|
|
||||||
|
const VectorXd step = solution.head(dof).cwiseMax(min_step).cwiseMin(max_step_at_q);
|
||||||
|
bool accepted = false;
|
||||||
|
double scale = 1.0;
|
||||||
|
constexpr int max_backtracks = 8;
|
||||||
|
for (int attempt = 0; attempt < max_backtracks; ++attempt, scale *= 0.5) {
|
||||||
|
VectorXd candidate = kinematics::clampToJointPositionLimits(best_q + scale * step, lower, upper);
|
||||||
|
if (clearance(candidate) <= best_clearance + min_improvement) continue;
|
||||||
|
q_full.segment(chain_q_start_, dof) = candidate;
|
||||||
|
auto error = poseErrorAt_(q_full, target_frame, base_target);
|
||||||
|
if (!error.allFinite()) continue;
|
||||||
|
if (weightedPoseError_(error).norm() >= tol_) {
|
||||||
|
// A null-space step is only first-order accurate. Correct
|
||||||
|
// its residual within the same local joint step bounds.
|
||||||
|
const MatrixXd candidate_jacobian = poseJacobianAt_(q_full, target_frame);
|
||||||
|
const VectorXd correction = moorePenrosePseudoInverse(candidate_jacobian) * error;
|
||||||
|
const VectorXd corrected_step = (candidate - best_q + correction).cwiseMax(min_step).cwiseMin(max_step_at_q);
|
||||||
|
candidate = kinematics::clampToJointPositionLimits(best_q + corrected_step, lower, upper);
|
||||||
|
q_full.segment(chain_q_start_, dof) = candidate;
|
||||||
|
error = poseErrorAt_(q_full, target_frame, base_target);
|
||||||
|
}
|
||||||
|
const double candidate_clearance = clearance(candidate);
|
||||||
|
if (candidate.allFinite() && weightedPoseError_(error).norm() < tol_ &&
|
||||||
|
candidate_clearance > best_clearance + min_improvement) {
|
||||||
|
best_q = candidate;
|
||||||
|
best_clearance = candidate_clearance;
|
||||||
|
accepted = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
q_full.segment(chain_q_start_, dof) = best_q;
|
||||||
|
if (!accepted) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
updateKinematics(q_full);
|
||||||
|
CMVR_LOG(DEBUG) << "[PinocchioQpIKSolver][POSE_JOINT_LIMIT_AVOIDANCE] initial_clearance_ratio="
|
||||||
|
<< initial_clearance << ", final_clearance_ratio=" << best_clearance
|
||||||
|
<< ", goal=" << vectorToString(best_q);
|
||||||
|
}
|
||||||
|
|
||||||
bool PinocchioQpIKSolver::ik(const Matrix4d &target_pose,
|
bool PinocchioQpIKSolver::ik(const Matrix4d &target_pose,
|
||||||
std::vector<double> &joints_angle,
|
std::vector<double> &joints_angle,
|
||||||
bool is_tcp) {
|
bool is_tcp) {
|
||||||
@ -367,15 +527,19 @@ namespace cmvr {
|
|||||||
// 约定:target_pose 是 base_frame_name_ 下的 ^baseT_target
|
// 约定:target_pose 是 base_frame_name_ 下的 ^baseT_target
|
||||||
pinocchio::SE3 base_M_target = matrix4ToSE3(target_pose);
|
pinocchio::SE3 base_M_target = matrix4ToSE3(target_pose);
|
||||||
|
|
||||||
// 子链当前关节角(局部 DOF 大小)
|
|
||||||
Eigen::VectorXd q_local = Eigen::Map<Eigen::VectorXd>(
|
|
||||||
cur_joints_angle_.data(), cur_joints_angle_.size());
|
|
||||||
|
|
||||||
Eigen::VectorXd q_full;
|
Eigen::VectorXd q_full;
|
||||||
if (!buildFullQFromInput(cur_joints_angle_, q_full, "ik(qp)")) {
|
if (!buildFullQFromInput(cur_joints_angle_, q_full, "ik(qp)")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const VectorXd &q_min = joint_pos_lower_limits_;
|
||||||
|
const VectorXd &q_max = joint_pos_upper_limits_;
|
||||||
|
const VectorXd &qd_max = joint_vel_limits_;
|
||||||
|
const VectorXd &qdd_max = qdd_max_global_;
|
||||||
|
VectorXd q_local = kinematics::clampToJointPositionLimits(
|
||||||
|
q_full.segment(chain_q_start_, chain_q_dof_), q_min, q_max);
|
||||||
q_full.segment(chain_q_start_, chain_q_dof_) = q_local;
|
q_full.segment(chain_q_start_, chain_q_dof_) = q_local;
|
||||||
|
auto error = weightedPoseError_(poseErrorAt_(q_full, target_frame_id, base_M_target));
|
||||||
|
|
||||||
// 初始化 QP(变量 = 子链 DOF,约束 = 子链 DOF,因为只做 box 约束)
|
// 初始化 QP(变量 = 子链 DOF,约束 = 子链 DOF,因为只做 box 约束)
|
||||||
solver_.Setup(chain_q_dof_, chain_q_dof_, qp_time_limit_);
|
solver_.Setup(chain_q_dof_, chain_q_dof_, qp_time_limit_);
|
||||||
@ -384,51 +548,15 @@ namespace cmvr {
|
|||||||
MatrixXd A_cost;
|
MatrixXd A_cost;
|
||||||
VectorXd b_cost;
|
VectorXd b_cost;
|
||||||
|
|
||||||
const VectorXd &q_min = joint_pos_lower_limits_;
|
for (int iter = 0; iter < max_iters_ && error.allFinite() && error.norm() >= tol_; ++iter) {
|
||||||
const VectorXd &q_max = joint_pos_upper_limits_;
|
|
||||||
const VectorXd &qd_max = joint_vel_limits_;
|
|
||||||
const VectorXd &qdd_max = qdd_max_global_;
|
|
||||||
|
|
||||||
for (int iter = 0; iter < max_iters_; ++iter) {
|
|
||||||
// 1) 用 q_full 做正解 + frame pose + J_full
|
|
||||||
updateKinematics(q_full);
|
|
||||||
|
|
||||||
const pinocchio::SE3 &oM_curr = data_->oMf[target_frame_id];
|
|
||||||
|
|
||||||
// 0Tbase
|
|
||||||
pinocchio::SE3 oM_base = pinocchio::SE3::Identity();
|
|
||||||
if (base_frame_id_ != (pinocchio::FrameIndex) (-1)) {
|
|
||||||
oM_base = data_->oMf[base_frame_id_];
|
|
||||||
}
|
|
||||||
|
|
||||||
// 0Ttarget = 0Tbase * baseTtarget
|
|
||||||
pinocchio::SE3 oM_target = oM_base * base_M_target;
|
|
||||||
|
|
||||||
// 当前 -> 目标的 SE3 误差
|
|
||||||
pinocchio::SE3 dM = oM_curr.inverse() * oM_target;
|
|
||||||
Eigen::Matrix<double, 6, 1> dx = pinocchio::log6(dM).toVector();
|
|
||||||
|
|
||||||
dx.head<3>() *= (1.0 - w_posrot_);
|
|
||||||
dx.tail<3>() *= w_posrot_;
|
|
||||||
|
|
||||||
// 6×nv Jacobian(LOCAL)
|
|
||||||
Eigen::Matrix<double, 6, Eigen::Dynamic> J_full(6, model().nv);
|
|
||||||
pinocchio::computeFrameJacobian(model(), *data_, q_full,
|
|
||||||
target_frame_id,
|
|
||||||
pinocchio::ReferenceFrame::LOCAL,
|
|
||||||
J_full);
|
|
||||||
|
|
||||||
// 抽取子链对应列 => 6×chain_q_dof_ 雅可比
|
|
||||||
MatrixXd J = J_full.middleCols(chain_v_start_, chain_v_dof_);
|
|
||||||
|
|
||||||
// 2) 构造代价:|| A_cost * dq - b_cost ||^2
|
// 2) 构造代价:|| A_cost * dq - b_cost ||^2
|
||||||
constexpr std::size_t n_eq = 6;
|
constexpr Eigen::Index n_eq = 6;
|
||||||
A_cost.resize(n_eq + chain_q_dof_, chain_q_dof_);
|
A_cost.resize(n_eq + chain_q_dof_, chain_q_dof_);
|
||||||
b_cost.resize(n_eq + chain_q_dof_);
|
b_cost.resize(n_eq + chain_q_dof_);
|
||||||
|
|
||||||
// 任务部分
|
// 任务部分
|
||||||
A_cost.block(0, 0, 6, chain_q_dof_) = J;
|
A_cost.topRows(n_eq) = poseJacobianAt_(q_full, target_frame_id);
|
||||||
b_cost.segment(0, 6) = dx;
|
b_cost.head(n_eq) = error;
|
||||||
|
|
||||||
// 阻尼项
|
// 阻尼项
|
||||||
A_cost.bottomRows(chain_q_dof_) = std::sqrt(lambda_) *
|
A_cost.bottomRows(chain_q_dof_) = std::sqrt(lambda_) *
|
||||||
@ -475,24 +603,18 @@ namespace cmvr {
|
|||||||
q_local = cmvr::kinematics::clampToJointPositionLimits(q_local, q_min, q_max);
|
q_local = cmvr::kinematics::clampToJointPositionLimits(q_local, q_min, q_max);
|
||||||
q_full.segment(chain_q_start_, chain_q_dof_) = q_local;
|
q_full.segment(chain_q_start_, chain_q_dof_) = q_local;
|
||||||
|
|
||||||
// 6) 收敛检查
|
error = weightedPoseError_(poseErrorAt_(q_full, target_frame_id, base_M_target));
|
||||||
double err_norm = b_cost.head(n_eq).norm();
|
if (dq_local.norm() < 1e-6) break;
|
||||||
if (err_norm < tol_) {
|
|
||||||
cur_joints_angle_.assign(q_local.data(), q_local.data() + q_local.size());
|
|
||||||
joints_angle = cur_joints_angle_;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (dq_local.norm() < 1e-6) {
|
|
||||||
cur_joints_angle_.assign(q_local.data(), q_local.data() + q_local.size());
|
|
||||||
joints_angle = cur_joints_angle_;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 未收敛,返回 best effort
|
const bool converged = error.norm() < tol_;
|
||||||
|
if (converged) {
|
||||||
|
improvePoseJointLimitDistance_(q_full, target_frame_id, base_M_target);
|
||||||
|
q_local = q_full.segment(chain_q_start_, chain_q_dof_);
|
||||||
|
}
|
||||||
cur_joints_angle_.assign(q_local.data(), q_local.data() + q_local.size());
|
cur_joints_angle_.assign(q_local.data(), q_local.data() + q_local.size());
|
||||||
joints_angle = cur_joints_angle_;
|
joints_angle = cur_joints_angle_;
|
||||||
return false;
|
return converged;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PinocchioQpIKSolver::solveVelocityBase(
|
bool PinocchioQpIKSolver::solveVelocityBase(
|
||||||
|
|||||||
@ -6,6 +6,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
#include <pinocchio/spatial/explog.hpp>
|
||||||
|
|
||||||
#include "common/io/proto_file_io.h"
|
#include "common/io/proto_file_io.h"
|
||||||
#include "cmvr/config/arm_config/arm_config.pb.h"
|
#include "cmvr/config/arm_config/arm_config.pb.h"
|
||||||
@ -44,6 +45,23 @@ config::PinocchioQpIKConfig loadQpConfig(const std::filesystem::path& root)
|
|||||||
return solver_config;
|
return solver_config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
config::PinocchioQpIKConfig loadXiaozhaoRightArmQpConfig(const std::filesystem::path& root)
|
||||||
|
{
|
||||||
|
config::ArmRootConfig root_config;
|
||||||
|
const auto config_path = root / "cmvr-es/config/devices/arm/xiaozhao_tchl_arm.pb.txt";
|
||||||
|
if (ProtoMessageIo::getProtoFromAsciiFile(config_path.string(), &root_config)) {
|
||||||
|
for (const auto& arm : root_config.arm().robot_arms()) {
|
||||||
|
if (arm.id() == "xiaozhao_tchl_right_arm") {
|
||||||
|
auto solver_config = arm.kinematics().pinocchio_qp_ik_solver();
|
||||||
|
solver_config.set_urdf_path((root / solver_config.urdf_path()).string());
|
||||||
|
return solver_config;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ADD_FAILURE() << "Missing Xiaozhao right-arm QP IK fixture";
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
TEST(PinocchioQpIKSolverTest, JointLimitAvoidanceIsInCartesianNullspace)
|
TEST(PinocchioQpIKSolverTest, JointLimitAvoidanceIsInCartesianNullspace)
|
||||||
{
|
{
|
||||||
Eigen::MatrixXd jacobian = Eigen::MatrixXd::Zero(6, 7);
|
Eigen::MatrixXd jacobian = Eigen::MatrixXd::Zero(6, 7);
|
||||||
@ -466,5 +484,90 @@ TEST(PinocchioQpIKSolverTest, PoseIkStillConvergesWithExistingConfiguration)
|
|||||||
EXPECT_LT((achieved_pose.block<3, 3>(0, 0) - target_pose.block<3, 3>(0, 0)).norm(), 1e-5);
|
EXPECT_LT((achieved_pose.block<3, 3>(0, 0) - target_pose.block<3, 3>(0, 0)).norm(), 1e-5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(PinocchioQpIKSolverTest, PoseIkJointLimitAvoidanceImprovesClearanceAtSameTcpPose)
|
||||||
|
{
|
||||||
|
const auto root = findProjectRoot();
|
||||||
|
ASSERT_FALSE(root.empty());
|
||||||
|
auto baseline_config = loadXiaozhaoRightArmQpConfig(root);
|
||||||
|
baseline_config.clear_pose_ik_joint_limit_avoidance();
|
||||||
|
auto avoided_config = baseline_config;
|
||||||
|
avoided_config.mutable_pose_ik_joint_limit_avoidance()->set_enable(true);
|
||||||
|
PinocchioQpIKSolver baseline_solver(baseline_config);
|
||||||
|
PinocchioQpIKSolver avoided_solver(avoided_config);
|
||||||
|
ASSERT_TRUE(baseline_solver.init());
|
||||||
|
ASSERT_TRUE(avoided_solver.init());
|
||||||
|
|
||||||
|
// Joint 6 starts near its 0.42 rad limit with room to move in the null space.
|
||||||
|
const std::vector<double> seed = {0.0986988, 0.786383, 1.43618, -1.62011,
|
||||||
|
-2.26993, 0.419, -0.135876};
|
||||||
|
Eigen::Matrix4d target_pose;
|
||||||
|
ASSERT_TRUE(baseline_solver.fk(seed, target_pose, true));
|
||||||
|
|
||||||
|
baseline_solver.update_joints_state(seed);
|
||||||
|
avoided_solver.update_joints_state(seed);
|
||||||
|
std::vector<double> baseline;
|
||||||
|
std::vector<double> avoided;
|
||||||
|
ASSERT_TRUE(baseline_solver.ik(target_pose, baseline, true));
|
||||||
|
ASSERT_TRUE(avoided_solver.ik(target_pose, avoided, true));
|
||||||
|
ASSERT_EQ(baseline.size(), seed.size());
|
||||||
|
ASSERT_EQ(avoided.size(), seed.size());
|
||||||
|
|
||||||
|
Eigen::VectorXd lower, upper;
|
||||||
|
ASSERT_TRUE(avoided_solver.getJointPositionLimits(lower, upper));
|
||||||
|
ASSERT_EQ(lower.size(), seed.size());
|
||||||
|
ASSERT_EQ(upper.size(), seed.size());
|
||||||
|
double baseline_clearance = 0.5;
|
||||||
|
double avoided_clearance = 0.5;
|
||||||
|
for (Eigen::Index i = 0; i < lower.size(); ++i) {
|
||||||
|
SCOPED_TRACE(i);
|
||||||
|
EXPECT_GE(avoided[i], lower[i]);
|
||||||
|
EXPECT_LE(avoided[i], upper[i]);
|
||||||
|
const double span = upper[i] - lower[i];
|
||||||
|
ASSERT_GT(span, 0.0);
|
||||||
|
baseline_clearance = std::min(
|
||||||
|
baseline_clearance, std::min(baseline[i] - lower[i], upper[i] - baseline[i]) / span);
|
||||||
|
avoided_clearance = std::min(
|
||||||
|
avoided_clearance, std::min(avoided[i] - lower[i], upper[i] - avoided[i]) / span);
|
||||||
|
}
|
||||||
|
EXPECT_GT(avoided_clearance, baseline_clearance + 1e-4);
|
||||||
|
EXPECT_LT(avoided[5], baseline[5] - 1e-4);
|
||||||
|
|
||||||
|
Eigen::Matrix4d achieved_pose;
|
||||||
|
ASSERT_TRUE(avoided_solver.fk(avoided, achieved_pose, true));
|
||||||
|
const pinocchio::SE3 achieved(achieved_pose.block<3, 3>(0, 0), achieved_pose.block<3, 1>(0, 3));
|
||||||
|
const pinocchio::SE3 target(target_pose.block<3, 3>(0, 0), target_pose.block<3, 1>(0, 3));
|
||||||
|
Eigen::Matrix<double, 6, 1> error = pinocchio::log6(achieved.inverse() * target).toVector();
|
||||||
|
error.head<3>() *= 1.0 - avoided_config.w_posrot();
|
||||||
|
error.tail<3>() *= avoided_config.w_posrot();
|
||||||
|
EXPECT_LE(error.norm(), avoided_config.tol());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PinocchioQpIKSolverTest, PoseIkJointLimitAvoidanceKeepsAlreadyCenteredSolution)
|
||||||
|
{
|
||||||
|
const auto root = findProjectRoot();
|
||||||
|
ASSERT_FALSE(root.empty());
|
||||||
|
auto config = loadXiaozhaoRightArmQpConfig(root);
|
||||||
|
auto* limits = config.mutable_joint_limit_policy()->mutable_limits();
|
||||||
|
const std::vector<double> seed = {0.1, 0.7, 1.4, -1.6, -2.2, 0.2, -0.1};
|
||||||
|
ASSERT_EQ(limits->joints_size(), seed.size());
|
||||||
|
for (int i = 0; i < limits->joints_size(); ++i) {
|
||||||
|
limits->mutable_joints(i)->set_q_lb(seed[i] - 0.2);
|
||||||
|
limits->mutable_joints(i)->set_q_ub(seed[i] + 0.2);
|
||||||
|
}
|
||||||
|
config.mutable_pose_ik_joint_limit_avoidance()->set_enable(true);
|
||||||
|
PinocchioQpIKSolver solver(config);
|
||||||
|
ASSERT_TRUE(solver.init());
|
||||||
|
|
||||||
|
Eigen::Matrix4d target_pose;
|
||||||
|
ASSERT_TRUE(solver.fk(seed, target_pose, true));
|
||||||
|
solver.update_joints_state(seed);
|
||||||
|
std::vector<double> result;
|
||||||
|
ASSERT_TRUE(solver.ik(target_pose, result, true));
|
||||||
|
ASSERT_EQ(result.size(), seed.size());
|
||||||
|
for (std::size_t i = 0; i < result.size(); ++i) {
|
||||||
|
EXPECT_NEAR(result[i], seed[i], 1e-12);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace cmvr
|
} // namespace cmvr
|
||||||
|
|||||||
@ -31,6 +31,11 @@ arm {
|
|||||||
max_iters: 100
|
max_iters: 100
|
||||||
tol: 1e-6
|
tol: 1e-6
|
||||||
qp_time_limit: 1e-2
|
qp_time_limit: 1e-2
|
||||||
|
pose_ik_joint_limit_avoidance {
|
||||||
|
enable: true
|
||||||
|
max_iters: 20
|
||||||
|
max_joint_step_rad: 0.02
|
||||||
|
}
|
||||||
joint_limit_policy {
|
joint_limit_policy {
|
||||||
limits {
|
limits {
|
||||||
enable: true
|
enable: true
|
||||||
|
|||||||
@ -4,6 +4,13 @@ package cmvr.config;
|
|||||||
|
|
||||||
import "cmvr/config/joint_limits_config.proto";
|
import "cmvr/config/joint_limits_config.proto";
|
||||||
|
|
||||||
|
message PoseIKJointLimitAvoidanceConfig {
|
||||||
|
// Optimize distance from joint limits after the pose IK converges.
|
||||||
|
bool enable = 1;
|
||||||
|
optional int32 max_iters = 2; // Defaults to 20.
|
||||||
|
optional double max_joint_step_rad = 3; // Defaults to 0.02 rad.
|
||||||
|
}
|
||||||
|
|
||||||
// 只管 QP 这个 IK 的参数
|
// 只管 QP 这个 IK 的参数
|
||||||
message PinocchioQpIKConfig {
|
message PinocchioQpIKConfig {
|
||||||
string urdf_path = 1;
|
string urdf_path = 1;
|
||||||
@ -17,4 +24,5 @@ message PinocchioQpIKConfig {
|
|||||||
double tol = 8;
|
double tol = 8;
|
||||||
double qp_time_limit = 9;
|
double qp_time_limit = 9;
|
||||||
JointLimitPolicyConfig joint_limit_policy = 10;
|
JointLimitPolicyConfig joint_limit_policy = 10;
|
||||||
|
PoseIKJointLimitAvoidanceConfig pose_ik_joint_limit_avoidance = 11;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user