1 /*
2  * Copyright (c) 2019-2020 Kevin Townsend (KTOWN)
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @addtogroup KINEMATICS Kinematics
9  *
10  * @brief Kinematics functions.
11  *
12  * @ingroup PHYSICS
13  *  @{ */
14 
15 /**
16  * @file
17  * @brief API header file for kinematics in zscilib.
18  *
19  * This file contains the zscilib kinematics APIs
20  */
21 
22 #ifndef ZEPHYR_INCLUDE_ZSL_KINEMATICS_H_
23 #define ZEPHYR_INCLUDE_ZSL_KINEMATICS_H_
24 
25 #include <zsl/zsl.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /**
32  * @brief Calculates the change in distance based on initial velocity,
33  *        time and acceleration.
34  *
35  * @param vi    Initial velocity in meters per second.
36  * @param time  Time in seconds.
37  * @param accel Acceleration in meters per second squared.
38  * @param dist  Pointer to the placeholder for calculated distance in meters.
39  *              Will be set to NAN if time is a negative value.
40  *
41  * @return 0 on success, and non-zero error code on failure.
42  */
43 int zsl_phy_kin_dist(zsl_real_t vi, zsl_real_t time, zsl_real_t accel,
44 		     zsl_real_t *dist);
45 
46 /**
47  * @brief Calculates the initial position of a moving body based on the final
48  *        position (xf), the initial velocity (vi), acceleration (a) and
49  *        time (t).
50  *
51  * @param vi    Initial velocity in meters per second.
52  * @param t     Time in seconds.
53  * @param a     Acceleration in meters per second squared.
54  * @param xf    Final position in meters.
55  * @param xi    Pointer to the placeholder for the initial position in meters.
56  *              Will be set to NAN if time is a negative value.
57  *
58  * @return 0 on success, and non-zero error code on failure.
59  */
60 int zsl_phy_kin_init_pos(zsl_real_t vi, zsl_real_t t, zsl_real_t a,
61 			 zsl_real_t xf, zsl_real_t *xi);
62 
63 /**
64  * @brief Calculates the initial position of a moving body based on the final
65  *        position (xf), the initial velocity (vi), the final velocity (vf) and
66  *        acceleration (a).
67  *
68  * @param vi    Initial velocity in meters per second.
69  * @param vf    Final velocity in meters per second.
70  * @param a     Acceleration in meters per second squared.
71  * @param xf    Final position in meters.
72  * @param xi    Pointer to the placeholder for the initial position in meters.
73  *              Will be set to NAN if the acceleration's value is zero.
74  *
75  * @return 0 on success, and non-zero error code on failure.
76  */
77 int zsl_phy_kin_init_pos2(zsl_real_t vi, zsl_real_t vf, zsl_real_t a,
78 			  zsl_real_t xf, zsl_real_t *xi);
79 
80 /**
81  * @brief Calculates the time in seconds that it takes an object to change its
82  *        velocity from vi to vf under a constant acceleration (accel).
83  *
84  * @param vi    Initial velocity in meters per second.
85  * @param vf    Final velocity in meters per second.
86  * @param accel Acceleration in meters per second squared.
87  * @param time  Pointer to the placeholder for calculated time in seconds. Will
88  *              be set to NAN if the time is negative or if the acceleration
89  *              is zero.
90  *
91  * @return 0 on success, and non-zero error code on failure.
92  */
93 int zsl_phy_kin_time(zsl_real_t vi, zsl_real_t vf, zsl_real_t accel,
94 		     zsl_real_t *time);
95 
96 /**
97  * @brief Calculates the velocity in meters per second of an object under a
98  *        constant acceleration (accel) based on its initial velocity (vi) and
99  *        the time passed since it started moving (time).
100  *
101  * @param vi    Initial velocity in meters per second.
102  * @param time  Time in seconds.
103  * @param accel Acceleration in meters per second squared.
104  * @param vf    Pointer to the output velocity in meters per second. Will
105  *              be set to NAN if the time is negative.
106  *
107  * @return 0 on success, and non-zero error code on failure.
108  */
109 int zsl_phy_kin_vel(zsl_real_t vi, zsl_real_t time, zsl_real_t accel,
110 		    zsl_real_t *vf);
111 
112 /**
113  * @brief Calculates the velocity in meters per second of an object under a
114  *        constant acceleration (accel) based on its initial velocity (vi) and
115  *        the distance traveled from the starting point (dist).
116  *
117  * @param vi    Initial velocity in meters per second.
118  * @param dist  Distance in meters.
119  * @param accel Acceleration in meters per second squared.
120  * @param vf    Pointer to the output velocity in meters per second. Will
121  *              be set to NAN if the velocity takes a complex value.
122  *
123  * @return 0 on success, and non-zero error code on failure.
124  */
125 int zsl_phy_kin_vel2(zsl_real_t vi, zsl_real_t dist, zsl_real_t accel,
126 		     zsl_real_t *vf);
127 
128 /**
129  * @brief Calculates the initial velocity in meters per second of an object
130  *        under a constant acceleration (a) based on its final velocity (vf)
131  *        and time (t).
132  *
133  * @param vf    Final velocity in meters per second.
134  * @param a     Acceleration in meters per second squared.
135  * @param t     Time in seconds.
136  * @param vi    Pointer to the output initial velocity in meters per second.
137  *              Will be set to NAN if the time is negative.
138  *
139  * @return 0 on success, and non-zero error code on failure.
140  */
141 int zsl_phy_kin_init_vel(zsl_real_t vf, zsl_real_t a, zsl_real_t t,
142 			 zsl_real_t *vi);
143 
144 /**
145  * @brief Calculates the initial velocity in meters per second of an object
146  *        under a constant acceleration (a) based on the distance it has
147  *        travelled (dist) and time (t).
148  *
149  * @param dist  Distance in meters.
150  * @param a     Acceleration in meters per second squared.
151  * @param t     Time in seconds.
152  * @param vi    Pointer to the output initial velocity in meters per second.
153  *              Will be set to NAN if the time is negative or zero.
154  *
155  * @return 0 on success, and non-zero error code on failure.
156  */
157 int zsl_phy_kin_init_vel2(zsl_real_t dist, zsl_real_t a, zsl_real_t t,
158 			  zsl_real_t *vi);
159 
160 /**
161  * @brief Calculates the initial velocity in meters per second of an object
162  *        under a constant acceleration (a) based on the distance it has
163  *        travelled (dist) and the final velocity (vf).
164  *
165  * @param vf    Final velocity in meters per second.
166  * @param a     Acceleration in meters per second squared.
167  * @param dist  Distance in meters.
168  * @param vi    Pointer to the output initial velocity in meters per second.
169  *              Will be set to NAN if the output velocity takes a complex value.
170  *
171  * @return 0 on success, and non-zero error code on failure.
172  */
173 int zsl_phy_kin_init_vel3(zsl_real_t vf, zsl_real_t a, zsl_real_t dist,
174 		          zsl_real_t *vi);
175 
176 /**
177  * @brief Calculates the average velocity in meters per second of a moving
178  *        object based on the time and distance.
179  *
180  * @param dist  Distance in meters.
181  * @param time  Time in seconds.
182  * @param vel   Pointer to the output velocity in meters per second. Will
183  *              be set to NAN if the time is negative or zero.
184  *
185  * @return 0 on success, and non-zero error code on failure.
186  */
187 int zsl_phy_kin_av_vel(zsl_real_t dist, zsl_real_t time, zsl_real_t *vel);
188 
189 /**
190  * @brief Calculates the acceleration of a moving body in meters per second
191  *        squared based on its initial (vi) and final (vf) velocities and time.
192  *
193  * @param vi    Initial velocity in meters per second.
194  * @param vf    Final velocity in meters per second.
195  * @param time  Time in seconds.
196  * @param accel Pointer to the output acceleration in meters per second
197  *              squared. Will be set to NAN if the time is negative or zero.
198  *
199  * @return 0 on success, and non-zero error code on failure.
200  */
201 int zsl_phy_kin_accel(zsl_real_t vi, zsl_real_t vf, zsl_real_t time,
202 		      zsl_real_t *accel);
203 
204 /**
205  * @brief Calculates the kinetic energy in joules of a moving body based on its
206  *        velocity (v) and mass (m).
207  *
208  * @param v     Velocity of the object in meters per second.
209  * @param m     Mass of the object in kilograms.
210  * @param ke    Pointer to the output kinetic energy in joules. Will be set to
211  *              NAN if the mass is negative.
212  *
213  * @return 0 on success, and non-zero error code on failure.
214  */
215 int zsl_phy_kin_ener(zsl_real_t v, zsl_real_t m, zsl_real_t *ke);
216 
217 #ifdef __cplusplus
218 }
219 #endif
220 
221 #endif /* ZEPHYR_INCLUDE_ZSL_KINEMATICS_H_ */
222 
223 /** @} */ /* End of kinematics group */
224