summaryrefslogtreecommitdiff
path: root/ao-tools/ao-fakeflight/ao-fakeflight.c
blob: 8a9925934961f5798f1ccc34cbe7a330c3a31ce7 (plain) (blame)
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
/*
 * Copyright © 2014 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "ao-fakeflight.h"

char *state_name[] = {
	[state_ascent] = "ascent",
	[state_drogue] = "drogue",
	[state_main] = "main",
	[state_landed] = "landed",
};

struct rocket o5778 = {
	.models = {
		[state_ascent] = {
			.cd = 0.5,
			.diameter = .100
		},
		[state_drogue] = {
			.cd = 1.3,
			.diameter = .300
		},
		[state_main] = {
			.cd = 1.3,
			.diameter = 1.0
		},
	},
	.empty_mass = 15.0,
	.main_deploy = 250,
	.motors = {
		[0] = {
			.fuel_mass = 18.021,
			.average_thrust = 5778.0,
			.burn_time = 8.52,
			.delay = 0.0,
		},
		[1] = {
			.fuel_mass = 18.021,
			.average_thrust = 5778.0,
			.burn_time = 0, /* 17.04 */
			.delay = 18.0,
		},
	},
};

struct rocket n5778 = {
	.models = {
		[state_ascent] = {
			.cd = 0.5,
			.diameter = .100
		},
		[state_drogue] = {
			.cd = 1.3,
			.diameter = .300
		},
		[state_main] = {
			.cd = 1.3,
			.diameter = 1.0
		},
	},
	.empty_mass = 15.0,
	.main_deploy = 250,
	.motors = {
		[0] = {
			.fuel_mass = 9.0105,
			.average_thrust = 5778.0,
			.burn_time = 4.26,
			.delay = 0.0,
		},
	},
};

/*
 * Move the flight one tick forward
 */
void
step(struct flight *f, struct rocket *r)
{
	double	drag = rocket_drag(f, r);
	double	thrust = rocket_thrust(f, r);
	double	gravity = rocket_gravity(f, r);
	double	mass = rocket_mass(f, r);
	double	accel;

	/* This is our acceleration relative to the earth */
	accel = (drag + thrust - gravity) / mass;

	/* Because the accelerometer is also affected by gravity, it
	 * only measures *other* forces applied to the rocket
	 */
	f->accel = (drag + thrust) / mass;

	f->altitude += f->speed * STEP + 0.5 * (accel * STEP) * (accel * STEP);
	f->speed += accel * STEP;
	f->time += STEP;
}

void
init(struct flight *f, struct rocket *r)
{
	double gravity = rocket_gravity(f, r);

	/* The pad is pushing up on the rocket to counter
	 * gravity, so the accelerometer, which only measures
	 * forces other than gravity, sees only this
	 */
	f->accel = gravity / rocket_mass(f, r);
	f->speed = 0;
	f->altitude = 0;
	f->time += STEP;
}

void
fly(FILE *file, struct rocket *r)
{
	struct flight f = {
		.time = -1,
		.altitude = 0,
		.speed = 0,
		.accel = 0,
		.state = state_ascent,
	};

	double	max_speed = 0;
	double	max_alt = 0;
	double	max_accel = 0;


	ao_show_header(file, r);
	while (f.time < 0) {
		init(&f, r);
		ao_log(file, &f, r);
	}
	while (f.altitude >= 0) {
		step(&f, r);
		switch (f.state) {
		case state_ascent:
			if (f.speed >= 0)
				break;
			f.state = state_drogue;
			/* fall through */
		case state_drogue:
			if (f.altitude > o5778.main_deploy)
				break;
			f.state = state_main;
			/* fall through */
		case state_main:
			break;
		}
		ao_log(file, &f, r);
		max_speed = fmax(max_speed, f.speed);
		max_accel = fmax(max_accel, f.accel);
		max_alt = fmax(max_alt, f.altitude);
	}
	fprintf(stderr, "max alt %9.1f speed %9.1f accel %9.1f\n",
		max_alt, max_speed, max_accel);
}

int
main(int argc, char **argv)
{
	fly(stdout, &o5778);
//	fly(stdout, &n5778);
	return 0;
}