| 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 - http://www.flightgear.org/~curt |
| 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 |
|
| 28 |
sub clear_logging { |
| 29 |
my( $fgfs ) = shift; |
| 30 |
|
| 31 |
my( $done ) = 0; |
| 32 |
my( $i ) = 0; |
| 33 |
my( $prop ); |
| 34 |
while ( !$done ) { |
| 35 |
$prop = &get_prop( $fgfs, "/logging/log[$lognum]/entry[$i]/property" ); |
| 36 |
if ( $prop ne "" ) { |
| 37 |
&set_prop( $fgfs, |
| 38 |
"/logging/log[$lognum]/entry[$i]/enabled", |
| 39 |
"false" ); |
| 40 |
} else { |
| 41 |
$done = 1; |
| 42 |
} |
| 43 |
$i++; |
| 44 |
} |
| 45 |
|
| 46 |
$field_index = 0; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
sub add_field { |
| 51 |
my( $fgfs ) = shift; |
| 52 |
my( $title ) = shift; |
| 53 |
my( $prop ) = shift; |
| 54 |
|
| 55 |
# spaces seem to not work well. |
| 56 |
$title =~ s/ /\_/g; |
| 57 |
|
| 58 |
# print "$title - $prop\n"; |
| 59 |
&set_prop( $fgfs, |
| 60 |
"/logging/log[$lognum]/entry[$field_index]/title", $title ); |
| 61 |
&set_prop( $fgfs, |
| 62 |
"/logging/log[$lognum]/entry[$field_index]/property", $prop ); |
| 63 |
&set_prop( $fgfs, |
| 64 |
"/logging/log[$lognum]/entry[$field_index]/enabled", "true" ); |
| 65 |
|
| 66 |
$field_index++; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
sub add_default_fields() { |
| 71 |
my( $fgfs ) = shift; |
| 72 |
|
| 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" ) ); |
| 110 |
|
| 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] ); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
sub start_logging { |
| 119 |
my( $fgfs ) = shift; |
| 120 |
my( $log_file ) = shift; |
| 121 |
|
| 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" ); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
sub stop_logging { |
| 130 |
my( $fgfs ) = shift; |
| 131 |
|
| 132 |
&set_prop( $fgfs, "/logging/log[$lognum]/enabled", "false" ); |
| 133 |
&send( $fgfs, "run data-logging-commit" ); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
sub quick_plot_vs_time { |
| 138 |
my( $data_file ) = shift; |
| 139 |
my( $plot_file ) = shift; |
| 140 |
my( $title ) = shift; |
| 141 |
my( $column ) = shift; |
| 142 |
|
| 143 |
print "quick plot -> $plot_file\n"; |
| 144 |
|
| 145 |
my( $tmpcmd ) = "$tmp_dir/plot_cmd_tmp.$$"; |
| 146 |
my( $tmpdata ) = "$tmp_dir/plot_data_tmp.$$"; |
| 147 |
my( $png_image ) = "$plot_file.png"; |
| 148 |
|
| 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" ); |
| 151 |
|
| 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"; |
| 160 |
print CMD "quit\n"; |
| 161 |
close( CMD ); |
| 162 |
|
| 163 |
# plot the graph |
| 164 |
system( "gnuplot $tmpcmd" ); |
| 165 |
|
| 166 |
# clean up all our droppings |
| 167 |
unlink( $tmpcmd ); |
| 168 |
unlink( $tmpdata ); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
sub quick_plot { |
| 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; |
| 179 |
|
| 180 |
print "quick plot -> $plot_file\n"; |
| 181 |
|
| 182 |
my( $tmpcmd ) = "$tmp_dir/plot_cmd_tmp.$$"; |
| 183 |
my( $tmpdata ) = "$tmp_dir/plot_data_tmp.$$"; |
| 184 |
my( $png_image ) = "$plot_file.png"; |
| 185 |
|
| 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" ); |
| 188 |
|
| 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"; |
| 197 |
print CMD "quit\n"; |
| 198 |
close( CMD ); |
| 199 |
|
| 200 |
# plot the graph |
| 201 |
system( "gnuplot $tmpcmd" ); |
| 202 |
|
| 203 |
# clean up all our droppings |
| 204 |
unlink( $tmpcmd ); |
| 205 |
unlink( $tmpdata ); |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
return 1; # make perl happy |