0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

dSPACEAdvent Calendar 2024

Day 8

dSPACEでPython実行時の画面フリーズをスレッド使用して回避

Posted at

1. はじめに

dSPACEのpythonプログラムにスリープ関数や繰り返しの多いfor文などがあると画面がフリーズして操作を受け付けなくなることがあると思います.処理が終わればフリーズから解放されるのですが,その間Time Plotterで時系列データが見られないなど弊害が生じることでしょう.
スレッドを使用してそれを回避するという話です.

2. 記述

スレッドを開始するコマンド

スレッドオブジェクト = STATread(関数名, ラベル, (引数1, 引数2,...)
FuncThread = STAThread(Wait, "Thread1", (pTime, Application,))
FuncThread.start()

3. 具体例

1秒ごとに値を保存し平均値を出すプログラム

  • 自動的にStart Measuring & Stop Measuring
  • スレッド内ではApplicationオブジェクトを直接参照できないので引数として渡します.
# Python modules
from win32com.client import Dispatch, DispatchWithEvents
from dspace.com.threads import STAThread
import time

# Platfrom object
MyPlatform = Application.ActiveExperiment.Platforms["Platform"]

def main():
    # Check the state
    if Application.CalibrationManagement.State == 0:
        print("[error] Please Go Online!")
        exit()

    # Define 
    ScopeVar = MyPlatform.ActiveVariableDescription.Variables["Platform()://Model Root/Scope/In1"]
    sleepTime = 1.0

    waitThread = STAThread(Wait, "Thread1", (sleepTime, ScopeVar, Application,))
    waitThread.start()

def Wait(sleepTime, ScopeVar, LocalApplication):
    # Start Measuring
    LocalApplication.MeasurementDataManagement.Start()

    # Start Measuring.
    print("[sys] Calculate average.")

    n = 5
    list = []
    sum = 0.0
    ave = 0.0

    for i in range(0, n):
        sum = sum + ScopeVar.ValueConverted
        list.append(ScopeVar.ValueConverted)
        time.sleep(sleepTime)
    ave = sum / n

    # Stop Measuring.
    LocalApplication.MeasurementDataManagement.Stop()

    print(list)
    print(ave)
    print("[sys] Finished.")

if __name__ == '__main__':
    main()

4. 実行結果

スレッドを使用しているのでスクリプト実行中も自由に画面移動などの操作ができます.

参考文献

[1] dSPACE, "FAQ 098:Pythonスレッドの使用",
https://www.dspace.com/ja/jpn/home/support/kb/faqs/faq098.cfm

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?