root / trunk / code / projects / diagnostic_station / tools / plot.rb @ 1300
History | View | Annotate | Download (2.16 KB)
1 | 1229 | deffi | #!/usr/bin/env ruby
|
---|---|---|---|
2 | |||
3 | 1300 | deffi | # 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 | 1231 | deffi | |
7 | 1300 | deffi | #write it
|
8 | 1229 | deffi | |
9 | 1300 | deffi | 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 | 1229 | deffi | puts "Paste output from station here please"
|
35 | |||
36 | while a=gets
|
||
37 | 1236 | deffi | a.chomp! |
38 | a.strip! |
||
39 | 1229 | deffi | |
40 | 1300 | deffi | if a =~ /^data encoder (0|1) (forward|backward) (.*)$/ |
41 | num=$1.to_i; dir=$2; data=$3 |
||
42 | 1229 | deffi | |
43 | 1300 | deffi | 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 | 1229 | deffi | |
50 | 1300 | deffi | data.split(' ').each { |pair|
|
51 | point=pair.split('/').map { |v| v.to_i };
|
||
52 | motor_data[num][dir][acc]<<point |
||
53 | } |
||
54 | end
|
||
55 | 1229 | deffi | end
|
56 | |||
57 | 1300 | deffi | |
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 | 1235 | deffi | end
|
66 | 1229 | deffi | |
67 | 1300 | deffi | 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 | 1229 | deffi | |
73 | 1300 | deffi | 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 | 1229 | deffi | |
80 | 1300 | deffi | plot(motor_rows, ['left', 'right']) |
81 | end
|
||
82 | 1229 | deffi | |
83 | 1236 | deffi | |
84 | 1300 | deffi | puts "Interrupt to close"
|
85 | begin
|
||
86 | while 1; end |
||
87 | rescue Interrupt |
||
88 | end
|
||
89 | 1229 | deffi |