def go_plan(): ret = (yield from bps.abs_set(pgm.fly.fly_start, 1)) st = StatusBase() enum_map = pgm.fly.scan_status.describe()[ pgm.fly.scan_status.name]['enum_strs'] def _done_cb(value, old_value, **kwargs): print(f'Old value {old_value} -> new value {value}') print( f'Old value type {type(old_value)} -> new value {type(value)}') try: old_value = enum_map[int(old_value)] except (TypeError, ValueError): ... try: value = enum_map[int(value)] except (TypeError, ValueError): ... if old_value != value and value == 'Ready': st._finished() pgm.fly.scan_status.clear_sub(_done_cb) if ret is not None: pgm.fly.scan_status.subscribe(_done_cb, run=False) else: st._finished() print('SIM MODE') return st
def _start_motion_thread(self, value, **kwargs): self._status = StatusBase() self._status_ready_event.set() if value == 'Unknown': self._status.set_exception( RuntimeError('Unknown not a valid target state')) else: td = threading.Thread(target=self._motion_thread) td.start()
def kickoff(): # plan for moving st = StatusBase() # move once every second for 2 min for i in range(24): # 24*5 = 120 for loc in np.linspace(-delta / 2, delta / 2, 5): # 5s, RE.loop.call_later(1, lambda v=loc: motor.set(v)) st._finished() return st
def test_pardon_failures(RE): from ophyd import StatusBase st = StatusBase() class Dummy: name = 'dummy' def set(self, val): return st dummy = Dummy() RE([Msg('set', dummy, 1)]) st._finished(success=False) RE([Msg('null')])
def wiggle_plan(dets, motor, delta, *, md=None): ''' dets, a list of detectors to read, must have det.cam.acquire_time attribute delta, relative range to wiggle move through range in 5 step ''' original_pos = motor.position() st = StatusBase() def kickoff(): # plan for moving yield Msg('null') # ??? # move once every second for 2 min for i in range(24): # 24*5 = 120 for loc in np.linspace(-delta/2, delta/2, 5): # 5s, RE.loop.call_later(1, lambda v=loc: motor.set(v)) return st def inner_plan(): yield from bps.trigger_and_read(dets) def clean_up(): yield from bps.mv(motor, original_pos) timeout = dets[0].cam.acquire_time.value rp = bp.ramp_plan(kickoff(), motor, inner_plan, timeout=timeout, md=md) return (yield from bpp.finalize_wrapper(rp, clean_up()))
class ExampleComboPositioner(Device, PositionerBase): """ A rewrite of ExamplePositioner that shows off the combobox. LCLS uses a few "positioner" objects that move to named strings instead of floating point positions, and this facilitates that behavior. """ user_readback = Cpt(Signal, value='OUT', kind='hinted') user_setpoint = Cpt(Signal, value='Unknown') motor_is_moving = Cpt(Signal, value=False) stop = None def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._status = None self._status_ready_event = threading.Event() enums = ('Unknown', 'OUT', 'TARGET1', 'TARGET2') self.user_readback.enum_strs = enums self.user_setpoint.enum_strs = enums def set(self, position): self._status_ready_event = threading.Event() self.user_setpoint.put(position) self._status_ready_event.wait() return self._status @user_setpoint.sub_value def _start_motion_thread(self, value, **kwargs): self._status = StatusBase() self._status_ready_event.set() if value == 'Unknown': self._status.set_exception( RuntimeError('Unknown not a valid target state')) else: td = threading.Thread(target=self._motion_thread) td.start() def _motion_thread(self): self.motor_is_moving.put(True) self.user_readback.put('Unknown') time.sleep(3) self.user_readback.put(self.user_setpoint.get()) self.motor_is_moving.put(False) self._status.set_finished()
def kickoff(): # This process should take a total of 5 seconds, but it will be # interrupted after 0.1 seconds via the timeout keyword passed to # ramp_plan below. yield Msg('null') for j in range(100): RE.loop.call_later(.05 * j, lambda j=j: mot.set(j)) return StatusBase()
def go_plan(): ret = (yield from bps.abs_set(pgm.fly.fly_start, 1)) st = StatusBase() enum_map = pgm.fly.scan_status.describe()[pgm.fly.scan_status.name]['enum_strs'] def _done_cb(value, old_value, **kwargs): old_value = enum_map[int(old_value)] value = enum_map[int(value)] if old_value != value and value == 'Ready': st._finished() pgm.fly.scan_status.clear_sub(_done_cb) if ret is not None: pgm.fly.scan_status.subscribe(_done_cb, run=False) else: st._finished() print('SIM MODE') return st
def go_plan(): ret = (yield from bps.abs_set(pgm.fly.fly_start, 1)) st = StatusBase() enum_map = pgm.fly.scan_status.describe()[ pgm.fly.scan_status.name]['enum_strs'] def _done_cb(value, old_value, **kwargs): old_value = enum_map[int(old_value)] value = enum_map[int(value)] if old_value != value and value == 'Ready': st._finished() pgm.fly.scan_status.clear_sub(_done_cb) if ret is not None: pgm.fly.scan_status.subscribe(_done_cb, run=False) else: st._finished() print('SIM MODE') return st
def test_ramp(RE): from ophyd.positioner import SoftPositioner from ophyd import StatusBase from ophyd.sim import SynGauss tt = SoftPositioner(name='mot') tt.set(0) dd = SynGauss('det', tt, 'mot', 0, 3) st = StatusBase() def kickoff(): yield Msg('null') for j, v in enumerate(np.linspace(-5, 5, 10)): RE.loop.call_later(.1 * j, lambda v=v: tt.set(v)) RE.loop.call_later(1.2, st._finished) return st def inner_plan(): yield from trigger_and_read([dd]) g = ramp_plan(kickoff(), tt, inner_plan, period=0.08) db = DocCollector() RE.subscribe(db.insert) rs = RE(g) if RE.call_returns_result: uid = rs.run_start_uids[0] else: uid = rs[0] assert db.start[0]['uid'] == uid assert len(db.descriptor[uid]) == 2 descs = {d['name']: d for d in db.descriptor[uid]} assert set(descs) == set(['primary', 'mot_monitor']) primary_events = db.event[descs['primary']['uid']] assert len(primary_events) > 11 monitor_events = db.event[descs['mot_monitor']['uid']] # the 10 from the updates, 1 from 'run at subscription time' assert len(monitor_events) == 11
def test_ramp(RE, db): from ophyd.positioner import SoftPositioner from ophyd import StatusBase from bluesky.examples import SynGauss tt = SoftPositioner(name='mot') tt.set(0) dd = SynGauss('det', tt, 'mot', 0, 3) st = StatusBase() def kickoff(): yield Msg('null') for j, v in enumerate(np.linspace(-5, 5, 10)): RE.loop.call_later(.1 * j, lambda v=v: tt.set(v)) RE.loop.call_later(1.2, st._finished) return st def inner_plan(): yield from trigger_and_read([dd]) g = ramp_plan(kickoff(), tt, inner_plan, period=0.08) RE.subscribe('all', db.mds.insert) RE.msg_hook = MsgCollector() rs_uid, = RE(g) hdr = db[-1] assert hdr.start.uid == rs_uid assert len(hdr.descriptors) == 2 assert set([d['name'] for d in hdr.descriptors]) == \ set(['primary', 'mot_monitor']) primary_events = list(db.get_events(hdr, stream_name='primary')) assert len(primary_events) > 11 monitor_events = list(db.get_events(hdr, stream_name='mot_monitor')) assert len(monitor_events) == 10
def _start_motion_thread(self, value, **kwargs): self.stop(success=True) self._status = StatusBase() self._status_ready_event.set() td = threading.Thread(target=self._motion_thread) td.start()
class ExamplePositioner(Device, PositionerBase): """ An example in-software positioner that works with the positioner widget. This behaves more or less like you'd expect a real motor to behave. """ user_readback = Cpt(Signal, value=0.0, kind='hinted', metadata={'precision': 3}) user_setpoint = Cpt(Signal, value=0.0) low_limit_switch = Cpt(Signal, value=False) high_limit_switch = Cpt(Signal, value=False) low_limit_travel = Cpt(Signal, value=-10.0) high_limit_travel = Cpt(Signal, value=10.0) velocity = Cpt(Signal, value=1.0) acceleration = Cpt(Signal, value=1.0) motor_is_moving = Cpt(Signal, value=False) error_message = Cpt(Signal, value='') cause_error = Cpt(Signal, value='') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._status = None self._status_ready_event = threading.Event() def set(self, position): self._status_ready_event.clear() self.user_setpoint.put(position) self._status_ready_event.wait() return self._status @user_setpoint.sub_value def _start_motion_thread(self, value, **kwargs): self.stop(success=True) self._status = StatusBase() self._status_ready_event.set() td = threading.Thread(target=self._motion_thread) td.start() def _motion_thread(self): self.motor_is_moving.put(True) while not self._status.done: self._step_position() self.motor_is_moving.put(False) def _step_position(self): time_step = 0.1 noise_factor = 0.1 noise = noise_factor * random.uniform(-1, 1) dist = self.user_setpoint.get() - self.user_readback.get() velo = self.velocity.get() * (1 + noise) step = velo * time_step if abs(dist) < step: self.user_readback.put(self.user_setpoint.get() + noise / 10) self._status.set_finished() elif dist > 0: self.user_readback.put(self.user_readback.get() + step) elif dist < 0: self.user_readback.put(self.user_readback.get() - step) time.sleep(time_step) def stop(self, success=False): if self._status is not None and not self._status.done: if success: self._status.set_finished() else: self._status.set_exception(RuntimeError('Move Interrupted')) @user_readback.sub_value def _update_position(self, value, **kwargs): if self.low_limit_travel.get() or self.high_limit_travel.get(): bot_hit = value <= self.low_limit_travel.get() top_hit = value >= self.high_limit_travel.get() self.low_limit_switch.put(bot_hit) self.high_limit_switch.put(top_hit) @low_limit_switch.sub_value @high_limit_switch.sub_value def _limit_hit(self, value, **kwargs): if value: self.stop(success=True) def clear_error(self): self.error_message.put('') @cause_error.sub_value def _cause_error(self, value, **kwargs): self.error_message.put(value)
def set(self, inp): st = StatusBase() RE.loop.call_later(1, lambda: st._finished(success=False)) return st
def trigger(self): st = StatusBase() fresh_RE.loop.call_later(1, lambda: st._finished(success=False)) return st
def kickoff(): yield Msg('null') for j in range(5): RE.loop.call_later(.05 * j, lambda j=j: mot.set(j)) return StatusBase()
def set(self, val): st = StatusBase() st._finished(success=False) return st
def trigger(self): st = StatusBase() threading.Timer(1, st._finished, kwargs=dict(success=False)).start() return st
def kickoff(): st = StatusBase() for i in range(20): yield from mvr(motor, -1) yield from mvr(motor, 1) return st
from bluesky.plans import ramp_plan from bluesky.plan_stubs import trigger_and_read from bluesky import Msg from bluesky.utils import RampFail import numpy as np import time from ophyd.positioner import SoftPositioner from ophyd import StatusBase from ophyd.sim import SynGauss tt = SoftPositioner(name='mot') tt.set(0) dd = SynGauss('det', tt, 'mot', 0, 3) st = StatusBase() def kickoff(): yield Msg('null') for j, v in enumerate(np.linspace(-2, 2, 10)): RE.loop.call_later(.1 * j, lambda v=v: tt.set(v)) RE.loop.call_later(1.2, st._finished) return st def inner_plan(): print(tt.position) yield from trigger_and_read([dd])