From d2830f3158c1525c3249e6a7026199e4481da2e2 Mon Sep 17 00:00:00 2001 From: gac-maloja Date: Tue, 25 Oct 2022 19:02:49 +0200 Subject: [PATCH] moved choice which quadrant is used from constructur to set_target_value; calculate the mean phase as current value for UndPhases; calculate the phase via convert_shift_to_phase as current value for UndPhase --- phases/full_polarization.py | 71 ++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/phases/full_polarization.py b/phases/full_polarization.py index 16dbe5d..0ad80c4 100644 --- a/phases/full_polarization.py +++ b/phases/full_polarization.py @@ -5,9 +5,11 @@ from slic.core.device import SimpleDevice from slic.core.task import Task from slic.utils import json_load from slic.utils import unpickle -from models.parallel_model import parallel2gap -from models.antiparallel_model import antiparallel2gap +from .models.parallel_model import parallel2gap +#from .models.antiparallel_model import antiparallel2gap +from .models.antiparallel_model import antiparallel_k2rad_fit, antiparallel_k2rad_fit from time import sleep +import numpy as np RADIAL_LIMIT = 5.1 @@ -16,10 +18,6 @@ UND_PERIOD_ANTIPARALLEL = 2 * UND_PERIOD_PARALLEL UNDU_FIRST = 10 UNDU_LAST = 22 SETPHASE = -50 -if SETPHASE >= 0.0: - REF_MAG_ARRAY = 'TL' -else: - REF_MAG_ARRAY = 'BL' def check_phase(phase): @@ -32,6 +30,10 @@ def convert_phase_to_shift(phase, und_period): ratio = und_period / 360.0 return phase * ratio / 2 +def convert_shift_to_phase(shift, und_period): + ratio = und_period / 360.0 + return shift / ratio * 2 + class UndPhases(Adjustable): @@ -52,8 +54,15 @@ class UndPhases(Adjustable): def get_current_value(self): - #raise NotImplementedError #TODO: how to do that? - return 123 + vals = np.array([p.get() for p in self.phases]) + vmin = vals.min() + vmax = vals.max() + vmean = vals.mean() + vstd = vals.std() + if vstd > 0.001: + print("there is a large spread in phase values:") + print(f"{vmin} ≤ ({vmean} ± {vstd}) ≤ {vmax}") + return vmean def set_target_value(self, value): @@ -78,14 +87,15 @@ class UndPhase(Adjustable): super().__init__(ID, **kwargs) self.params = params self.isparallel = isparallel - self.shift = UndShift(ID) if isparallel else UndShiftQuadrants(ID, REF_MAG_ARRAY) + self.shift = UndShift(ID) if isparallel else UndShiftQuadrants(ID) self.radial = UndRadial(ID) self.totalk = UndTotalK(ID) def get_current_value(self): - #raise NotImplementedError #TODO: how to do that? - return 123 + shift = self.shift.get_current_value() + und_period = UND_PERIOD_PARALLEL if self.isparallel else UND_PERIOD_ANTIPARALLEL + return convert_shift_to_phase(shift, und_period) def set_target_value(self, value): @@ -155,39 +165,48 @@ class UndRadial(UndShiftRadialBase): class UndShiftQuadrants(Adjustable): - def __init__(self, ID, which, accuracy=0.001): + def __init__(self, ID, accuracy=0.001): ID += "SHIFT" - which = which.upper() + super().__init__(ID, units="xxx") - opposites = { + self.opposites = opposites = { "TL": "BR", "BL": "TR" } inverted_opposites = {v: k for k, v in opposites.items()} opposites.update(inverted_opposites) - all_names = opposites.keys() - opposite = opposites[which] - self.names = names = [which, opposite] - for n in all_names: - if n not in names: - names.append(n) + self.others = others = { + "TL": "BL", + "TR": "BR" + } + inverted_others = {v: k for k, v in others.items()} + others.update(inverted_others) + + + self.all_names = names = opposites.keys() pvnames = [f"{ID}-{n}" for n in names] - adjs = {n.lower(): PVAdjustable(pvn + "-SET", pvn, accuracy=accuracy) for n, pvn in zip(names, pvnames)} + + adjs = {n: PVAdjustable(pvn + "-SET", pvn, accuracy=accuracy) for n, pvn in zip(names, pvnames)} self.adjs = SimpleDevice(ID, **adjs) - self.which_adj = which_adj = adjs[which.lower()] - super().__init__(F"{ID}-{which}", units=which_adj.units) self.pv_on = PV(ID + "-ON") self.pv_go = PV(ID + "-GO") def set_target_value(self, value): - names = (n.lower() for n in self.names) + which = "TL" if value >= 0 else "BL" + opposite = self.opposites[which] + + names = [which, opposite] + for n in self.all_names: + if n not in names: + names.append(n) + vals = (value, -value, 0, 0) tasks = [self.adjs.__dict__[n].set_target_value(v) for n, v in zip(names, vals)] @@ -203,7 +222,9 @@ class UndShiftQuadrants(Adjustable): def get_current_value(self): - return self.which_adj.get_current_value() + vals = [self.adjs.__dict__[n].get_current_value() for n in self.all_names] + print(vals) + return max(vals) #TODO: this is not final def is_moving(self): return any(a.is_moving() for a in self.adjs)