1 /*
2 * Copyright (c) 2019 Kevin Townsend
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <stdio.h>
8 #include <math.h>
9 #include <errno.h>
10 #include <zephyr/kernel.h>
11 #include <zephyr/kernel.h>
12 #include <zsl/zsl.h>
13 #include <zsl/physics/projectiles.h>
14
projectiles_demo(void)15 void projectiles_demo(void)
16 {
17 printf("Physics projectile API demo\n");
18 printf("---------------------------\n\n");
19
20 /* Initial values: */
21 zsl_real_t vi = 12.4; /* Initial velocity. */
22 zsl_real_t theta = 1.1; /* Angle. */
23 zsl_real_t xi = 2.2; /* Initial horizontal position. */
24 zsl_real_t yi = 15; /* Initial height. */
25
26 printf("A rock is lauched from a %.2f meter tall building with\n", yi);
27 printf("a velocity of %f and an angle of %f. Consider\n", vi, theta);
28 printf("the origin x coordinate to be %f meters behind the\n", xi);
29 printf("launching point.\n\n");
30
31 printf("(a) Decompose the initial velocity into its components.\n");
32
33 zsl_real_t vih, viv;
34
35 zsl_phy_proj_init_vel(vi, theta, &vih, &viv);
36
37 printf("The horizontal component is: %f.\n", vih);
38 printf("The vertical component is: %f.\n\n", viv);
39
40 printf("(b) Calculate the time it takes the rock to reach a height\n");
41 printf("of 20 meters on its ascending phase.\n\n");
42
43 zsl_real_t t;
44
45 zsl_phy_proj_time_first(viv, yi, 20.0, &t);
46
47 printf("It takes the rock %f seconds to reach 20 meters.\n\n", t);
48
49 printf("(c) How much time does it take the rock to reach\n");
50 printf("the ground?\n\n");
51
52 zsl_phy_proj_time(viv, yi, 0.0, &t);
53
54 printf("It takes the rock %f seconds to reach the ground.\n\n", t);
55
56 printf("(d) How much time has elapsed when it reaches the\n");
57 printf("maximum height?\n\n");
58
59 /* At maximum height the vertical velocity is zero: */
60 zsl_phy_proj_time2(viv, 0.0, &t);
61
62 printf("It takes the rock %f seconds to reach the peak height.\n\n", t);
63
64 printf("(e) Calculate its position and total velocity 2 seconds\n");
65 printf("after being launched. What's the angle between its velocity\n");
66 printf("and the ground at that instant?\n\n");
67
68 zsl_real_t xf, yf, vfv, vf, thetaf;
69
70 /* Calculate the position (xf, yf) at t = 2 seconds. */
71 zsl_phy_proj_hor_motion(vih, 2.0, xi, &xf);
72 zsl_phy_proj_ver_motion(viv, 2.0, yi, &yf);
73
74 /* Calculate the vertical velocity at t = 2 seconds. */
75 zsl_phy_proj_ver_vel(viv, 2.0, &vfv);
76
77 /* Calculate the total velocity at t = 2 seconds. */
78 zsl_phy_proj_vel(vih, vfv, &vf);
79
80 /* Calculate the angle at t = 2 seconds. */
81 zsl_phy_proj_angle(vih, vfv, &thetaf);
82
83 printf("The position is (%f, %f) meters. The total\n", xf, yf);
84 printf("velocity is %f meters per second and the angle is\n", vf);
85 printf("%f radians.\n\n", thetaf);
86
87 printf("(f) When the rock is 13.2 meters from the launch position,\n");
88 printf("what is its height?\n\n");
89
90 /* 13.2 meters from the point of launch means 13.2 + 2.2 = 15.4 meters
91 * from the origin. */
92 zsl_phy_proj_trajectory(vih, viv, xi, yi, 15.4, &yf);
93
94 printf("The height is %f meters.\n\n", yf);
95
96 printf("(g) What is the total horizontal distance it travels?\n\n");
97
98 zsl_real_t dist;
99
100 zsl_phy_proj_range(vih, viv, xi, yi, &dist);
101
102 printf("The total range is %f meters.\n\n", dist);
103 }
104
main(void)105 void main(void)
106 {
107 printf("\n\nzscilib projectiles demo\n\n");
108
109 projectiles_demo();
110
111 }
112