Move THORLABS automatic stage with Python [for research]

[Part 1] High-speed wavelength switching of multi-wavelength LED light source for microscopes using pySerial [Part 2] Controlling the camera with Python [For research]

Introduction

I would like to share how to control automatic stages in Python. I didn't even look for an article on how to control Thorlabs Inc.'s automatic stages in Python. Therefore, I decided to write it myself, and wrote an article about the essence of know-how gained through trial and error. The automatic stage used this time is the stage of Thorlabs Inc. This stage can be incorporated into the company's microscope. The self-made microscope is expandable, giving you more freedom. By centralizing measurement and analysis, analog microscopic observations can be automated and multivariate data analysis can be seamlessly introduced.

Thorlabs automatic stage behavior

stage.gif

Development environment

Equipment to use

-Small stepping motor ZFS25B (Thorlabs Inc.) single_stage_actuator_A3-500.jpg -Stepping Motor Controller KST101 (Thorlabs Inc.) 8121_KST101_SG.jpg

-Controller Hub KCH301 (Thorlabs Inc.) KCH301_horizontal_brackets-300.jpg

Installation

The following two are required to operate the Thorlabs automatic stage.

kinesis Install kinesis from here. Kinesis is a library that includes GUI-based software and DLL (Dynamic Link Library) files that operate in a local PC environment. Select the version that suits your PC environment. A GUI-based application is included, but if you don't use it this time and Thorlabs.MotionControl.KCube.StepperMotor.dll is saved in the following directory, there is no problem. C:/Program Files/Thorlabs/Kinesis

This time it's Windows 10 x64, so select Kinesis 64-Bit Software for 64-Bit Windows. Click Download.

Installing. ..

Done.

** MSL-Equipment module ** Install the module MSL-Equipment to control the Thorlabas automatic stage. This library is published free of charge by the New Zealand research institute Measurement Standards Laboratory of New Zealand.

First, copy and paste the following command and execute it.

Create a virtual environment thorlabs_demo with the conda command.

conda create -n thorlabs_demo python=3.6

Install MSL-Equipment with the following command.

pip install https://github.com/MSLNZ/msl-equipment/archive/master.zip

For example, try the following with Anaconda prompt. The installation is complete. You're ready.

Installation procedure (original) https://msl-equipment.readthedocs.io/en/latest/install.html

Operation check

The sample program looks for kst101.py in the following directory. C: \ Users \ your username \ anaconda3 \ Lib \ site-packages \ msl \ examples \ equipment \ resources \ thorlabs You can copy and paste the program below. Now, let's take a look at the sample program. It seems to work just by changing the part of serial = '26001809' to the serial number of the device you are using.

kst101.py


"""
This example shows how to communicate with Thorlabs KST101, KCube Stepper Motor.
"""

