[WIP] fixes for gas10ka / varioxB
treat proxy for io correctly
This commit is contained in:
@ -18,53 +18,82 @@
|
||||
# Jael Celia Lorenzana <jael-celia.lorenzana@psi.ch>
|
||||
# *****************************************************************************
|
||||
|
||||
"""PI control for furnance"""
|
||||
"""soft PI control"""
|
||||
|
||||
import time
|
||||
from frappy.core import Writable, Attached, Parameter, FloatRange, Readable, BoolType, ERROR, IDLE
|
||||
from frappy.lib import clamp
|
||||
from frappy.datatypes import LimitsType, EnumType
|
||||
from frappy.mixins import HasOutputModule
|
||||
|
||||
class PI(Writable):
|
||||
input = Attached(Readable, 'the input module')
|
||||
output = Attached(Writable, 'the output module')
|
||||
relais = Attached(Writable, 'the interlock relais')
|
||||
|
||||
class PImixin(HasOutputModule, Writable):
|
||||
p = Parameter('proportional term', FloatRange(0), readonly=False)
|
||||
i = Parameter('integral term', FloatRange(0), readonly=False)
|
||||
control_active = Parameter('control flag', BoolType(), readonly=False, default=False)
|
||||
value = Parameter(unit='degC')
|
||||
output_range = Parameter('min output', LimitsType(FloatRange()), default=(0, 0), readonly=False)
|
||||
output_func = Parameter('output function', EnumType(lin=0, square=1), readonly=False, default=0)
|
||||
value = Parameter(unit='K')
|
||||
_lastdiff = None
|
||||
_lasttime = 0
|
||||
|
||||
_clamp_limits = None
|
||||
|
||||
def doPoll(self):
|
||||
super().doPoll()
|
||||
if self._clamp_limits is None:
|
||||
out = self.output_module
|
||||
if hasattr(out, 'max_target'):
|
||||
if hasattr(self, 'min_target'):
|
||||
self._clamp_limits = lambda v, o=out: clamp(v, o.read_min_target(), o.read_max_target())
|
||||
else:
|
||||
self._clamp_limits = lambda v, o=out: clamp(v, 0, o.read_max_target())
|
||||
elif hasattr(out, 'limit'): # mercury.HeaterOutput
|
||||
self._clamp_limits = lambda v, o=out: clamp(v, 0, o.read_limit())
|
||||
else:
|
||||
self._clamp_limits = lambda v: v
|
||||
self.log.info('OR %r', self.output_range)
|
||||
if self.output_range == (0.0, 0.0):
|
||||
self.output_range = (0, self._clamp_limits(float('inf')))
|
||||
self.log.info('OR %r', self.output_range)
|
||||
if not self.control_active:
|
||||
return
|
||||
self.value = self.input.value
|
||||
self.status = IDLE, 'controlling'
|
||||
now = time.time()
|
||||
deltat = min(10, now-self._lasttime)
|
||||
deltat = clamp(0, now-self._lasttime, 10)
|
||||
self._lasttime = now
|
||||
diff = self.target - self.value
|
||||
if self.value > 300:
|
||||
self.write_control_active(False)
|
||||
return
|
||||
if self._lastdiff is None:
|
||||
self._lastdiff = diff
|
||||
deltadiff = diff - self._lastdiff
|
||||
self._lastdiff = diff
|
||||
output = self.output.target
|
||||
out = self.output_module
|
||||
output = out.target
|
||||
if self.output_func == 'square':
|
||||
output = match.sqrt(max(0, output))
|
||||
output += self.p * deltadiff + self.i * deltat * diff
|
||||
if output > 100:
|
||||
output = 100
|
||||
elif output < 0:
|
||||
output = 0
|
||||
self.output.write_target(output)
|
||||
|
||||
if self.output_func == 'square':
|
||||
output = output ** 2
|
||||
output = self._clamp_limits(output)
|
||||
out.update_target(self.name, clamp(output, *self.output_range))
|
||||
|
||||
def write_control_active(self, value):
|
||||
if not value:
|
||||
self.output.write_target(0)
|
||||
|
||||
self.output_module.write_target(0)
|
||||
|
||||
def write_target(self, target):
|
||||
if not self.control_active:
|
||||
self.activate_control()
|
||||
|
||||
|
||||
class PI(PImixin, Writable):
|
||||
input = Attached(Readable, 'the input module')
|
||||
relais = Attached(Writable, 'the interlock relais', mandatory=False)
|
||||
|
||||
def read_value(self):
|
||||
return self.input.value
|
||||
|
||||
def write_target(self, value):
|
||||
self.control_active = True
|
||||
self.relais.write_target(1)
|
||||
|
||||
|
||||
super().write_target(value)
|
||||
if self.relais:
|
||||
self.relais.write_target(1)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user