Main Content

フィットネス モニタリング デバイスからのデータの収集

この例では、Bluetooth® Low Energy 通信を使用して、フィットネス モニタリング デバイスからデータを集めてプロットする方法を説明します。

ハードウェア セットアップ

この例では、Under Armour® 心拍数モニター ベルトと Under Armour スマート ランニング シューズ 1 足を使用します。どちらのデバイスも Bluetooth Low Energy 通信をサポートします。

デバイスの検出と接続

最初に、Bluetooth Low Energy デバイスを MATLAB® で検索することにより、それらが接続をサポートすることを確認します。関数 blelist は、アドバタイズしている近くの Bluetooth Low Energy 周辺デバイスをスキャンします。

blelist
ans=14×5 table
    Index           Name                           Address                    RSSI    Advertisement
    _____    ___________________    ______________________________________    ____    _____________

      1      ""                     "21996E3A-8F31-496D-B332-79D03B759BC7"    -65     [1×1 struct] 
      2      ""                     "12377421-4EA3-4F77-9365-EFD580C34DE9"    -66     [1×1 struct] 
      3      "UA E39 MODULE"        "8206F662-BA4A-483F-A908-516FF506BFB0"    -67     [1×1 struct] 
      4      ""                     "CF65AD36-2146-4CA1-BCEA-FED47F6195CA"    -76     [1×1 struct] 
      5      "UA Footpod 239AE2"    "CF7B1A17-4104-4D7B-AE70-1837CAE9C9D0"    -78     [1×1 struct] 
      6      ""                     "67A1A92A-5F1C-4AF8-857A-99F194E9A5F8"    -82     [1×1 struct] 
      7      ""                     "5609D68D-0EED-41D7-BE19-F3ACAA119C7A"    -84     [1×1 struct] 
      8      ""                     "5A17DF46-6F71-4DAC-AF0D-0F28E3911187"    -85     [1×1 struct] 
      9      ""                     "9675A0FA-0394-468B-B908-040696E1C5BC"    -88     [1×1 struct] 
     10      ""                     "61540D17-C2DD-41D4-B107-E2E7374B11F4"    -88     [1×1 struct] 
     11      ""                     "C2A5CCC3-CA6C-4688-AAE9-A5BE039561F9"    -92     [1×1 struct] 
     12      ""                     "AF5E1195-4088-4A4B-ADA5-0BD3C91BFE62"    -93     [1×1 struct] 
     13      ""                     "83C69EFB-0FAD-4A35-B167-79C51A1F245D"    -94     [1×1 struct] 
     14      ""                     "993276FD-07EC-433D-85E1-E144B289B648"    -95     [1×1 struct] 

デバイスが MATLAB で見つかったら、ble を呼び出して接続します。デバイス名が一意であれば名前を指定し、そうでなければデバイスのアドレスを指定します。

belt = ble("UA E39 MODULE")
belt = 
  ble with properties:

               Name: "UA E39 MODULE"
            Address: "8206F662-BA4A-483F-A908-516FF506BFB0"
          Connected: 1
           Services: [4×2 table]
    Characteristics: [22×5 table]

Show services and characteristics
shoe = ble("UA Footpod 239AE2")
shoe = 
  ble with properties:

               Name: "UA Footpod 239AE2"
            Address: "CF7B1A17-4104-4D7B-AE70-1837CAE9C9D0"
          Connected: 1
           Services: [8×2 table]
    Characteristics: [39×5 table]

Show services and characteristics

ble オブジェクト belt の Characteristics プロパティにアクセスします。このデバイスには "Heart Rate" サービスがあり、そこには "Heart Rate Measurement" 特性が含まれています。

