diff --git a/pxii_bec/macros/policies.py.save b/pxii_bec/macros/policies.py.save new file mode 100644 index 0000000..0b06b83 --- /dev/null +++ b/pxii_bec/macros/policies.py.save @@ -0,0 +1,73 @@ +'''Define guard policies for devices in the beamline.''' +# from guards import GuardViolation, guards_setup + + +def is_sample_area_clear_for_beamstop(d): + '''Check if the sample area is clear of diag_y, coll_y, and gonx''' + g = guards_setup(d) + if g["diag_y_out"].check() and g["coll_y_out"].check() and g["gonx_out"].check(): + return True + return False + + +def is_sample_area_clear_for_gonx(d): + '''Check if the sample area is clear of diag_y and bs_z''' + g = guards_setup(d) + if g["diag_y_out"].check() and g["bs_work_min"].check(): + return True + return False + + +def make_gon_x_policy(d): + '''Create the policy for gon_x''' + def gon_x_policy(target): + cfg = d["gon_x"].positions + if target >= cfg["out"] and not is_sample_area_clear_for_gonx(d): + raise GuardViolation("Sample area is not clear") + + return gon_x_policy + + +def make_bs_z_policy(d): + '''Create the policy for bs_z''' + def bs_z_policy(target): + """Checks that the target position is within limits""" + cfg = d["bs_z"].positions + # Lower bound + if target < cfg["work_min"] and not is_sample_area_clear_for_beamstop(d): + raise GuardViolation("Sample area is not clear") + if target < cfg["min"]: + raise GuardViolation( + f"Requested beamstop Z {target} is below recommended minimum {cfg['min']}" + ) + # Upper bound + if d["bl_pos"].pos == "in" and target > cfg["max_blin"]: + raise GuardViolation( + f"Beamstop Z cannot move beyond {cfg['max_blin']} when backlight is IN" + ) + + return bs_z_policy + + +def make_diag_y_policy(d): + '''Create the policy for diag_y''' + def diag_y_policy(target): + cfg = d["diag_y"].positions + # Don't move in if the goniometer is >= 'in' + if d["gon_x"].actual >= d["gon_x"].positions["in"] and target > {cfg['out']}: + raise GuardViolation( + f"Diagnostic device cannot move beyond {cfg['out']} mm when goniometer is not OUT" + ) + if d['cryo_pos'].pos == "in" and target > cfg["out"]: + raise GuardViolation( + f"Diagnostic device cannot move beyond {cfg['out']} mm when cryo is IN" + ) + + return diag_y_policy + + +def attach_policies(d): + '''Attach the policies to the devices''' + d["bs_z"].policy = make_bs_z_policy(d) + d["gon_x"].policy = make_gon_x_policy(d) + d["diag_y"].policy = make_diag_y_policy(d)