Esempio n. 1
0
    def __init__(self, port, baudrate=9600, **kwds):
        self.lock = RLock(debug=True)

        params = dict(
            maximum_current=100,
            maximum_acceleration=1000,
            maximum_speed=2000,
            ramp_divisor=7,
            pulse_divisor=3,
            standby_current=0,
            mixed_decay_threshold=-1,
            encoder_prescaler=8192,   # causes encoder_position to have exactly the same resolution as the encoder itself
            microstep_resolution=5,
            fullstep_threshold=0,
            stall_detection_threshold=0,
            freewheeling=1,
        )
        for k, v in kwds.items():
            if k not in params:
                raise NameError("Unknown MFC1 parameter '%s'" % k)
            params[k] = v

        self.mcm = TMCM140(port, baudrate)
        self.mcm.stop_program()
        self.mcm.stop()
        self.mcm.set_params(**params)
        self._target_position = self.mcm['encoder_position']
        self.mcm.set_global('gp0', self._target_position)
        self._upload_program()

        self._move_status = {}
        self._last_move_id = -1
Esempio n. 2
0
 def __init__(self, port):
     port = SerialDevice.normalizePortName(port)
     if port in SutterMPC200.DEVICES:
         raise Exception("The port %s is already accessed by another instance of this class. Use getDevice(port) instead.")
     SutterMPC200.DEVICES[port] = self
     self.lock = RLock()
     self.port = port
     SerialDevice.__init__(self, port=self.port, baudrate=128000)
     self.scale = [0.0625e-6]*3  # default is 16 usteps per micron
     self._moving = False
Esempio n. 3
0
    def __init__(self, port, baudrate=9600, **kwds):
        self.lock = RLock(debug=True)

        # Constant that affects ability to seek to encoder position.
        # Low values (~10) cause a very slow approach to the target encoder position.
        # High values (~2000) cause the motor to overshoot the target position
        self.tracking_const = kwds.pop('tracking_const', 400)

        params = dict(
            maximum_current=100,
            maximum_acceleration=1000,
            maximum_speed=2000,
            ramp_divisor=7,
            pulse_divisor=3,
            standby_current=0,
            mixed_decay_threshold=-1,
            encoder_prescaler=
            8192,  # causes encoder_position to have exactly the same resolution as the encoder itself
            microstep_resolution=5,
            fullstep_threshold=0,
            stall_detection_threshold=0,
            freewheeling=1,
        )

        optional_params = [
            'mixed_decay_threshold', 'stall_detection_threshold',
            'fullstep_threshold'
        ]

        for k, v in kwds.items():
            if k not in params:
                raise NameError("Unknown MFC1 parameter '%s'" % k)
            params[k] = v

        self.mcm = TMCM140(port, baudrate)
        self.mcm.stop_program()
        self.mcm.stop()

        for k, v in params.items():
            try:
                self.mcm.set_param(k, v)
            except TMCMError as err:
                if err.status == 3 and k in optional_params:
                    # Some parameters are not supported on all motors; ignore these silently
                    pass
                else:
                    print(k, v)
                    raise
        self._target_position = self.mcm['encoder_position']
        self.mcm.set_global('gp0', self._target_position)
        self._upload_program()

        self._move_status = {}
        self._last_move_id = -1
Esempio n. 4
0
 def __init__(self, port, baudrate=9600, module_addr=1):
     """
     port: serial COM port (eg. COM3 or /dev/ttyACM0)
     baudrate: 9600 by default
     module_addr: 1 by default
     """
     self.lock = RLock(debug=True)
     self.port = port
     assert isinstance(module_addr, int)
     assert module_addr > 0
     self.module_addr = module_addr
     self.module_str = chr(module_addr + 64)
     self._waiting_for_reply = False
     SerialDevice.__init__(self, port=self.port, baudrate=baudrate)
