Spherical pendulum


This example models a spherical pendulum. The pivot point is modeled using a Spherical joint, the pendulum rod is modeled using a FixedTranslation and the mass is modeled using a Body. In this example, we choose the joint to be the root (joints are often better root objects than bodies).

using Multibody
using ModelingToolkit
using Plots
using JuliaSimCompiler
using OrdinaryDiffEq

t = Multibody.t
D = Differential(t)
world = Multibody.world

systems = @named begin
    joint = Spherical(enforceState=true, isroot=true, phi = 1)
    bar = FixedTranslation(r = [0, -1, 0])
    body = Body(; m = 1, isroot = false)

connections = [connect(world.frame_b, joint.frame_a)
            connect(joint.frame_b, bar.frame_a)
            connect(bar.frame_b, body.frame_a)]

@named model = ODESystem(connections, t, systems = [world; systems])
ssys = structural_simplify(IRSystem(model))

prob = ODEProblem(ssys, [
                    D.(joint.phi) .=> 0;
                    D.(D.(joint.phi)) .=> 0;
], (0, 10))

sol = solve(prob, Rodas4())
@assert SciMLBase.successful_retcode(sol)

plot(sol, idxs = [body.r_0...])
Example block output

3D animation

Multibody.jl supports automatic 3D rendering of mechanisms, we use this feature to illustrate the result of the simulation below:

import CairoMakie
Multibody.render(model, sol; z = -5, filename = "spherical.gif") # Use "spherical.mp4" for a video file