Skip to content

Calling a function with inputs and outputs created by @variables triggers a MethodError #781

@ferrolho

Description

@ferrolho

Hi! It is my first time using ModelingToolkit.jl. My goal is to be able to evaluate large and sparse Jacobians and Hessians for already-defined functions which are not easy to write algebraically. From what I have read in the docs and from what I understood from this video, I believe the right thing to do is to create symbolic inputs and outputs with @variables, and then send those through my functions to have ModelingToolkit.jl do its thing. I am running into some issues, though.

For now, I am trying to start simple. I am just trying to evaluate a test function f! (which calls TORA.inverse_dynamics_defects!) with symbolic inputs and outputs. While the normal call to the function works (using Float inputs and outputs), the call to the same function with inputs and outputs created by @variables fails. The test code is below, as well as the stacktrace of the error.

using MeshCat
using ModelingToolkit
using TORA

vis = Visualizer()
robot = create_robot_kuka_iiwa_14(vis)

f!(dx, x) = TORA.inverse_dynamics_defects!(dx, robot, x, 1e-2)

n_out = robot.n_q + robot.n_v  # next state (from integration)
n_in = robot.n_q + robot.n_v + robot.n_τ +  # current state + current controls,
       robot.n_q + robot.n_v                # and next state (from discretisation)

let  # works
    x = rand(n_in)
    dx = zeros(n_out)
    f!(dx, x)
end

let  # does not work
    @variables dx[1:n_out] x[1:n_in]
    f!(dx, x)
end
MethodError: no method matching AbstractFloat(::Operation)
Closest candidates are:
  AbstractFloat(::T) where T<:Number at boot.jl:716
  AbstractFloat(!Matched::Bool) at float.jl:258
  AbstractFloat(!Matched::Int8) at float.jl:259
  ...

Stacktrace:
 [1] float(::Operation) at ./float.jl:277
 [2] sincos(::Operation) at ./special/trig.jl:204
 [3] Tuple at /home/henrique/.julia/packages/Rotations/3gsMV/src/angleaxis_types.jl:52 [inlined]
 [4] convert at /home/henrique/.julia/packages/StaticArrays/LJQEe/src/convert.jl:10 [inlined]
 [5] Rotations.RotMatrix{3,Expression,9}(::Rotations.AngleAxis{Expression}) at /home/henrique/.julia/packages/Rotations/3gsMV/src/core_types.jl:83
 [6] convert(::Type{Rotations.RotMatrix{3,Expression,9}}, ::Rotations.AngleAxis{Expression}) at /home/henrique/.julia/packages/Rotations/3gsMV/src/core_types.jl:20
 [7] joint_transform at /home/henrique/.julia/packages/RigidBodyDynamics/2qxSc/src/joint_types/revolute.jl:61 [inlined]
 [8] joint_transform at /home/henrique/.julia/packages/RigidBodyDynamics/2qxSc/src/joint.jl:191 [inlined]
 [9] macro expansion at /home/henrique/.julia/packages/TypeSortedCollections/Z4ytl/src/TypeSortedCollections.jl:173 [inlined]
 [10] map! at /home/henrique/.julia/packages/TypeSortedCollections/Z4ytl/src/TypeSortedCollections.jl:164 [inlined]
 [11] _update_transforms!(::RigidBodyDynamics.MechanismState{Operation,Float64,Expression,TypeSortedCollections.TypeSortedCollection{Tuple{Array{RigidBodyDynamics.Joint{Float64,RigidBodyDynamics.Revolute{Float64}},1}},1}}) at /home/henrique/.julia/packages/RigidBodyDynamics/2qxSc/src/mechanism_state.jl:692
 [12] update_transforms! at /home/henrique/.julia/packages/RigidBodyDynamics/2qxSc/src/mechanism_state.jl:686 [inlined]
 [13] spatial_accelerations!(::RigidBodyDynamics.CustomCollections.IndexDict{RigidBodyDynamics.BodyID,Base.OneTo{RigidBodyDynamics.BodyID},RigidBodyDynamics.Spatial.SpatialAcceleration{Operation}}, ::RigidBodyDynamics.MechanismState{Operation,Float64,Expression,TypeSortedCollections.TypeSortedCollection{Tuple{Array{RigidBodyDynamics.Joint{Float64,RigidBodyDynamics.Revolute{Float64}},1}},1}}, ::RigidBodyDynamics.CustomCollections.SegmentedVector{RigidBodyDynamics.JointID,Operation,Base.OneTo{RigidBodyDynamics.JointID},Array{Operation,1}}) at /home/henrique/.julia/packages/RigidBodyDynamics/2qxSc/src/mechanism_algorithms.jl:388
 [14] inverse_dynamics! at /home/henrique/.julia/packages/RigidBodyDynamics/2qxSc/src/mechanism_algorithms.jl:550 [inlined]
 [15] inverse_dynamics!(::RigidBodyDynamics.CustomCollections.SegmentedVector{RigidBodyDynamics.JointID,Operation,Base.OneTo{RigidBodyDynamics.JointID},Array{Operation,1}}, ::RigidBodyDynamics.CustomCollections.IndexDict{RigidBodyDynamics.BodyID,Base.OneTo{RigidBodyDynamics.BodyID},RigidBodyDynamics.Spatial.Wrench{Operation}}, ::RigidBodyDynamics.CustomCollections.IndexDict{RigidBodyDynamics.BodyID,Base.OneTo{RigidBodyDynamics.BodyID},RigidBodyDynamics.Spatial.SpatialAcceleration{Operation}}, ::RigidBodyDynamics.MechanismState{Operation,Float64,Expression,TypeSortedCollections.TypeSortedCollection{Tuple{Array{RigidBodyDynamics.Joint{Float64,RigidBodyDynamics.Revolute{Float64}},1}},1}}, ::RigidBodyDynamics.CustomCollections.SegmentedVector{RigidBodyDynamics.JointID,Operation,Base.OneTo{RigidBodyDynamics.JointID},Array{Operation,1}}) at /home/henrique/.julia/packages/RigidBodyDynamics/2qxSc/src/mechanism_algorithms.jl:549
 [16] inverse_dynamics_defects!(::Array{Operation,1}, ::Robot, ::Array{Operation,1}, ::Float64) at /home/henrique/git/TORA.jl/src/constraints/dynamics.jl:76
 [17] f!(::Array{Operation,1}, ::Array{Operation,1}) at ./In[4]:1
 [18] top-level scope at In[7]:3
 [19] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

I wonder if I am doing something wrong... 🤔 And if not, is the problem that some of the dependencies need to be rewritten in order to be compatible with ModelingToolkit.jl? E.g., the stacktrace shows that the error happens somewhere in Rotations.jl; so, does it mean I'd have to make sure the functionality provided by Rotations.jl is written in a way that is compatible with ModelingToolkit.jl?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions