Gyroscope

The gyroscope returns readings that measure the angular velocity of the device along each axis. The measurements are in radians per second (rad/s), where counterclockwise rotations are positive when looking from the positive end of the axis, and negative otherwise.

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

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

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

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

    // Create variables to store the current gyroscope values.
    float x_velocity, y_velocity, z_velocity;

    bps_initialize();

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

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

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

        if (event_domain == sensor_get_domain())
        {
            switch (event_code)
            {
                case SENSOR_GYROSCOPE_READING:
                    // Get the current gyroscope values.
                    sensor_event_get_xyz(current_event, &x_velocity, 
                                         &y_velocity, &z_velocity);
                    // Adjust application based on gyroscope values.
                    break;

                default:
                    break;
            }
        }
    }

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

    return 0;
}                
            

Last modified: 2013-12-21

comments powered by Disqus