# Using GLMakie to animate a crank mechanism

**URL:** <https://discourse.julialang.org/t/using-glmakie-to-animate-a-crank-mechanism/79552>\
**Category:** New to Julia\
**Tags:** plotting, glmakie, animations\
**Created:** [April 15, 2022, 9:11pm UTC](https://discourse.julialang.org/t/using-glmakie-to-animate-a-crank-mechanism/79552 "2022-04-15T21:11:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [April 15, 2022, 9:11pm UTC](https://discourse.julialang.org/t/using-glmakie-to-animate-a-crank-mechanism/79552/1 "2022-04-15T21:11:07Z")

</div>

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](https://www.youtube.com/watch?v=L-gyDvhjzGQ&t=2s), 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.

---

<div class="post-metadata">

**Author:** ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)\
**Post date:** [April 15, 2022, 9:30pm UTC](https://discourse.julialang.org/t/using-glmakie-to-animate-a-crank-mechanism/79552/2 "2022-04-15T21:30:58Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Ogweno\_Emmanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ogweno_emmanuel/32/35482_2.png) [@Ogweno\_Emmanuel](https://discourse.julialang.org/u/Ogweno_Emmanuel)\
**Post date:** [April 17, 2022, 6:46am UTC](https://discourse.julialang.org/t/using-glmakie-to-animate-a-crank-mechanism/79552/3 "2022-04-17T06:46:33Z")

</div>

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](https://discourse.julialang.org/t/using-glmakie-to-animate-crank-mechanism/79594)  
For some reason, I’m unable to edit this topic despite the account being mine. I’d appreciate all the help I could get.
