class Session::Recorder

Public Class Methods

new(obj) click to toggle source
# File lib/robotic-arm.rb, line 15
def initialize(obj)
  @log = []
  @recording = false
  @obj = obj
end

Public Instance Methods

playback() click to toggle source
# File lib/robotic-arm.rb, line 133
def playback()      play @log          end
record(classname, methodname) click to toggle source
# 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?() click to toggle source
# File lib/robotic-arm.rb, line 132
def recording?()    @recording         end
reverse_play() click to toggle source
# File lib/robotic-arm.rb, line 134
def reverse_play()  play @rlog.reverse end
start() click to toggle source
# File lib/robotic-arm.rb, line 21
def start
  @log = [{:time => Time.now, :method => :stop}]
  @recording = true
end
stop() click to toggle source
# 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

Private Instance Methods

play(log) click to toggle source
# File lib/robotic-arm.rb, line 138
def play(log)      

  log.each do |record|
    
    component, action = *[:class, :method].map{|x| record[x]}
          
    unless component.nil? then
      @obj.method(component).call.method(action).call
    else
      @obj.method(action).call
    end
    
    sleep record[:sleep].to_f            
  end
end
swap_state(methodname) click to toggle source
# File lib/robotic-arm.rb, line 154
def swap_state(methodname)
  case methodname
    when :up
      :down
    when :down
      :up
    when :left
      :right
    when :right
      :left
    when :open
      :close
    when :close
      :open
    else
      methodname
  end
end