Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ let
excludedirs = String[]
excludefiles = String[]
if VERSION >= v"1.6"
push!(excludefiles, "6. Symbolics.jl")
push!(excludefiles, "7. Rigorous error bounds using IntervalArithmetic.jl")
Comment on lines +14 to +15

@ferrolho ferrolho Nov 9, 2024

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit annoying... I have already spent a lot of time on this PR, but I haven't been able to fix the issues with these two notebooks.

  • For 6. Symbolics.jl, I am not sure how to fix it and there is an open issue for this (Symbolic Derivatives with SymPy do not work #630). Update: I have managed to get the 6. Symbolics.jl example notebook back up and running in 5155344.
  • For 7. Rigorous error bounds using IntervalArithmetic.jl I have already applied a few updates to use the new syntax and API of IntervalArithmetic.jl, but not all the cells of the notebook are fixed yet.

end
for subdir in readdir(exampledir)
Expand Down
49 changes: 30 additions & 19 deletions examples/6. Symbolics/6. Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@

# ## Setup

using Quaternions
using RigidBodyDynamics
using StaticArrays
using Symbolics

#-

## Type piracy needed in order to make Symbolics.jl types interact well with Quaternions.jl.
## This should be avoided — see https://docs.julialang.org/en/v1/manual/style-guide/#avoid-type-piracy.
function Base.:/(q::Quaternions.Quaternion, x::Symbolics.Num)
return Quaternions.Quaternion(q.s / x.val, q.v1 / x.val, q.v2 / x.val, q.v3 / x.val)
end
Comment on lines +16 to +20

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took this from https://github.com/dionysos-dev/Dionysos.jl/blob/master/BipedRobot/src/piracy.jl to check if it would fix the issue in #630 and it did! So, we can close that issue when we merge this PR.


# ## Create symbolic parameters
# * Masses: $m_1, m_2$
Expand All @@ -18,9 +26,9 @@ using Symbolics
# * Center of mass locations (w.r.t. preceding joint axis): $c_1, c_2$
# * Gravitational acceleration: $g$

inertias = @variables m_1 m_2 I_1 I_2 positive = true
lengths = @variables l_1 l_2 c_1 c_2 real = true
gravitational_acceleration = @variables g real = true
inertias = @variables m₁ m₂ I₁ I₂
lengths = @variables l₁ l₂ c₁ c₂
gravitational_acceleration = @variables g
params = [inertias..., lengths..., gravitational_acceleration...]
transpose(params)

Expand All @@ -35,9 +43,9 @@ world = root_body(double_pendulum) # the fixed 'world' rigid body

# Attach the first (upper) link to the world via a revolute joint named 'shoulder'
inertia1 = SpatialInertia(CartesianFrame3D("upper_link"),
moment=I_1 * axis * transpose(axis),
com=SVector(zero(T), zero(T), c_1),
mass=m_1)
moment=I₁ * axis * transpose(axis),
com=SVector(zero(T), zero(T), c₁),
mass=m₁)
body1 = RigidBody(inertia1)
joint1 = Joint("shoulder", Revolute(axis))
joint1_to_world = one(Transform3D{T}, frame_before(joint1), default_frame(world));
Expand All @@ -46,13 +54,13 @@ attach!(double_pendulum, world, body1, joint1,

# Attach the second (lower) link to the world via a revolute joint named 'elbow'
inertia2 = SpatialInertia(CartesianFrame3D("lower_link"),
moment=I_2 * axis * transpose(axis),
com=SVector(zero(T), zero(T), c_2),
mass=m_2)
moment=I₂ * axis * transpose(axis),
com=SVector(zero(T), zero(T), c₂),
mass=m₂)
body2 = RigidBody(inertia2)
joint2 = Joint("elbow", Revolute(axis))
joint2_to_body1 = Transform3D(
frame_before(joint2), default_frame(body1), SVector(zero(T), zero(T), l_1))
frame_before(joint2), default_frame(body1), SVector(zero(T), zero(T), l₁))
attach!(double_pendulum, body1, body2, joint2,
joint_pose=joint2_to_body1)

Expand All @@ -64,26 +72,29 @@ x = MechanismState(double_pendulum);

# Set the joint configuration vector of the MechanismState to a new vector of symbolic variables
q = configuration(x)
for i in eachindex(q)
q[i] = symbols("q_$i", real=true)
end
q .= Symbolics.variables(:q, 1:length(q))

# Set the joint velocity vector of the MechanismState to a new vector of symbolic variables
v = velocity(x)
for i in eachindex(v)
v[i] = symbols("v_$i", real=true)
end
v .= Symbolics.variables(:v, 1:length(q))


# ## Compute dynamical quantities in symbolic form

# Mass matrix
simplify.(mass_matrix(x))
simplify.(mass_matrix(x), expand=true)


# Kinetic energy
simplify(kinetic_energy(x))
simplify(kinetic_energy(x), expand=true)


# Potential energy
simplify(gravitational_potential_energy(x))
try
## This throws `ERROR: TypeError: non-boolean (Num) used in boolean context` because
## of `m > 0 || return zero(cache_eltype(state))` in `gravitational_potential_energy`.
## See https://docs.sciml.ai/Symbolics/stable/manual/faq/ for more details.
simplify(gravitational_potential_energy(x), expand=true)
catch e
e
end
Comment on lines +93 to +100

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cell is not working because of the short-circuiting on this line:

m > 0 || return zero(cache_eltype(state))

I tried removing the short-circuiting statement and it seems to fix it, but the repercussions of doing that are not clear to me. I am afraid that not having the early return might affect performance somehow, but on the other hand I could not see it being used as part of other methods...

@tkoolen would be the best person to chip in. 😛

6 changes: 3 additions & 3 deletions examples/6. Symbolics/Manifest.toml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/6. Symbolics/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Quaternions = "94ee1d12-ae83-5a48-8b1c-48b8ff168ae0"
RigidBodyDynamics = "366cf18f-59d5-5db9-a4de-86a9f6786172"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
1 change: 0 additions & 1 deletion test/test_notebooks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ let
notebookdir = joinpath(@__DIR__, "..", "examples")
excludedirs = [".ipynb_checkpoints"]
excludefiles = String[]
push!(excludefiles, "6. Symbolics.ipynb") # Disabled until https://github.com/JuliaGeometry/Quaternions.jl/issues/123 is solved.
push!(excludefiles, "7. Rigorous error bounds using IntervalArithmetic.ipynb") # Manifest used for 1.1 doesn't work for 1.0.
for (root, dir, files) in walkdir(notebookdir)
basename(root) in excludedirs && continue
Expand Down