Esempio n. 5
0
    def __init__(self, port=None, name=None, baudrate=None):
        self.lock = RLock()

        if name is not None:
            assert port is None, "May not specify both name and port."
            if self.availableDevices is None:
                self.enumerateDevices()
            if name not in self.availableDevices:
                raise ValueError(
                    'Could not find Scientifica device with description "%s". Options are: %s'
                    % (name, list(self.availableDevices.keys())))
            port = self.availableDevices[name]

        if port is None:
            raise ValueError("Must specify either name or port.")

        self.port = self.normalizePortName(port)
        if self.port in self.openDevices:
            raise RuntimeError("Port %s is already in use by %s" %
                               (port, self.openDevices[self.port]))

        # try both baudrates, regardless of the requested rate
        # (but try the requested rate first)
        baudrate = 9600 if baudrate is None else int(baudrate)
        if baudrate == 9600:
            baudrates = [9600, 38400]
        elif baudrate == 38400:
            baudrates = [38400, 9600]
        else:
            raise ValueError('invalid baudrate %s' % baudrate)

        connected = False
        for baudrate in baudrates:
            try:
                SerialDevice.__init__(self, port=self.port, baudrate=baudrate)
                self.write('type\r')
                self.readUntil('\r', timeout=1)
                connected = True
                break
            except TimeoutError:
                pass

        if not connected:
            raise RuntimeError(
                "No response received from Scientifica device at %s. (tried baud rates: %s)"
                % (port, ', '.join(map(str, baudrates))))

        Scientifica.openDevices[self.port] = self
        self._readAxisScale()
Esempio n. 6
0
    def __init__(self, port=None, name=None, baudrate=None, ctrl_version=2):
        self.lock = RLock()

        if name is not None:
            if isinstance(name, str):
                name = name.encode()
            assert port is None, "May not specify both name and port."
            if self.availableDevices is None:
                self.enumerateDevices()
            if name not in self.availableDevices:
                raise ValueError('Could not find Scientifica device with description "%s". Options are: %s' % 
                    (name, list(self.availableDevices.keys())))
            port = self.availableDevices[name]

        if port is None:
            raise ValueError("Must specify either name or port.")
            
        self.port = self.normalizePortName(port)
        if self.port in self.openDevices:
            raise RuntimeError("Port %s is already in use by %s" % (port, self.openDevices[self.port]))

        # try both baudrates, regardless of the requested rate
        # (but try the requested rate first)
        baudrate = 9600 if baudrate is None else int(baudrate)
        if baudrate == 9600:
            baudrates = [9600, 38400]
        elif baudrate == 38400:
            baudrates = [38400, 9600]
        else:
            raise ValueError('invalid baudrate %s' % baudrate)

        # Attempt connection
        connected = False
        for baudrate in baudrates:
            try:
                SerialDevice.__init__(self, port=self.port, baudrate=baudrate)
                try:
                    sci = self.send('scientifica', timeout=0.2)
                except RuntimeError:
                    # try again because prior communication at a different baud rate may have garbled serial communication.
                    sci = self.send('scientifica', timeout=1.0)

                if sci != b'Y519':
                    # Device responded, not scientifica.
                    raise ValueError("Received unexpected response from device at %s. (Is this a scientifica device?)" % port)
                connected = True
                break
            except TimeoutError:
                pass

        if not connected:
            raise RuntimeError("No response received from Scientifica device at %s. (tried baud rates: %s)" % (port, ', '.join(map(str, baudrates))))

        Scientifica.openDevices[self.port] = self
        self._version = float(self.send('ver'))
        if ctrl_version is not None and ((self._version >= 3) != (ctrl_version >= 3)):
            name = self.getDescription()
            err = RuntimeError("Scientifica device %s uses controller version %s, but version %s was requested. Warning: speed and acceleration"
                               " parameter values are NOT compatible between controller versions." % (name, self._version, ctrl_version))
            err.dev_version = self._version
            raise err

        self._readAxisScale()