I am trying to animate a crank mechanism using the GLMakie package. I was following this youtube tutorial, Making animations and interactive applications in Makie.jl - YouTube , and here’s the code I wrote.
using GLMakie
crank = 3.3
fixed = 6.8
coupler = 2
output = 9.3
θ1 = (61/90)*pi
k1 = fixed/crank
k2 = fixed/output
k3 = ((output^2 - coupler^2 + output^2 + fixed^2)/(2 *crank* output))
P1 = [0;0]
p4x = fixed*-cos(θ1)
p4y = fixed*-sin(θ1)
function mysqrt(x)
if x < 0
0.5
else
sqrt(x)
end
end
θ2 = Array(0:0.05:2*pi)
function xycoordinates(θ2)
A = (1-k2)*cos(θ2) - k1 + k3
B = -2*sin(θ2)
C = k1 - (1 + k2)*cos(θ2 + k3)
b = mysqrt(B^2 - 4A*C)
θ4 = 2*(atan((-B - b)/2A, (-B + b)/2A))
p2x = crank*cos(θ2)
p2y = crank*sin(θ2)
p3x = output*-cos(θ4)
p3y = output*sin(θ4)
return p2x, p2y, p3x, p3y
end
function progress_for_one_step!(θ2)
step!(θ2)
u = θ2.u
return xycoordinates(u)
end
p2x, p3x, p2y, p3x = xycoordinates(θ2)
mechanism = Observable([Point2f0(0,0), Point2f0(p2x,p2y), Point2f0(p3x,p3y), Point2f0(p4x,p4y)])
fig = Figure(); display(fig)
ax = Axis(fig[1,1])
lines!(ax, mechanism; linewidth=4, color = :purple)
ax.title = “crank mechanism”
function animstep!(θ2, mechanism)
p2x, p3x, p2y, p3x = progress_for_one_step!(θ2)
mechanism[] = [Point2f0(0,0), Point2f0(p2x,p2y), Point2f0(p3x,p3y), Point2f0(p4x,p4y)]
end
for i in 1:1000
animstep!(θ2, mechanism)
sleep(0.001)
end
Almost every line of the code runs except the line “p2x, p3x, p2y, p3x = xycoordinates(θ2)” which prevents the plot from animating. I think the problem could be linked to my “progress_for_one_step” function and how my data is presented in the program. Could someone help me in identifying and correcting the error? Thank you.