LoginSignup
0
0

IoT向け小型センサ・ノード開発キット「STWIN.box 」を使ってセンサデータ収集② python scriptを試す

Last updated at Posted at 2024-05-13

前回の記事では、「STWIN.box」(STEVAL-STWINBX1)とセンサ・データ収集用ファームウェア「FP-SNS-DATALOG2」を使って、GUIツール(hsddatalog_gui.py)による操作を試してみました。
前回記事: IoT向け小型センサ・ノード開発キット「STWIN.box 」を使ってセンサ・データ収集① セットアップ & GUI操作

このツールは、リアルタイムでのプロット表示 & データ保存をGUI画面で操作できます。次に、収集したログデータを別の環境で活用するために、その用途に応じて変換する(例: *.DAT → *.csv)必要が出てきます。現時点では、このGUIツールはデータ変換に対応していませんが、変換用途のpythonスクリプトがいくつか用意されています。今回はこちらのコマンド操作をいくつか試してみます。

Python Script
Script 内容 Note
hsdatalog_plot.py データプロット表示 本記事で紹介
hsdatalog_cli.py コマンド操作でのデータロギング 本記事で紹介
hsdatalog_data_export.py .datデータをCSVデータに変換 本記事で紹介
hsdatalog_dataframes.py pandas用データフレーム表示 本記事で紹介
hsdatalog_to_nanoedge.py NanoEdge AIツールへインポート用
hsdatalog_to_unico.py Unico-GUIツールへインポート用
hsdatalog_to_wav.py マイクデータをwavファイルへ変換
hsdatalog_gui.py GUI起動。リアルタイムプロット、データロギングをGUIで操作可能。

参考資料: ユーザマニュアルUM3106

1. 準備

使用環境は、前回と同じです。環境のセットアップができていない場合、前回記事の“1.ハードウェアの準備”および“2.ソフトウェアの準備”の章を参考にセットアップしてください。準備完了後、コマンドウインドウを起動します。

本記事では前回と同様にAnacondaプロンプトを使います。Utilitiesフォルダ下の以下場所まで移動。
(base) D:\...\Utilities\HSDPython_SDK\examples>

100_anaconda.png

ここに(\examples)、前回のGUIツール(Hsdatalog_gui.py)で保存したデータフォルダが既にある状態で進めていきます。

2. hsdatalog_plot.py データプロット表示

“hsdatalog_plot.py”は、グラフ表示用のscriptです。
FP-SNS-DATALOG1の時と違って、センサ名指定時はDATファイル名そのものを入力します。
(DATALOG1の時はセンサ名のみを入力する形式で、ファイル名末尾の“_acc”といったサフィックスを省略する必要がありました)
ヘルプメニュー(“-h”)に、DATALOG1とDATALOG2それぞれにコマンド入力例があるのでそれを参照してください。

コマンド: python hsdatalog_plot.py データフォルダ名 [OPTIONS]

実行例: iis2dlpcセンサの加速度データをプロット
(base) D:\...\Utilities\HSDPython_SDK\examples>python hsdatalog_plot.py 20230629_18_12_11 -s iis2dlpc_acc

hsdatalog_plotのhelp
hsdatalog_plot.py (help)
(base) D:\_MySTM32Cube3\STM32CubeFP_DATALOG2_V2.1.1\Utilities\HSDPython_SDK\examples>python hsdatalog_plot.py -h
Usage: hsdatalog_plot.py [OPTIONS] ACQ_FOLDER

Options:
  -s, --sensor_name TEXT          Component name (Sensor or Algorithm) - use
                                  "all" to extract all active Component data,
                                  otherwise select a specific Component by
                                  name
  -st, --start_time INTEGER       Start Time - Data plot will start from this
                                  time (seconds)
  -et, --end_time INTEGER         End Time - Data plot will end up in this
                                  time (seconds)
  -r, --raw_data                  Uses Raw data (not multiplied by
                                  sensitivity)
  -l, --labeled                   Plot data including information about
                                  annotations taken during acquisition (if
                                  any)
  -p, --subplots                  Multiple subplot for multi-dimensional
                                  sensors
  -fp, --fft_plots                Display frequency plots for inertial sensors
                                  and microphones
  -cdm, --custom_device_model <INTEGER INTEGER TEXT>...
                                  Upload a custom Device Template Model (DTDL)
  -v, --version                   hsdatalog_plot tool version number
  -d, --debug                     [DEBUG] Check for corrupted data and
                                  timestamps
  -h,  --help                     Show this message and exit.
  --help                          Show this message and exit.

