Skip to content

Commit 90356fb

Browse files
committed
add rendering for 2D primstic
1 parent 0cf7f71 commit 90356fb

File tree

2 files changed

+71
-45
lines changed

2 files changed

+71
-45
lines changed

ext/Render.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,23 @@ function render!(scene, ::Union{typeof(P.FixedTranslation), typeof(P.BodyShape)}
751751
true
752752
end
753753

754+
function render!(scene, ::typeof(P.Prismatic), sys, sol, t)
755+
sol(sol.t[1], idxs=sys.render)==true || return true # yes, == true
756+
r_0a = get_fun(sol, [sys.frame_a.x, sys.frame_a.y])
757+
r_0b = get_fun(sol, [sys.frame_b.x, sys.frame_b.y])
758+
color = get_color(sys, sol, :purple)
759+
thing = @lift begin
760+
r1 = Point3f(r_0a($t)..., 0)
761+
r2 = Point3f(r_0b($t)..., 0)
762+
origin = r1#(r1+r2) ./ 2
763+
extremity = r2#-r1 # Double pendulum is a good test for this
764+
radius = Float32(sol($t, idxs=sys.radius))
765+
Makie.GeometryBasics.Cylinder(origin, extremity, radius)
766+
end
767+
mesh!(scene, thing; color, specular = Vec3f(1.5), shininess=20f0, diffuse=Vec3f(1))
768+
true
769+
end
770+
754771
function render!(scene, ::typeof(P.Revolute), sys, sol, t)
755772
sol(sol.t[1], idxs=sys.render)==true || return true # yes, == true
756773
r_0 = get_fun(sol, [sys.frame_a.x, sys.frame_a.y])

src/PlanarMechanics/joints.jl

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,16 @@ https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b70839146
7676
end
7777

7878
"""
79-
Prismatic(; name, rx, ry, f, s = 0, use_flange = false)
79+
Prismatic(; name, f, s = 0, axisflange = false)
8080
A prismatic joint
8181
82-
# parameters
83-
- `x`: [m] x-direction of the rod wrt. body system at phi=0
84-
- `y`: [m] y-direction of the rod wrt. body system at phi=0
82+
# Parameters
83+
- `r`: [m, m] x,y-direction of the rod wrt. body system at phi=0
8584
- `constant_f`: [N] Constant force in direction of elongation
8685
- `constant_s`: [m] Constant elongation of the joint"
87-
- `use_flange=false`: If `true`, a force flange is enabled, otherwise implicitly grounded"
86+
- `axisflange=false`: If `true`, a force flange is enabled, otherwise implicitly grounded"
8887
89-
# states
88+
# Variables
9089
- `s(t)`: [m] Elongation of the joint
9190
- `v(t)`: [m/s] Velocity of elongation
9291
- `a(t)`: [m/s²] Acceleration of elongation
@@ -95,67 +94,77 @@ A prismatic joint
9594
# Connectors
9695
- `frame_a` [Frame](@ref)
9796
- `frame_b` [Frame](@ref)
98-
- `fixed` [Fixed](@ref) if `use_flange == false`
99-
- `flange_a` [Flange](@ref) if `use_flange == true`
100-
- `support` [Support](@ref) if `use_flange == true`
97+
- `fixed` [Fixed](@ref) if `axisflange == false`
98+
- `flange_a` [Flange](@ref) if `axisflange == true`
99+
- `support` [Support](@ref) if `axisflange == true`
101100
102101
https://github.com/dzimmer/PlanarMechanics/blob/743462f58858a808202be93b708391461cbe2523/PlanarMechanics/Joints/Prismatic.mo
103102
"""
104103
@component function Prismatic(;
105104
name,
106-
x,
107-
y,
105+
r = [0,0],
106+
s = 0,
107+
v = 0,
108108
constant_f = 0,
109109
constant_s = 0,
110-
use_flange = false)
110+
axisflange = false,
111+
render = true,
112+
radius = 0.1,
113+
color = [0,0.8,1,1],
114+
)
111115
@named partial_frames = PartialTwoFrames()
116+
systems = @named begin
117+
fixed = TranslationalModelica.Support()
118+
end
112119
@unpack frame_a, frame_b = partial_frames
113-
@named fixed = TranslationalModelica.Support()
114-
systems = [frame_a, frame_b, fixed]
115120

