Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Sync_UB_Matrix #340

Open
prjemian opened this issue Aug 13, 2024 · 1 comment
Open

add Sync_UB_Matrix #340

prjemian opened this issue Aug 13, 2024 · 1 comment

Comments

@prjemian
Copy link
Contributor

prjemian commented Aug 13, 2024

Is there a way motor positions (and UB matrices) of the two engines can be synchronized automatically?

class Sync_UB_Matrix:
    """Copy the UB matrix from source to target diffractometers."""

    def __init__(self, source: Diffractometer, target: Diffractometer):
        self.source = source
        self.target = target
        self.source.UB.subscribe(self.sync_callback)

        # initialize
        self.sync_callback(self.source.UB.get())

    def cleanup(self, *args, **kwargs):
        """Remove all our subscriptions to ophyd objects."""
        self.source.UB.clear_sub(self.sync_callback)

    def sync_callback(self, value=None, **kwargs):
        if value is None:
            raise RuntimeError(f"sync_callback: {value=!r}  {kwargs=!r}")
        ub_source = value
        print(f"Copy UB={ub_source=} from {self.source.name} to {self.source.name}")
        self.target.UB.put(ub_source)

        for axis in self.source.real_positioners._fields:
            ptarget = getattr(self.target, axis)
            if isinstance(ptarget, SoftPositioner):
                # If the target is a simulated motor, sync it with the source.
                psource = getattr(self.source, axis)
                ptarget.move(psource.position)
                print(f"Sync {self.target.name}.{axis}={ptarget.position}")


try:
    sync_UB.cleanup()
except Exception:
    pass
sync_UB = Sync_UB_Matrix(polar, polar_psi)

@strempfer - Thanks for the suggestion!

@prjemian
Copy link
Contributor Author

The __init__() method should raise if the two diffractometers do not have the same geometry.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant