Revision 1300 trunk/code/projects/diagnostic_station/tools/plot.rb

View differences:

plot.rb
1 1
#!/usr/bin/env ruby
2 2

  
3
file0='/tmp/data0.tmp'
4
file1='/tmp/data1.tmp'
3
# All rows must have the same length. rows are arrays of [x y]. titles are strings.
4
def plot(rows, titles)
5
	datafile='/tmp/data.tmp'
5 6

  
6
points0f=[]; points0b=[]; points1f=[]; points1b=[]
7
	#write it
7 8

  
9
	num_points=rows[0].size
10

  
11
        File.open(datafile, 'w') { |f|
12
		(0...num_points).each { |n|
13
			f.puts rows.map { |row| row[n] }.join(' ')
14
		}
15
        }
16

  
17
	# plot it
18
	num_rows=rows.size
19

  
20
	p=IO.popen('gnuplot', 'w')
21
        #p.puts("set xlabel 'Station encoders'")
22
        #p.puts("set ylabel 'Robot encoders'")
23

  
24
	(0...num_rows).each { |n|
25
		plotcmd=(n==0)?"plot":"replot"
26
		p.puts "#{plotcmd} '#{datafile}' using #{2*n+1}:#{2*n+2} with linespoints title '#{titles[n]}'";
27
	}
28

  
29
end
30

  
31
encoder_data=Hash.new { |hash,key| hash[key]=Hash.new { |h,k| h[k]=[] } }
32
motor_data=Hash.new { |hash,key| hash[key]=Hash.new { |h,k| h[k]=Hash.new { |hh,kk| hh[kk]=[] } } }
33

  
8 34
puts "Paste output from station here please"
9 35

  
10 36
while a=gets
11 37
        a.chomp!
12 38
        a.strip!
13 39

  
14
        if a!="" && !(a =~ /^#/)
15
                if a =~ /^data encoder (0|1) (forward|backward) (.*)$/
16
                        num=$1; dir=$2; data=$3
40
	if a =~ /^data encoder (0|1) (forward|backward) (.*)$/
41
		num=$1.to_i; dir=$2; data=$3
17 42

  
18
                        if    (num=="0" && dir=="forward" ); points=points0f
19
                        elsif (num=="0" && dir=="backward"); points=points0b
20
                        elsif (num=="1" && dir=="forward" ); points=points1f
21
                        elsif (num=="1" && dir=="backward"); points=points1b
22
                        else                               ; points=[]
23
                        end
43
		data.split(' ').each { |pair|
44
			point=pair.split('/').map { |v| v.to_i };
45
			encoder_data[num][dir]<<point
46
		}
47
	elsif a =~ /^data motor (0|1) (forward|backward) (increasing|decreasing) (.*)$/
48
		num=$1.to_i; dir=$2; acc=$3; data=$4
24 49

  
25
                        data.split(' ').each { |pair| point=pair.split('/'); points<<point }
26
                end
27
        end
50
		data.split(' ').each { |pair|
51
			point=pair.split('/').map { |v| v.to_i };
52
			motor_data[num][dir][acc]<<point
53
		}
54
	end
28 55
end
29 56

  
30
def write_points(filename, points)
31
        File.open(filename, 'w') { |f|
32
                points.sort { |a,b|
33
                        a[0].to_i <=> b[0].to_i
34
                }.each { |point|
35
                        puts "#{point[0]} #{point[1]}"
36
                        f.puts "#{point[0]} #{point[1]}"
37
                }
38
        }
57

  
58
if !encoder_data.empty?
59
	encoder_rows=[0,1].map { |n| encoder_data[n]['forward']+encoder_data[n]['backward'] }
60

  
61
	encoder_rows[0].sort! { |a,b| a[0]<=>b[0] }
62
	encoder_rows[1].sort! { |a,b| a[0]<=>b[0] }
63

  
64
	plot(encoder_rows, ['left', 'right'])
39 65
end
40 66

  
41
write_points(file0, points0f+points0b)
42
write_points(file1, points1f+points1b)
67
if !motor_data.empty?
68
	motor_data[0]['backward']['increasing'].each { |x| x[0]=-x[0] }
69
	motor_data[0]['backward']['decreasing'].each { |x| x[0]=-x[0] }
70
	motor_data[1]['backward']['increasing'].each { |x| x[0]=-x[0] }
71
	motor_data[1]['backward']['decreasing'].each { |x| x[0]=-x[0] }
43 72

  
44
IO.popen('gnuplot', 'w') { |p|
45
        p.puts("set xlabel 'Station encoders'")
46
        p.puts("set ylabel 'Robot encoders'")
47
        p.puts("plot   '#{file0}' using 1:2 with linespoints title 'Left'")
48
        p.puts("replot '#{file1}' using 1:2 with linespoints title 'Right'")
73
	motor_rows=[0,1].map { |n|
74
		motor_data[n]['forward']['increasing']+\
75
		motor_data[n]['forward']['decreasing']+\
76
		motor_data[n]['backward']['increasing']+\
77
		motor_data[n]['backward']['decreasing']
78
	}
49 79

  
50
		#p.puts ("set terminal postscript color landscape");
51
		#p.puts ("set output 'test.ps'");
52
		#p.puts ("replot");
80
	plot(motor_rows, ['left', 'right'])
81
end
53 82

  
54
        p.puts("pause -1");
55 83

  
56
        while (1)
57
        end
58
}
84
puts "Interrupt to close"
85
begin
86
	while 1; end
87
rescue Interrupt
88
end
59 89

  
60
# Never reached
61
#File.delete('data0.tmp', 'data1.tmp')
90

  

Also available in: Unified diff