From d7ec186ca29b0b9d4a2f384e4f22172dc5e9b2f0 Mon Sep 17 00:00:00 2001
From: Egor Sobolev <egor.sobolev@xfel.eu>
Date: Wed, 7 Sep 2022 10:05:01 +0200
Subject: [PATCH 1/2] Add monitoring of the motor position

---
 .../AgipdLitFrameFinder.py                    | 27 ++++++++++++-------
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/src/agipdLitFrameFinder/AgipdLitFrameFinder.py b/src/agipdLitFrameFinder/AgipdLitFrameFinder.py
index 3c1938a..2fa8b44 100644
--- a/src/agipdLitFrameFinder/AgipdLitFrameFinder.py
+++ b/src/agipdLitFrameFinder/AgipdLitFrameFinder.py
@@ -504,11 +504,13 @@ class AgipdLitFrameFinder(Device):
         self._queued_data = OrderedDict()
         self._connected = set()
         self.trigger_event = None
+        self.motor_position = None
         self._unknown_event = False
         self._input_status = AlarmCondition.NONE
         self._ready = False
         self._light_in_dark_mode = False
         self._timer = None
+        self._motor = None
 
     async def onInitialization(self):
         """ This method will be called when the device starts.
@@ -538,9 +540,7 @@ class AgipdLitFrameFinder(Device):
             if self.motor.deviceId:
                 # connect to motor
                 self._motor = await connectDevice(self.motor.deviceId)
-                get_property(self._motor, self.motor.positionProperty.value)
-            else:
-                self._motor = None
+                #get_property(self._motor, self.motor.positionProperty.value)
 
             self._reference = {}
             for event_name, event_code in EVENT_CODE.items():
@@ -589,15 +589,18 @@ class AgipdLitFrameFinder(Device):
 
     async def monitor(self):
         """Monitors states of shutters and detecotor"""
-        states = [shut.state for shut, _ in self._shutters]
-        states += [self.agipd.state]
+        properties = [shut.state for shut, _ in self._shutters]
+        properties += [self.agipd.state]
         if self._timer:
             trigger = get_property(self._timer, self.trigger.name.value)
-            states += [trigger.event, trigger.delay]
+            properties += [trigger.event, trigger.delay]
+        if self._motor:
+            position_property = get_property(self._motor, self.motor.positionProperty.value)
+            properties += [position_property]
 
         await self.update_state()
         while True:
-            await waitUntilNew(*states)
+            await waitUntilNew(*properties)
             await self.update_state()
 
     async def update_state(self):
@@ -618,6 +621,10 @@ class AgipdLitFrameFinder(Device):
                 else:
                     self.logger.info(f"Use reference for macro pulse event {event}.")
 
+        if self._motor:
+            self.motor_position = get_property(
+                self._motor, self.motor.positionProperty.value).value
+
         level = self.trump_alarm_level()
         if level == AlarmCondition.ALARM:
             await self.stop_sending()
@@ -839,10 +846,10 @@ class AgipdLitFrameFinder(Device):
         elif self.alignMethod.value == 'by reference delay':
             frame_timestep = RATE_TO_STRIDE[f"{self.ref.repetitionRate:.1f}"] * TICS_PER_PULSE
             shift = self.ref.delay + self.ref.frame * frame_timestep - self.trigger_delay
-            if self._motor:
-                position = get_property(self._motor, self.motor.positionProperty).value
+            if self.motor_position is not None:
                 unit_coef = self.motor.unitConversionCoef.value
-                shift -= unit_coef * (self.ref.position - position) / SPEED_OF_LIGHT
+                shift -= (unit_coef * (self.ref.position
+                                       - self.motor_position) / SPEED_OF_LIGHT)
 
             shift = round(shift / TICS_PER_PULSE)
             ref_pulse = EVENT_PULSE[self.trigger_event](pulses) + self.ref.pulse
-- 
GitLab


From a3615faba1abfdc80c16ebfb25a48a8a8562dec9 Mon Sep 17 00:00:00 2001
From: Egor Sobolev <egor.sobolev@xfel.eu>
Date: Wed, 7 Sep 2022 10:26:54 +0200
Subject: [PATCH 2/2] Add log message on the initialisation completion

---
 src/agipdLitFrameFinder/AgipdLitFrameFinder.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/agipdLitFrameFinder/AgipdLitFrameFinder.py b/src/agipdLitFrameFinder/AgipdLitFrameFinder.py
index 2fa8b44..ce042fa 100644
--- a/src/agipdLitFrameFinder/AgipdLitFrameFinder.py
+++ b/src/agipdLitFrameFinder/AgipdLitFrameFinder.py
@@ -540,7 +540,7 @@ class AgipdLitFrameFinder(Device):
             if self.motor.deviceId:
                 # connect to motor
                 self._motor = await connectDevice(self.motor.deviceId)
-                #get_property(self._motor, self.motor.positionProperty.value)
+                get_property(self._motor, self.motor.positionProperty.value)
 
             self._reference = {}
             for event_name, event_code in EVENT_CODE.items():
@@ -572,6 +572,7 @@ class AgipdLitFrameFinder(Device):
 
         background(self.report_skipped_trains)
         background(self.monitor)
+        self.logger.info("Device is initialised and started")
 
     async def onDestruction(self):
         await self.stop_sending()
-- 
GitLab