Skip to content

add PlanarMechanics module #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
103 changes: 103 additions & 0 deletions ext/Render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Render
using Makie
using Multibody
import Multibody: render, render!, loop_render, encode, decode, get_rot, get_trans, get_frame
import Multibody.PlanarMechanics as P
using Rotations
using LinearAlgebra
using ModelingToolkit
Expand Down Expand Up @@ -679,4 +680,106 @@ function rot_from_line(d)
y = y ./ norm(y)
RotMatrix{3}([x y d])
end

# ==============================================================================
## PlanarMechanics
# ==============================================================================
function get_rot_fun_2d(sol, frame)
phifun = get_fun(sol, frame.phi)
function (t)
phi = phifun(t)
[cos(phi) -sin(phi); sin(phi) cos(phi)]
end
end

function get_frame_fun_2d(sol, frame)
R = get_rot_fun_2d(sol, frame)
tr = get_fun(sol, [frame.x, frame.y])
function (t)
[R(t) tr(t); 0 0 1]
end
end

function render!(scene, ::typeof(P.Body), sys, sol, t)
sol(sol.t[1], idxs=sys.render)==true || return true # yes, == true
color = get_color(sys, sol, :purple)
r_cm = get_fun(sol, collect(sys.r))
framefun = get_frame_fun_2d(sol, sys.frame)
radius = sol(sol.t[1], idxs=sys.radius) |> Float32
thing = @lift begin # Sphere
# Ta = framefun($t)
# coords = (Ta*[0; 0; 1])[1:2] # r_cm($t)

coords = r_cm($t)
point = Point3f(coords..., 0)
Sphere(point, Float32(radius))
end
mesh!(scene, thing; color, specular = Vec3f(1.5), shininess=20f0, diffuse=Vec3f(1))
false
end


function render!(scene, ::Union{typeof(P.FixedTranslation), typeof(P.BodyShape)}, sys, sol, t)
sol(sol.t[1], idxs=sys.render)==true || return true # yes, == true
r_0a = get_fun(sol, [sys.frame_a.x, sys.frame_a.y])
r_0b = get_fun(sol, [sys.frame_b.x, sys.frame_b.y])
color = get_color(sys, sol, :purple)
thing = @lift begin
r1 = Point3f(r_0a($t)..., 0)
r2 = Point3f(r_0b($t)..., 0)
origin = r1#(r1+r2) ./ 2
extremity = r2#-r1 # Double pendulum is a good test for this
radius = Float32(sol($t, idxs=sys.radius))
Makie.GeometryBasics.Cylinder(origin, extremity, radius)
end
mesh!(scene, thing; color, specular = Vec3f(1.5), shininess=20f0, diffuse=Vec3f(1))
true
end

function render!(scene, ::typeof(P.Revolute), sys, sol, t)
sol(sol.t[1], idxs=sys.render)==true || return true # yes, == true
r_0 = get_fun(sol, [sys.frame_a.x, sys.frame_a.y])
n = [0,0,1]
color = get_color(sys, sol, :red)

rotfun = get_rot_fun_2d(sol, sys.frame_a)
radius = try
sol(sol.t[1], idxs=sys.radius)
catch
0.05f0
end |> Float32
length = try
sol(sol.t[1], idxs=sys.length)
catch
radius
end |> Float32
thing = @lift begin
O = [r_0($t)..., 0]
R_w_a = cat(rotfun($t), 1, dims=(1,2))
n_w = R_w_a*n # Rotate to the world frame
p1 = Point3f(O + length*n_w)
p2 = Point3f(O - length*n_w)
Makie.GeometryBasics.Cylinder(p1, p2, radius)
end
mesh!(scene, thing; color, specular = Vec3f(1.5), shininess=20f0, diffuse=Vec3f(1))
true
end

function render!(scene, ::Union{typeof(P.Spring), typeof(P.SpringDamper)}, sys, sol, t)
sol(sol.t[1], idxs=sys.render)==true || return true # yes, == true
r_0a = get_fun(sol, [sys.frame_a.x, sys.frame_a.y])
r_0b = get_fun(sol, [sys.frame_b.x, sys.frame_b.y])
color = get_color(sys, sol, :blue)
n_wind = sol(sol.t[1], idxs=sys.num_windings)
radius = sol(sol.t[1], idxs=sys.radius) |> Float32
N = sol(sol.t[1], idxs=sys.N) |> Int
thing = @lift begin
r1 = Point3f(r_0a($t)..., 0)
r2 = Point3f(r_0b($t)..., 0)
spring_mesh(r1,r2; n_wind, radius, N)
end
plot!(scene, thing; color)
true
end

end
2 changes: 2 additions & 0 deletions src/Multibody.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,7 @@ include("robot/robot_components.jl")
include("robot/FullRobot.jl")


export PlanarMechanics
include("PlanarMechanics/PlanarMechanics.jl")

end
28 changes: 28 additions & 0 deletions src/PlanarMechanics/PlanarMechanics.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Library to model planar mechanical multi-body systems inspired by https://github.com/dzimmer/PlanarMechanics
"""

module PlanarMechanics

import ModelingToolkitStandardLibrary.Mechanical.Rotational
import ModelingToolkitStandardLibrary.Mechanical.TranslationalModelica
using ModelingToolkit: t_nounits as t, D_nounits as D
using ModelingToolkit
using ...Blocks: RealInput, RealOutput
import ...@symcheck
import ..Multibody

export Frame, FrameResolve, PartialTwoFrames, ZeroPosition
include("utils.jl")

export Fixed, Body, FixedTranslation, Spring, Damper, SpringDamper
include("components.jl")

export Revolute, Prismatic
include("joints.jl")

export AbsolutePosition,
RelativePosition, AbsoluteVelocity, RelativeVelocity, AbsoluteAcceleration,
RelativeAcceleration, connect_sensor
include("sensors.jl")
end
Loading
Loading