#####################  gripper.rb -- brent@mbari.org  #######################
# $Source: /home/cvs/ESP/gen2/software/esp/lib/gripper.rb,v $
#    Copyright (C) 2003 MBARI
#    MBARI Proprietary Information. All rights reserved.
# $Id: gripper.rb,v 1.4 2005/05/07 05:27:20 brent Exp $
#
#     Control the gripper associated with the specified servo channel
#
############################################################################

require 'i2c/servo'

class Gripper
  
  def initialize(gripperName, dwarf, gripperNumber, maxDuration=20)
  #return a new gripper object
  #dwarf is the network target that receives replies and sources requests
  #gripperNumber is its servo channel (typically 0..1)
  #maxDuration is the max # of seconds to await a reply
    @name=gripperName
    @channel=gripperNumber
    @dwarf=dwarf
    @maxDuration = maxDuration
    @state="in an unknown state".intern
  end
  attr_accessor :name, :maxDuration
  attr_reader :state

  def grip
    Thread.log.recordMethod self, :grip
    begin
      @dwarf.grip @channel
    rescue =>err
      err.message[0,0]="Cannot close #{@name}:  "
      raise
    end
    @state = :closed
  end
  alias_method :close, :grip
  
  def release
    Thread.log.recordMethod self, :release
    begin
      @dwarf.release @channel
    rescue =>err
      err.message[0,0]="Cannot open #{@name}:  "
      raise
    end
    @state = :opened
  end
  alias_method :open, :release
  
  def to_s
    "#{@name} is #{@state}"
  end
  alias_method :asIRBtext, :to_s
  
end #Gripper class
