### loadFlush
#this will move the flush puck from the flush position to the requested clamp
def loadFlush toPosition

#check that a valid position
case "#{toPosition}"
 when "processing" then puts "loading flush puck to processing"
 when "collection" then puts "loading flush puck to collection"
 else raise "not a valid location #{toPosition}"
end

#check to make sure clamp is not already closed 
#which could mean a puck already there

case "#{toPosition}"
 when "processing"
   if PC.position == PC.at(:close) then raise "Processing clamp CLOSED" end
 when "collection" 
   if CC.position == CC.at(:close) then raise "Collection clamp CLOSED" end
 else raise "not a valid location #{toPosition}"
end


Hand.open
Arm.grab :flush
Arm.to toPosition
case "#{toPosition}"
 when "processing" then PC.close
 when "collection" then CC.close
 else raise "not a valid location #{toPosition}"
end
Arm.retract
end #end of loadFlush

### returnFlush

#moves the flush puck back to the garage
def returnFlush toPosition

#check that a valid position
case "#{toPosition}"
 when "processing" then puts "returning flush puck from processing"
 when "collection" then puts "returning flush puck from collection"
 else raise "not a valid location #{toPosition}"
end

#check to make sure clamp is not already closed 
#which could mean a puck already there

case "#{toPosition}"
 when "processing"
   if PC.position == PC.at(:open) then raise "Processing clamp OPEN" end
 when "collection" 
   if CC.position == CC.at(:open) then raise "Collection clamp OPEN" end
 else raise "not a valid location #{toPosition}"
end


Hand.open
Arm.grab toPosition
case "#{toPosition}"
 when "processing" then PC.open
 when "collection" then CC.open
 else raise "not a valid location #{toPosition}"
end
Arm.releaseAt :flush
end #end of returnFlush


### loadPuck
#This is used to move a puck from fromtube to toPosition

def loadPuck fromTube, toPosition

case toPosition
 when :processing then puts "loading from tube #{fromTube} to processing"
 when :collection then puts "loading from tube #{fromTube} to collection"
 else raise "not a valid location #{toPosition}"
end

#check to make sure clamp is not already closed 
#which could mean a puck already there

case "#{toPosition}"
 when "processing"
   if PC.position == PC.at(:close) then raise "Processing clamp CLOSED" end
 when "collection" 
   if CC.position == CC.at(:close) then raise "Collection clamp CLOSED" end
 else raise "not a valid location #{toPosition}"
end

Hand.open
Forearm.to :retract, MFretract
unless SC.position == SC.at(fromTube)
  SE.down
  SC.to fromTube
end 
Arm.to :elevator
SE.to 0, SEpick
if SE.amount < -0.2
  SE.down
Hand.open
Forearm.to :retract, MFretract
  raise "Tube Empty -- no puck to load"
end
Hand.close
case "#{toPosition}"
 when "processing" then PC.open
 when "collection" then CC.open
 else raise "not a valid location #{toPosition}"
end
downEv = Thread :downElevator do 
  SE.down
  SE.coast
  SC.coast
end
Arm.to toPosition
case "#{toPosition}"
 when "processing" then PC.close
 when "collection" then CC.close
 else raise "not a valid location #{toPosition}"
end
Hand.open
Forearm.to :retract, MFretract
Elbow.coast
Forearm.coast
downEv.join
end

###unloadPuck
#moves a puck from position back to carousel

def unloadPuck fromPosition, toTube


case "#{fromPosition}"
 when "processing" then puts "moving puck from processing to store in tube #{toTube}"
 when "collection" then puts "moving puck from collection to store in tube #{toTube}"
 else raise "not a valid location #{fromPosition}"
end

#check to make sure clamp is not already closed 
#which could mean a puck already there

case "#{fromPosition}"
 when "processing"
   if PC.position == PC.at(:open) then raise "Processing clamp OPEN" end
 when "collection" 
   if CC.position == CC.at(:open) then raise "Collection clamp OPEN" end
 else raise "not a valid location #{fromPosition}"
end

Arm.retract
unless SC.position == SC.at(toTube)
  SE.down
  SC.to toTube
end
Arm.to :elevator
SE.to :top, SEplace 
SE.configure  #we don't want SE to move when hand with puck break the beam
#then grab puck and go to elevator
Arm.grab fromPosition
case "#{fromPosition}"
 when "processing" then PC.open
 when "collection" then CC.open
 else raise "not a valid location #{fromPosition}"
end
Arm.to :elevator
Elbow.coast
Forearm.to Forearm[:elevator, -300],MFplace
Hand.open
Forearm.to :elevator
SE.down 
Arm.retract
end

### shifPuck
def shiftPuck fromTube, toTube
#first move arm out of the way
Arm.retract
unless SC.position == SC.at(fromTube)
  SE.down
  SC.to fromTube
end
Arm.to :elevator
SE.to :top, SEpick 
SE.configure  #we don't want SE to move when hand break beam
if SE.amount < -0.2
  SE.down
  Hand.open
  Forearm.to :retract, MFretract
  raise "Tube Empty -- no puck to load"
end
#then grab puck and go to elevator
Arm.grab :elevator
#and move out of the way
Forearm.to :retract
Elbow.to :flush
SE.down
SC.to toTube
SE.to :top, SEplace 
SE.configure  #we don't want SE to move when hand break beam
#then grab puck and go to elevator
Arm.to :elevator
Elbow.coast
Forearm.to Forearm[:elevator, -300],MFplace
Hand.open
Forearm.to :elevator
SE.down 
Arm.retract

end #end of shiftPuck



###imagePuck
#This routine retrieves a puck from the fromPosition
#and places it at the camera seal for imaging

def imagePuck fromPosition, toTube

case "#{fromPosition}"
 when "processing" then puts "moving puck from processing to imaging, store in tube #{toTube}"
 when "collection" then puts "moving puck from collection to imaging, store in tube #{toTube}"
 else raise "not a valid location #{fromPosition}"
end

#check to make sure clamp is not already closed 
#which could mean a puck already there

case "#{fromPosition}"
 when "processing"
   if PC.position == PC.at(:open) then raise "Processing clamp OPEN" end
 when "collection" 
   if CC.position == CC.at(:open) then raise "Collection clamp OPEN" end
 else raise "not a valid location #{fromPosition}"
end

Arm.retract
unless SC.position == SC.at(toTube)
  SE.down
  SC.to toTube
end
Arm.to :elevator
SE.to :top, SEplace 
SE.configure  #we don't want SE to move when hand break beam
#then grab puck and go to elevator
Arm.grab fromPosition
case "#{fromPosition}"
 when "processing" then PC.open
 when "collection" then CC.open
 else raise "not a valid location #{fromPosition}"
end
Arm.to :elevator
Elbow.coast
Forearm.to Forearm[:elevator, -300],MFplace
Hand.open
Forearm.to :elevator
SE.down 1
SE.to :up, SEseal
Log.record "Lights, camera, ACTION!"

end

### finishImage
#this routine just puts away the puck after the image is complete
def finishImage
SE.coast
SE.down
Arm.retract
end
