简体   繁体   中英

Calling super().__init__() on subclass of ForceElement causes No constructor defined

I am trying to create a custom ForceElement as follows

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())

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.

Please take a look at the docs here for ForceElement ; "ForceElement allows modeling state and time dependent forces in a MultibodyTree model". That is, a force element that is a function of the torque on the wheel can not be modeled as a ForceElement . I believe that what you want is a FrontWheelSystem , being a LeafSystem , that output the force you want to 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() .

Summarizing a few comments into the correct answer

By 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 .

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

Ref: pybind11 docs , ForceElement binding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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