cancel
Showing results for 
Search instead for 
Did you mean: 

How to read in accelerometer data from STEVAL-WeSU1 into MATLAB

TWest.3
Associate II

Hi, apologies in advance for what is probably a very elementary question. I am a clinical physiologist trying to program this unit to record experiments with patients with movement disorders.

I have got as far as connecting to the device over the BLE protocol using some simple MATLAB commands to connect to the WeSU characteristic + service. This is where I am at:

%%%

device = ble('WeSU')

chr = characteristic(device,"00000000-0001-11E1-9AB4-0002A5D5C51B","00E00000-0001-11E1-AC36-0002A5D5C51B")

 X = read(chr);

ACC = typecast(uint8(X([2:3])),'uint16');

%%%

Where

X =[ 192    3   65    0    9    0  237    3   19    0   15    0  156  255  233  255  207  254  241    1]

I know I am trying to convert into native units but I don't understand enough from the datasheet to get me going! Line "ACC..." is my guess at it, but its not working out.

(A) Am I subscribing to the correct characteristic?

(B) How do I properly convert from the raw data format to the native units of the accelerometer/gyroscope?

Any help (or pointers to past discussions) would be hugely appreciated!

Thanks

Tim

1 ACCEPTED SOLUTION

Accepted Solutions
TWest.3
Associate II

Just for anyone else with this problem- I have a solution:

(1) Connect to device and setup characteristic:

 WESU = ble("WeSu");
chr = characteristic(WESU,"00000000-0001-11E1-9AB4-0002A5D5C51B","00E00000-0001-11E1-AC36-0002A5D5C51B");
 

We are connecting the "00E00000-0001-11E1-AC36-0002A5D5C51B" characteristic which is an aggregate of the movement sensors. It follows this form:

0693W00000FBPB6QAP.png(2) Read in the data:

X = read(chr)';
% where:
X' =     [73     4   246   255   156     3   182     1   214   255   136     1    18   254    30     0   201   254   194     1]

(3) As you can see in the figure above, we need to access Bytes 2-7 for the accelerometer, and convert from the native unsigned 8-bit integers ("unit8") that MATLAB reads in, to signed 16-bit integers ("int16"):

XYZ = double(typecast(uint8(X(3:8)),'int16'));

(4) Finally, convert to engineering units, following the formula helpfully sourced in the comment by Eleon, above:

XYZ = (XYZ.*61)/1000;

You can see the output below:

0693W00000FBPJFQA5.pngGood luck!

View solution in original post

4 REPLIES 4
Eleon BORLINI
ST Employee

Hi Tim @TWest.3​ ,

Let me try to answer your question here below:

(A) The characteristics looks correct, you could try to require only the 3 axis data using the following string --> 00800000-0001-11E1-AC36-0002A5D5C51B

0693W00000Dm0SXQAZ.png(B) The accelerometer (IMU) mounted on the STEVAL-WESU is the LSM6DS3, so you can refer, for example, to the C drivers on Github for an easy conversion formula from the LSB units to the physical ones (see lsm6ds3_reg.c)

// Accelerometer
float_t lsm6ds3_from_fs2g_to_mg(int16_t lsb)
{
  return ((float_t)lsb * 61.0f / 1000.0f);
}
 
// Gyroscope
float_t lsm6ds3_from_fs125dps_to_mdps(int16_t lsb)
{
  return ((float_t)lsb   * 4375.0f / 1000.0f);
}

If my reply answered your question, please click on Select as Best at the bottom of this post. This will help other users with the same issue to find the answer faster. 

-Eleon

TWest.3
Associate II

Hi Eleon, thanks again for the response.

(1) Thanks for confirming the characteristic- I have swapped to the UUID you suggested.

(2) Thanks for tracking down the conversion - I understand this will take a 16 bit integer and convert it into the physical units of the sensor. However, I am still stuck with regards to how I reconstruct the 16 bit integer from the raw data that I read from the device.

I read in the data with the above characteristics:

device = ble('WeSU');
chr = characteristic(device,"00000000-0001-11E1-9AB4-0002A5D5C51B","00000080-0001-11E1-AC36-0002A5D5C51B");
X = read(chr)';

This yields a 20x1 double. e.g.:

X = [ 85   15  139  217   82   63   41  148  214  190   87  167  170  190  251   70   63   62    0    0];

However, table 6 you sent above makes me expect a vector of 7x1.

his is where I am stuck - how do I convert this into the correct format. I figure this list is a set of 8 bit integers that I should convert to 16 bit integers, using MATLABs typecast function which converts from uint8 to uint16 by parsing the vector in pairs:

        ACC= double(typecast(uint8(X(bInts)),'uint16'));

This however isn't yielding any sensible output that conforms to movement - I figure I am reading something in the wrong order?

Thanks again for your assistance.

-Tim

TWest.3
Associate II

Just for anyone else with this problem- I have a solution:

(1) Connect to device and setup characteristic:

 WESU = ble("WeSu");
chr = characteristic(WESU,"00000000-0001-11E1-9AB4-0002A5D5C51B","00E00000-0001-11E1-AC36-0002A5D5C51B");
 

We are connecting the "00E00000-0001-11E1-AC36-0002A5D5C51B" characteristic which is an aggregate of the movement sensors. It follows this form:

0693W00000FBPB6QAP.png(2) Read in the data:

X = read(chr)';
% where:
X' =     [73     4   246   255   156     3   182     1   214   255   136     1    18   254    30     0   201   254   194     1]

(3) As you can see in the figure above, we need to access Bytes 2-7 for the accelerometer, and convert from the native unsigned 8-bit integers ("unit8") that MATLAB reads in, to signed 16-bit integers ("int16"):

XYZ = double(typecast(uint8(X(3:8)),'int16'));

(4) Finally, convert to engineering units, following the formula helpfully sourced in the comment by Eleon, above:

XYZ = (XYZ.*61)/1000;

You can see the output below:

0693W00000FBPJFQA5.pngGood luck!

Thank you @TWest.3​ for sharing the details of your solution!

Of course, it will be helpful for all the Community

-Eleon