Gravity

Gravity is a fused sensor that removes the effects of user (or linear) acceleration. A BPS event with the code SENSOR_GRAVITY_READING is generated when a new gravity reading is available.

Gravity is calculated by subtracting the device's linear acceleration from its acceleration: GRAVITY = ACCELERATION - LINEAR ACCELERATION

Gravity values are reported in meters per second per second (m/s2).

A BPS event with the code SENSOR_GRAVITY_READING is generated when new gravity values are available. You can call the function sensor_event_get_xyz() to retrieve the gravity values from the event.

The code sample below demonstrates how you can use the gravity sensor in your app:

#include <stddef.h>
#include <bps/bps.h>
#include <bps/sensor.h>

int main(void)
{
    int eventDomain;
    unsigned int event_code;

    // Create variables to store the current linear acceleration values.
    float x_gravity, y_gravity, z_gravity;

    bps_initialize();

    if (sensor_is_supported(SENSOR_TYPE_GRAVITY))
    {
        sensor_request_events(SENSOR_TYPE_GRAVITY);
    }

    while (1)
    {
        bps_event_t *current_event = NULL;
        bps_get_event(&current_event, -1);

        eventDomain = bps_event_get_domain(current_event);
        event_code = bps_event_get_code(current_event);

        if (eventDomain == sensor_get_domain())
        {
            switch (event_code)
            {
                case SENSOR_GRAVITY_READING:
                    // Get the current gravity values.
                    sensor_event_get_xyz(current_event, 
                                         &x_gravity, 
                                         &y_gravity, 
                                         &z_gravity);
                    // Adjust application based on gravity values.
                    break;

                default:
                    break;
            }
        }
    }

    // Program cleanup
    sensor_stop_events(SENSOR_TYPE_GRAVITY);
    bps_shutdown();

    return 0;
}                
            

Last modified: 2013-12-21



Got questions about leaving a comment? Get answers from our Disqus FAQ.

comments powered by Disqus