def wait_for_completion(self): message_type = c_word() message_id = c_word() message_data = c_dword() kcdc.CC_WaitForMessage(self._serialno, byref(message_type), byref(message_id), byref(message_data)) while int(message_type.value) != 2 or int(message_id.value) != 0: kcdc.CC_WaitForMessage(self._serialno, byref(message_type), byref(message_id), byref(message_data))
MOT_TravelModes = c_int # enum MOT_LimitsSoftwareApproachPolicy DisallowIllegalMoves = c_short(0x00) AllowPartialMoves = c_short(0x01) AllowAllMoves = c_short(0x02) MOT_LimitsSoftwareApproachPolicy = c_short # enum MOT_HomeLimitSwitchDirection MOT_LimitSwitchDirectionUndefined = c_short(0x00) MOT_ReverseLimitSwitch = c_short(0x01) MOT_ForwardLimitSwitch = c_short(0x02) MOT_HomeLimitSwitchDirection = c_short # enum MOT_LimitSwitchModes MOT_LimitSwitchModeUndefined = c_word(0x00) MOT_LimitSwitchIgnoreSwitch = c_word(0x01) MOT_LimitSwitchMakeOnContact = c_word(0x02) MOT_LimitSwitchBreakOnContact = c_word(0x03) MOT_LimitSwitchMakeOnHome = c_word(0x04) MOT_LimitSwitchBreakOnHome = c_word(0x05) MOT_PMD_Reserved = c_word(0x06) MOT_LimitSwitchIgnoreSwitchSwapped = c_word(0x81) MOT_LimitSwitchMakeOnContactSwapped = c_word(0x82) MOT_LimitSwitchBreakOnContactSwapped = c_word(0x83) MOT_LimitSwitchMakeOnHomeSwapped = c_word(0x84) MOT_LimitSwitchBreakOnHomeSwapped = c_word(0x85) MOT_LimitSwitchModes = c_word # enum MOT_LimitSwitchSWModes MOT_LimitSwitchSWModeUndefined = c_word(0)
MOT_DirectionSense = c_short # enum MOT_JogModes MOT_JogModeUndefined = c_short(0x00) MOT_Continuous = c_short(0x01) MOT_SingleStep = c_short(0x02) MOT_JogModes = c_short # enum MOT_StopModes MOT_StopModeUndefined = c_short(0x00) MOT_Immediate = c_short(0x01) MOT_Profiled = c_short(0x02) MOT_StopModes = c_short # enum MOT_ButtonModes MOT_ButtonModeUndefined = c_word(0x00) MOT_JogMode = c_word(0x01) MOT_Preset = c_word(0x02) MOT_ButtonModes = c_word # enum MOT_LimitSwitchModes MOT_LimitSwitchModeUndefined = c_word(0x00) MOT_LimitSwitchIgnoreSwitch = c_word(0x01) MOT_LimitSwitchMakeOnContact = c_word(0x02) MOT_LimitSwitchBreakOnContact = c_word(0x03) MOT_LimitSwitchMakeOnHome = c_word(0x04) MOT_LimitSwitchBreakOnHome = c_word(0x05) MOT_PMD_Reserved = c_word(0x06) MOT_LimitSwitchIgnoreSwitchSwapped = c_word(0x81) MOT_LimitSwitchMakeOnContactSwapped = c_word(0x82) MOT_LimitSwitchBreakOnContactSwapped = c_word(0x83)
MOT_HomeLimitSwitchDirection = c_short # enum MOT_JogModes MOT_JogModeUndefined = c_short(0x00) MOT_Continuous = c_short(0x01) MOT_SingleStep = c_short(0x02) MOT_JogModes = c_short # enum MOT_StopModes MOT_StopModeUndefined = c_short(0x00) MOT_Immediate = c_short(0x01) MOT_Profiled = c_short(0x02) MOT_StopModes = c_short # enum MOT_ButtonModes MOT_ButtonModeUndefined = c_word(0x00) MOT_JogMode = c_word(0x01) MOT_Preset = c_word(0x02) MOT_ButtonModes = c_word # enum MOT_VelocityProfileModes MOT_Trapezoidal = c_word(0x00) MOT_SCurve = c_word(0x02) MOT_VelocityProfileModes = c_word # enum MOT_LimitSwitchModes MOT_LimitSwitchModeUndefined = c_word(0x00) MOT_LimitSwitchIgnoreSwitch = c_word(0x01) MOT_LimitSwitchMakeOnContact = c_word(0x02) MOT_LimitSwitchBreakOnContact = c_word(0x03) MOT_LimitSwitchMakeOnHome = c_word(0x04)
def wait_for_completion(self, id="homed", MAX_WAIT_TIME=5): """ A blocking function to ensure a task has been finished. Used to for the following functions: homing, moving, stopping, or updating limits. The id parameter must be specificed for the correct operation ("homed", "moved", "stopped", or "limit_updated"). If the task is not finished after MAX_WAIT_TIME, a RuntimeError is raised. If the id is "homed", MAX_WAIT_TIME is ignored. Parameters ---------- id : str, optional must be either "homed", "moved", "stopped", or "limit_updated" (default is "homed"). MAX_WAIT_TIME : int, optional The maximum time to wait for the task to complete. Defaults to 5 seconds. Ignored if id is "homed" or if value is 0. Raises ------ RuntimeError If the task is not finished after MAX_WAIT_TIME seconds. """ log.debug("Entering `wait_for_completion()`") # TODO: "id" is a reserved keyword in Python. Change this! message_type = c_word() message_id = c_word() message_data = c_dword() cond = self.CONDITIONS.index(id) log.debug(f"Condition index: {cond} - {id} ") # If the motor is already stopped, perhaps because it reached a limit, # it will wait forever for a message! So just return. if id == "stopped": if not self.is_moving(): log.debug("Exiting `wait_for_completion()`") return elif id == "homed": # Homing might take a while. MAX_WAIT_TIME = 0 while kcdc.CC_MessageQueueSize(self._serialno) <= 0: log.debug(f"Waiting for message (KDC101 '{self.serialno}')") time.sleep(0.2) kcdc.CC_WaitForMessage( self._serialno, byref(message_type), byref(message_id), byref(message_data) ) log.debug("Entering wait loop") start = time.time() while int(message_type.value) != 2 or int(message_id.value) != cond: end = time.time() if (end - start > MAX_WAIT_TIME) and (MAX_WAIT_TIME != 0): log.debug(f"Message queue size: {kcdc.CC_MessageQueueSize(self._serialno)}") raise RuntimeError( f"Waited for {MAX_WAIT_TIME} seconds for {id} to complete. " f"Message type: {message_type.value}, Message ID: {message_id.value}") if kcdc.CC_MessageQueueSize(self._serialno) <= 0: log.debug(f"Waiting for message (KDC101 '{self.serialno}')") time.sleep(0.2) continue kcdc.CC_WaitForMessage( self._serialno, byref(message_type), byref(message_id), byref(message_data)) log.debug("Exiting `wait_for_completion()`")
KMOT_WheelDirectionSense = c_short # enum MOT_JogModes MOT_JogModeUndefined = c_short(0x00) MOT_Continuous = c_short(0x01) MOT_SingleStep = c_short(0x02) MOT_JogModes = c_short # enum MOT_StopModes MOT_StopModeUndefined = c_short(0x00) MOT_Immediate = c_short(0x01) MOT_Profiled = c_short(0x02) MOT_StopModes = c_short # enum MOT_ButtonModes MOT_ButtonModeUndefined = c_word(0x00) MOT_JogMode = c_word(0x01) MOT_Preset = c_word(0x02) MOT_ButtonModes = c_word # enum MOT_LimitSwitchModes MOT_LimitSwitchModeUndefined = c_word(0x00) MOT_LimitSwitchIgnoreSwitch = c_word(0x01) MOT_LimitSwitchMakeOnContact = c_word(0x02) MOT_LimitSwitchBreakOnContact = c_word(0x03) MOT_LimitSwitchMakeOnHome = c_word(0x04) MOT_LimitSwitchBreakOnHome = c_word(0x05) MOT_PMD_Reserved = c_word(0x06) MOT_LimitSwitchIgnoreSwitchSwapped = c_word(0x81) MOT_LimitSwitchMakeOnContactSwapped = c_word(0x82) MOT_LimitSwitchBreakOnContactSwapped = c_word(0x83)