Thanks for taking time to help!
I have played with example a bit but I cannot find a simulate(...)
that takes external wrenches
The Dynamics
objects from RigidBodySim
does not support external wrenches as well
struct Dynamics{M, JointCollection, C, P}
statecache::StateCache{M, JointCollection}
τcache::SegmentedVectorCache{JointID, Base.OneTo{JointID}}
resultcache::DynamicsResultCache{M}
control!::C
setparams!::P
end
but I could replicate simulate()
functionality (following one of the examples)
using RigidBodySim
open_loop_dynamics = Dynamics(mechanism)
problem = ODEProblem(Dynamics(mechanism, PeriodicController(τ, Δt, pdcontrol!)), state, (0., 5.));
sol = solve(problem, Tsit5())
setanimation!(vis, sol, realtime_rate=0.5)
I decided to modify simulate()
. Ideally, I would like to get the external wrench from a function similar to control!()
. For now, this compiles but it doesn’t seem to behave as expected, i.e. pulling the pendulum until it is suspended to the side (like in the image above). It looks like the wrench is not constantly applied
function simulate2(
state0::MechanismState{X},
final_time,
control! = zero_torque!,
externalwrenches::AbstractDict{BodyID,<:Wrench} = NullDict{BodyID,Wrench{X}}();
Δt = 1e-4,
stabilization_gains = Nothing,
) where {X}
# T = cache_eltype(state0)
T = Float64
result = DynamicsResult{T}(state0.mechanism)
control_torques = similar(velocity(state0))
closed_loop_dynamics! =
let result = result,
control_torques = control_torques,
ext_wrenches = ext_wrenches,
stabilization_gains = stabilization_gains
# https://github.com/JuliaLang/julia/issues/15276
function (v̇::AbstractArray, ṡ::AbstractArray, t, state)
# dynamics!(result, state, control_torques; stabilization_gains=stabilization_gains)
control!(control_torques, t, state)
dynamics!(
result,
state,
control_torques,
ext_wrenches;
stabilization_gains = stabilization_gains,
)
copyto!(v̇, result.v̇)
copyto!(ṡ, result.ṡ)
nothing
end
end
tableau = runge_kutta_4(T)
storage = ExpandingStorage{T}(state0, ceil(Int64, final_time / Δt * 1.001)) # very rough overestimate of number of time steps
integrator = MuntheKaasIntegrator(state0, closed_loop_dynamics!, tableau, storage)
integrate(integrator, final_time, Δt)
return storage.ts, storage.qs, storage.vs
end
ts, qs, vs = simulate2(state, 10.0, spring_damper!, external_wrenches, Δt = 1e-3);
MeshCatMechanisms.animate(vis, ts, qs; realtimerate = 1.0);