summaryrefslogtreecommitdiff
path: root/ao-tools/lib/cc-process.c
blob: f2307a8207818436391dd6560f18699424d4aba5 (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
/*
 * Copyright © 2009 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 "cc.h"
#include <stdlib.h>
#include <math.h>
#include <string.h>

static void
cook_timed(struct cc_timedata *td, struct cc_perioddata *pd,
	   double start_time, double stop_time,
	   double omega_pass, double omega_stop, double error)
{
	struct cc_perioddata	*unfiltered, *filtered;

	unfiltered = cc_period_make(td, start_time, stop_time);
	filtered = cc_period_low_pass (unfiltered, omega_pass, omega_stop, error);
	*pd = *filtered;
	free (filtered);
	free (unfiltered->data);
	free (unfiltered);
}

static double
barometer_to_altitude(double b, double pad_alt)
{
	return cc_barometer_to_altitude(b) - pad_alt;
}

struct cc_flightcooked *
cc_flight_cook(struct cc_flightraw *raw)
{
	struct cc_flightcooked *cooked;
	double			flight_start = 0;
	double			flight_stop = 0;
	int			start_set = 0;
	int			stop_set = 0;
	int			i;
	struct cc_timedata	*accel;
	struct cc_timedata	*accel_speed;
	struct cc_timedata	*accel_pos;
	struct cc_timedata	*pres;
	struct cc_perioddata	*pres_speed;
	struct cc_perioddata	*pres_accel;

	if (raw->accel.num == 0)
		return NULL;

	cooked = calloc (1, sizeof (struct cc_flightcooked));

	/*
	 * Find flight start and stop times by looking at
	 * state transitions. The stop time is set to the time
	 * of landing, which may be long after it landed (due to radio
	 * issues). Refine this value by looking through the sensor data
	 */
	for (i = 0; i < raw->state.num; i++) {
		if (!start_set && raw->state.data[i].value > ao_flight_pad) {
			flight_start = raw->state.data[i].time - 10;
			start_set = 1;
		}
		if (!stop_set && raw->state.data[i].value > ao_flight_main) {
			flight_stop = raw->state.data[i].time;
			stop_set = 1;
		}
	}

	if (!start_set || flight_start < raw->accel.data[0].time)
		flight_start = raw->accel.data[0].time;
	if (stop_set) {
		for (i = 0; i < raw->accel.num - 1; i++) {
			if (raw->accel.data[i+1].time >= flight_stop) {
				flight_stop = raw->accel.data[i].time;
				break;
			}
		}
	} else {
		flight_stop = raw->accel.data[raw->accel.num-1].time;
	}
	cooked->flight_start = flight_start;
	cooked->flight_stop = flight_stop;

	/* Integrate the accelerometer data to get speed and position */
	accel = cc_timedata_convert(&raw->accel, cc_accelerometer_to_acceleration, raw->ground_accel);
	cooked->accel = *accel;
	free(accel);
	accel_speed = cc_timedata_integrate(&cooked->accel, flight_start - 10, flight_stop);
	accel_pos = cc_timedata_integrate(accel_speed, flight_start - 10, flight_stop);

#define ACCEL_OMEGA_PASS	(2 * M_PI * 20 / 100)
#define ACCEL_OMEGA_STOP	(2 * M_PI * 30 / 100)
#define BARO_OMEGA_PASS		(2 * M_PI * .5 / 100)
#define BARO_OMEGA_STOP		(2 * M_PI * 1 / 100)
#define FILTER_ERROR		(1e-8)

	cook_timed(&cooked->accel, &cooked->accel_accel,
		   flight_start, flight_stop,
		   ACCEL_OMEGA_PASS, ACCEL_OMEGA_STOP, FILTER_ERROR);
	cook_timed(accel_speed, &cooked->accel_speed,
		   flight_start, flight_stop,
		   ACCEL_OMEGA_PASS, ACCEL_OMEGA_STOP, FILTER_ERROR);
	free(accel_speed->data); free(accel_speed);
	cook_timed(accel_pos, &cooked->accel_pos,
		   flight_start, flight_stop,
		   ACCEL_OMEGA_PASS, ACCEL_OMEGA_STOP, FILTER_ERROR);
	free(accel_pos->data); free(accel_pos);

	/* Filter the pressure data */
	pres = cc_timedata_convert(&raw->pres, barometer_to_altitude,
				   cc_barometer_to_altitude(raw->ground_pres));
	cooked->pres = *pres;
	free(pres);
	cook_timed(&cooked->pres, &cooked->pres_pos,
		   flight_start, flight_stop,
		   BARO_OMEGA_PASS, BARO_OMEGA_STOP, FILTER_ERROR);
	/* differentiate twice to get to acceleration */
	pres_speed = cc_perioddata_differentiate(&cooked->pres_pos);
	pres_accel = cc_perioddata_differentiate(pres_speed);

	cooked->pres_speed = *pres_speed;
	free(pres_speed);
	cooked->pres_accel = *pres_accel;
	free(pres_accel);

	/* copy state */
	cooked->state.num = raw->state.num;
	cooked->state.size = raw->state.num;
	cooked->state.data = calloc(cooked->state.num, sizeof (struct cc_timedataelt));
	memcpy(cooked->state.data, raw->state.data, cooked->state.num * sizeof (struct cc_timedataelt));
	cooked->state.time_offset = raw->state.time_offset;
	return cooked;
}

#define if_free(x)	((x) ? free(x) : (void) 0)

void
cc_flightcooked_free(struct cc_flightcooked *cooked)
{
	if_free(cooked->accel_accel.data);
	if_free(cooked->accel_speed.data);
	if_free(cooked->accel_pos.data);
	if_free(cooked->pres_pos.data);
	if_free(cooked->pres_speed.data);
	if_free(cooked->pres_accel.data);
	if_free(cooked->gps_lat.data);
	if_free(cooked->gps_lon.data);
	if_free(cooked->gps_alt.data);
	if_free(cooked->state.data);
	if_free(cooked->accel.data);
	if_free(cooked->pres.data);
	free(cooked);
}