# this "if" statement is used so that Sphinx does not execute this script when the docs are being built
if __name__ == '__main__':
    import os
    from pprint import pprint

    from msl.equipment import EquipmentRecord, ConnectionRecord, Backend
    from msl.equipment.resources.thorlabs import MotionControl

    # ensure that the Kinesis folder is available on PATH
    os.environ['PATH'] += os.pathsep + 'C:/Program Files/Thorlabs/Kinesis'

    # rather than reading the EquipmentRecord from a database we can create it manually
    record = EquipmentRecord(
        manufacturer='Thorlabs',
        model='KST101',
        serial='26002319',  # update the serial number for your KST101
        connection=ConnectionRecord(
            backend=Backend.MSL,
            address='SDK::Thorlabs.MotionControl.KCube.StepperMotor.dll',
        ),
    )

    def wait():
        motor.clear_message_queue()
        while True:
            status = motor.convert_message(*motor.wait_for_message())['id']
            if status == 'Homed' or status == 'Moved':
                break
            position = motor.get_position()
            real = motor.get_real_value_from_device_unit(position, 'DISTANCE')
            print('  at position {} [device units] {:.3f} [real-world units]'.format(position, real))

    # avoid the FT_DeviceNotFound error
    MotionControl.build_device_list()

    # connect to the KCube Stepper Motor
    motor = record.connect()
    print('Connected to {}'.format(motor))

    # load the configuration settings (so that we can use the get_real_value_from_device_unit() method)
    motor.load_settings()

    # start polling at 200 ms
    motor.start_polling(200)

    # home the device
    print('Homing...')
    motor.home()
    wait()
    print('Homing done. At position {} [device units]'.format(motor.get_position()))

    # move to position 100000
    print('Moving to 100000...')
    motor.move_to_position(100000)
    wait()
    print('Moving done. At position {} [device units]'.format(motor.get_position()))

    # move by a relative amount of -5000
    print('Moving by -5000...')
    motor.move_relative(-5000)
    wait()
    print('Moving done. At position {} [device units]'.format(motor.get_position()))

    # jog forwards
    print('Jogging forwards by {} [device units]'.format(motor.get_jog_step_size()))
    motor.move_jog('Forwards')
    wait()
    print('Jogging done. At position {} [device units]'.format(motor.get_position()))

    # stop polling and close the connection
    motor.stop_polling()
    motor.disconnect()

    # you can access the default settings for the motor to pass to the set_*() methods
    print('\nThe default motor settings are:')
    pprint(motor.settings)

There was an 8-digit serial number on the back of the kst101.

Change serial = '26001809' in the above program to serial = '26002319'. Once you run the program and it works, you're done.

If you have any other questions, please feel free to contact us using the form below.

http://www.opto-line.co.jp/contact/

Recommended Posts

Move THORLABS automatic stage with Python [for research]
THORLABS camera CS2100M-USB runs on Python [for research]
Getting Started with Python for PHPer-Classes
Getting Started with Python for PHPer-Functions
[Let's play with Python] Aiming for automatic sentence generation ~ Completion of automatic sentence generation ~
Easy keyword extraction with TermExtract for Python
INSERT into MySQL with Python [For beginners]
WEB scraping with Python (for personal notes)
Manually ssh registration for coreserver with python
Use DeepL with python (for dissertation translation)
Memo to ask for KPI with python
Amplify images for machine learning with python
Automatic update method with python Pyinstaller exe
Tips for using python + caffe with TSUBAME
[Shakyo] Encounter with Python for machine learning
Process multiple lists with for in Python
Getting Started with Python for PHPer-Super Basics
[Let's play with Python] Aiming for automatic sentence generation ~ Perform morphological analysis ~
Debug for mysql connection with python mysql.connector
[Python] Read images with OpenCV (for beginners)
WebApi creation with Python (CRUD creation) For beginners
Preparation for scraping with python [Chocolate flavor]
[For beginners] Try web scraping with Python
Causal reasoning and causal search with Python (for beginners)
Automatic operation of Chrome with Python + Selenium + pandas
[Translation] Getting Started with Rust for Python Programmers
Building an Anaconda environment for Python with pyenv
Play with Lambda layer (python) for about 5 minutes
Use logger with Python for the time being
Wrap C with Cython for use from Python
~ Tips for Python beginners from Pythonista with love ① ~
Django with Python Tools 2.2 for Visual Studio (PTVS 2.2)
[Python] Collect images with Icrawler for machine learning [1000 images]
Image Processing with Python Environment Setup for Windows
Note for formatting numbers with python format function
Commands for creating a python3 environment with virtualenv
Wrap C ++ with Cython for use from Python
Memo for editing scenes with Blender python (W.I.P.)
~ Tips for Python beginners from Pythonista with love ② ~
[Introduction for beginners] Working with MySQL in Python
[# 1] Make Minecraft with Python. ~ Preliminary research and design ~
Settings for getting started with MongoDB in python
2016-10-30 else for Python3> for:
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Data analysis for improving POG 1 ~ Web scraping with Python ~