4
4

ControlDesk上でsimulinkブロックをPythonの変数として扱う方法

Last updated at Posted at 2024-08-26

はじめに

計測用ソフトウェアのControlDeskではSimulinkのブロックをVariableとして
センサ値などを変数として扱います.
基本はInstrumentをLayoutに設置し,Variableとリンクします.
しかし,Automation機能の1つであるPython記述による計測自動化などを実行したい場合,
その変数をプログラム内部で扱う必要があります.
今回はその記述手法について,イベント処理を用いて簡単にまとめました.

環境

  • Windows 10 64bit (Home)
  • CPU: Intel Core i7-9750H 2.60GHz
  • メモリ: 16GB
  • dSPACE board: MicroLabBox (ds1202)
  • ControlDesk: 7.5 (ver. 4.3以降)
  • MATLAB: R2021b
  • Python: 3.7?

手順

1. プログラムを記述する場所

Automationタブ -> Configure Events
variable_python_ConfigureEvents.png
Event Configurationウィンドウ -> Experimentタブ -> CalibrationManagement
基本的にExperimentの中から目的のEvenet Handlerをダブルクリックしてプログラムに反映します.
今回は試しにGo Onlineボタンを押したとき発火するプログラムとしてOnlineCalibrationStartedを選択.
すると,以下のようなプログラムが表示されます.
この関数内にボタンを押したときに実行したいことを記述します.
variable_python_default_program.png
そのまま画面右下のOKボタンを押して元の画面に戻ります.
※Cancelや×ボタンで閉じると記述したプログラムが反映されないので注意!
Go Onlineを押すとInterpreterコントロールバーにprint()の内容が出力されます.
※Interpreterが画面上に存在しない場合:Viewタブ -> Swith Controlbars -> Interpreterをクリック.
variable_python_ConfigureEvents_Experiment_OnlineCalibration_exe.png

2. 構文

2.1. プラットフォームの抽出

プラットフォームとは,dSPACEのボード本体のことです.
デフォルトは"Platform"という名前になっています.

MyPlatform = Application.ActiveExperiment.Platforms["PLATFORM_NAME"]

2.2. 扱いたい変数のパスを取得

変数はPathとして定義されており,Propertyから右クリックでコピーできます.
variable_python_Properties_Path.png
下記コードの[ ]内にVariableのPathを入力することにより,Simulink上のブロックを識別する.

# Pathから直接指定
# <class 'win32com.client.CDispatch'>
myVar = myPlatform.ActiveVariableDescription.Variables["PlatformName()://Model Root/Constant/Value"]
# インデックス指定も可能
myVar = myPlatform.ActiveVariableDescription.Variables[8]

2.3. 変数にアクセス

Variableオブジェクトの持つ数値はValueConverted (float型)属性に格納されています.

# Interpreterタブのコンソールに数値を出力
print(myVar.ValueConverted)
# 属性に数値を代入
myVar.ValueConverted = 100.0

3. 実行例

比例電磁弁を制御する簡単なプログラムでConstantブロックの値を変更してみます.

variable_python.png
↑使用するsimulinkプロジェクト
https://drive.google.com/drive/folders/1Xkvh1oQzEWb-FYsvmoVCNdy-vL23dO5w?usp=drive_link

[ブロックの説明]
solenoid valve [MPa]: 指令値監視用
pressure [MPa]: 指令値
・Data Type Conversion: Constantブロックに直接Scopeを接続すると
 ControlDesk内でVariableのSignalが空欄になることを防止

Numeric InputとDisplayのInstrumentを配置し,以下のコードを記述します.
Go Online時に指令値を0.100 MPaにするプログラム

Application.ActiveExperiment.Platforms["Platform"]

def On_Application_CalibrationManagement_OnlineCalibrationStarted():
    """
    The application has changed state to online calibration.
    Syntax    : On_Application_CalibrationManagement_OnlineCalibrationStarted()
    Parameters: None
    """
    print("On_Application_CalibrationManagement_OnlineCalibrationStarted")

    PressVar = MyPlatform.ActiveVariableDescription.Variables["Platform()://Model Root/pressure [MPa]/Value"]
    print(type(PressVar))
    PressVar.ValueConverted = 0.100
    print("PressVar: " + format(PressVar.ValueConverted, '.3f'))

variable_python_ConfigureEvents_Experiment_example.png

実行結果
指令値のnumeric inputの表記が0.100になり,interperterにも出力されています.
variable_python_ConfigureEvents_Experiment_example_exe.png

参考文献

[1] dSPACE, "ツールの自動化によるControlDeskでの変数への直接的なアクセス",
https://www.dspace.com/ja/jpn/home/support/kb/faqs/faq101.cfm,
(ref. 2023/10/08)

[2] dSPACE, "ControlDeskがツール自動化により次のレベルに到達",
https://www.dspace.com/ja/jpn/home/news/engineers-insights/blog-inc-controldesk-0517.cfm,
(ref. 2023/10/08)

4
4
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
4
4