summaryrefslogtreecommitdiff
path: root/ao-tools/altosui/AltosGraphUI.java
blob: d945c333cffa870e62e313f21595d1cea82d6df4 (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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301

// Copyright (c) 2010 Anthony Towns
// GPL v2 or later

package altosui;

import java.io.*;
import java.util.ArrayList;

import javax.swing.JFrame;
import java.awt.Color;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import altosui.AltosCsvReader;
import altosui.AltosDataPoint;
import altosui.AltosGraphTime;

public class AltosGraphUI extends JFrame 
{
    static final private Color red = new Color(194,31,31);
    static final private Color green = new Color(31,194,31);
    static final private Color blue = new Color(31,31,194);
    static final private Color black = new Color(31,31,31);

    static private class OverallGraphs {
        AltosGraphTime.Element height = 
            new AltosGraphTime.TimeSeries("Height (m)", "Height (AGL)", red) {
                public void gotTimeData(double time, AltosDataPoint d) {
                    series.add(time, d.height()); 
                } 
            };
    
        AltosGraphTime.Element speed =
            new AltosGraphTime.TimeSeries("Speed (m/s)", "Vertical Speed", green) { 
                public void gotTimeData(double time, AltosDataPoint d) {
                    if (d.state() < Altos.ao_flight_drogue) {
                        series.add(time, d.accel_speed());
                    } else {
                        series.add(time, d.baro_speed());
                    }
                }
            };
    
        AltosGraphTime.Element acceleration =
            new AltosGraphTime.TimeSeries("Acceleration (m/s\u00B2)", 
                    "Axial Acceleration", blue) 
            {
                public void gotTimeData(double time, AltosDataPoint d) {
                    series.add(time, d.acceleration());
                }
            };
    
        AltosGraphTime.Element temperature =
            new AltosGraphTime.TimeSeries("Temperature (\u00B0C)", 
                    "Board temperature", red) 
            {
                public void gotTimeData(double time, AltosDataPoint d) {
                    series.add(time, d.temperature());
                }
            };
    
        AltosGraphTime.Element drogue_voltage =
            new AltosGraphTime.TimeSeries("Voltage (V)", "Drogue Continuity", blue) 
            {
                public void gotTimeData(double time, AltosDataPoint d) {
                    series.add(time, d.drogue_voltage());
                }
            };
    
        AltosGraphTime.Element main_voltage =
            new AltosGraphTime.TimeSeries("Voltage (V)", "Main Continuity", green) 
            {
                public void gotTimeData(double time, AltosDataPoint d) {
                    series.add(time, d.main_voltage());
                }
            };
    
        AltosGraphTime.Element e_pad    = new AltosGraphTime.StateMarker(Altos.ao_flight_pad, "Pad");
        AltosGraphTime.Element e_boost  = new AltosGraphTime.StateMarker(Altos.ao_flight_boost, "Boost");
        AltosGraphTime.Element e_fast   = new AltosGraphTime.StateMarker(Altos.ao_flight_fast, "Fast");
        AltosGraphTime.Element e_coast  = new AltosGraphTime.StateMarker(Altos.ao_flight_coast, "Coast");
	AltosGraphTime.Element e_drogue = new AltosGraphTime.StateMarker(Altos.ao_flight_drogue, "Drogue");
	AltosGraphTime.Element e_main   = new AltosGraphTime.StateMarker(Altos.ao_flight_main, "Main");
        AltosGraphTime.Element e_landed = new AltosGraphTime.StateMarker(Altos.ao_flight_landed, "Landed");
    
        protected AltosGraphTime myAltosGraphTime(String suffix) {
            return (new AltosGraphTime("Overall " + suffix))
                .addElement(e_boost)
                .addElement(e_drogue)
                .addElement(e_main)
                .addElement(e_landed);
        }
    
        public ArrayList<AltosGraph> graphs() {
            ArrayList<AltosGraph> graphs = new ArrayList<AltosGraph>();
    
            graphs.add( myAltosGraphTime("Summary")
                    .addElement(height)
                    .addElement(speed)
                    .addElement(acceleration) );
    
            graphs.add( myAltosGraphTime("Altitude")
                    .addElement(height) );
    
            graphs.add( myAltosGraphTime("Speed")
                    .addElement(speed) );
    
            graphs.add( myAltosGraphTime("Acceleration")
                    .addElement(acceleration) );
    
            graphs.add( myAltosGraphTime("Temperature")
                    .addElement(temperature) );
    
            graphs.add( myAltosGraphTime("Continuity")
                    .addElement(drogue_voltage)
                    .addElement(main_voltage) );
    
            return graphs;
        }
    }
    
    static private class AscentGraphs extends OverallGraphs {
        protected AltosGraphTime myAltosGraphTime(String suffix) {
            return (new AltosGraphTime("Ascent " + suffix) {
                public void addData(AltosDataPoint d) {
                    int state = d.state();
                    if (Altos.ao_flight_boost <= state && state <= Altos.ao_flight_coast) {
                        super.addData(d);
                    }
                }
            }).addElement(e_boost)
              .addElement(e_fast)
              .addElement(e_coast);
        }
    }
    
    static private class DescentGraphs extends OverallGraphs {
        protected AltosGraphTime myAltosGraphTime(String suffix) {
            return (new AltosGraphTime("Descent " + suffix) {
                public void addData(AltosDataPoint d) {
                    int state = d.state();
                    if (Altos.ao_flight_drogue <= state && state <= Altos.ao_flight_main) {
                        super.addData(d);
                    }
                }
            }).addElement(e_drogue)
              .addElement(e_main);
            // ((XYGraph)graph[8]).ymin = new Double(-50);
        }
    }

    public AltosGraphUI(JFrame frame)
    {
        super("Altos Graph");

        AltosGraphDataChooser chooser;
        chooser = new AltosGraphDataChooser(frame);
        Iterable<AltosDataPoint> reader = chooser.runDialog();
        if (reader == null)
            return;
        
        init(reader, 0);
    }

    public AltosGraphUI(Iterable<AltosDataPoint> data, int which) 
    {
        super("Altos Graph");
        init(data, which);
    }

    private void init(Iterable<AltosDataPoint> data, int which) {
        AltosGraph graph = createGraph(data, which);

        JFreeChart chart = graph.createChart();
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setMouseWheelEnabled(true);
        chartPanel.setPreferredSize(new java.awt.Dimension(800, 500));
        setContentPane(chartPanel);

        pack();

        RefineryUtilities.centerFrameOnScreen(this);

        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setVisible(true);
    }

    private static AltosGraph createGraph(Iterable<AltosDataPoint> data,
            int which)
    {
        return createGraphsWhich(data, which).get(0);
    }

    private static ArrayList<AltosGraph> createGraphs(
            Iterable<AltosDataPoint> data)
    {
        return createGraphsWhich(data, -1);
    }

    private static ArrayList<AltosGraph> createGraphsWhich(
            Iterable<AltosDataPoint> data, int which)
    {
        ArrayList<AltosGraph> graph = new ArrayList<AltosGraph>();
        graph.addAll((new OverallGraphs()).graphs());
        graph.addAll((new AscentGraphs()).graphs());
        graph.addAll((new DescentGraphs()).graphs());

        if (which > 0) {
            if (which >= graph.size()) {
                which = 0;
            }
            AltosGraph g = graph.get(which);
            graph = new ArrayList<AltosGraph>();
            graph.add(g);
        }

        for (AltosDataPoint dp : data) {
            for (AltosGraph g : graph) {
                g.addData(dp);
            }
        }

        return graph;
    }

    public static void main(String[] args) 
        throws java.io.FileNotFoundException, java.io.IOException 
    {
        if (args.length < 1 || 2 < args.length)
        {
            System.out.println("Please specify telemetry csv");
            return;
        }

        AltosCsvReader csv = new AltosCsvReader(args[0]);
        if (args.length == 1) {
            for (AltosGraph g : createGraphs(csv)) {
                g.toPNG();
            }
        } else {
            int which = Integer.parseInt(args[1].trim());
            AltosGraphUI demo = new AltosGraphUI(csv, which);
        }
    }
}

/* gnuplot bits...
 *
300x400

--------------------------------------------------------
TOO HARD! :)

"ascent-gps-accuracy.png" "Vertical error margin to apogee - GPS v Baro (m)"
    5:($7 < 6 ? $24-$11 : 1/0)
"descent-gps-accuracy.png" "Vertical error margin during descent - GPS v Baro (m)"
    5:($7 < 6 ? 1/0 : $24-$11)

set output "overall-gps-accuracy.png"
set ylabel "distance above sea level (m)"
plot "telemetry.csv" using 5:11 with lines ti "baro altitude" axis x1y1, \
    "telemetry.csv" using 5:24 with lines ti "gps altitude" axis x1y1

set term png tiny size 700,700 enhanced
set xlabel "m"
set ylabel "m"
set polar
set grid polar
set rrange[*:*]
set angles degrees

set output "overall-gps-path.png"
#:30 with yerrorlines
plot "telemetry.csv" using (90-$33):($7 == 2 ? $31 : 1/0) with lines ti "pad", \
    "telemetry.csv" using (90-$33):($7 == 3 ? $31 : 1/0) with lines ti "boost", \
    "telemetry.csv" using (90-$33):($7 == 4 ? $31 : 1/0) with lines ti "fast", \
    "telemetry.csv" using (90-$33):($7 == 5 ? $31 : 1/0) with lines ti "coast", \
    "telemetry.csv" using (90-$33):($7 == 6 ? $31 : 1/0) with lines ti "drogue", \
    "telemetry.csv" using (90-$33):($7 == 7 ? $31 : 1/0) with lines ti "main", \
    "telemetry.csv" using (90-$33):($7 == 8 ? $31 : 1/0) with lines ti "landed"

set output "ascent-gps-path.png"
plot "telemetry.csv" using (90-$33):($7 == 2 ? $31 : 1/0):30 with lines ti "pad", \
    "telemetry.csv" using (90-$33):($7 == 3 ? $31 : 1/0):20 with lines ti "boost", \
    "telemetry.csv" using (90-$33):($7 == 4 ? $31 : 1/0):10 with lines ti "fast", \
    "telemetry.csv" using (90-$33):($7 == 5 ? $31 : 1/0):5 with lines ti "coast"

set output "descent-gps-path.png"
plot "telemetry.csv" using (90-$33):($7 == 6 ? $31 : 1/0) with lines ti "drogue", \
    "telemetry.csv" using (90-$33):($7 == 7 ? $31 : 1/0) with lines ti "main", \
    "telemetry.csv" using (90-$33):($7 == 8 ? $31 : 1/0) with lines ti "landed"

 */