def setNumaxes_d(self): with self._lock: if not self.device: return for i in range(3): # AMC can only control axis [0..2] if AMC.setOutput(self.device, i, True) != 0: break else: AMC.setMove(self.device, i, False) self._info['numaxes'] = i
def setRange_d(self): with self._lock: if not self.device: return t_range = [] # End of Travel detection for i in range(self._info['numaxes']): AMC.setEotOutputDeactive(self.device, i, True) t_range.append((None, None)) # 总行程 self._info['range'] = tuple(t_range)
def connect(self, ip='192.168.1.1', *args, **kwargs): with self._lock: if self.device: return self._device_info.update(ip=ip) self.device = AMC.connect(ip) self._init_d()
def waitontarget(device, axis, timeout=60, eottimeout=1): maxtime = time.time() + timeout target_range = AMC.getTargetRange(device, axis)[1] target_pos = AMC.getTargetPosition(device, axis)[1] # eot detection max_interval = int(eottimeout / 0.1) + 1 last_pos = AMC.getPosition(device, axis)[1] i_interval = 0 while True: errno, status = AMC.getStatusMoving(device, axis) if errno: raise SystemError( 'System error! Please reconnect the device.') if status == 1: # status == 1 for moving status pos = AMC.getPosition(device, axis)[1] if abs(pos - target_pos) < target_range: AMC.setMove(device, axis, False) break # no-blocking End of Travel detection if i_interval != max_interval: i_interval += 1 else: i_interval = 0 pos = AMC.getPosition(device, axis)[1] if abs(last_pos - pos) < target_range: AMC.setMove(device, axis, False) return False last_pos = pos if time.time() > maxtime: raise SystemError( 'waitontarget() timed out after %.1f seconds' % timeout) time.sleep(0.1) else: break return True
def getPosition(self): with self._lock: if not self.device: return pos = [ AMC.getPosition(self.device, i)[1] for i in range(self._info['numaxes']) ] self._info['position'] = pos return tuple(pos)
def close(self): with self._lock: if not self.device: return try: for i in range(self._info['numaxes']): AMC.setMove(self.device, i, False) AMC.setOutput(self.device, i, False) AMC.close(self.device) except Exception as e: print(str(e)) finally: self.device = None self._device_info.clear()
def move(self, targets, timeout=60, eottimeout=1): with self._lock: if len(targets) != self._info['numaxes']: raise TypeError( 'The number of axes set is not equal to the' 'number of axes of the device.') try: self._moveState = True if not self.device: raise SystemError('No available device.') # out of range judgement targets = [int(t) for t in targets] for i in range(self._info['numaxes']): _range = self._info['range'][i] if (None not in _range): if (targets[i] > _range[1]) or (targets[i] < _range[0]): raise OutOfRange( 'Sorry, out of range in axis {}'.format( str(i))) else: continue # move to targets print('{} targets: {}'.format(str(self.name), str(targets))) for i, target in enumerate(targets): AMC.setTargetPosition(self.device, i, target) AMC.setMove(self.device, i, True) # wait states = [None] * self._info['numaxes'] for i in range(self._info['numaxes']): states[i] = amctools.waitontarget( self.device, i, timeout, eottimeout) self._moveState = False # update range! if not all(states): err_info = "" for i, state in enumerate(states): if not state: pos = AMC.getPosition(self.device, i)[1] _range = list(self._info['range'][i]) if pos < targets[i]: _range[1] = pos else: _range[0] = pos self._info['range'][i] = tuple(_range) print('range for axis {} is: {}'.format( str(i), str(_range))) err_info += 'Sorry, out of range in axis {}. ' \ 'Position {} Range {}\n'.format(str(i), str(pos), str(_range)) raise OutOfRange(err_info) except SystemError: self._moveState = None self.close() raise except: for i in range(self._info['numaxes']): AMC.setMove(self.device, i, False) self._moveState = False raise