#there is ~1 - 2ml of sample in the CS.

#The flush puck is loaded in Collection
#The PS is empty and ready

#priming 

#when the CS needs to be cleaned then set cleanCS to something
#like this:   samplePassA :cleanCS

def samplePassA cleanCS = nil

  loadFlush :collection

  #put 1.5ml of flush into PS
  PSV.to :PSR
  PSR.to :flush
  PS.to 1.5
  delay 5
  PSV.to :waste1
  PS.to 0
  PSV.to :PSR
  PSR.to :air
  PS.fill

  #Backflush PSV - CBV line to CTV spit
  PSV.connect CBV
  CTV.to :air
  PS.to 0

  #clear the line of fluid
  PSV.to :air
  PS.fill
  PSV.to :CBV
  PS.to 0

  if (cleanCS) then
     CSV.to CSR
     CSR.to :flush
     CS.to 4
     delay 5
     CSR.to :air
     CS.fill
     CSV.connect CTV
     CBV.to :waste1
     CS.to 0
     
     CSV.to CSR
     CSR.to :flush
     CS.to 4
     delay 5
     CSR.to :air
     CS.fill
     CSV.connect CTV
     CBV.to :waste1
     CS.to 0     
  end
  
  returnFlush :collection
  
end
 
#this is the continuation from samplePassA.
#but samplePassB should be run after the 
#flush puck has been removed and a filter
#puck in its place.

def primePassB
  #priming CS for testing
  Arm.grab :flush
  Arm.to :collection
  CC.close
  CSV.to :waste
  CS.to 0
  CSR.to :flush
  CSV.to :CSR
  CS.to 1.85
  delay 5
  CSR.to :air
  CS.to 8
  delay 5
end

def pumpCSto ambientPress, thresholdPSI, maxStrokes=5
#pump up CS from ambient up to specified threshold pressure
#returns true if succeeded, false if thresholdPress could not be reached
  puckPSI = Sampler::CanPSI
  CSpressurize.maxOutPress = ambientPress + thresholdPSI
  maxStrokes.times {
    return true if Sampler.pressureStroke puckPSI, CSpressurize
    puckPSI = Sampler.intakePress - ambientPress + Sampler::CanPSI
    CS.fill CSfast
  }
  false
end


def samplePassB bubblePt=45, maxStrokes=2  #filter bubblepoint in PSI

#  primePassB
# get ambient pressure
  CTV.to :air
  Delay.sleep 1
  ambientPress = Sampler.intakePress
  
  CBV.connect PSV
  CTV.connect CSV
  PS.fill
  PSV.to :air
  PS.to 0
  PSV.to :CBV
  PS.to 0.5
  CS.to 0
  delay 7
  PSV.to :waste2  #should be positive pressure
  PS.to 0
  PSV.to :CBV
  
  Log.record "Pumping puck up to #{bubblePt}PSI..."
  unless pumpCSto (ambientPress, bubblePt, maxStrokes)
    raise "Did not reach #{bubblePt}psi after #{maxStrokes} CS strokes"
  end

  delay 5
  PS.to 1.1
  delay 3
  PSV.to :air
  PS.fill
  
  CSV.to :air  #vent pressure to air (may spit)
  delay 2
  CSV.to :waste
  CS.to 0
  
  CSV.connect CBV
  CS.to 3
  delay 5
  CTV.to :air
  CSV.to :air
  delay 1
  CSV.to :waste
  CS.to 0
  #now ready to unload filter puck
  #Arm.grab :collection
  #CC.open
  #Arm.releaseAt :flush
  
end

#and then load flush
#at this point the line need to be flushed as in samplePassA
#but since samplePassA uses PS, it must wait for 
#the PS to deliver the sample to the array puck.
#

#this routine flushes the lower loop of the Processing stage
#uses the flushpuck, loads and unloads it.

def psLoopFlush
	#this should be run after SH2 completes 
	loadFlush :processing
	PSV.to :PSR
	PSR.to :flush
	PS.to 1
	PSR.to :air
	PS.fill
	PSV.to :PTV
	PTV.to :puck
	PBV.to :waste1
	PS.to 0
	PSV.to :PSR
	PSR.to :flush
	PS.to .5
	PSR.to :air
	PS.fill
	PSV.to :PTV
	PS.to 2
	PBV.to :PSV
	PS.to 0
	PSV.to :PBV
	PS.fill
	returnFlush :processing
	PSV.to :waste1
	PS.to 0
end
