3 # logging.pl - Handle logging
5 # Written by Curtis L. Olson, started February 2004
7 # Copyright (C) 2004 Curtis L. Olson - curt@flightgear.org
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;
28 # my( $server, $port, $timeout );
29 # my( $last_plot_image ) = "";
30 # my( $image_handle );
33 # my( $field_choice );
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;
49 $prop = &get_prop( $fgfs, "/logging/log[$lognum]/entry[$i]/property" );
52 "/logging/log[$lognum]/entry[$i]/enabled",
69 # spaces seem to not work well.
72 print "$title - $prop\n";
74 "/logging/log[$lognum]/entry[$field_index]/title", $title );
76 "/logging/log[$lognum]/entry[$field_index]/property", $prop );
78 "/logging/log[$lognum]/entry[$field_index]/enabled", "true" );
84 sub add_default_fields() {
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" ) );
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] );
132 my( $log_file ) = shift;
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" );
144 &set_prop( $fgfs, "/logging/log[$lognum]/enabled", "false" );
145 &send( $fgfs, "run data-logging-commit" );
150 my( $data_file ) = shift;
151 my( $plot_file ) = shift;
152 my( $title ) = shift;
153 my( $column ) = shift;
155 print "quick plot -> $plot_file\n";
157 my( $tmpcmd ) = "$tmp_dir/plot_cmd_tmp.$$";
158 my( $tmpdata ) = "$tmp_dir/plot_data_tmp.$$";
159 my( $png_image ) = "$plot_file.png";
161 # strip the leading header off the file so gnuplot doesn't squawk
162 system( "tail +2 $data_file | sed -e \"s/,/ /g\" > $tmpdata" );
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";
176 system( "gnuplot $tmpcmd" );
178 # clean up all our droppings
184 return 1; # make perl happy