116-
if use_flange
117-
@named flange_a = TranslationalModelica.Flange(; f = constant_f, constant_s)
118-
push!(systems, flange_a)
119-
@named support = TranslationalModelica.Support()
120-
push!(systems, support)
121+
if axisflange
122+
more_systems = @named begin
123+
flange_a = TranslationalModelica.Flange(; f = constant_f, constant_s)
124+
support = TranslationalModelica.Support()
125+
end
126+
systems = [systems, more_systems]
121127
end
122128

123-
vars = @variables begin
124-
s(t) = 0.0
125-
v(t) = 0.0
126-
a(t) = 0.0
127-
f(t) = 0.0
129+
pars = @parameters begin
130+
(r[1:2] = r), [description="Direction of the rod wrt. body system at phi=0"]
131+
render = render, [description="Render the joint in animations"]
132+
radius = radius, [description="Radius of the body in animations"]
133+
color[1:4] = color, [description="Color of the body in animations"]
128134
end
129135

130-
R = [cos(frame_a.phi) -sin(frame_a.phi);
131-
sin(frame_a.phi) cos(frame_a.phi)]
132-
e0 = R * [x, y]
133-
r0 = e0 * s
136+
vars = @variables begin
137+
(s(t) = s), [state_priority = 2, description="Joint coordinate"]
138+
(v(t) = v), [state_priority = 2]
139+
a(t)
140+
f(t)
141+
e0(t)[1:2]
142+
(r0(t)[1:2]=r), [description="Translation vector of the prismatic rod resolved w.r.t. inertial frame"]
143+
end
134144

145+
e = Multibody._normalize(r)
146+
R = ori_2d(frame_a)
147+
135148
eqs = [
136-
# ifelse(constant_s === nothing, s ~ s, s ~ constant_s),
137-
ifelse(constant_f === nothing, f ~ f, f ~ constant_f),
138-
v ~ D(s),
139-
a ~ D(v),
149+
e0 .~ R * e
150+
r0 .~ e0 * s
151+
v ~ D(s)
152+
a ~ D(v)
140153
# rigidly connect positions
141-
frame_a.x + r0[1] ~ frame_b.x,
142-
frame_a.y + r0[2] ~ frame_b.y,
143-
frame_a.phi ~ frame_b.phi,
144-
frame_a.fx + frame_b.fx ~ 0,
145-
frame_a.fy + frame_b.fy ~ 0,
146-
frame_a.tau + frame_b.tau + r0[1] * frame_b.fy - r0[2] * frame_b.fx ~ 0,
154+
frame_a.x + r0[1] ~ frame_b.x
155+
frame_a.y + r0[2] ~ frame_b.y
156+
frame_a.phi ~ frame_b.phi
157+
frame_a.fx + frame_b.fx ~ 0
158+
frame_a.fy + frame_b.fy ~ 0
159+
frame_a.tau + frame_b.tau + r0[1] * frame_b.fy - r0[2] * frame_b.fx ~ 0
147160
e0[1] * frame_a.fx + e0[2] * frame_a.fy ~ f
148161
]
149162

150-
if use_flange
163+
if axisflange
151164
push!(eqs, connect(fixed.flange, support))
152165
else
153166
# actutation torque
154-
push!(eqs, constant_f ~ 0)
167+
push!(eqs, f ~ 0)
155168
end
156-
157-
pars = []
158-
159-
return compose(ODESystem(eqs, t, vars, pars; name = name),
160-
systems...)
169+
return extend(ODESystem(eqs, t, vars, pars; name, systems), partial_frames)
161170
end

0 commit comments

Comments
 (0)