对ForceElement的子类调用super()。\uuu init\uu()会导致未定义构造函数

2024-09-30 14:23:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图创建一个定制的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())

但是在ForceElement.__init__(self, front_wheel.model_instance())行上得到以下错误

TypeError: FrontWheelForce: No constructor defined!

Tags: instanceselfnodeindexmodelinitdefpdb
3条回答

请看文档here中的ForceElement;“ForceElement允许在多体树模型中建模状态和时间相关的力”。也就是说,作为车轮扭矩函数的力元素不能建模为ForceElement。我相信你想要的是一个FrontWheelSystem,作为一个LeafSystem,输出你想要建模的力。您可以通过任一执行器将模型的外力施加到设备上 连接到get_actuation_input_port(),或作为外部施加的空间力连接到get_applied_spatial_force_input_port()。你知道吗

你没有给我们看父母的定义。你知道吗

我有点惊讶你没有看到这个诊断:

TypeError: object.__init__() takes exactly one argument (the instance to initialize)

我想你使用的框架会引发“无构造函数” 提醒您还有一些代码要实现 在使用父类之前。你知道吗

把一些评论归纳成正确的答案

作者:ekhumoro

The error message suggests the ForceElement class does not support subclassing. That is, the python bindings for drake do not wrap the __init__ method for this class - so presumably ForceElement.__init__ will raise an AttributeError.

作者: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

参考号: pybind11 docs^{}binding

相关问题 更多 >