]> git.mxchange.org Git - flightgear.git/blob - scripts/perl/examples/logging.pl
A bit more Makefile massaging.
[flightgear.git] / scripts / perl / examples / logging.pl
1 #!/usr/bin/perl
2 #
3 # logging.pl - Handle logging
4 #
5 # Written by Curtis L. Olson, started February 2004
6 #
7 # Copyright (C) 2004  Curtis L. Olson - curt@flightgear.org
8 #
9 # This code is placed in the public domain by Curtis L. Olson.
10 # There is no warranty, etc. etc. etc.
11 #
12 # $Id$
13 # ----------------------------------------------------------------------------
14
15
16 use strict;
17
18 require "telnet.pl";
19
20 my( $lognum ) = 1;
21
22 my( @FIELDS, @FIELDS_E );
23 my( $tmp_dir ) = "/tmp";
24
25 my( $field_index ) = 0;
26
27 # my( $page );
28 # my( $server, $port, $timeout );
29 # my( $last_plot_image ) = "";
30 # my( $image_handle );
31 # my( $junk );
32 # my( @PLOTFIELDS );
33 # my( $field_choice );
34 # my( $plot_field );
35
36 # my( $home ) = $ENV{"HOME"};
37 # my( $log_dir ) = "$home/ATC/data";
38 # my( $log_file ) = "$log_dir/default.txt";
39 # my( $plot_file ) = $log_file;
40
41
42 sub clear_logging {
43     my( $fgfs ) = shift;
44
45     my( $done ) = 0;
46     my( $i ) = 0;
47     my( $prop );
48     while ( !$done ) {
49         $prop = &get_prop( $fgfs, "/logging/log[$lognum]/entry[$i]/property" );
50         if ( $prop ne "" ) {
51             &set_prop( $fgfs,
52                        "/logging/log[$lognum]/entry[$i]/enabled",
53                        "false" );
54         } else {
55             $done = 1;
56         }
57         $i++;
58     }
59
60     $field_index = 0;
61 }
62
63
64 sub add_field {
65     my( $fgfs ) = shift;
66     my( $title ) = shift;
67     my( $prop ) = shift;
68
69     # spaces seem to not work well.
70     $title =~ s/ /\_/g;
71
72     print "$title - $prop\n";
73     &set_prop( $fgfs,
74                "/logging/log[$lognum]/entry[$field_index]/title", $title );
75     &set_prop( $fgfs,
76                "/logging/log[$lognum]/entry[$field_index]/property", $prop );
77     &set_prop( $fgfs,
78                "/logging/log[$lognum]/entry[$field_index]/enabled", "true" );
79
80     $field_index++;
81 }
82
83
84 sub add_default_fields() {
85     my( $fgfs ) = shift;
86
87     push( @FIELDS, ( "Longitude (deg)", "/position/longitude-deg" ) );
88     push( @FIELDS, ( "Latitude (deg)", "/position/latitude-deg" ) );
89     push( @FIELDS, ( "Altitude (ft MSL)", "/position/altitude-ft" ) );
90     push( @FIELDS, ( "Altitude (ft AGL)", "/position/altitude-agl-ft" ) );
91     push( @FIELDS, ( "Roll (deg)", "/orientation/roll-deg" ) );
92     push( @FIELDS, ( "Pitch (deg)", "/orientation/pitch-deg" ) );
93     push( @FIELDS, ( "Heading (deg)", "/orientation/heading-deg" ) );
94     push( @FIELDS, ( "Calibrated Air Speed (kt)", "/velocities/airspeed-kt" ) );
95     push( @FIELDS, ( "Vertical Speed (fps)",
96                      "/velocities/vertical-speed-fps" ) );
97     push( @FIELDS, ( "Roll Rate (degps)", "/orientation/roll-rate-degps" ) );
98     push( @FIELDS, ( "Pitch Rate (degps)", "/orientation/pitch-rate-degps" ) );
99     push( @FIELDS, ( "Yaw Rate (degps)", "/orientation/yaw-rate-degps" ) );
100     push( @FIELDS, ( "Alpha (deg)", "/orientation/alpha-deg" ) );
101     push( @FIELDS, ( "Beta (deg)", "/orientation/side-slip-deg" ) );
102     push( @FIELDS, ( "Prop (RPM)", "/engines/engine[0]/rpm" ) );
103     push( @FIELDS, ( "Left Aileron Pos (norm)",
104                      "/surface-positions/left-aileron-pos-norm" ) );
105     push( @FIELDS, ( "Right Aileron Pos (norm)",
106                      "/surface-positions/right-aileron-pos-norm" ) );
107     push( @FIELDS, ( "Elevator Pos (norm)",
108                      "/surface-positions/elevator-pos-norm" ) );
109     push( @FIELDS, ( "Rudder Pos (norm)",
110                      "/surface-positions/rudder-pos-norm" ) );
111     push( @FIELDS, ( "Flap Pos (norm)",
112                      "/surface-positions/flap-pos-norm" ) );
113     push( @FIELDS, ( "Nose Wheel Pos (norm)",
114                      "/surface-positions/nose-wheel-pos-norm" ) );
115     push( @FIELDS, ( "Wheel Pos (norm)", "/controls/flight/aileron" ) );
116     push( @FIELDS, ( "Column Pos (norm)", "/controls/flight/elevator" ) );
117     push( @FIELDS, ( "Trim Wheel Pos (norm)",
118                      "/controls/flight/elevator-trim" ) );
119     push( @FIELDS, ( "Pedal Pos (norm)", "/controls/flight/rudder" ) );
120     push( @FIELDS, ( "Throttle Pos (norm)",
121                      "/controls/engines/engine[0]/throttle" ) );
122
123     # initially enable all fields
124     for ( my($i) = 0; $i <= ($#FIELDS / 2); ++$i ) {
125         &add_field( $fgfs, $FIELDS[2*$i], $FIELDS[2*$i+1] );
126     }
127 }
128
129
130 sub start_logging {
131     my( $fgfs ) = shift;
132     my( $log_file ) = shift;
133
134     &set_prop( $fgfs, "/logging/log[$lognum]/filename", $log_file );
135     &set_prop( $fgfs, "/logging/log[$lognum]/interval-ms", "100" );
136     &set_prop( $fgfs, "/logging/log[$lognum]/enabled", "true" );
137     &send( $fgfs, "run data-logging-commit" );
138 }
139
140
141 sub stop_logging {
142     my( $fgfs ) = shift;
143
144     &set_prop( $fgfs, "/logging/log[$lognum]/enabled", "false" );
145     &send( $fgfs, "run data-logging-commit" );
146 }
147
148
149 sub quick_plot {
150     my( $data_file ) = shift;
151     my( $plot_file ) = shift;
152     my( $title ) = shift;
153     my( $column ) = shift;
154
155     print "quick plot -> $plot_file\n";
156
157     my( $tmpcmd ) = "$tmp_dir/plot_cmd_tmp.$$";
158     my( $tmpdata ) = "$tmp_dir/plot_data_tmp.$$";
159     my( $png_image ) = "$plot_file.png";
160
161     # strip the leading header off the file so gnuplot doesn't squawk
162     system( "tail +2 $data_file | sed -e \"s/,/ /g\" > $tmpdata" );
163
164     # create the gnuplot command file
165     open( CMD, ">$tmpcmd" );
166     print CMD "set terminal png color\n";
167     print "png_image = $png_image\n";
168     print CMD "set output \"$png_image\"\n";
169     print CMD "set xlabel \"Time (sec)\"\n";
170     print CMD "set ylabel \"$title\"\n";
171     print CMD "plot \"$tmpdata\" using 1:$column title \"$title\" with lines\n";
172     print CMD "quit\n";
173     close( CMD );
174
175     # plot the graph
176     system( "gnuplot $tmpcmd" );
177
178     # clean up all our droppings
179     unlink( $tmpcmd );
180     unlink( $tmpdata );
181 }
182
183
184 return 1;                    # make perl happy