fix new statemachine with ips magfield

+ add on_error, on_restart etc. to states.py
+ add n_retry argument to mercury change/multichange
This commit is contained in:
2022-12-19 15:12:00 +01:00
parent 397ca350f8
commit b7a1f17e5e
4 changed files with 119 additions and 67 deletions

View File

@ -26,7 +26,7 @@ from secop.lib.enum import Enum
from secop.errors import BadValueError, HardwareError
from secop_psi.magfield import Magfield, SimpleMagfield, Status
from secop_psi.mercury import MercuryChannel, off_on, Mapped
from secop.lib.statemachine import Retry
from secop.states import Retry
Action = Enum(hold=0, run_to_set=1, run_to_zero=2, clamped=3)
hold_rtoz_rtos_clmp = Mapped(HOLD=Action.hold, RTOS=Action.run_to_set,
@ -65,6 +65,13 @@ class SimpleField(MercuryChannel, SimpleMagfield):
obj = object.__new__(newclass)
return obj
def initModule(self):
super().initModule()
try:
self.write_action(Action.hold)
except Exception as e:
self.log.error('can not set to hold %r', e)
def read_value(self):
return self.query('PSU:SIG:FLD')
@ -94,12 +101,12 @@ class SimpleField(MercuryChannel, SimpleMagfield):
def set_and_go(self, value):
self.setpoint = self.change('PSU:SIG:FSET', value)
assert self.write_action('hold') == 'hold'
assert self.write_action('run_to_set') == 'run_to_set'
assert self.write_action(Action.hold) == Action.hold
assert self.write_action(Action.run_to_set) == Action.run_to_set
def start_ramp_to_target(self, sm):
# if self.action != 'hold':
# assert self.write_action('hold') == 'hold'
# if self.action != Action.hold:
# assert self.write_action(Action.hold) == Action.hold
# return Retry
self.set_and_go(sm.target)
sm.try_cnt = 5
@ -116,13 +123,11 @@ class SimpleField(MercuryChannel, SimpleMagfield):
return Retry
def final_status(self, *args, **kwds):
print('FINAL-hold')
self.write_action('hold')
self.write_action(Action.hold)
return super().final_status(*args, **kwds)
def on_restart(self, sm):
print('ON_RESTART-hold', sm.sm)
self.write_action('hold')
self.write_action(Action.hold)
return super().on_restart(sm)
@ -136,7 +141,7 @@ class Field(SimpleField, Magfield):
_field_mismatch = None
__init = True
__switch_heater_fix = 0
__switch_fixed_until = 0
def doPoll(self):
super().doPoll()
@ -208,7 +213,8 @@ class Field(SimpleField, Magfield):
value = self.query('PSU:SIG:SWHT', off_on)
now = time.time()
if value != self.switch_heater:
if now < self.__switch_heater_fix:
if now < self.__switch_fixed_until:
self.log.debug('correct fixed switch time')
# probably switch heater was changed, but IPS reply is not yet updated
if self.switch_heater:
self.switch_on_time = time.time()
@ -228,8 +234,10 @@ class Field(SimpleField, Magfield):
self.log.info('switch heater already %r', value)
# we do not want to restart the timer
return value
self.__switch_heater_fix = time.time() + 10
return self.change('PSU:SIG:SWHT', value, off_on)
self.__switch_fixed_until = time.time() + 10
self.log.debug('switch time fixed for 10 sec')
result = self.change('PSU:SIG:SWHT', value, off_on, n_retry=0) # no readback check
return result
def start_ramp_to_field(self, sm):
if abs(self.current - self.persistent_field) <= self.tolerance:
@ -241,9 +249,7 @@ class Field(SimpleField, Magfield):
if self.switch_heater:
self.log.warn('switch is already on!')
return self.ramp_to_field
self.log.warn('wait first for switch off current=%g pf=%g', self.current, self.persistent_field)
return Retry
self.status = Status.PREPARING, 'wait for switch off'
self.log.warn('wait first for switch off current=%g pf=%g %r', self.current, self.persistent_field, e)
sm.after_wait = self.ramp_to_field
return self.wait_for_switch
return self.ramp_to_field
@ -270,7 +276,7 @@ class Field(SimpleField, Magfield):
return Retry
def wait_for_switch(self, sm):
if not self.delay(10):
if not sm.delta(10):
return Retry
try:
self.log.warn('try again')
@ -282,8 +288,8 @@ class Field(SimpleField, Magfield):
def start_ramp_to_zero(self, sm):
try:
assert self.write_action('hold') == 'hold'
assert self.write_action('run_to_zero') == 'run_to_zero'
assert self.write_action(Action.hold) == Action.hold
assert self.write_action(Action.run_to_zero) == Action.run_to_zero
except (HardwareError, AssertionError) as e:
self.log.warn('switch not yet ready %r', e)
self.status = Status.PREPARING, 'wait for switch off'
@ -298,6 +304,6 @@ class Field(SimpleField, Magfield):
sm.try_cnt -= 1
if sm.try_cnt < 0:
raise
assert self.write_action('hold') == 'hold'
assert self.write_action('run_to_zero') == 'run_to_zero'
assert self.write_action(Action.hold) == Action.hold
assert self.write_action(Action.run_to_zero) == Action.run_to_zero
return Retry