-> Script execution examples:

-> HSDatalog1:
   python hsdatalog_plot.py ..\..\STWIN_acquisition_examples\STWIN_00001
   python hsdatalog_plot.py ..\..\STWIN_acquisition_examples\STWIN_00001 -s all
   python hsdatalog_plot.py ..\..\STWIN_acquisition_examples\STWIN_00002 -s IIS3DWB -st 1 -et 10
   python hsdatalog_plot.py ..\..\STWIN_acquisition_examples\STWIN_00002 -s all -l
   python hsdatalog_plot.py ..\..\STWIN_acquisition_examples\STWIN_00002 -l -p -r

-> HSDatalog2:
   python hsdatalog_plot.py ..\..\STWIN.box_acquisition_examples\20221017_13_18_08
   python hsdatalog_plot.py ..\..\STWIN.box_acquisition_examples\20221017_13_18_08 -s all
   python hsdatalog_plot.py ..\..\STWIN.box_acquisition_examples\20221017_13_18_08 -s iis3dwb_acc -st 1 -et 10
   python hsdatalog_plot.py ..\..\STWIN.box_acquisition_examples\20221017_13_18_08 -s all -l
   python hsdatalog_plot.py ..\..\STWIN.box_acquisition_examples\20221017_13_18_08 -l -p -r

出力結果:

200_plot_iis3dwb.png

3. hsdatalog_cli.py コマンドラインからロギング実行

「STWIN」の記事で紹介したcli_example.exe(実行用バッチファイルは、“USB_DataLog_Run.bat”)と機能的にほぼ同じですが、こちらはPythonで書かれています。

コマンド: python hsdatalog_cli.py [OPTIONS]

実行例:
(base) D:\...\Utilities\HSDPython_SDK\examples>python hsdatalog_cli.py

cliは“コマンドライン・インタフェース”の略だと想定されますが、起動後はなぜかマウス操作で行います。コマンドウインドウ内の以下文字部分をクリックして操作します。

  • Start: ログスタート(自動スタート)
  • Stop: 停止
  • Quit: 終了

300_cli.png

hsdatalog_cli.pyのhelp
hsdatalog_cli.py (help)
(base) D:\_MySTM32Cube3\STM32CubeFP_DATALOG2_V1.1.0\Utilities\HSDPython_SDK\examples>python hsdatalog_cli.py -h
Usage: hsdatalog_cli.py [OPTIONS]

