AlexShkarin / pyLabLib

Python package for device control and experiment automation
http://pylablib.readthedocs.io
GNU General Public License v3.0
142 stars 34 forks source link

pylablib.devices.Thorlabs.KinesisMotor.wait_move() raise Exception in sub-thread #88

Open fire9291 opened 4 months ago

fire9291 commented 4 months ago

I'm working on a Python-based GUI program. A button on the main window connects a slot called on_btn_start_clicked(). I want to scan a z-stack with Thorlabs Kinesis Stage. So I created a threading that processes this scan.

` @pyqtSlot() def on_btn_start_clicked(self):

...

    self.motor = Thorlabs.KinesisMotor("27601469", scale="MTS50-Z8")
    t_process = threading.Thread(target=self.thread_processing)
    t_process.start()

def thread_processing(self): scanList = np.linspace(0, 10, 10) for zp in scanList: motor.move_to(zp * 1e-3) motor.wait_move()`

However, in the sub-threading, every function involves inquiring (wait_move oris_moving) will raise an exception.

File "D:\software\Anaconda\envs\py38\lib\site-packages\pylablib\devices\Thorlabs\kinesis.py", line 152, in recv_comm raise ThorlabsError("unexpected command received: expected 0x{:04x}, got 0x{:04x}".format(expected_id,messageID)) pylablib.devices.Thorlabs.base.ThorlabsError: unexpected command received: expected 0x042a, got 0x0001 python-BaseException

I tried to move thread_processing into slots like below. It runs correctly but this leads to the UI program stuck because of long time execution. @pyqtSlot() def on_btn_start_clicked(self):

...

    self.motor = Thorlabs.KinesisMotor("27601469", scale="MTS50-Z8")
    scanList = np.linspace(0, 10, 10)
    for zp in scanList:
        motor.move_to(zp * 1e-3)
        motor.wait_move()

Waiting for your response. Thanks a lot.

AlexShkarin commented 4 months ago

Hi!

In general, sending commands to the devices in different threads should work. However, these commands should be synchronized, and you must make sure that you're not sending commands from two threads simultaneously; otherwise, the messages sent to the device will interfere with each other, possibly resulting in unexpected responses that you are seeing. Are you sure that the motor object is only referenced and used in the moving thread, and not anywhere else? Can you provide a more complete snippet of the code?

Sincerely,

Alexey