belt.Characteristics
ans=22×5 table
        ServiceName                      ServiceUUID                                   CharacteristicName                                CharacteristicUUID               Attributes 
    ____________________    ______________________________________    _____________________________________________________    ______________________________________    ____________

    "Heart Rate"            "180D"                                    "Heart Rate Measurement"                                 "2A37"                                    {["Notify"]}
    "Heart Rate"            "180D"                                    "Body Sensor Location"                                   "2A38"                                    {["Read"  ]}
    "Heart Rate"            "180D"                                    "Heart Rate Control Point"                               "2A39"                                    {["Write" ]}
    "Battery Service"       "180F"                                    "Battery Level"                                          "2A19"                                    {1×2 string}
    "Device Information"    "180A"                                    "System ID"                                              "2A23"                                    {["Read"  ]}
    "Device Information"    "180A"                                    "Model Number String"                                    "2A24"                                    {["Read"  ]}
    "Device Information"    "180A"                                    "Serial Number String"                                   "2A25"                                    {["Read"  ]}
    "Device Information"    "180A"                                    "Firmware Revision String"                               "2A26"                                    {["Read"  ]}
    "Device Information"    "180A"                                    "Hardware Revision String"                               "2A27"                                    {["Read"  ]}
    "Device Information"    "180A"                                    "Software Revision String"                               "2A28"                                    {["Read"  ]}
    "Device Information"    "180A"                                    "Manufacturer Name String"                               "2A29"                                    {["Read"  ]}
    "Device Information"    "180A"                                    "IEEE 11073-20601 Regulatory Certification Data List"    "2A2A"                                    {["Read"  ]}
    "Device Information"    "180A"                                    "PnP ID"                                                 "2A50"                                    {["Read"  ]}
    "Custom"                "21A51000-4C86-11E2-BCFD-0800200C9A66"    "Custom"                                                 "21A51011-4C86-11E2-BCFD-0800200C9A66"    {1×2 string}
    "Custom"                "21A51000-4C86-11E2-BCFD-0800200C9A66"    "Custom"                                                 "21A51021-4C86-11E2-BCFD-0800200C9A66"    {1×2 string}
    "Custom"                "21A51000-4C86-11E2-BCFD-0800200C9A66"    "Custom"                                                 "21A51022-4C86-11E2-BCFD-0800200C9A66"    {["Write" ]}
      ⋮

ble オブジェクト shoe の Characteristics プロパティにアクセスします。このデバイスには "Running Speed and Cadence" サービスがあり、そこには "RSC Measurement" 特性が含まれています。

shoe.Characteristics
ans=39×5 table
            ServiceName                         ServiceUUID                                   CharacteristicName                                CharacteristicUUID               Attributes 
    ___________________________    ______________________________________    _____________________________________________________    ______________________________________    ____________

    "Device Information"           "180A"                                    "System ID"                                              "2A23"                                    {["Read"  ]}
    "Device Information"           "180A"                                    "Model Number String"                                    "2A24"                                    {["Read"  ]}
    "Device Information"           "180A"                                    "Serial Number String"                                   "2A25"                                    {["Read"  ]}
    "Device Information"           "180A"                                    "Firmware Revision String"                               "2A26"                                    {["Read"  ]}
    "Device Information"           "180A"                                    "Hardware Revision String"                               "2A27"                                    {["Read"  ]}
    "Device Information"           "180A"                                    "Software Revision String"                               "2A28"                                    {["Read"  ]}
    "Device Information"           "180A"                                    "Manufacturer Name String"                               "2A29"                                    {["Read"  ]}
    "Device Information"           "180A"                                    "IEEE 11073-20601 Regulatory Certification Data List"    "2A2A"                                    {["Read"  ]}
    "Device Information"           "180A"                                    "PnP ID"                                                 "2A50"                                    {["Read"  ]}
    "Battery Service"              "180F"                                    "Battery Level"                                          "2A19"                                    {1×2 string}
    "Running Speed and Cadence"    "1814"                                    "RSC Measurement"                                        "2A53"                                    {["Notify"]}
    "Running Speed and Cadence"    "1814"                                    "RSC Feature"                                            "2A54"                                    {["Read"  ]}
    "Running Speed and Cadence"    "1814"                                    "SC Control Point"                                       "2A55"                                    {1×2 string}
    "Custom"                       "21A54000-4C86-11E2-BCFD-0800200C9A66"    "Custom"                                                 "21A54001-4C86-11E2-BCFD-0800200C9A66"    {1×2 string}
    "Custom"                       "21A54000-4C86-11E2-BCFD-0800200C9A66"    "Custom"                                                 "21A54002-4C86-11E2-BCFD-0800200C9A66"    {1×2 string}
    "Custom"                       "21A54000-4C86-11E2-BCFD-0800200C9A66"    "Custom"                                                 "21A54003-4C86-11E2-BCFD-0800200C9A66"    {1×3 string}
      ⋮

心拍数データの読み取り

次に、サービスと特性の情報を指定することにより、"Heart Rate Measurement" 特性のオブジェクトを作成します。

hr = characteristic(belt, "heart rate", "heart rate measurement")
hr = 
  Characteristic with properties:

             Name: "Heart Rate Measurement"
             UUID: "2A37"
       Attributes: "Notify"
      Descriptors: [1x3 table]
 DataAvailableFcn: []