Options:
  -o, --output_folder TEXT        Output folder (this will be created if it
                                  doesn't exist)
  -s, --sub_datetime_folder BOOLEAN
                                  Put automatic datetime sub-folder in Output
                                  folder [HighSpeedDatalogv2 Only] (this will
                                  be created if it doesn't exist)
  -an, --acq_name TEXT            Acquisition name
  -ad, --acq_desc TEXT            Acquisition description
  -f, --file_config TEXT          Device configuration file (JSON)
  -u, --ucf_file TEXT             UCF Configuration file for MLC or ISPU
  -iof, --ispu_out_fmt TEXT       ISPU output format descrition json. If
                                  passed, this json will be saved in
                                  acquisition folder
  -t, --time_sec INTEGER          Duration of the current acquisition
                                  [seconds]
  -i, --interactive_mode          Interactive mode. It allows to select a
                                  connected device, get info and start the
                                  acquisition process
  -v, --version                   HSDatalogToUnico Converter tool version
                                  number
  -h, --help                      Show this message and exit.

-> Script execution examples:
   python hsdatalog_cli.py
   python hsdatalog_cli.py -o .\your_out_folder
   python hsdatalog_cli.py -t 10
   python hsdatalog_cli.py -i
   python hsdatalog_cli.py -t 20 -an your_acq_name -ad your_acq_descr
   python hsdatalog_cli.py -f ..\STWIN_config_examples\DeviceConfig.json -u ..\STWIN_config_examples\UCF_examples\ism330dhcx_six_d_position.ucf

使ってみたものの、あまり利用用途が見えず、こちらを使うのであればGUIツールであるhsdatalog_gui.pyの方がベターと感じました。また、自動で定期的にログ取得する場合など、PC経由でバッチファイルから実行するケースでは、cli_example.exeの使用をおすすめします。

cli_example.exe 使用例:
→バッチファイルで定期的に自動実行し、PC内へのログデータ保存を想定
cli_example.exe -f .\sample\device_config.json -t 20 -y

上記コマンドの設定例
・センサ設定用コンフィグファイルを指定 (-f device_config.json)
・センシング時間 = 20秒(-t 20)
・ユーザからのキー確認を省略(-y)

4. hsdatalog_data_export.py データ変換

.DATファイルを、CSV / TSVファイルに変換できます。

コマンド: python hsdatalog_data_export.py データフォルダ名 [OPTIONS]

実行例: [20240228_17_45_55]フォルダ内のセンサ・データファイル(.DAT)すべてを変換。
(base) D:\...\Utilities\HSDPython_SDK\examples>python hsdatalog_data_export.py 20240228_17_45_55 -s all

"-s"スイッチの後にセンサ名を指定することで、個別変換も可能。新規に[20240228_17_45_55_Exported]フォルダが作成され、その中に変換後(フォーマット指定省略時のデフォルトは“.csv”)のファイルが保存されます。

400_dataexport.png

hsdatalog_data_export.pyのhelp
hsdatalog_data_export.py (help)
(base) D:\_MySTM32Cube3\STM32CubeFP_DATALOG2_V1.1.0\Utilities\HSDPython_SDK\examples>python hsdatalog_data_export.py -h
Usage: hsdatalog_data_export.py [OPTIONS] ACQ_FOLDER

Options:
  -o, --output_folder TEXT     Output folder (this will be created if it
                               doesn't exist)
  -f, --file_format [CSV|TSV]  Exported file format
  -s, --sensor_name TEXT       Sensor Name - use "all" to extract all active
                               sensors data, otherwise select a specific
                               sensor by name
  -st, --start_time INTEGER    Start Time - Data conversion will start from
                               this time (seconds)
  -et, --end_time INTEGER      End Time - Data conversion will end up in this
                               time (seconds)
  -r, --raw_data               Uses Raw data (not multiplied by sensitivity)
  -l, --labeled                Includes annotations taken during acquisition
                               (if any) in the exported data
  -d, --debug                  [DEBUG] Check for corrupted data and timestamps
  -h, --help                   Show this message and exit.

-> Script execution examples:

-> HSDatalog1:
   python hsdatalog_data_export.py ..\..\STWIN_acquisition_examples\STWIN_00001
   python hsdatalog_data_export.py ..\..\STWIN_acquisition_examples\STWIN_00001 -s all
   python hsdatalog_data_export.py ..\..\STWIN_acquisition_examples\STWIN_00001 -s ISM330DHCX -f TSV -r
   python hsdatalog_data_export.py ..\..\STWIN_acquisition_examples\STWIN_00001 -o ..\..\STWIN_acquisition_examples\EXPORTED_DATA_FOLDER 


-> HSDatalog2:
   python hsdatalog_data_export.py ..\..\STWIN.box_acquisition_examples\20221017_13_04_39 -s all
   python hsdatalog_data_export.py ..\..\STWIN.box_acquisition_examples\20221017_13_04_39 -s ism330dhcx_gyro
   python hsdatalog_data_export.py ..\..\STWIN.box_acquisition_examples\20221017_13_04_39 -s ism330dhcx_gyro -r
   python hsdatalog_data_export.py ..\..\STWIN.box_acquisition_examples\20221017_13_04_39 -f TSV -s ism330dhcx_gyro
   python hsdatalog_data_export.py ..\..\STWIN.box_acquisition_examples\20221017_13_04_39 -f CSV -s ism330dhcx_gyro -l -o .\expDataByTags

5. hsdatalog_dataframes.py データフレーム表示

5-1. スクリプト実行

データフレーム(Pandasで使われるデータ構造)として取り込んだ時の、「何行 x 何列」といった情報が表示されます。試した感じでは用途がよくわかりませんでした(表示のみ?)。

コマンド: python hsdatalog_dataframes.py データフォルダ名 [OPTIONS]

実行例: [20240228_17_45_55]フォルダ内のセンサファイル(.DAT)すべてをデータフレーム表示。
(base) D:\...\Utilities\HSDPython_SDK\examples>python hsdatalog_dataframes.py 20240228_17_45_55 -s all

hsdatalog_dataframes.pyのhelp
hsdatalog_dataframes.py (help)
(base) D:\_MySTM32Cube3\STM32CubeFP_DATALOG2_V1.1.0\Utilities\HSDPython_SDK\examples>python hsdatalog_dataframes.py -h
Usage: hsdatalog_dataframes.py [OPTIONS] ACQ_FOLDER

Options:
  -s, --sensor_name TEXT     Sensor Name - use "all" to obtain all active
                             sensors dataframes, otherwise select a specific
                             sensor by name
  -st, --start_time INTEGER  Start Time - Data conversion will start from this
                             time (seconds)
  -et, --end_time INTEGER    End Time - Data conversion will end up in this
                             time (seconds)
  -r, --raw_data             Uses Raw data (not multiplied by sensitivity)
  -l, --labeled              Includes annotations taken during acquisition (if
                             any) in the extracted dataframe
  -d, --debug                [DEBUG] Check for corrupted data and timestamps
  -h, --help                 Show this message and exit.

-> Script execution examples:

-> HSDatalog1:
   python hsdatalog_dataframes.py ..\..\STWIN_acquisition_examples\STWIN_00002
   python hsdatalog_dataframes.py ..\..\STWIN_acquisition_examples\STWIN_00002 -s all
   python hsdatalog_dataframes.py ..\..\STWIN_acquisition_examples\STWIN_00002 -s IIS3DWB -l -r

-> HSDatalog2:
   python hsdatalog_dataframes.py ..\..\STWIN.box_acquisition_examples\20221017_13_04_39
   python hsdatalog_dataframes.py ..\..\STWIN.box_acquisition_examples\20221017_13_04_39 -s all
   python hsdatalog_dataframes.py ..\..\STWIN.box_acquisition_examples\20221017_13_04_39 -s iis3dwb_acc -l -r

実行結果(一部):

500_dataframe_.png

今回確認した限りでは、-sスイッチの後にセンサ名を指定した場合、何も出力されませんでした。all指定した場合のみ受け付けるようです。スクリプトの不備かもしれません。

補足: (Pandas)データフレームへの変換

方法1: read_csvで読み込む

いったんhsdatalog_dataframes.pyから離れて、データフレームとして実際に活用するための他の手順を試してみます。作業ツールとしてJupyter Notebookを使います。
最初に、hsdatalog_data_export.pyで一度csvに変換しておきます。続いて、できたファイルをPandasのread_csvで読み込むのが一番簡単です。

502_dataframe_csv.png

方法2: サンプルスクリプトを利用する

念のため、.DATファイルを直接データフレームに変換できるかどうか調べてみましたが、以下使えそうなサンプルが同じフォルダ内に入っていました。

nb_hsdatalog_converters.ipynb (Jupyter Notebookファイル)

多少煩雑ですが、こちらでもデータフレームへの変換は可能です。

510_dataframe_example_jpynb.png

上のサンプルを参考にして、取り込んでみます。

511_dataframe_step1_.png

取り込まれたデータ"dataframe"は、リスト構造だったので(紛らわしいですが、“dataframe”という名前のリスト構造体変数です)、続いてデータフレームに変換します。
私の方では背景が理解できませんでしたが、この"dataframe"リストは2重構造になっているようです。第1要素[0]の中に、リスト全体(ここでは270000行のリスト)が格納されている形になっていました。
したがって、データフレームとして取り込む場合は次のようにします。
df=pd.DataFrame(dataframe[0][:])

512_dataframe_step2_.png

残りのコマンドについては、次回記事でトライします。

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