Statistics
| Revision:

root / trunk / code / projects / diagnostic_station / tools / plot.rb @ 1300

History | View | Annotate | Download (2.16 KB)

1
#!/usr/bin/env ruby
2

    
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'
6

    
7
        #write it
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

    
34
puts "Paste output from station here please"
35

    
36
while a=gets
37
        a.chomp!
38
        a.strip!
39

    
40
        if a =~ /^data encoder (0|1) (forward|backward) (.*)$/
41
                num=$1.to_i; dir=$2; data=$3
42

    
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
49

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

    
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'])
65
end
66

    
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] }
72

    
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
        }
79

    
80
        plot(motor_rows, ['left', 'right'])
81
end
82

    
83

    
84
puts "Interrupt to close"
85
begin
86
        while 1; end
87
rescue Interrupt
88
end
89

    
90