Added simple gnuplot interface.
authorLadislav Laska <laska@kam.mff.cuni.cz>
Wed, 21 Aug 2013 22:47:20 +0000 (00:47 +0200)
committerLadislav Laska <laska@kam.mff.cuni.cz>
Wed, 21 Aug 2013 22:47:20 +0000 (00:47 +0200)
accutool

index f0d48add03f7a49016bb0d939b855b75af4e1712..e236d4e03a2de68fe5384908c8e7007e72694e23 100755 (executable)
--- a/accutool
+++ b/accutool
@@ -11,7 +11,7 @@ use List::Util qw(reduce sum);
 my $opt_source = "/dev/ttyUSB0";
 my $opt_baud = 9600;
 my $opt_log = "accucell.log";
-my $opt_output = "-";
+my $opt_output = undef;
 my $opt_gnuplot = "VT,AT";
 my $opt_help = 0;
 
@@ -102,3 +102,54 @@ if ($raw_data ne "") {
 } else {
        print "No data to process.";
 }
+
+if ($opt_gnuplot ne "") {
+       my @csv_fields = split /,/, ','.(split /\n/, $raw_data)[0];
+       my %csv_index;
+       @csv_index{@csv_fields} = (0..$#csv_fields);
+
+#set y2range [0:5]
+#set yrange [0:0.5]
+       my $gp_script = <<END ;
+set datafile separator ","
+set y2tics border
+set ytics nomirror
+set xlabel "Time in seconds"
+set ylabel "current (A), charge (Ah)"
+set y2label "voltage (V)"
+END
+       my $sep = "plot ";
+       my @graphs = split /,/, $opt_gnuplot;
+       my %gp_choices = (
+               "vt" => [$csv_index{voltage}, "Battery voltage"],
+               "it" => [$csv_index{current}, "Battery current"],
+               "v1t" => [$csv_index{cell_voltage_1}, "Cell 1 voltage"],
+               "v2t" => [$csv_index{cell_voltage_2}, "Cell 2 voltage"],
+               "v3t" => [$csv_index{cell_voltage_3}, "Cell 3 voltage"],
+               "v4t" => [$csv_index{cell_voltage_4}, "Cell 4 voltage"],
+               "v5t" => [$csv_index{cell_voltage_5}, "Cell 5 voltage"],
+               "v6t" => [$csv_index{cell_voltage_6}, "Cell 6 voltage"],
+               "v7t" => [$csv_index{cell_voltage_7}, "Cell 7 voltage"],
+               "v8t" => [$csv_index{cell_voltage_8}, "Cell 8 voltage"],
+               );
+       foreach my $graph (map { lc } @graphs) {
+               if (defined $gp_choices{$graph}) {
+                       my ($field, $label) = @{$gp_choices{$graph}};
+                       $gp_script .= "$sep \"$opt_source\" using 1:$field with lines title \"$label\"";
+                       $sep = ",";
+               } else {
+                       warn "Ignoring unknown gnuplot argument $graph.";
+               }
+       }
+
+       $gp_script .= "\n";
+
+       if (not defined $opt_output) {
+               system("echo 'set terminal qt\n' '$gp_script' | gnuplot -persist");
+       } else {
+               open( FH, ">$opt_output" ) or die "Could not open output file '$opt_output'.";
+               print FH $gp_script;
+               close FH;
+       }
+
+}