#!/usr/bin/env ruby
# $Source: /home/cvs/ESP/gen2/software/esp/example/loadpuck.rb,v $
# $Id: loadpuck.rb,v 1.5 2003/04/09 21:46:55 brent Exp $

require 'ESP'     #load the standard support library
require 'mission' #load mission specific support library

# Multi-threaded example of efficiently loading a puck
# A new thread rotates the carousel and then raises the elevator while
# the main thread moves the shuttle into position taking care not to
# interfere with the elevator

def loadpuck (puckType=:unspecified, sourceTube)
# Load the top puck from sourceTube into the shuttle
#  puckType is an object (typlically a symbol) denoting the type of puck desired
#           it may be anything -- e.g. :popTop, :wetArchive, :process, 17, etc.

  interlock = Lock :loadPuck,
                :elevatorDown, :shuttleAtLoad, :puckUp, :shuttleClear
# elevatorThread = 
  Thread (:elevator) {
    Elevator.home
    interlock.signal :elevatorDown
    Carousel.align sourceTube
    interlock.await :shuttleAtLoad
    Elevator.up
    interlock.signal(:puckUp).await(:shuttleClear)
    Elevator.home
  }
  Shuttle.nearLoadPosition       #close to load but not interfering
  interlock.await :elevatorDown
  Shuttle.toLoadPosition
  interlock.signal(:shuttleAtLoad).await(:puckUp)
  Shuttle.nearLoadPosition         #so puck dosn't go back down with elevator
  Puck.inShuttle puckType, sourceTube  #record for tracking & later reporting
  interlock.signal :shuttleClear
  # note that the elevator may still homing
  # To wait until the elevator stops before returning, comment the following:
# elevatorThread.join
end
