简体   繁体   English

PyDrake:从机器人中提取 AutoDiff 梯度(通过微分逆运动学控制)

[英]PyDrake: Extracting AutoDiff gradients from a robot (controlled via Differential Inverse Kinematics)

I am using PyDrake do build a simple model of a Franka Emika Panda robot arm which picks up and places a brick.我正在使用PyDrake构建一个简单的 model 的 Franka Emika Panda 机器人 arm,它可以捡起并放置一块砖。

I would like to observe how a change in the initial chosen starting position of my brick affects a custom target loss function.我想观察我的砖的初始选择的起始 position 的变化如何影响自定义目标损失 function。 Therefore, I would like to use the AutoDiffXd functionality built into Drake to automatically extract the derivative of my loss function at the end of simulation with respect to my initial inputs.因此,我想使用 Drake 内置的AutoDiffXd功能在模拟结束时针对我的初始输入自动提取我的损失 function 的导数。

I build my system with as normal, then run ToAutoDiffXd() to convert the respective systems to an autodiff version.我像往常一样构建我的系统,然后运行ToAutoDiffXd()将相应的系统转换为 autodiff 版本。 However, I get the following error:但是,我收到以下错误:

The object named [Inverse Kinematics] of type

    drake::manipulation::planner::DifferentialInverseKinematicsIntegrator

does not support ToAutoDiffXd

No luck, it seems my controller class (which leverages DifferentialInverseKinematicsIntegrator ) does not support autodiff conversion.不走运,看来我的 controller class (利用了DifferentialInverseKinematicsIntegrator )不支持自动差异转换。 Since this system is essentially a convenient wrapper class for the DoDifferentialInverseKinematics class, I tried instead manually creating an IK controller, and feeding the autodiff variables to DoDifferentialInverseKinematics directly.由于该系统本质上是 DoDifferentialInverseKinematics class 的便捷包装器DoDifferentialInverseKinematics ,因此我尝试手动创建 IK controller,并直接将变量自动馈入DoDifferentialInverseKinematics However, this also does not support autodiff it seems:但是,这似乎也不支持 autodiff:

DoDifferentialInverseKinematics(example_auto, v_current, desired_spatial_velocity, jac_wrt_v, DifferentialInverseKinematicsParameters(num_positions=2, num_velocities=2))

TypeError: DoDifferentialInverseKinematics(): incompatible function arguments. The following argument types are supported:
    1. (q_current: numpy.ndarray[numpy.float64[m, 1]], v_current: numpy.ndarray[numpy.float64[m, 1]], V: numpy.ndarray[numpy.float64[m, 1]], J: numpy.ndarray[numpy.float64[m, n]], parameters: pydrake.manipulation.planner.DifferentialInverseKinematicsParameters) -> pydrake.manipulation.planner.DifferentialInverseKinematicsResult
    2. (robot: drake::multibody::MultibodyPlant<double>, context: pydrake.systems.framework.Context_[float], V_WE_desired: numpy.ndarray[numpy.float64[6, 1]], frame_E: drake::multibody::Frame<double>, parameters: pydrake.manipulation.planner.DifferentialInverseKinematicsParameters) -> pydrake.manipulation.planner.DifferentialInverseKinematicsResult
    3. (robot: drake::multibody::MultibodyPlant<double>, context: pydrake.systems.framework.Context_[float], X_WE_desired: pydrake.common.eigen_geometry.Isometry3_[float], frame_E: drake::multibody::Frame<double>, parameters: pydrake.manipulation.planner.DifferentialInverseKinematicsParameters) -> pydrake.manipulation.planner.DifferentialInverseKinematicsResult

Invoked with: array([[<AutoDiffXd 0.5 nderiv=2>],
       [<AutoDiffXd 0.3 nderiv=2>]], dtype=object), array([0., 0.]), array([0., 0., 0., 1., 0., 0.]), array([[0. , 0. ],
       [0. , 0. ],
       [0. , 0. ],
       [0.3, 0. ],
       [0. , 0. ],
       [0. , 0. ]]), <pydrake.manipulation.planner.DifferentialInverseKinematicsParameters object at 0x7f6f5061c330>

I tried looking up the C++ documentation for DoDifferentialKinematics for clues.我尝试查找 DoDifferentialKinematics 的C++ 文档以获取线索。 It does indeed seem that this function only accepts the double scalar type.确实,这个 function 似乎只接受双标量类型。 However, a note on DoDifferentialKinematics 's implementation remarks that essentially all that happens under the hood is that this function runs a MathematicalProgram .但是,关于DoDifferentialKinematics实现的注释指出,本质上所有发生的事情都是这个 function 运行一个MathematicalProgram My understanding is that weaving AutoDiff through a MathematicalProgram is supported in Drake.我的理解Drake 支持通过MathematicalProgram程序编织 AutoDiff。

So my question is: What is the best way for me to accomplish my goal?所以我的问题是:我实现目标的最佳方式是什么? Should I just manually recreate a custom auto-diff version of DifferentialInverseKinematics using the MathematicalProgram API?我是否应该使用 MathematicalProgram API 手动重新创建一个自定义的自动差异版本的差分逆运动? Will this even succeed?这甚至会成功吗? Additionally, is there an easier alternative?另外,有没有更简单的选择?

Your deductions look correct to me, except perhaps the very last comment about MathematicalProgram .你的推论对我来说是正确的,除了关于MathematicalProgram的最后一条评论。 MathematicalProgram knows how to consume AutoDiffXd , but to take the gradient of the solution of a MathematicalProgram optimization, one needs to take the gradients of the optimality conditions (KKT). MathematicalProgram知道如何使用AutoDiffXd ,但是要获取MathematicalProgram优化解的梯度,需要获取最优性条件 (KKT) 的梯度。 We have an issue on this here: https://github.com/RobotLocomotion/drake/issues/4267 .我们在这里有一个问题: https://github.com/RobotLocomotion/drake/issues/4267 I will cross-post this issue there to see if there is any update.我将在此处交叉发布此问题以查看是否有任何更新。

Depending on what you are trying to do with inverse kinematics, it might be that a simpler approach (taking the pseudo-inverse of the Jacobian) would work just fine for you.根据您尝试对逆运动学进行的操作,可能是一种更简单的方法(采用雅可比行列式的伪逆)对您来说效果很好。 In that workflow, you would write your own DifferentialInverseKinematics system like in http://manipulation.csail.mit.edu/pick.html and make it support AutoDiffXd .在该工作流程中,您将编写自己的DifferentialInverseKinematics系统,例如http://manipulation.csail.mit.edu/pick.html并使其支持AutoDiffXd (This could happen in either python or c++). (这可能发生在 python 或 c++ 中)。

The challenge of computing the gradient of a general optimization problem, is that it requires the Hessian of the constraint/costs.计算一般优化问题的梯度的挑战在于它需要约束/成本的 Hessian。 This would require we use nested AutoDiffScalar type to compute the Hessian, and currently we don't support that.这需要我们使用嵌套的 AutoDiffScalar 类型来计算 Hessian,目前我们不支持。

In DifferentialInverseKinematics, it solves a quadratic program.在 DifferentialInverseKinematics 中,它求解二次规划。 The Hessian of the QP cost/constraints are fixed. QP 成本/约束的 Hessian 矩阵是固定的。 Hence it is possible that we can write a special function, to just differentiate the solution of the QP.因此,我们可以编写一个特殊的 function 来区分 QP 的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM