python-3.x - 解决 Atlas IK(无约束)失败?

标签 python-3.x drake

我正在尝试解决 Atlas 的 IK 问题,以实现类似 https://www.youtube.com/watch?v=LLGuGAs0cto 的目标。在使用 AddPositionConstraint 进行几次失败尝试后,我意识到即使没有任何约束,IK 问题也会失败(Strandbeest 也会失败)。

from pydrake.all import MultibodyPlant, FindResourceOrThrow, Parser, InverseKinematics, Solve
import numpy as np

plant = MultibodyPlant(0.001)
# filename = FindResourceOrThrow("drake/examples/multibody/strandbeest/Strandbeest.urdf")
filename = FindResourceOrThrow("drake/examples/atlas/urdf/atlas_minimal_contact.urdf")
Parser(plant).AddModelFromFile(filename)
plant.Finalize()
ik = InverseKinematics(plant=plant, with_joint_limits=False)
# epsilon = 1e-2
# r_foot_position = np.array([0.0, -0.08, 0.1])
# ik.AddPositionConstraint(
        # frameB=plant.GetFrameByName("r_foot"),
        # p_BQ=np.zeros(3),
        # frameA=plant.world_frame(),
        # p_AQ_upper=r_foot_position+epsilon,
        # p_AQ_lower=r_foot_position-epsilon)
result = Solve(ik.prog())
print(f"Success? {result.is_success()}")

我有什么遗漏的吗?我认为无约束 IK 问题是微不足道的

最佳答案

Atlas 机器人有一个 float 底座骨盆,其方向由四元数表示。四元数具有单位长度约束 zᵀz = 1。另一方面,Drake 中的优化问题默认使用零值向量作为初始猜测。对于单位长度约束,这个零向量是一个不好的初始猜测,因为 zᵀz 的梯度在 z=0 时为零,因此基于梯度的非线性求解器不知道如何用零梯度移动初始猜测。

一个快速的解决方案是使用更好的初始猜测

pelvis = plant.GetBodyByName("pelvis")
q0 = np.zeros((plant.num_positions(),))
# Set the initial guess of the pelvis quaternion as [1, 0, 0, 0]
q0[pelvis.floating_positions_start(): pelvis.floating_positions_start() + 4] = np.array([1, 0, 0, 0])
# Use q0 as the initial guess
result = Solve(ik.prog(), q0)

获得合理初始猜测的更好方法是通过上下文

context = plant.CreateDefaultContext()
q0 = plant.GetPositions(context)
result = Solve(ik.prog(), q0)

CreateDefaultContext() 将初始化四元数,使其具有单位长度。

我们有关于 debugging MathematicalProgram 的教程,当 MathematicalProgram 没有给出您想要的结果时,您可能会发现它很有用。在这里,如果您调用 result.GetInfeasibleConstraintNames(ik.prog()) ,它将打印单位长度约束。

关于python-3.x - 解决 Atlas IK(无约束)失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66882969/

相关文章:

drake - Drake 中的接触机制

drake - 接触雅可比的时间导数

python - 对数据框进行操作以将行转换为单独的列

python - 用于 pydrake 模拟的 MultibodyPlant、SceneGraph、Context 和 Simulator 的结构

drake - 在 `MultibodyPlant` 状态向量中查找自由体的(位置/速度)索引的推荐方法是什么?

python-3.x - 如何获得将外力映射到广义力的矩阵?

python - 如何从 for 循环返回多个具有唯一名称的 pandas 数据帧?

python - 我有一个巨大的 If 语句列表,想清理我的代码

python - flask : `@after_this_request` 不工作

python-3.x - 权限错误: [WinError 5] Access is denied for creating file