如何在 Drake 的自定义工厂中设置输入端口?
How do you set up input ports in custom plants in Drake?
我目前正致力于在 Drake 中实现一个非线性系统,所以我一直在设置一个 LeafSystem
作为我的工厂。在此 tutorial 之后,我能够使系统的被动版本正常运行(无可变控制输入)。但是本教程不包括在这些自定义系统中定义输入,我也没有找到任何示例。有人可以帮我解决下面列出的几个问题吗?
- 如下图代码,我只是简单的猜测了
DeclareVectorInputPort
这个函数,但是感觉应该还有类似于CopyStateOut
的第三个回调。我是否应该添加一个仅将输入端口设置为标量 phi
(我想要的电压输入)的回调?
- 当我调用
CalcTimeDerivatives
时,我应该如何访问phi
?它应该作为另一个参数传递还是应该使用上述回调将 phi 设置为 DEAContinuousSys
的某个可调用成员?
- 在
CalcTimeDerivatives
的末尾,我也觉得应该有一种更有效的方法来将所有导数(速度和加速度)设置为单个向量,而不是单独分配[=19的每个索引=],但我无法弄清楚。提示?
from pydrake.systems.framework import BasicVector, LeafSystem
class DEAContinuousSys(LeafSystem):
def __init__(self):
LeafSystem.__init__(self)
self.DeclareContinuousState(2) # two state variables: lam, lam_d
self.DeclareVectorOutputPort('Lam', BasicVector(2), self.CopyStateOut) # two outputs: lam_d, lam_dd
self.DeclareVectorInputPort('phi', BasicVector(1)) # help: guessed at this one, do I need a third argument?
# TODO: add input instead of constant phi
def DoCalcTimeDerivatives(self, context, derivatives):
Lam = context.get_continuous_state_vector() # get state
Lam = Lam.get_value() # cast as regular array
Lam_d = DEA.dynamics(Lam, None, phi) # derive acceleration (no timestep required) TODO: CONNECT VOLTAGE INPUT
derivatives.get_mutable_vector().SetAtIndex(0, Lam_d[0]) # set velocity
derivatives.get_mutable_vector().SetAtIndex(1, Lam_d[1]) # set acceleration
def CopyStateOut(self, context, output):
Lam = context.get_continuous_state_vector().CopyToVector()
output.SetFromVector(Lam)
continuous_sys = DEAContinuousSys()
你的开端不错!通常输入端口连接到其他系统的输出端口,因此它们不需要有自己的 "Calc" 方法。但是,您也可以使用 port.FixValue(context, value)
(DeclareVectorInputPort()
返回 port
对象)将它们设置为固定值。 Here 是 C++ 文档的 link。我不确定 Python 是否等效,但应该非常相似。
如果 C++ 文档不充分,希望其他人可以为您指出 Python 示例。
您可以看看欠驱动笔记中的 quadrotor2d 示例:
https://github.com/RussTedrake/underactuated/blob/master/underactuated/quadrotor2d.py#L44
我目前正致力于在 Drake 中实现一个非线性系统,所以我一直在设置一个 LeafSystem
作为我的工厂。在此 tutorial 之后,我能够使系统的被动版本正常运行(无可变控制输入)。但是本教程不包括在这些自定义系统中定义输入,我也没有找到任何示例。有人可以帮我解决下面列出的几个问题吗?
- 如下图代码,我只是简单的猜测了
DeclareVectorInputPort
这个函数,但是感觉应该还有类似于CopyStateOut
的第三个回调。我是否应该添加一个仅将输入端口设置为标量phi
(我想要的电压输入)的回调? - 当我调用
CalcTimeDerivatives
时,我应该如何访问phi
?它应该作为另一个参数传递还是应该使用上述回调将 phi 设置为DEAContinuousSys
的某个可调用成员? - 在
CalcTimeDerivatives
的末尾,我也觉得应该有一种更有效的方法来将所有导数(速度和加速度)设置为单个向量,而不是单独分配[=19的每个索引=],但我无法弄清楚。提示?
from pydrake.systems.framework import BasicVector, LeafSystem
class DEAContinuousSys(LeafSystem):
def __init__(self):
LeafSystem.__init__(self)
self.DeclareContinuousState(2) # two state variables: lam, lam_d
self.DeclareVectorOutputPort('Lam', BasicVector(2), self.CopyStateOut) # two outputs: lam_d, lam_dd
self.DeclareVectorInputPort('phi', BasicVector(1)) # help: guessed at this one, do I need a third argument?
# TODO: add input instead of constant phi
def DoCalcTimeDerivatives(self, context, derivatives):
Lam = context.get_continuous_state_vector() # get state
Lam = Lam.get_value() # cast as regular array
Lam_d = DEA.dynamics(Lam, None, phi) # derive acceleration (no timestep required) TODO: CONNECT VOLTAGE INPUT
derivatives.get_mutable_vector().SetAtIndex(0, Lam_d[0]) # set velocity
derivatives.get_mutable_vector().SetAtIndex(1, Lam_d[1]) # set acceleration
def CopyStateOut(self, context, output):
Lam = context.get_continuous_state_vector().CopyToVector()
output.SetFromVector(Lam)
continuous_sys = DEAContinuousSys()
你的开端不错!通常输入端口连接到其他系统的输出端口,因此它们不需要有自己的 "Calc" 方法。但是,您也可以使用 port.FixValue(context, value)
(DeclareVectorInputPort()
返回 port
对象)将它们设置为固定值。 Here 是 C++ 文档的 link。我不确定 Python 是否等效,但应该非常相似。
如果 C++ 文档不充分,希望其他人可以为您指出 Python 示例。
您可以看看欠驱动笔记中的 quadrotor2d 示例: https://github.com/RussTedrake/underactuated/blob/master/underactuated/quadrotor2d.py#L44