dSPACE ControlDesk 計測の自動化

## 1. はじめに
ControlDeskの計測からCSVのエクスポートまでの一連の流れをPythonでやってしまいましょう．

## 2. 記述
今回はPushボタンを押したらそこから10秒分のデータをRecordingしてCSVにエクスポートするプログラムを説明します．

```python
# -- coding: utf-8 --
# 日本語対応
import serial
import time
import sys
import datetime
from win32com.client import Dispatch, DispatchWithEvents
from dspace.com.threads import STAThread

# プラットフォームオブジェクト
MyPlatform = Application.ActiveExperiment.Platforms["Platform"]
# Experiment実行ファイル(.DSE)のパス
dspaceDir = Application.ActiveExperiment.DirectoryName

# Pushボタンのイベント駆動プログラム
def On_Application_LayoutManagement_LoadCell_Push0020Button_150_210_ButtonClicked(Sender, Button):
    """
    Syntax : On_Application_LayoutManagement_LoadCell_Push0020Button_150_210_ButtonClicked
    Purpose: 
    Parameters: Sender, Button
    """

    # Create file name.
    date = datetime.datetime.now()
    dateStr = str(date.year) + '{0:02d}'.format(date.month) + '{0:02d}'.format(date.day)
    timeStr = '{0:02d}'.format(date.hour) + '{0:02d}'.format(date.minute) +'{0:02d}'.format(date.second)
    # ex) 20211116-120350_test_030[A].idf
    fileBaseName = dateStr + "-" + timeStr + "_" + "test" + "_" + '{0:01f}'.format(current) + "[A]"
    fileName = fileBaseName + ".idf"
    print "fileName: ", fileName
    csvPath = dspaceDir + "\\Measurement Data" + "\\" + fileBaseName + ".csv"
    print("csvPath: ", csvPath)

    recThread = STAThread(RecordingThread, "RecordingThread", (fileName, csvPath, Application,))
    recThread.start()

# レコーディング用のスレッド関数
def RecordingThread(fileName, csvPath, LocalApplication):
    # Recording Settings.
    # Get recorder object.
    # 自作のRecoderを選択する場合はMainRecoder->Recorders["レコーダ名"]に置き換える
    MainRecorder = LocalApplication.MeasurementDataManagement.MainRecorder

    # Set to automatic file naming.
    MainRecorder.AutomaticNamingEnabled = False

    # Add measurement to experiment.
    MainRecorder.AddToExperimentEnabled = True

    # Set number to start automatic naming.
    #MainRecorder.AutomaticNamingStartIndex = 10
    
    # Set number of digits for automatic naming.
    #MainRecorder.AutomaticNamingMinimumDigits = 3

    # Add record to measurement data pool.
    MainRecorder.OpenInMeasurementDataPoolEnabled = True

    # Get f_Ki_1 signal.
    #FKiSignal = LocalApplication.MeasurementDataManagement.MeasurementConfiguration.Signals.Item(
    #    FKiVariableConnectionString % (self.DemoPlatform.Name, OnChangeRasterName))
    #EncoderLIn1Signal = LocalApplication.MeasurementDataManagement.MeasurementConfiguration.Signals.Item(3)

    print("Recorder state: ", LocalApplication.MeasurementDataManagement.MainRecorder.State)

    # Remove signal f_Ki_1 from main recorder.
    #MainRecorder.Signals.Remove(EncoderLIn1Signal)
    
    # Base File Name of Recorder.
    MainRecorder.BaseFileName = fileName
    
    time.sleep(0.5)
    # Immediate Recording.
    MainRecorder.Start()
    time.sleep(10)
        
    print("Recorder state: ", LocalApplication.MeasurementDataManagement.MainRecorder.State)

    # Stop Recording.
    LocalApplication.MeasurementDataManagement.Stop()

    # Get Measurement object.
    Measurement = LocalApplication.MeasurementDataManagement.Measurements.Item(fileName)
    # Export.
    Measurement.Export(csvPath, True)
    print ("Main recording is finished.")
```