Linear acceleration

Linear acceleration is the acceleration of the device minus the acceleration due to gravity. This value is calculated by combining inputs from several device sensors, and the result is returned from the linear accelerometer sensor type (SENSOR_TYPE_LINEAR_ACCEL). These values are in meters per second per second (m/s2).

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

The code sample below demonstrates how you can use the linear accelerometer 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 linear acceleration values.
  float x_accel, y_accel, z_accel;
  float magnitude;

  bps_initialize();

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

  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_LINEAR_ACCEL_READING:
          // Get the current linear acceleration values.
          sensor_event_get_xyz(current_event, 
                               &x_accel, 
                               &y_accel, 
                               &z_accel);
          // Calculate the magnitude of linear acceleration.
          magnitude = sqrtf((x_accel * x_accel) + 
                            (y_accel * y_accel) + 
                            (z_accel * z_accel));
          // Adjust application based on linear acceleration
          // values and magnitude.
          break;

        default:
          break;
      }
    }
  }

  // Program cleanup
  sensor_stop_events(SENSOR_TYPE_LINEAR_ACCEL);
  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