简体   繁体   English

在 ForceElement 的子类上调用 super().__init__() 导致未定义构造函数

[英]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 presumably ForceElement.__init__ will raise an AttributeError .也就是说,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.

相关问题 子类构造函数调用错误__init__ - Subclass constructor calling wrong __init__ 为什么在父类__init __()中调用super()会改变子类__init __()的行为? - Why does calling super() in parent class __init__() change the subclass __init__() behavior? 在子类的__init__中设置Super的属性 - Setting a Super's property in a Subclass' __init__ 使用子类和super时__init__的参数 - Arguments to __init__ when using a subclass and super 父类和子类之间的__init__参数不匹配 - __init__ argument mismatch between super and subclass 在 OrderedDict 子类中覆盖没有 super() 的 __init__ 但父构造函数仍然有效 - Overwriting __init__ without super() in OrderedDict subclass but parent constructor still works 在 fractions.Fraction 子类中调用 super().__init__(*args, **kwargs) 只接受要初始化的实例 - Calling super().__init__(*args, **kwargs) in a fractions.Fraction subclass accepts only the instance to initialize python:在__init__方法中过早地调用super().__ init__? - python: calling super().__init__ too early in the __init__ method? 超级构造函数对__init__使用* args和** kwargs - super constructor use *args and **kwargs for __init__ 在Python中调用超级__init__时出错 - Error calling super __init__ in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM