フィルターのクリア

Converting two decimal numbers into binary, concatenating the resulting 8 bit integers to get 16 bit signed integer and converting to signed float (IMU)

16 ビュー (過去 30 日間)
So I have this 6DOF IMU that spits out data packets as follows:
sensorData = [85 13 109 85 12 147 12 0 156 0 71 255 229 10 144 252];
The acceleration in x axis for example, would be found by converting 12 and 147 to 8 bit binary numbers, concatenating them to get signed 16 bit binary number and converting them to signed floating point number.
I am not sure how to properly write the code to do the job. Here's my code for example, which doesn't work as expected:
sensorData = read(characteristic(b, "FF06", "FF08"));
z(i,1) = (typecast(uint16(bin2dec(strcat(dec2bin(sensorData(5),8), dec2bin(sensorData(6),8)))),'int16'))/SFacc; %SFacc = 8192 LSB/ms^-2
Am I doing something wrong? The values I receive are not what I am expecting.

採用された回答

Walter Roberson
Walter Roberson 2019 年 11 月 23 日
double( swapbytes(typecast(uint8(sensorData), 'int16')) ) / SFacc
  4 件のコメント
Walter Roberson
Walter Roberson 2019 年 11 月 23 日
Yes, it takes groups of two uint8 to form the int16.
All supported versions of MATLAB for a number of years have been on Intel architecture, which is "little endian". For little endian, the byte pair A, B when bundled together into a single 16 bit entity, are mathematically A + 256*B not 256*A + B . The swapbytes() reorders the byte pairs within the 16 bit numbers.
Swapnil Sayan Saha
Swapnil Sayan Saha 2019 年 11 月 23 日
The concatenation indeed follows little endian (as per the datasheet of the IMU).

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by