Show descriptors

その後、現在の心拍数測定値をデバイスから読み取ります。

data = read(hr)
data = 1×4

    22    96    73     3

Bluetooth Low Energy Specification List の Heart Rate Service 仕様に従い、"Heart Rate Measurement" 特性の値にはフラグ バイトが含まれ、その後に 1 つまたは多数の心拍数の値が続きます。測定値の形式は、フラグ値に依存します。生のデータを拍数/分 (bpm) 単位の心拍数に変換します。

flag = uint8(data(1));
% Get the first bit of the flag, which indicates the format of the heart rate value
heartRateValueFormat = bitget(flag, 1);
if heartRateValueFormat == 0
    % Heart rate format is uint8
    heartRate = data(2);
else
    % Heart rate format is uint16
    heartRate = double(typecast(uint8(data(2:3)), 'uint16'));
end
fprintf('Heart rate measurement: %d(bpm)\n', heartRate);
Heart rate measurement: 96(bpm)

走行速度と歩数データの読み取り

同様に、サービスと特性の情報を指定することにより、"RSC Measurement" 特性のオブジェクトを作成します。

rsc = characteristic(shoe, "running speed and cadence", "rsc measurement")
rsc = 
  Characteristic with properties:

             Name: "RSC Measurement"
             UUID: "2A53"
       Attributes: "Notify"
      Descriptors: [1x3 table]
 DataAvailableFcn: []

Show descriptors

次に、現在の走行速度と歩数の値をデバイスから読み取ります。

data = read(rsc)
data = 1×10

     3     0     0     0     0     0    84    57     0     0

Bluetooth Low Energy Specification List の Running Speed and Cadence Service 仕様に従い、"RSC Measurement" 特性の値には、瞬時速度を表す 2 バイトと瞬時歩数を表す 1 バイトが含まれています。生のデータを、メートル/秒 (m/s) 単位の走行速度とステップ/分単位の歩数に変換します。

instantaneousSpeed = double(typecast(uint8(data(2:3)), 'uint16'))/256;
instantaneousCadence = data(4);
fprintf('Instantaneous speed: %.2f(m/s) and instantaneous cadence: %d(steps per minute)\n', instantaneousSpeed, instantaneousCadence);
Instantaneous speed: 0.00(m/s) and instantaneous cadence: 0(steps per minute)

心拍数と走行速度の相関

心拍数と RSC のデータを読み取った後、走行セッション中のライブ データを追跡し、2 つの測定値を関連付けてフィットネスの状態を解析します。

フィットネス データの相関を示すために、まず 2 つのプロットを作成して、ループ内にデータ点を追加することでアニメーションを作成します。

% Create a plot for running speed against heart rate
axSpeed = axes('XLim', [0, 5], 'YLim', [60, 220]);
xlabel(axSpeed, 'Running speed (m/s)');
ylabel(axSpeed, 'Heart rate (bpm)');
subplot(1, 2, 1, axSpeed);
hSpeed = animatedline(axSpeed, 'Marker', 'o', 'MarkerFaceColor', 'green');

% Create a plot for running cadence against heart rate
axCadence = axes('XLim', [0, 200], 'YLim', [60 220]);
xlabel(axCadence, 'Running cadence (steps per minute)');
ylabel(axCadence, 'Heart rate (bpm)');
subplot(1, 2, 2, axCadence);
hCadence = animatedline(axCadence, 'Marker', 'o', 'MarkerFaceColor', 'blue');

次に、ループ内でデバイスのデータを読み取り、ユーザーが歩行からジョギングへ、さらに走行へと進むにつれてプロットを更新します。

for loop = 1:30
    % Get heart rate data
    data = read(hr);
    flag = uint8(data(1));
    heartRateValueFormat = bitget(flag, 1);
    if heartRateValueFormat == 0
        heartRate = data(2);
    else
        heartRate = double(typecast(uint8(data(2:3)), 'uint16'));
    end
    
    % Get running speed data
    data = read(rsc);
    instantaneousSpeed = double(typecast(uint8(data(2:3)), 'uint16'))/256;
    instantaneousCadence = data(4);
    
    % Update plot with new data
    addpoints(hSpeed, instantaneousSpeed, heartRate);
    addpoints(hCadence, instantaneousCadence, heartRate);
    drawnow;
end

これらのプロットは、走行速度と歩数の値が上がると心拍数が増えることを概ね示しています。

デバイスからの切断

デバイス オブジェクトでの作業が終了したら、それらをクリアします。

clear belt shoe