3 # logging.pl - Handle logging
5 # Written by Curtis L. Olson, started February 2004
7 # Copyright (C) 2004 Curtis L. Olson - http://www.flightgear.org/~curt
9 # This code is placed in the public domain by Curtis L. Olson.
10 # There is no warranty, etc. etc. etc.
13 # ----------------------------------------------------------------------------
22 my( @FIELDS, @FIELDS_E );
23 my( $tmp_dir ) = "/tmp";
25 my( $field_index ) = 0;
35 $prop = &get_prop( $fgfs, "/logging/log[$lognum]/entry[$i]/property" );
38 "/logging/log[$lognum]/entry[$i]/enabled",
55 # spaces seem to not work well.
58 # print "$title - $prop\n";
60 "/logging/log[$lognum]/entry[$field_index]/title", $title );
62 "/logging/log[$lognum]/entry[$field_index]/property", $prop );
64 "/logging/log[$lognum]/entry[$field_index]/enabled", "true" );
70 sub add_default_fields() {
73 push( @FIELDS, ( "Longitude (deg)", "/position/longitude-deg" ) );
74 push( @FIELDS, ( "Latitude (deg)", "/position/latitude-deg" ) );
75 push( @FIELDS, ( "Altitude (ft MSL)", "/position/altitude-ft" ) );
76 push( @FIELDS, ( "Altitude (ft AGL)", "/position/altitude-agl-ft" ) );
77 push( @FIELDS, ( "Roll (deg)", "/orientation/roll-deg" ) );
78 push( @FIELDS, ( "Pitch (deg)", "/orientation/pitch-deg" ) );
79 push( @FIELDS, ( "Heading (deg)", "/orientation/heading-deg" ) );
80 push( @FIELDS, ( "Calibrated Air Speed (kt)", "/velocities/airspeed-kt" ) );
81 push( @FIELDS, ( "Vertical Speed (fps)",
82 "/velocities/vertical-speed-fps" ) );
83 push( @FIELDS, ( "Roll Rate (degps)", "/orientation/roll-rate-degps" ) );
84 push( @FIELDS, ( "Pitch Rate (degps)", "/orientation/pitch-rate-degps" ) );
85 push( @FIELDS, ( "Yaw Rate (degps)", "/orientation/yaw-rate-degps" ) );
86 push( @FIELDS, ( "Alpha (deg)", "/orientation/alpha-deg" ) );
87 push( @FIELDS, ( "Beta (deg)", "/orientation/side-slip-deg" ) );
88 push( @FIELDS, ( "Prop (RPM)", "/engines/engine[0]/rpm" ) );
89 push( @FIELDS, ( "Left Aileron Pos (norm)",
90 "/surface-positions/left-aileron-pos-norm" ) );
91 push( @FIELDS, ( "Right Aileron Pos (norm)",
92 "/surface-positions/right-aileron-pos-norm" ) );
93 push( @FIELDS, ( "Elevator Pos (norm)",
94 "/surface-positions/elevator-pos-norm" ) );
95 push( @FIELDS, ( "Elevator Trim Tab Pos (norm)",
96 "/surface-positions/elevator-trim-tab-pos-norm" ) );
97 push( @FIELDS, ( "Rudder Pos (norm)",
98 "/surface-positions/rudder-pos-norm" ) );
99 push( @FIELDS, ( "Flap Pos (norm)",
100 "/surface-positions/flap-pos-norm" ) );
101 push( @FIELDS, ( "Nose Wheel Pos (norm)",
102 "/surface-positions/nose-wheel-pos-norm" ) );
103 push( @FIELDS, ( "Wheel Pos (norm)", "/controls/flight/aileron" ) );
104 push( @FIELDS, ( "Column Pos (norm)", "/controls/flight/elevator" ) );
105 push( @FIELDS, ( "Trim Wheel Pos (norm)",
106 "/controls/flight/elevator-trim" ) );
107 push( @FIELDS, ( "Pedal Pos (norm)", "/controls/flight/rudder" ) );
108 push( @FIELDS, ( "Throttle Pos (norm)",
109 "/controls/engines/engine[0]/throttle" ) );
111 # initially enable all fields
112 for ( my($i) = 0; $i <= ($#FIELDS / 2); ++$i ) {
113 &add_field( $fgfs, $FIELDS[2*$i], $FIELDS[2*$i+1] );
120 my( $log_file ) = shift;
122 &set_prop( $fgfs, "/logging/log[$lognum]/filename", $log_file );
123 &set_prop( $fgfs, "/logging/log[$lognum]/interval-ms", "100" );
124 &set_prop( $fgfs, "/logging/log[$lognum]/enabled", "true" );
125 &send( $fgfs, "run data-logging-commit" );
132 &set_prop( $fgfs, "/logging/log[$lognum]/enabled", "false" );
133 &send( $fgfs, "run data-logging-commit" );
137 sub quick_plot_vs_time {
138 my( $data_file ) = shift;
139 my( $plot_file ) = shift;
140 my( $title ) = shift;
141 my( $column ) = shift;
143 print "quick plot -> $plot_file\n";
145 my( $tmpcmd ) = "$tmp_dir/plot_cmd_tmp.$$";
146 my( $tmpdata ) = "$tmp_dir/plot_data_tmp.$$";
147 my( $png_image ) = "$plot_file.png";
149 # strip the leading header off the file so gnuplot doesn't squawk
150 system( "tail -n +2 $data_file | sed -e \"s/,/ /g\" > $tmpdata" );
152 # create the gnuplot command file
153 open( CMD, ">$tmpcmd" );
154 print CMD "set terminal png\n";
155 print "png_image = $png_image\n";
156 print CMD "set output \"$png_image\"\n";
157 print CMD "set xlabel \"Time (sec)\"\n";
158 print CMD "set ylabel \"$title\"\n";
159 print CMD "plot \"$tmpdata\" using 1:$column title \"$title\" with lines\n";
164 system( "gnuplot $tmpcmd" );
166 # clean up all our droppings
173 my( $data_file ) = shift;
174 my( $plot_file ) = shift;
175 my( $xtitle ) = shift;
176 my( $ytitle ) = shift;
177 my( $xcolumn ) = shift;
178 my( $ycolumn ) = shift;
180 print "quick plot -> $plot_file\n";
182 my( $tmpcmd ) = "$tmp_dir/plot_cmd_tmp.$$";
183 my( $tmpdata ) = "$tmp_dir/plot_data_tmp.$$";
184 my( $png_image ) = "$plot_file.png";
186 # strip the leading header off the file so gnuplot doesn't squawk
187 system( "tail -n +2 $data_file | sed -e \"s/,/ /g\" > $tmpdata" );
189 # create the gnuplot command file
190 open( CMD, ">$tmpcmd" );
191 print CMD "set terminal png\n";
192 print "png_image = $png_image\n";
193 print CMD "set output \"$png_image\"\n";
194 print CMD "set xlabel \"$xtitle\"\n";
195 print CMD "set ylabel \"$ytitle\"\n";
196 print CMD "plot \"$tmpdata\" using $xcolumn:$ycolumn title \"$xtitle vs. $ytitle\" with lines\n";
201 system( "gnuplot $tmpcmd" );
203 # clean up all our droppings
209 return 1; # make perl happy