## Script file for Flushing/Injecting/Extracting
## Target: MRS System, 13 July 2012 Deployment

##############################################################################
# Internal Variables
##############################################################################

# time to wait after a flush before injecting
$flushSettlingTime = 2

##############################################################################
# Script Variables
##############################################################################

$startupWait = 14400
$flushTime = 600
$stirCycleTime = 42

## $incTime no longer used in this script
#Note - Minimum incTime is 8*($flushTime+$flushSettlingTime+$mixingDelay)
#$incTime = 43200

# Need to calculate how many stir cycles we need per incubation period.
# Assumptions:
#   Incubation period is 12 hours
#   Flushing takes 10 minutes (see $flushTime)
#   So for a blank cycle, we need to stir for 11:20 (hours:minutes)
#     and the number of stir cycles is 680 minutes * 60 / 42 seconds = 971
#   Injection cycles take longer for the flush+injection time, so there are
#     slightly fewer stir cycles

$blankStirCycles = 971
$injStirCycles = 954

## Injection volumes in ml
$inj3_ml = 50
$inj4_ml = 50
$inj7_ml = 50
$inj8_ml = 50

## Seconds to stir after injecting
$injStirTime = 120


##############################################################################
# Script Body
##############################################################################

# First, wait for the user to type "$startMRS = 1".
# Set a timeout of 4 hours in case comms is lost

wait_variable $startMRS, $startupWait


##############################################################################
# Deployment consists of 4 cycles -- Blank cycle, injection cycle, blank, injection
# Since they're duplicates, we can repeat twice for blank then injection
#

repeat 2
## Blank cycle
  output_on flush1
  repeat_interval 14, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next
  output_off flush1

  output_on flush2
  repeat_interval 14, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next
  output_off flush2

  output_on flush3
  repeat_interval 14, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next
  output_off flush3

  output_on flush4
  repeat_interval 14, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next
  output_off flush4

## Flushing complete in blank cycle, now just stir
  repeat_interval $blankStirCycles, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next

## Completed blank cycle
## Injection cycle starts here
#  Chambers 1, 2, 5, and 6 are still blanks
  output_on flush1
  repeat_interval 14, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next
  output_off flush1

  output_on flush2
  repeat_interval 14, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next
  output_off flush2

  output_on flush3
  repeat_interval 14, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next
  output_off flush3
# inject co2 into chamber 3 while stirring.  Follow with more stirring
  output_on stir3
  pulse_on co2_inj3, $inj3_ml
  pulse_on stir3, $injStirTime
# Now do the same for chamber 7, which was flushed with chamber 3
  output_on stir7
  pulse_on co2_inj7, $inj7_ml
  pulse_on stir7, $injStirTime

  output_on flush4
  repeat_interval 14, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next
  output_off flush4
# inject co2 into chamber 4 while stirring.  Follow with more stirring
  output_on stir4
  pulse_on co2_inj4, $inj4_ml
  pulse_on stir4, $injStirTime
# Now do the same for chamber 8, which was flushed with chamber 4
  output_on stir8
  pulse_on co2_inj8, $inj8_ml
  pulse_on stir8, $injStirTime

## Flushing and injection complete, now just stir
  repeat_interval $injStirCycles, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next

next

## All experimental cycles are done.
## Now just stir forever
  repeat_interval forever, $stirCycleTime
    pulse_on stir1, 5
    pulse_on stir2, 5
    pulse_on stir3, 5
    pulse_on stir4, 5
    pulse_on stir5, 5
    pulse_on stir6, 5
    pulse_on stir7, 5
    pulse_on stir8, 5
  next

