[英]Calling super().__init__() on subclass of ForceElement causes No constructor defined
I am trying to create a custom ForceElement
as follows我正在尝试按如下方式创建自定义
ForceElement
class FrontWheelForce(ForceElement):
def __init__(self, plant):
front_wheel = plant.GetBodyByName("front_wheel")
front_wheel_node_index = front_wheel.index()
pdb.set_trace()
ForceElement.__init__(self, front_wheel.model_instance())
But get the following error on the line ForceElement.__init__(self, front_wheel.model_instance())
但是在
ForceElement.__init__(self, front_wheel.model_instance())
行上得到以下错误
TypeError: FrontWheelForce: No constructor defined!
You didn't show us the parent's definition.你没有向我们展示父母的定义。
I'm a little surprised you didn't see this diagnostic:我有点惊讶你没有看到这个诊断:
TypeError: object.__init__() takes exactly one argument (the instance to initialize)
I imagine the framework you're using raises "no constructor" as a reminder that you have some more code to implement before using that parent class.我想您使用的框架会引发“无构造函数”,以提醒您在使用父 class 之前还有更多代码要实现。
Please take a look at the docs here for ForceElement
;请查看此处的文档
ForceElement
; "ForceElement allows modeling state and time dependent forces in a MultibodyTree model". “ForceElement 允许在 MultibodyTree 模型中建模 state 和时间相关力”。 That is, a force element that is a function of the torque on the wheel can not be modeled as a
ForceElement
.也就是说,车轮上的扭矩为 function 的力元不能建模为
ForceElement
。 I believe that what you want is a FrontWheelSystem
, being a LeafSystem
, that output the force you want to model.我相信您想要的是
FrontWheelSystem
,作为LeafSystem
,output 是您想要的 model 的力量。 You can apply the external force of your model to the plant through either actuators connected to get_actuation_input_port()
, or as externally applied spatial forces connected to get_applied_spatial_force_input_port()
.您可以通过连接到 get_actuation_input_port() 的执行器或连接到
get_actuation_input_port()
的外部应用空间力将 model 的外力施加到get_applied_spatial_force_input_port()
。
Summarizing a few comments into the correct answer总结几条评论成正确答案
By ekhumoro通过 ekhumoro
The error message suggests the
ForceElement
class does not support subclassing.错误消息表明
ForceElement
class 不支持子类化。 That is, the python bindings for drake do not wrap the__init__
method for this class - so presumablyForceElement.__init__
will raise anAttributeError
.也就是说,drake 的 python 绑定不会为这个 class 包装
__init__
方法 - 所以大概ForceElement.__init__
会引发AttributeError
。
By Eric Cousineau埃里克·库西诺
this (ForceElement) is not written as a trampoline class, which is necessary for pybind11 to permit Python-subclassing of a bound C++ class
这个(ForceElement)不是一个蹦床class,这是pybind11允许绑定C++ class的Python子类所必需的
Ref: pybind11 docs , ForceElement
binding参考: pybind11 文档,
ForceElement
绑定
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.