Index: plot.rb
===================================================================
--- plot.rb	(revision 1299)
+++ plot.rb	(revision 1300)
@@ -1,10 +1,36 @@
 #!/usr/bin/env ruby
 
-file0='/tmp/data0.tmp'
-file1='/tmp/data1.tmp'
+# All rows must have the same length. rows are arrays of [x y]. titles are strings.
+def plot(rows, titles)
+	datafile='/tmp/data.tmp'
 
-points0f=[]; points0b=[]; points1f=[]; points1b=[]
+	#write it
 
+	num_points=rows[0].size
+
+        File.open(datafile, 'w') { |f|
+		(0...num_points).each { |n|
+			f.puts rows.map { |row| row[n] }.join(' ')
+		}
+        }
+
+	# plot it
+	num_rows=rows.size
+
+	p=IO.popen('gnuplot', 'w')
+        #p.puts("set xlabel 'Station encoders'")
+        #p.puts("set ylabel 'Robot encoders'")
+
+	(0...num_rows).each { |n|
+		plotcmd=(n==0)?"plot":"replot"
+		p.puts "#{plotcmd} '#{datafile}' using #{2*n+1}:#{2*n+2} with linespoints title '#{titles[n]}'";
+	}
+
+end
+
+encoder_data=Hash.new { |hash,key| hash[key]=Hash.new { |h,k| h[k]=[] } }
+motor_data=Hash.new { |hash,key| hash[key]=Hash.new { |h,k| h[k]=Hash.new { |hh,kk| hh[kk]=[] } } }
+
 puts "Paste output from station here please"
 
 while a=gets
@@ -11,51 +37,54 @@
         a.chomp!
         a.strip!
 
-        if a!="" && !(a =~ /^#/)
-                if a =~ /^data encoder (0|1) (forward|backward) (.*)$/
-                        num=$1; dir=$2; data=$3
+	if a =~ /^data encoder (0|1) (forward|backward) (.*)$/
+		num=$1.to_i; dir=$2; data=$3
 
-                        if    (num=="0" && dir=="forward" ); points=points0f
-                        elsif (num=="0" && dir=="backward"); points=points0b
-                        elsif (num=="1" && dir=="forward" ); points=points1f
-                        elsif (num=="1" && dir=="backward"); points=points1b
-                        else                               ; points=[]
-                        end
+		data.split(' ').each { |pair|
+			point=pair.split('/').map { |v| v.to_i };
+			encoder_data[num][dir]<<point
+		}
+	elsif a =~ /^data motor (0|1) (forward|backward) (increasing|decreasing) (.*)$/
+		num=$1.to_i; dir=$2; acc=$3; data=$4
 
-                        data.split(' ').each { |pair| point=pair.split('/'); points<<point }
-                end
-        end
+		data.split(' ').each { |pair|
+			point=pair.split('/').map { |v| v.to_i };
+			motor_data[num][dir][acc]<<point
+		}
+	end
 end
 
-def write_points(filename, points)
-        File.open(filename, 'w') { |f|
-                points.sort { |a,b|
-                        a[0].to_i <=> b[0].to_i
-                }.each { |point|
-                        puts "#{point[0]} #{point[1]}"
-                        f.puts "#{point[0]} #{point[1]}"
-                }
-        }
+
+if !encoder_data.empty?
+	encoder_rows=[0,1].map { |n| encoder_data[n]['forward']+encoder_data[n]['backward'] }
+
+	encoder_rows[0].sort! { |a,b| a[0]<=>b[0] }
+	encoder_rows[1].sort! { |a,b| a[0]<=>b[0] }
+
+	plot(encoder_rows, ['left', 'right'])
 end
 
-write_points(file0, points0f+points0b)
-write_points(file1, points1f+points1b)
+if !motor_data.empty?
+	motor_data[0]['backward']['increasing'].each { |x| x[0]=-x[0] }
+	motor_data[0]['backward']['decreasing'].each { |x| x[0]=-x[0] }
+	motor_data[1]['backward']['increasing'].each { |x| x[0]=-x[0] }
+	motor_data[1]['backward']['decreasing'].each { |x| x[0]=-x[0] }
 
-IO.popen('gnuplot', 'w') { |p|
-        p.puts("set xlabel 'Station encoders'")
-        p.puts("set ylabel 'Robot encoders'")
-        p.puts("plot   '#{file0}' using 1:2 with linespoints title 'Left'")
-        p.puts("replot '#{file1}' using 1:2 with linespoints title 'Right'")
+	motor_rows=[0,1].map { |n|
+		motor_data[n]['forward']['increasing']+\
+		motor_data[n]['forward']['decreasing']+\
+		motor_data[n]['backward']['increasing']+\
+		motor_data[n]['backward']['decreasing']
+	}
 
-		#p.puts ("set terminal postscript color landscape");
-		#p.puts ("set output 'test.ps'");
-		#p.puts ("replot");
+	plot(motor_rows, ['left', 'right'])
+end
 
-        p.puts("pause -1");
 
-        while (1)
-        end
-}
+puts "Interrupt to close"
+begin
+	while 1; end
+rescue Interrupt
+end
 
-# Never reached
-#File.delete('data0.tmp', 'data1.tmp')
+
