Example of a hook for checking the motor limits before starting the scan. If the scan range is outside the motor limits this macro will print out an error message asking for stopping the scan with ctrl-C, otherwise the scan will continue. Up to now the errors inside the hooks are warnings and the scan continues. In new Sardana versions an error in the hook will interrupt the scan. In that case this macro will interrupt the scan, the printout of the error and the sleep statement can then be removed.
class gh_pre_scan(Macro): param_def = [ ] def run(self): door = PyTango.DeviceProxy(HasyUtils.getLocalDoorNames()[0]) running_macros = door.RunningMacros scan_macro = door.RunningMacros[1] args_str = scan_macro.split("(")[1] scan_args = args_str.split(",") outside_limits = 0 motor_proxy = [] for i in range(0, int(len(scan_args)/4)): self.output(i) motor_name = scan_args[0 + 4*i].replace(" ", "") pos_start = scan_args[1 + 4*i] pos_stop = scan_args[2 + 4*i] motor_proxy.append(PyTango.DeviceProxy(motor_name)) attrConfig = motor_proxy[i].get_attribute_config_ex("Position") pos_min = attrConfig[0].min_value pos_max = attrConfig[0].max_value if pos_start > pos_stop: pos_start_tmp = pos_start pos_start = pos_stop pos_stop = pos_start_tmp if float(pos_start) < float(pos_min) or float(pos_stop) > float(pos_max): self.error("Scan range outside motor limits for motor %s " % motor_name) outside_limits = 1 if outside_limits: self.error("Stop scan with ctrl-C or it will continue in 10 s") time.sleep(10) raise ValueError("Scan range outside motor limits") return True