'Which android sensors to use to detect motion in a sleep study?

I am building a framework for a Linux host to collect events while a subject is sleeping. One part involves strapping an android phone to the subject and running HyperImu, an app that records selected sensors and sends them to the host as UDP packets.

For the first pass, I want to detect when a subject tosses and turns. (Further on, I hope to distinguish turns to the right or left from getting up or returning to bed.)

Android Developers describe a Significant Motion Sensor, but, unfortunately, this is a trigger event that I always see as zero. I have not found which combination of sensors produce this. I have looked at the distributions of the magnitudes of the Linear Acceleration and Rotation vectors but I have not been able to connect these to motion or stability.

I would like to understand how to combine the available sensors to do this.

EDIT
This appears to be wrong:

Just now, I have seen Note: When a device is at rest, the output of the gravity sensor should be identical to that of the accelerometer.

This is what I get with the phone sitting on a table, screen up:

                             [,1]        [,2]         [,3]
gravity_sensor.x                0  0.07383085  0.072416830
gravity_sensor.y                0  0.05186246  0.050198150
gravity_sensor.z                0  9.80958600  9.809605000
linear_acceleration_sensor.x    0 -0.08544645 -0.059478775
linear_acceleration_sensor.y    0  0.07358940 -0.039030086
linear_acceleration_sensor.z    0  0.03905296  0.006094933


Solution 1:[1]

The gravity sensor is the accelerometer and it is working correctly.
This is what a typical reading for a stationary mobile device should be.
Look at the 'gravity_sensor.z' which gives the value to be around 9.8.
That's the gravitational acceleration which is always there on the phone (g = 9.81m2/sec).
The linear sensors would give try to give the acceleration metrics after removing the effects of gravity. so, that might be what you want.


Now, moving on to the next part, If you want to record toss and turns of a sleeping subject, the gyroscope sensor's output is what you want.
The gyro would give you the rotational details of the device.
Any big changes in that would mean the subject has changed it's sleeping position.


Eg -
Let's say the subject is sleeping face up. that would give the rotational output as x~0,y~0,z~0
Now, let's assume the subject is now sleeping on it's right arm, that would mean the phone has now rotated and should have it's rotational details somewhere along the lines of x~0,y~0,z~90 (note this is in degrees and your values might differ)

Solution 2:[2]

In this table, the gravitational vector seems to reliably identify the positions that interest me:
rotational bodge

--------------------------------------------------------
     position        clock    gravX     gravY    gravZ  
------------------- ------- --------- --------- --------
      onBack         0000    0.08792   -0.359     9.8   

 turningUpperRight   0130    -5.149    0.2289    8.343  

      onRight        0300    -9.758    0.9697    0.0754 

 turningLowerRight   0430     -5.34     1.234    -8.132 

     onStomach       0600    0.2071     1.061    -9.747 

 turningLowerLeft    0730     5.566    0.6752    -8.046 

      onLeft         0900     9.653    -0.1737   -1.722 

 turningUpperLeft    1030     8.764    -0.863    4.315  

       erect          NA     0.08891    9.584     2.09  

      reading         NA     -1.168     5.905    -7.746 
--------------------------------------------------------

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vaishnav Kanhirathingal
Solution 2 Steve Dutky