Class: Session::Recorder

Inherits:
Object
  • Object
show all
Defined in:
lib/robotic-arm.rb

Instance Method Summary collapse

Constructor Details

#initialize(obj) ⇒ Recorder

Returns a new instance of Recorder.



15
16
17
18
19
# File 'lib/robotic-arm.rb', line 15

def initialize(obj)
  @log = []
  @recording = false
  @obj = obj
end

Instance Method Details

#playbackObject



133
# File 'lib/robotic-arm.rb', line 133

def playback()      play @log          end

#record(classname, methodname) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/robotic-arm.rb', line 123

def record(classname, methodname)
              
  @log << {
        time: Time.now, 
        class: classname.to_sym, 
        method: methodname.to_sym
  }
end

#recording?Boolean

Returns:

  • (Boolean)


132
# File 'lib/robotic-arm.rb', line 132

def recording?()    @recording         end

#reverse_playObject



134
# File 'lib/robotic-arm.rb', line 134

def reverse_play()  play @rlog.reverse end

#startObject



21
22
23
24
# File 'lib/robotic-arm.rb', line 21

def start
  @log = [{:time => Time.now, :method => :stop}]
  @recording = true
end

#stopObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/robotic-arm.rb', line 26

def stop
  
  return unless @recording == true
  @log << {time: Time.now, class: nil, :method => :stop}
  
  @recording = false

  # convert the time to elapsed time in seconds

  @log.first[:time] = @log[1][:time]
  t1 = @log.first[:time]
  @log.first[:sleep] = 0
  @log[1..-2].each_with_index do |record,i|
    t2 = record[:time]
    @log[i][:sleep] = (t2 - t1).round(2)
    t1 = t2
  end
  


  matched = []
  @rlog = @log.map{|x| x.clone}      
  
  @rlog.reverse[0..-2].each_with_index do |record,i |

    next if matched.include? i

    j = @rlog.reverse[(i+1)..-1].map{|x| x[:class]}.index(record[:class])                 

    if j then

      tmp = record.clone
      record[:method] = @rlog.reverse[i+j+1][:method]
      record[:sleep] = @rlog.reverse[i+j+1][:sleep]
      @rlog.reverse[i+j+1][:method] = tmp[:method]
      @rlog.reverse[i+j+1][:sleep] = tmp[:sleep]
      matched << i+j+1
    end
          
  end
  
  @rlog.first[:sleep] = 0
=begin      
 # The following code includes factors for adjusting the duration of 
   robotic arm movements, as I had observed the distance travelled by
   the arm n a downward motion != distance travelled in a upward motion
   for a fixed duration.
   A more accurate approach would be to add offset times for an actual
   recorded session.

  @rlog.each do |record|
    
    if record[:sleep] then

      record[:method] = swap_state record[:method] 
      
      if record[:method] == :stop then
        #record[:sleep] = (record[:sleep] - 0.03).round(2) 
      elsif record[:class] == :shoulder and record[:method] == :up then
        record[:sleep] = (record[:sleep] * 1.14).round(2)
      elsif record[:class] == :elbow and record[:method] == :up then
        record[:sleep] = (record[:sleep] * 1.14).round(2)            
      elsif record[:class] == :shoulder and record[:method] == :down then
        record[:sleep] = (record[:sleep] / 1.14).round(2)
      elsif record[:class] == :elbow and record[:method] == :down then
        record[:sleep] = (record[:sleep] / 1.14).round(2)            

      elsif record[:class] == :gripper and record[:method] == :open then
        record[:sleep] = (record[:sleep] / 1.14).round(2)
      elsif record[:class] == :gripper and record[:method] == :close then
        record[:sleep] = (record[:sleep] * 1.14).round(2)                       

      end

    end                        
  end
  
  @log.each do |record|
    
    if record[:sleep] then
      
      if record[:method] == :stop then
        #record[:sleep] = (record[:sleep] - 0.03).round(2) 
      elsif record[:class] == :shoulder and record[:method] == :up then
        record[:sleep] = (record[:sleep] * 1.24).round(2)
      elsif record[:class] == :shoulder and record[:method] == :down then
        record[:sleep] = (record[:sleep] / 1.05).round(2)
      elsif record[:class] == :elbow and record[:method] == :down then
        record[:sleep] = (record[:sleep] / 1.05).round(2)            
      end

    end
  end
=end      
  @log
end