Using GLMakie to animate a crank mechanism

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)/(2crankoutput))

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.

Your chances for an answer would considerably increase, if you could provide us with a consistently formatted M(inimal)W(orking)E(xample) using markdown triple backticks separating code from text. It would allow us to simply copy your code into our REPL…

I mistakenly used the quotation marks ‘’’ instead of the triple backticks. I’ve reposted the question under a different topic here with the mistakes corrected here, Using GLMakie to animate crank mechanism
For some reason, I’m unable to edit this topic despite the account being mine. I’d appreciate all the help I could get.

1 Like