proc make17tfNv {} {
  Layout 17tf
  catch {
    makeobject flowtf array logged
    flowtf makeitem int 1
    flowtf makeitem closed 0.4
    flowtf makeitem opened 4.0
    flowtf makeitem target 2.0
    flowtf makeitem mode fixed
    flowtf makeitem pos 0.5
  }
  hsetprop /nv read flowtask17tf
}

proc flowtask17tf {} {
  if {[result flowtf mode] eq "controlled"} {
    set pos [result flowtf pos]
    set apos [result 17tf nv]
    if {$pos > $apos + 0.1} {
      set pos [expr $apos + 0.1]
    }
    if {$pos < $apos - 0.1} {
      set pos [expr $apos - 0.1]
    }
    set dif [expr [result flowtf target] - [result nv flow]]
    set new [expr $pos + $dif * [result flowtf int] * 0.001]
    if {$new < [result flowtf closed]} {
      set new [result flowtf closed]
    }
    if {$new > [result flowtf opened]} {
      set new [result flowtf opened]
    }
    flowtf pos $new
    if {[result 17tf valvestatus] eq "Idle"} {
      17tf setposition $new
    }
  } else {
#clientput "FIXED"
  }
  return idle
}

proc 17tfLayout {} {
  Group nv17tf "flow control"
}

proc nv17tfGroup {} {

  RadioGroup "flowtf mode"

  Tip "control flow at a given setpoint"
  RadioButton controlled
  NoNewline
  Tip "fixed needle valve"
  RadioButton fixed
  Newline

  Input "pressure target" "flowtf target"
  Input "gain" "flowtf int"
  Input "min" "flowtf closed"
  Input "max" "flowtf opened"
   
}
