forked from cooperrc/me5180-project_02
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproject_02.jl
More file actions
2016 lines (1615 loc) · 60.3 KB
/
project_02.jl
File metadata and controls
2016 lines (1615 loc) · 60.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### A Pluto.jl notebook ###
# v0.20.23
#> [frontmatter]
#> title = "project 2_rev3_written_in"
using Markdown
using InteractiveUtils
# ╔═╡ 91f15f2c-7b4e-4f5f-8d45-9ebef77ce7dc
using LinearAlgebra, Plots
# ╔═╡ f17103ea-06bf-11f1-a2b0-79e68ed152eb
md"""# Project_02 - Multibody kinematic modeling

In this project, a rigid bar is connected to two sliding pistons along
the diagonal tracks. As the pistons move along the tracks, the rigid bar rotates at a constant rate, $\dot{\theta}_3 = 2~rad/s$. The figure above has three _relative_ ccoordinate systems that move with the bodies:
1. $x_1-y_1-$ describes piston 1 position and orientation, $\theta_1$
2. $x_2-y_2-$ describes piston 2 position and orientation, $\theta_2$
3. $x_3-y_3-$ describes the rigid bar position and orientation, $\theta_3$
Each of the pistons are on tracks at $\pm 45^o$ and the rotating rigid
bar is 10 cm. The hinges are mounted to the center of the pistons
connecting the ends of the rigid bar.
In this project, you need to
1. determine constraint equations $C(\mathbf{q},~t)$
2. solve for the velocities, $\dot{q}$ and accelerations, $\ddot{q}$
3. visualize the motion of the system as the rigid bar goes through at least one full rotation
"""
# ╔═╡ b79f52a8-8c00-4b70-9fc5-164b7735e70d
md"""# 1. Determine the Constraint equations C(q,t)
The system consists of two pistons constrained to slide along fixed diagonal tracks and connected by a rigid bar of fixed length L=0.10m
The pistons are connected to the ends of the rigid bar through ideal pin joints located at the piston centers.
The rigid bar rotates with a prescribed constant angular velocity $\theta_3$.
A global inertial coordinate system $(x, y)$ is defined with its origin at the intersection point of the two tracks. Each piston moves along a straight line defined by its track angles $\alpha_1$ and $\alpha_2$, respectively
**Generalized Coordinates:**
Instead of modeling each body with full rigid‑body coordinates, we use reduced generalized coordinates ->
$q=\begin{bmatrix}
s_1 \\
s_2 \\
\theta_3
\end{bmatrix}$
**Geometry:**
The geometry of $r_1$ and $r_2$ are expressed below
$\mathbf{r}_{1} = s_1\begin{bmatrix}
cos{\theta}_{1} \\
sin{\theta}_{1}
\end{bmatrix}=s_1\begin{bmatrix}
cos(45) \\
sin(45)
\end{bmatrix}$
$\mathbf{r}_{2} = s_2\begin{bmatrix}
cos{\theta}_{2} \\
sin{\theta}_{2}
\end{bmatrix}=s_2\begin{bmatrix}
cos(135) \\
sin(135)
\end{bmatrix}$
**Setting up the Constraint Equation:**
The global position vectors of the pistons are written directly from the track geometry. The rigid bar enforces a distance constraint between the pistons.
$(\mathbf{r}_{2}-\mathbf{r}_{1})^2=L^2$
$C(q,t)=(\mathbf{r}_{2}-\mathbf{r}_{1})*(\mathbf{r}_{2}-\mathbf{r}_{1})-L^2=0$
$\mathbf{r}_{2}-\mathbf{r}_{1}=\begin{bmatrix}
s_2cos(135)-s_1cos(45) \\
s_2sin(135)-s_1sin(45)
\end{bmatrix}=\begin{bmatrix}
s_2(-\frac{\sqrt{2}}{2})-s_1(\frac{\sqrt{2}}{2}) \\
s_2(\frac{\sqrt{2}}{2})-s_1(\frac{\sqrt{2}}{2})
\end{bmatrix}$
**Final Constraint Equation:**
Substituting the piston position expression gives
$C(q,t)=s_1^2+s_2^2-L^2=0$
**For Julia/Pluto Purposes:**
$C(q,t)=A^2-b^2=0$
$A=\mathbf{r}_{2}-\mathbf{r}_{1}=\begin{bmatrix}
s_2cos(135)-s_1cos(45) \\
s_2sin(135)-s_1sin(45)
\end{bmatrix}$
$b=L=\begin{bmatrix}
Lcos{\theta}_{3} \\
Lsin{\theta}_{3}
\end{bmatrix}$
"""
# ╔═╡ 279da94d-6a6a-4884-bc51-09a7c90edafe
md"""
**Vector from Piston 1 to Piston 2:**
${\theta}_{3}(t)=0$
$\mathbf{r}_{21} = \begin{bmatrix}
Lcos{\theta}_{3} \\
Lsin{\theta}_{3}
\end{bmatrix}$
$\mathbf{r}_{2} = \mathbf{r}_{1}+\mathbf{r}_{21}$
This vector is constrained to remain aligned with the rigid bar and have magnitude L.
**XY Directions:**
The constraint is enforced independently in the x- and y-directions, producing two scaler equations->
$s_2\begin{bmatrix}
cos(135) \\
sin(135)
\end{bmatrix}=s_1\begin{bmatrix}
cos(45) \\
sin(45)
\end{bmatrix} + \begin{bmatrix}
Lcos{\theta}_{3} \\
Lsin{\theta}_{3}
\end{bmatrix}$
$s_2\frac{-2}{\sqrt{2}}=s_1\frac{2}{\sqrt{2}}+Lcos{\theta}_{3}$
$s_2\frac{2}{\sqrt{2}}=s_1\frac{2}{\sqrt{2}}+Lsin{\theta}_{3}$
**Addition Method then isolate s value:**
Solving the coupled linear equations yields the pistion displacements->
s_1:
$0=\frac{4}{\sqrt{2}}s_1+L(cos{\theta}_{3}+sin{\theta}_{3})$
$0=2\sqrt{2}s_1+L(cos{\theta}_{3}+sin{\theta}_{3})$
$s_1=-\frac{L}{2\sqrt{2}}(cos{\theta}_{3}+sin{\theta}_{3})$
s_2:
$\frac{4}{\sqrt{2}}s_2=L(sin{\theta}_{3}-cos{\theta}_{3})$
$2{\sqrt{2}}s_2=L(sin{\theta}_{3}-cos{\theta}_{3})$
$s_2=\frac{L}{2\sqrt{2}}(sin{\theta}_{3}-cos{\theta}_{3})$
"""
# ╔═╡ a31c2198-98d0-4e79-ba9b-74487a3fd967
md"""# 2. Solve for Velocities $\dot{q}$ and accelerations $\ddot{q}$
**Velocity:**
Differentiating the constraint equation with respect to time gives ->
$C(\dot{q},t)=2{s}_{1}\dot{s_1}+2{s}_{2}\dot{s_2}=0$
$C(\dot{q},t)={s}_{1}\dot{s_1}+{s}_{2}\dot{s_2}=0$
$C(\dot{q},t)=(-\frac{L}{2\sqrt{2}}(cos{\theta}_{3}+sin{\theta}_{3}))\dot{s_1}+(\frac{L}{2\sqrt{2}}(sin{\theta}_{3}-cos{\theta}_{3}))\dot{s_2}=0$
s_1:
$\dot{s_1}=\frac{L}{\sqrt{2}}(sin{\theta}_{3}-cos{\theta}_{3})$
s_2:
$\dot{s_2}=\frac{L}{\sqrt{2}}(cos{\theta}_{3}+sin{\theta}_{3})$
"""
# ╔═╡ 891f182c-18f8-401d-a984-b1b3de2e67b0
md"""
**Acceleration:**
Differentitating once more ->
$C(\ddot{q},t)=\dot{s_1}^2+s_1\ddot{s_1}+\dot{s_2}^2+s_2\ddot{s_2}=0$
$C(\ddot{q},t)=\dot{s_1}^2+\dot{s_2}^2+s_1\ddot{s_1}+s_2\ddot{s_2}=0$
$C(\ddot{q},t)=(\frac{L}{\sqrt{2}}(sin{\theta}_{3}-cos{\theta}_{3}))^2+(\frac{L}{\sqrt{2}}(cos{\theta}_{3}+sin{\theta}_{3}))^2+(-\frac{L}{2\sqrt{2}}(cos{\theta}_{3}+sin{\theta}_{3}))\ddot{s_1}+(\frac{L}{2\sqrt{2}}(sin{\theta}_{3}-cos{\theta}_{3}))\ddot{s_2}=0$
s_1:
$\ddot{s_1}=\frac{2L}{\sqrt{2}}(cos{\theta}_{3}+sin{\theta}_{3})$
s_2:
$\ddot{s_2}=\frac{2L}{\sqrt{2}}(-sin{\theta}_{3}+cos{\theta}_{3})$
"""
# ╔═╡ ea9a70e5-5790-4c11-97ae-690e6d9eb516
# -------------------------
# Parameters
# -------------------------
begin
L = 0.10 # bar length in meters (10 cm)
ω3 = 2.0 # rad/s
θ30 = 0.0 # initial angle
# Verify these track angles from your original assignment
α1 = π/4 # example: 45 deg
α2 = 3*π/4 # example: 45 deg (shift in angle accounted for later)
end
# ╔═╡ fbe15d42-7162-4218-88e1-018ed677b3f0
# Time
tspan = range(0, 4π/ω3, length=300) # one full rotation
# ╔═╡ bcb494f9-a3df-46a7-9348-e2eac204e190
md"""
${\theta}_{3}(t)={\theta}_{3}+{\omega}_{3}t$
"""
# ╔═╡ b8e47bce-4ec8-4bf1-b64b-817e597a65b2
# -------------------------
# Functions
# -------------------------
θ3(t) = θ30 + ω3*t
# ╔═╡ 2cddd741-e951-432e-9c5c-8853488d3c6d
md"""
Position Inputs (Assumes ${\alpha}_{1}$ & ${\alpha_2}$ are equivalent):
$A=\begin{bmatrix}
~-cos({\alpha}_{1})~~~cos({\alpha}_{2}) \\
~-sin({\alpha}_{1})~~sin({\alpha}_{2})
\end{bmatrix}$
$b=\begin{bmatrix}
Lcos({\theta_3}) \\
Lsin({\theta_3})
\end{bmatrix}$
$s=A,~b$
"""
# ╔═╡ 588c45c6-4633-4e7f-9534-57709602170f
function solve_positions(t, α1, α2, L)
θ = θ3(t)
A = [
-cos(α1) cos(α2)
-sin(α1) sin(α2)
]
b = [
L*cos(θ)
L*sin(θ)
]
s = A \ b
return s[1], s[2]
end
# ╔═╡ f09f7a94-7d0c-4485-ba10-be8f65eb456d
md"""
Velocity Inputs:
$A=\begin{bmatrix}
~-cos({\alpha}_{1})~~~cos({\alpha}_{2}) \\
~-sin({\alpha}_{1})~~sin({\alpha}_{2})
\end{bmatrix}$
$b=\begin{bmatrix}
-L{\omega}_{3}^2sin({\theta_3}) \\
L{\omega}_{3}^2cos({\theta_3})
\end{bmatrix}$
$\dot{s}=A,~b$
"""
# ╔═╡ f3004cb6-61b9-4eda-9472-8b313377577d
function solve_velocities(t, α1, α2, L, ω3)
θ = θ3(t)
A = [
-cos(α1) cos(α2)
-sin(α1) sin(α2)
]
b = [
-L*ω3*sin(θ)
L*ω3*cos(θ)
]
sdot = A \ b
return sdot[1], sdot[2]
end
# ╔═╡ b3151f63-6c9f-4bf9-ae4c-9fa89d6bc1c0
md"""
Acceleration Inputs:
$A=\begin{bmatrix}
~-cos({\alpha}_{1})~~~cos({\alpha}_{2}) \\
~-sin({\alpha}_{1})~~sin({\alpha}_{2})
\end{bmatrix}$
$b=\begin{bmatrix}
-L{\omega}_{3}^2cos({\theta_3}) \\
-L{\omega}_{3}^2sin({\theta_3})
\end{bmatrix}$
$\ddot{s}=A,~b$
"""
# ╔═╡ a9ad74bb-1d7a-486c-a65c-295aaad37750
function solve_accelerations(t, α1, α2, L, ω3)
θ = θ3(t)
A = [
-cos(α1) cos(α2)
-sin(α1) sin(α2)
]
b = [
-L*ω3^2*cos(θ)
-L*ω3^2*sin(θ)
]
sddot = A \ b
return sddot[1], sddot[2]
end
# ╔═╡ 735b0c96-cdb9-450e-a89d-1c76f2b15560
begin
#Conditioning / singularity check for the 2×2 solve A*s = b
# A depends only on the track angles α1 and α2 (constants in this project).
A = [
-cos(α1) cos(α2)
-sin(α1) sin(α2)
]
condA = cond(A)
# Thresholds (rule of thumb)
cond_warn = 100.0
cond_bad = 1000.0
md"Conditioning check: cond(A) = $(round(condA, digits=3)) (warn > $(cond_warn), bad > $(cond_bad))"
end
# ╔═╡ 6c4c1d6c-05c9-4eba-ad6b-57d74b94f1c3
begin
# Plot cond(A) as a constant line across the simulation time span
condA_vals = fill(condA, length(tspan))
p = plot(
tspan, condA_vals,
xlabel="Time (s)",
ylabel="cond(A)",
title="Condition Number of Solve Matrix A",
legend=false,
lw=2
)
hline!(p, [cond_warn], linestyle=:dash, lw=2)
hline!(p, [cond_bad], linestyle=:dashdot, lw=2)
p
end
# ╔═╡ 1f6e208c-e8bd-4974-9973-51b7d1a571f3
md"""
### Conditioning check
The 2×2 solve (A*s = b) is well-conditioned with cond(A) = 1.0, so the kinematics are far from singular and numerically stable.
"""
# ╔═╡ b1bd29d2-aabe-490d-8e58-1854f2de485f
# -------------------------
# Compute results
# -------------------------
s1_vals = Float64[]
# ╔═╡ 0a8aec35-ae14-491c-9926-becdad7bbf46
s2_vals = Float64[]
# ╔═╡ f4f50ebf-b7cc-4d13-be8f-90ea731502d5
s1dot_vals = Float64[]
# ╔═╡ bcb7995f-2061-495b-8f1f-b134d5222c04
s2dot_vals = Float64[]
# ╔═╡ 66646852-6945-4a81-98aa-9ca3cd5b54dd
s1ddot_vals = Float64[]
# ╔═╡ d25d2756-1f09-46f4-80b3-96389c4eca6c
s2ddot_vals = Float64[]
# ╔═╡ 1d0dcfeb-c2cc-4d09-88db-d6e9e430d7cc
md"""# 3. Visualize the Motion of the System as a Rigid Bar Goes through at least one Full Rotation
"""
# ╔═╡ a6889fc8-bbac-473a-9abf-1276db8eb901
for t in tspan
s1, s2 = solve_positions(t, α1, α2, L)
s1dot, s2dot = solve_velocities(t, α1, α2, L, ω3)
s1ddot, s2ddot = solve_accelerations(t, α1, α2, L, ω3)
push!(s1_vals, s1)
push!(s2_vals, s2)
push!(s1dot_vals, s1dot)
push!(s2dot_vals, s2dot)
push!(s1ddot_vals, s1ddot)
push!(s2ddot_vals, s2ddot)
end
# ╔═╡ 2c48c51f-97ba-4c5c-aeed-afd29f735e65
md"""
## Piston Kinematics As A Function of S1 and S2
"""
# ╔═╡ 753ef8f1-c7e6-4728-ba19-69b852f108bb
plot(
tspan,
[s1_vals s2_vals],
xlabel = "Time (s)",
ylabel = "Position along track (m)",
label = ["Piston 1" "Piston 2"],
linewidth = 2,
title = "Piston Positions vs Time"
)
# ╔═╡ c31c4349-193d-46d8-a0f9-e3e414e9409d
plot(
tspan,
[s1dot_vals s2dot_vals],
xlabel = "Time (s)",
ylabel = "Velocity along track (m/s)",
label = ["s1_dot(t)" "s2_dot(t)"],
linewidth = 2,
title = "Piston Velocities vs Time"
)
# ╔═╡ eb01dc5a-77ee-412d-9468-1157ef8d4d36
plot(
tspan,
[s1ddot_vals s2ddot_vals],
xlabel = "Time (s)",
ylabel = "Acceleration along track (m/s^2)",
label = ["s1_ddot(t)" "s2_ddot(t)"],
linewidth = 2,
title = "Piston Accelerations vs Time"
)
# ╔═╡ e44beb54-0152-47c5-ac2c-050eff89646c
md"""
## Piston Kinematics In Global Coordinate System
"""
# ╔═╡ bf6e093d-d926-4512-b2b0-ee67e8e7959b
begin
function piston1_xy(s1)
return s1*cos(α1), s1*sin(α1)
end
function piston2_xy(s2)
return -s2*cos(α2), -s2*sin(α2)
end
end
# ╔═╡ 6996a516-67aa-4297-b0f1-0580e064886a
begin
function piston1_v_xy(s1dot)
return s1dot*cos(α1), s1dot*sin(α1)
end
function piston2_v_xy(s2dot)
return -s2dot*cos(α2), -s2dot*sin(α2)
end
end
# ╔═╡ 16d56fb2-b4ba-4bb2-9d9f-04910d339a41
begin
function piston1_a_xy(s1ddot)
return s1ddot*cos(α1), s1ddot*sin(α1)
end
function piston2_a_xy(s2ddot)
return -s2ddot*cos(α2), -s2ddot*sin(α2)
end
end
# ╔═╡ 8c91c608-e929-45c9-9f45-5c9e89de1317
xp11,xp12 = piston1_xy(s1_vals)
# ╔═╡ 7c745e4b-8263-4e1f-969e-9e1d64ef3278
xp21,xp22 = piston2_xy(s2_vals)
# ╔═╡ 863f1020-cbc9-41eb-b251-54ba20a8c718
begin
xv11,xv12 = piston1_v_xy(s1dot_vals)
xv21,xv22 = piston2_v_xy(s2dot_vals)
end
# ╔═╡ ba0416bb-c88d-461b-a204-528476a7743b
begin
xa11,xa12 = piston1_a_xy(s1ddot_vals)
xa21,xa22 = piston2_a_xy(s2ddot_vals)
end
# ╔═╡ ad5a72a9-f54f-43d8-887f-60384e0eb72f
#Animation of the position of Piston 1 in the overall global coordinate system
begin
displace_p1 = @animate for k in eachindex(tspan)
plot(
tspan,
[xp11, xp12];
xlabel = "Time (s)",
ylabel = "Position along track (m)",
label = ["X" "Y"],
linewidth = 2,
title = "Piston 1 Position vs Time"
)
scatter!([tspan[k]], [xp11[k]];
c=:dodgerblue, ms=6, label="")
scatter!([tspan[k]], [xp12[k]];
c=:tomato, ms=6, label="")
end
gif(displace_p1, "piston1_position_trace.gif"; fps=45)
end
# ╔═╡ c5c20310-9f64-438d-a246-2f0f06fafe92
#Animation of the position of Piston 2 in the overall global coordinate system
begin
displace_p2 = @animate for k in eachindex(tspan)
plot(
tspan,
[xp21, xp22];
xlabel = "Time (s)",
ylabel = "Position along track (m)",
label = ["X" "Y"],
linewidth = 2,
title = "Piston 2 Position vs Time"
)
scatter!([tspan[k]], [xp21[k]];
c=:dodgerblue, ms=6, label="")
scatter!([tspan[k]], [xp22[k]];
c=:tomato, ms=6, label="")
end
gif(displace_p2, "piston1_position_trace.gif"; fps=45)
end
# ╔═╡ a5b189a8-a5c7-4a69-a4a6-53fb256b1f76
md"""
## Piston Velocity
"""
# ╔═╡ 4c938496-9204-4c15-8ffe-e6a8242f430b
begin
velocity_p1 = @animate for k in eachindex(tspan)
plot(
tspan,
[xv11, xv12];
xlabel = "Time (s)",
ylabel = "Velocity (m/s)",
label = ["Xdot" "Ydot"],
linewidth = 2,
title = "Piston 1 Velocity vs Time"
)
scatter!([tspan[k]], [xv11[k]];
c=:dodgerblue, ms=6, label="")
scatter!([tspan[k]], [xv12[k]];
c=:tomato, ms=6, label="")
end
gif(velocity_p1, "piston1_velocity_trace.gif"; fps=45)
end
# ╔═╡ 6eba0cea-c34d-4905-b05b-55e8290ab6a7
begin
velocity_p2 = @animate for k in eachindex(tspan)
plot(
tspan,
[xv21, xv22];
xlabel = "Time (s)",
ylabel = "Velocity (m/s)",
label = ["Xdot" "Ydot"],
linewidth = 2,
title = "Piston 2 Velocity vs Time"
)
scatter!([tspan[k]], [xv21[k]];
c=:dodgerblue, ms=6, label="")
scatter!([tspan[k]], [xv22[k]];
c=:tomato, ms=6, label="")
end
gif(velocity_p2, "piston2_velocity_trace.gif"; fps=45)
end
# ╔═╡ 10ed4dbe-5873-4091-9239-9b4c353e8783
md"""
## Acceleration
"""
# ╔═╡ 9dcb799e-91e2-41f3-bb90-f5c0ee48add6
begin
accel_p1 = @animate for k in eachindex(tspan)
plot(
tspan,
[xa11, xa12];
xlabel = "Time (s)",
ylabel = "Acceleration (m/s^2)",
label = ["Xddot" "Yddot"],
linewidth = 2,
title = "Piston 1 Acceleration vs Time"
)
scatter!([tspan[k]], [xa11[k]];
c=:dodgerblue, ms=6, label="")
scatter!([tspan[k]], [xa12[k]];
c=:tomato, ms=6, label="")
end
gif(accel_p1, "piston1_accel_trace.gif"; fps=45)
end
# ╔═╡ 5711b475-6cf5-4c28-b4fe-87c40a92a881
begin
accel_p2 = @animate for k in eachindex(tspan)
plot(
tspan,
[xa21, xa22];
xlabel = "Time (s)",
ylabel = "Acceleration (m/s^2)",
label = ["Xddot" "Yddot"],
linewidth = 2,
title = "Piston 2 Acceleration vs Time"
)
scatter!([tspan[k]], [xa21[k]];
c=:dodgerblue, ms=6, label="")
scatter!([tspan[k]], [xa22[k]];
c=:tomato, ms=6, label="")
end
gif(accel_p2, "piston2_accel_trace.gif"; fps=45)
end
# ╔═╡ c307baa6-eb57-4be8-b529-57be29344c9d
begin
figure_p1_x = plot(
tspan,
[xp11, xv11, xa11];
xlabel = "Time (s)",
ylabel = "Vector Value",
label = ["Position" "Velocity" "Acceleration"],
linewidth = 2,
title = "Piston 1 X"
)
figure_p1_y = plot(
tspan,
[xp12, xv12, xa12];
xlabel = "Time (s)",
ylabel = "Vector Value",
label = ["Position" "Velocity" "Acceleration"],
linewidth = 2,
title = "Piston 1 Y"
)
plot(figure_p1_x,figure_p1_y, plot_title = "Piston 1 Kinematics")
end
# ╔═╡ 6e34c8c0-1b01-4425-b3bc-a52e94efb977
begin
figure_p2_x = plot(
tspan,
[xp21, xv21, xa21];
xlabel = "Time (s)",
ylabel = "Vector Value",
label = ["Position" "Velocity" "Acceleration"],
linewidth = 2,
title = "Piston 2 X"
)
figure_p2_y = plot(
tspan,
[xp22, xv22, xa22];
xlabel = "Time (s)",
ylabel = "Vector Value",
label = ["Position" "Velocity" "Acceleration"],
linewidth = 2,
title = "Piston 2 Y"
)
plot(figure_p2_x,figure_p2_y, plot_title = "Piston 2 Kinematics")
end
# ╔═╡ 586cd0be-3bca-4338-a451-429cd6268414
md"""# System Rotation Animation Code"""
# ╔═╡ a354eb0a-91ea-4cbe-8bfb-6e0338d987eb
#Defines Channel Directions for Plots
begin
channel1_direction = (cos(α1), sin(α1))
channel2_direction = (cos(α2), sin(α2)) # crossed channels
end
# ╔═╡ edef31e2-ed59-4c67-a2fd-7ed2fe27834c
begin
#Generates the Block Visual
function rectangle_shape(cx, cy, dx, dy; length=0.04, width=0.02)
dir = [dx, dy] / norm([dx, dy])
nrm = [-dir[2], dir[1]]
hL, hW = length/2, width/2
pts = [ dir*hL+nrm*hW, dir*hL-nrm*hW,
-dir*hL-nrm*hW, -dir*hL+nrm*hW ]
Shape(cx .+ getindex.(pts,1), cy .+ getindex.(pts,2))
end
#Generates the slot visuals
function slot_shape(cx, cy, dx, dy; half_length=0.25, half_width=0.025)
dir = [dx, dy] / norm([dx, dy])
nrm = [-dir[2], dir[1]]
ends = [-half_length*dir, half_length*dir]
pts = [ends[1]+half_width*nrm, ends[2]+half_width*nrm,
ends[2]-half_width*nrm, ends[1]-half_width*nrm]
Shape(cx .+ getindex.(pts,1), cy .+ getindex.(pts,2))
end
end
# ╔═╡ 17c657dc-a2ad-4741-b75a-9b5f00526401
begin
channel1 = slot_shape(0.0, 0.0, channel1_direction...; half_length=0.14)
channel2 = slot_shape(0.0, 0.0, channel2_direction...; half_length=0.14)
end
# ╔═╡ 67941a28-6362-4735-a97a-c6484326c13d
begin
time_s = tspan
x1 = xp11; y1 = xp12
x2 = xp21; y2 = xp22
x_min = -0.15; x_max = 0.15
y_min = -0.15; y_max = 0.15
anim = @animate for k in eachindex(x1)
plot(; xlim=(x_min, x_max), ylim=(y_min, y_max), aspect_ratio=:equal,
legend=false, title="Mechanism motion θ̇₃ = 2 rad/s t=$(round(time_s[k], digits=3)) s")
plot!([x1[k], x2[k]], [y1[k], y2[k]]; lw=4) # connecting link
scatter!([x1[k]], [y1[k]]; ms=8) # piston 1
scatter!([x2[k]], [y2[k]]; ms=8) # piston 2
end
gif(anim, "mechanism.gif"; fps=60)
``
end
# ╔═╡ 3464bcbe-9588-4fc1-aa50-5e11d0d01051
dt = tspan[2]-tspan[1]
# ╔═╡ 4e1db290-43a8-4552-99f2-e44a1f7f2186
gif(anim, "mechanism.gif", fps=1/dt)
# ╔═╡ dde15418-4f60-47cb-853b-c048422ceb08
begin
anim3 = @animate for k in eachindex(x1)
plot(; aspect_ratio=:equal, legend=:topright,
xlim=(-0.35,0.35), ylim=(-0.35,0.35),
title="ω₃ = 2 rad/s t=$(round(time_s[k],digits=1)) s")
plot!(channel1; c=:gray60, linecolor=:gray60, label= false)
plot!(channel2; c=:gray60, linecolor=:gray60, label= false)
piston1_shape = rectangle_shape(x1[k], y1[k], channel1_direction...)
piston2_shape = rectangle_shape(x2[k], y2[k], channel2_direction...)
plot!(piston1_shape; c=:dodgerblue, linecolor=:navy,
label = (k == 1 ? "Piston 1" : "Piston 1"))
plot!(piston2_shape; c=:tomato, linecolor=:maroon,
label = (k == 1 ? "Piston 2" : "Piston 2"))
plot!([x1[k], x2[k]], [y1[k], y2[k]]; lw=4, c=:black,
label = (k == 1 ? "Connecting bar" : "Connector Bar"))
end
gif(anim3, "mechanism_w_geometry.gif"; fps= 45)
end
# ╔═╡ 00000000-0000-0000-0000-000000000001
PLUTO_PROJECT_TOML_CONTENTS = """
[deps]
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
[compat]
Plots = "~1.41.4"
"""
# ╔═╡ 00000000-0000-0000-0000-000000000002
PLUTO_MANIFEST_TOML_CONTENTS = """
# This file is machine-generated - editing it directly is not advised
julia_version = "1.12.5"
manifest_format = "2.0"
project_hash = "1767cefe1e3f3a0c2359842695780e9f15740bc6"
[[deps.AliasTables]]
deps = ["PtrArrays", "Random"]
git-tree-sha1 = "9876e1e164b144ca45e9e3198d0b689cadfed9ff"
uuid = "66dad0bd-aa9a-41b7-9441-69ab47430ed8"
version = "1.1.3"
[[deps.ArgTools]]
uuid = "0dad84c5-d112-42e6-8d28-ef12dabb789f"
version = "1.1.2"
[[deps.Artifacts]]
uuid = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
version = "1.11.0"
[[deps.Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
version = "1.11.0"
[[deps.BitFlags]]
git-tree-sha1 = "0691e34b3bb8be9307330f88d1a3c3f25466c24d"
uuid = "d1d4a3ce-64b1-5f1a-9ba4-7e7e69966f35"
version = "0.1.9"
[[deps.Bzip2_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "1b96ea4a01afe0ea4090c5c8039690672dd13f2e"
uuid = "6e34b625-4abd-537c-b88f-471c36dfa7a0"
version = "1.0.9+0"
[[deps.Cairo_jll]]
deps = ["Artifacts", "Bzip2_jll", "CompilerSupportLibraries_jll", "Fontconfig_jll", "FreeType2_jll", "Glib_jll", "JLLWrappers", "LZO_jll", "Libdl", "Pixman_jll", "Xorg_libXext_jll", "Xorg_libXrender_jll", "Zlib_jll", "libpng_jll"]
git-tree-sha1 = "a21c5464519504e41e0cbc91f0188e8ca23d7440"
uuid = "83423d85-b0ee-5818-9007-b63ccbeb887a"
version = "1.18.5+1"
[[deps.CodecZlib]]
deps = ["TranscodingStreams", "Zlib_jll"]
git-tree-sha1 = "962834c22b66e32aa10f7611c08c8ca4e20749a9"
uuid = "944b1d66-785c-5afd-91f1-9de20f533193"
version = "0.7.8"
[[deps.ColorSchemes]]
deps = ["ColorTypes", "ColorVectorSpace", "Colors", "FixedPointNumbers", "PrecompileTools", "Random"]
git-tree-sha1 = "b0fd3f56fa442f81e0a47815c92245acfaaa4e34"
uuid = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
version = "3.31.0"
[[deps.ColorTypes]]
deps = ["FixedPointNumbers", "Random"]
git-tree-sha1 = "67e11ee83a43eb71ddc950302c53bf33f0690dfe"
uuid = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
version = "0.12.1"
weakdeps = ["StyledStrings"]
[deps.ColorTypes.extensions]
StyledStringsExt = "StyledStrings"
[[deps.ColorVectorSpace]]
deps = ["ColorTypes", "FixedPointNumbers", "LinearAlgebra", "Requires", "Statistics", "TensorCore"]
git-tree-sha1 = "8b3b6f87ce8f65a2b4f857528fd8d70086cd72b1"
uuid = "c3611d14-8923-5661-9e6a-0046d554d3a4"
version = "0.11.0"
[deps.ColorVectorSpace.extensions]
SpecialFunctionsExt = "SpecialFunctions"
[deps.ColorVectorSpace.weakdeps]
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
[[deps.Colors]]
deps = ["ColorTypes", "FixedPointNumbers", "Reexport"]
git-tree-sha1 = "37ea44092930b1811e666c3bc38065d7d87fcc74"
uuid = "5ae59095-9a9b-59fe-a467-6f913c188581"
version = "0.13.1"
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.3.0+1"
[[deps.ConcurrentUtilities]]
deps = ["Serialization", "Sockets"]
git-tree-sha1 = "21d088c496ea22914fe80906eb5bce65755e5ec8"
uuid = "f0e56b4a-5159-44fe-b623-3e5288b988bb"
version = "2.5.1"
[[deps.Contour]]
git-tree-sha1 = "439e35b0b36e2e5881738abc8857bd92ad6ff9a8"
uuid = "d38c429a-6771-53c6-b99e-75d170b6e991"
version = "0.6.3"
[[deps.DataAPI]]
git-tree-sha1 = "abe83f3a2f1b857aac70ef8b269080af17764bbe"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.16.0"
[[deps.DataStructures]]
deps = ["OrderedCollections"]
git-tree-sha1 = "e357641bb3e0638d353c4b29ea0e40ea644066a6"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.19.3"
[[deps.Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"
version = "1.11.0"
[[deps.Dbus_jll]]
deps = ["Artifacts", "Expat_jll", "JLLWrappers", "Libdl"]
git-tree-sha1 = "473e9afc9cf30814eb67ffa5f2db7df82c3ad9fd"
uuid = "ee1fde0b-3d02-5ea6-8484-8dfef6360eab"
version = "1.16.2+0"
[[deps.DelimitedFiles]]
deps = ["Mmap"]
git-tree-sha1 = "9e2f36d3c96a820c678f2f1f1782582fcf685bae"
uuid = "8bb1440f-4735-579b-a4ab-409b98df4dab"
version = "1.9.1"
[[deps.DocStringExtensions]]
git-tree-sha1 = "7442a5dfe1ebb773c29cc2962a8980f47221d76c"
uuid = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
version = "0.9.5"
[[deps.Downloads]]
deps = ["ArgTools", "FileWatching", "LibCURL", "NetworkOptions"]
uuid = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
version = "1.7.0"
[[deps.EpollShim_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "8a4be429317c42cfae6a7fc03c31bad1970c310d"
uuid = "2702e6a9-849d-5ed8-8c21-79e8b8f9ee43"
version = "0.0.20230411+1"
[[deps.ExceptionUnwrapping]]
deps = ["Test"]
git-tree-sha1 = "d36f682e590a83d63d1c7dbd287573764682d12a"
uuid = "460bff9d-24e4-43bc-9d9f-a8973cb893f4"
version = "0.1.11"
[[deps.Expat_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
git-tree-sha1 = "27af30de8b5445644e8ffe3bcb0d72049c089cf1"
uuid = "2e619515-83b5-522b-bb60-26c02a35a201"
version = "2.7.3+0"
[[deps.FFMPEG]]
deps = ["FFMPEG_jll"]
git-tree-sha1 = "95ecf07c2eea562b5adbd0696af6db62c0f52560"
uuid = "c87230d0-a227-11e9-1b43-d7ebe4e7570a"
version = "0.4.5"
[[deps.FFMPEG_jll]]
deps = ["Artifacts", "Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "JLLWrappers", "LAME_jll", "Libdl", "Ogg_jll", "OpenSSL_jll", "Opus_jll", "PCRE2_jll", "Zlib_jll", "libaom_jll", "libass_jll", "libfdk_aac_jll", "libva_jll", "libvorbis_jll", "x264_jll", "x265_jll"]
git-tree-sha1 = "01ba9d15e9eae375dc1eb9589df76b3572acd3f2"
uuid = "b22a6f82-2f65-5046-a5b2-351ab43fb4e5"
version = "8.0.1+0"
[[deps.FileWatching]]