ao-tools: Add ao-fakeflight
[fw/altos] / ao-tools / ao-fakeflight / ao-rocket.c
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao-fakeflight.h"
19
20 /* Mass (kg) of the airframe */
21 double
22 rocket_mass(struct flight *f, struct rocket *r)
23 {
24         double  time = f->time;
25         double  m = r->empty_mass;
26         int     n;
27
28         for (n = 0; n < MAX_MOTORS; n++) {
29                 struct motor *motor = &r->motors[n];
30                 if (motor->burn_time) {
31                         double fuel_time_remain = fmin (motor->burn_time, fmax(motor->burn_time - (time - motor->delay), 0.0));
32                         double fuel_fraction = fuel_time_remain / motor->burn_time;
33                         m += fuel_fraction * motor->fuel_mass;
34                 }
35         }
36         return m;
37 }
38
39 /* Force (N) due to thrust */
40 double
41 rocket_thrust(struct flight *f, struct rocket *r)
42 {
43         double  time = f->time;
44         double  thrust = 0.0;
45         int     n;
46
47         for (n = 0; n < MAX_MOTORS; n++) {
48                 struct motor *motor = &r->motors[n];
49                 if (motor->burn_time) {
50                         if (motor->delay <= time && time < motor->delay + motor->burn_time)
51                                 thrust += motor->average_thrust;
52                 }
53         }
54         return thrust;
55 }
56
57 /* Drag (N) due to air resistance */
58 double
59 rocket_drag(struct flight *f, struct rocket *r)
60 {
61         double  pressure = cc_altitude_to_pressure(f->altitude);
62         double  temperature = cc_altitude_to_temperature(f->altitude);
63         double  drag = force_drag(f->speed, density_air(pressure, temperature),
64                           r->models[f->state].cd,
65                           area_circle(r->models[f->state].diameter));
66
67         /* drag operates in opposition to motion through air */
68         if (f->speed >= 0)
69                 drag = -drag;
70
71         return drag;
72 }
73
74 /* Force (N) due to gravity */
75 double
76 rocket_gravity(struct flight *f, struct rocket *r)
77 {
78         double  gravity = force_gravity(rocket_mass (f, r), f->altitude);
79 }