LoginSignup
3
1

More than 1 year has passed since last update.

MATLAB Simulinkでリアルタイムシミュレーション

Last updated at Posted at 2022-04-22
  • Simulinkのシミュレーション時間はデフォルトだと現実と離れていてサンプルタイムを1秒としても現実時間の1秒通りには動いてくれません。SimulinkはStateFlowがGUIベースで作れて便利なのでどうするか悩んだ結果MathWorksに相談した結果、回答を頂いたのでお礼のつもりで現実時間とどれくらい合っているか検証してみました。

※CPUの使用率はSimulinkではなくPCすべての使用率を計っています

検証内容は随時追記していきます

ページングとは

ページングのやり方

1.PNG

2.PNG

CPU使用率取得プログラム

import os
import sys
from time import time
import psutil

with open(os.path.join('output', 'time.csv'), 'w') as fp:
    fp.write('timestamp,cpu_percent\n')
    
    try:
        while True:
            per = psutil.cpu_percent(interval=0.5)
            t = time()
            fp.write(f'{t},{per}\n')
    except:
        pass

検証方法

ページングの条件を変化させてCPU使用率と待機時間(詳細は下記)がどうなるか5分計測して先頭と末尾のデータ100件を除外

検証用PC性能

名前 性能
CPU i5-6300U
Memory 16GB
OS Win10
MATLAB 2021a

検証1 ページングによるCPU使用率と実行周期について

  • Simulinkブロック図

3.PNG

  • MATLAB Function
function [now_time, diff_time] = fcn(prev)
coder.extrinsic('now');
coder.extrinsic('datetime');
coder.extrinsic('datestr');
coder.extrinsic('hms');

t = datetime('now', 'Format', 'HH:mm:ss.SSS');
h = 0;
m = 0;
s = 0;
[h, m, s] = hms(t);
now_time = 3600*h + 60*m + s;
diff_time = now_time-prev;


検証1結果

ページング 最小値(CPU 使用率 %) 最大値(CPU 使用率 %) 中央値(CPU 使用率 %) 標準偏差(CPU 使用率 %) 最小値(待機時間 s) 最大値(待機時間 s) 中央値(待機時間 s) 標準偏差(待機時間 s)
なし 25.0 100 32.1 11.067752 0 0.078 0.001 0.00063399
1 5.3 66.9 26.1 10.596488 0.196 0.205 0.2 0.00094149
0.1 4.5 65.9 25.2 11.168334 1.995 2.004 2 0.0018012
10 12.1 66.2 31.3 10.387546 0.011 0.072 0.02 0.0013022
10, 10 19.3 71.6 37.7 8.257467 0.002, 0.009 0.329, 0.189 0.018, 0.018 0.041822, 0.042251
0.1, 0.1 9.0 84.8 28.0 10.280704 1.991, 1.994 2.006, 2.005 2, 2 0.0028953, 0.0020017
なし、なし、なし、なし 49.3 94.1 68.1 10.50 - - - -
1.8, 1.8, 1.8, 1.8 17.2 66.4 38.6 8.563457 0.099, 0.07, 0.091, 0.095 0.131, 0.359, 0.439, 0.39 0.111, 0.111, 0.111, 0.111 0.0019042, 0.008355, 0.010881, 0.0082017

実験2 ページング機能+ROSのCPU使用率の調査

Publish→Subscribe間の時間

Publish ページング Subscribe ページング 最小値(CPU 使用率 %) 最大値(CPU 使用率 %) 中央値(CPU 使用率 %) 標準偏差(CPU 使用率 %) 最小値(転送時間 s) 最大値(転送時間 s) 中央値(転送時間 s) 標準偏差(転送時間 s)
なし なし 47.4 100.0 67.3 12.1 0.0090 0.3050 0.0850 0.0366
なし 1 47.0 100.0 69.1 13.9 0.0090 0.0690 0.0520 0.0148
なし 3 48.5 100.0 68.5 12.3 0.0080 0.1940 0.0160 0.0087
1 なし 26.5 96.6 45.3 11.9 0.0050 0.2730 0.0830 0.0620
1 1 15.2 82.3 29.7 9.9 0.1470 0.2250 0.1970 0.0050
1 3 18.2 84.2 33.2 11.3 0.0080 0.2060 0.1300 0.0538
3 なし 34.4 93.1 48.9 11.6 0.0100 0.2590 0.0760 0.0359
3 1 15.2 79.4 32.2 9.26 0.0070 0.0710 0.0120 0.0128
3 3 13.6 74.4 29.5 10.2 0.0070 0.1270 0.0360 0.0123
なし(A)、なし(B)、なし(C)、なし(D) なし 41.4 100.0 60.6 11.7 0.1670, 0.1110, 0.0580, 0.0070 0.5610, 0.3830, 0.2290, 0.1290 0.2110, 0.1500, 0.0950, 0.0360 0.0253, 0.0209, 0.0182, 0.0146
3(A), 3(B), 3(C), 3(D) 3 31.1 75.0 46.8 10.1 0.0090, 0.0140, 0.0230, 0.0180 0.1700, 0.1770, 0.2520, 0.2450 0.0410, 0.0480, 0.0600, 0.0540 0.0191, 0.0188, 0.0190, 0.0188

A : TwistStamped
B : Vector3Stamped
C : AccelStamped
D : PointStamped

  • Simulinkブロック図 Publish

2.PNG

  • Simulinkブロック図 Subscribe

3.PNG

3
1
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
3
1