chiark / gitweb /
A working version, ish
authorAlexandra Lanes <ajlanes@chiark.greenend.org.uk>
Sun, 3 Jul 2022 21:09:14 +0000 (23:09 +0200)
committerAlexandra Lanes <ajlanes@chiark.greenend.org.uk>
Sun, 3 Jul 2022 21:09:14 +0000 (23:09 +0200)
legofocuser.py

index 59c81d633d9ef337699a79b703a1eeb6885b5135..34ac757ee76f523800cd02addedbca8e23233d21 100755 (executable)
@@ -16,6 +16,11 @@ https://www.indilib.org/developers/driver-howto.html#h2-properties
 
 
 class LegoFocuser(device):
+
+    offset = 0
+    speed = 50
+    direction = 1
+    flip = 1
     
     def ISGetProperties(self, device=None):
         """Property Definiations are generated
@@ -67,7 +72,7 @@ class LegoFocuser(device):
                 
                 focus = self.IUUpdate(device, name, names, values)
 
-                self.update_speed_direction()
+                self.update_speed()
 
                 # No exceptions, so set the status to OK
 
@@ -95,10 +100,13 @@ class LegoFocuser(device):
                 focpos = self.IUUpdate(device, name, names, values)
                 focposval = focpos["FOCUS_RELATIVE_POSITION"].value
 
-                # Ticks are degrees for us.  Speed is the motor's
-                # default
+                self.IDMessage(f"Request to focus relative by {focposval}")
+                
+                # Ticks are degrees for us.
                 
-                self.motor.run_for_degrees(focposval, blocking=False)
+                self.motor.run_for_degrees(focposval,
+                                           speed = self.speed * self.direction * self.flip,
+                                           blocking=False)
 
                 # We now need to set the state to Busy until the motor
                 # stops running.
@@ -126,11 +134,18 @@ class LegoFocuser(device):
                 focpos = self.IUUpdate(device, name, names, values)
                 focposval = focpos["FOCUS_ABSOLUTE_POSITION"].value
 
-                # Assuming 0 is the bottom of the focuser travel
+                # both focposval and curpos are in real units, rather
+                # than what is reported by the motor.
+
+                curpos = self.get_position()
+
+                self.IDMessage(f"Moving from {curpos} to {focposval}")
+                
                 # we move to the new position
 
-                curpos = self.motor.get_position()
-                self.motor.run_for_degrees(focposval-curpos, blocking=False)
+                self.motor.run_for_degrees(focposval-curpos,
+                                           speed = self.speed * self.direction * self.flip,
+                                           blocking=False)
                 
                 # We now need to set the state to Busy until the motor
                 # stops running.
@@ -147,6 +162,17 @@ class LegoFocuser(device):
                 self.IDMessage(f"IUUpdate error: {error}")
                 raise
 
+        if name == "FOCUS_SYNC":
+
+            try:
+                focus = self.IUUpdate(device, name, names, values)
+                syncvalue = focus["FOCUS_SYNC_VALUE"].value
+                curpos = self.motor.get_position()
+                self.offset = syncvalue-curpos
+            except Exception as error:
+                self.IDMessage(f"FOCUS_SYNC error: {error}")
+                raise
+            
         if name == "FOCUS_TIMER":
 
             try:
@@ -183,18 +209,26 @@ class LegoFocuser(device):
                     self.motor = Motor(portval)
                     conn.state = "Ok"
                     self._is_connected = True
-                    
                 self.IDSet(conn)
 
             except Exception as error:
                 self.IDMessage(f"IUUpdate error: {error}")
                 raise
-            
+        if name == "FOCUS_REVERSE_MOTION":
+            try:
+                focus = self.IUUpdate(device, name, names, values, Set=True)
+
+                self.update_direction()
+            except Exception as error:
+                self.IDMessage(f"FOCUS_REVERSE_MOTION error: {error}")
+                focus.state='Alert'
+                self.IDSet(focus)
+                
         if name == "FOCUS_MOTION":
             try:
                 focus = self.IUUpdate(device, name, names, values, Set=True)
 
-                self.update_speed_direction()
+                self.update_direction()
 
                 # No exceptions, so set the status to OK
 
@@ -224,7 +258,7 @@ class LegoFocuser(device):
                 
     def checkmotoridle(self):
         afocpos = self.IUFind("ABS_FOCUS_POSITION")
-        afocpos["FOCUS_ABSOLUTE_POSITION"].value = self.motor.get_position()
+        afocpos["FOCUS_ABSOLUTE_POSITION"].value = self.get_position()
         if self.motor._runmode == MotorRunmode.NONE:
             focpos = self.IUFind("REL_FOCUS_POSITION")
             focpos.state = "Ok"
@@ -234,6 +268,12 @@ class LegoFocuser(device):
             self.IEAddTimer(100, self.checkmotoridle)
         self.IDSet(afocpos)
 
+    def get_position(self):
+        # Get the motor position, accounting for offset
+        curpos = self.motor.get_position()
+        self.IDMessage(f"position is {curpos} and offset is {self.offset}")
+        return curpos+self.offset
+        
     def run_until_resistance(self):
 
         # run motor until it appears it encounters resistance
@@ -248,17 +288,21 @@ class LegoFocuser(device):
         while abs(pos-ipos) < 15:
             self.motor.run_for_degrees(360)
             self.motor.run_to_position(ipos)
-            pos = self.motor.get_position()
+            pos = self.get_position()
             revs += 1
             
         return revs
             
-    def update_speed_direction(self):
-
+    def update_speed(self):
+        
         # Absolute speed
 
-        speed = int(self.IUFind("FOCUS_SPEED")["FOCUS_SPEED_VALUE"].value)
+        self.speed = int(self.IUFind("FOCUS_SPEED")["FOCUS_SPEED_VALUE"].value)
 
+        self.IDMessage(f"Updating speed to {speed}")
+
+    def update_direction(self):
+        
         # since the Build HAT API uses positive/negative speed
         # for direction, pull out the direction of focus motion
                 
@@ -268,10 +312,9 @@ class LegoFocuser(device):
 
         if (self.IUFind("FOCUS_REVERSE_MOTION"))["ENABLED"].value == 'On':
             direction *= -1
-
-        # And send the result to the HAT.
-        
-        self.motor.set_default_speed(speed * direction)
+            self.flip = -1
+        else:
+            self.flip = 1
 
 
 lf = LegoFocuser()