class LegoFocuser(device):
+
+ offset = 0
+ speed = 50
+ direction = 1
+ flip = 1
def ISGetProperties(self, device=None):
"""Property Definiations are generated
focus = self.IUUpdate(device, name, names, values)
- self.update_speed_direction()
+ self.update_speed()
# No exceptions, so set the status to OK
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.
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.
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:
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
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"
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
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
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()