OrientationSensor.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (C) 2011 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdint.h>
  17. #include <math.h>
  18. #include <sys/types.h>
  19. #include <utils/Errors.h>
  20. #include <hardware/sensors.h>
  21. #include "OrientationSensor.h"
  22. #include "SensorDevice.h"
  23. #include "SensorFusion.h"
  24. namespace android {
  25. // ---------------------------------------------------------------------------
  26. OrientationSensor::OrientationSensor()
  27. : mSensorDevice(SensorDevice::getInstance()),
  28. mSensorFusion(SensorFusion::getInstance())
  29. {
  30. // FIXME: instead of using the SensorFusion code, we should use
  31. // the SENSOR_TYPE_ROTATION_VECTOR instead. This way we could use the
  32. // HAL's implementation.
  33. }
  34. bool OrientationSensor::process(sensors_event_t* outEvent,
  35. const sensors_event_t& event)
  36. {
  37. if (event.type == SENSOR_TYPE_ACCELEROMETER) {
  38. if (mSensorFusion.hasEstimate()) {
  39. vec3_t g;
  40. const float rad2deg = 180 / M_PI;
  41. const mat33_t R(mSensorFusion.getRotationMatrix());
  42. g[0] = atan2f(-R[1][0], R[0][0]) * rad2deg;
  43. g[1] = atan2f(-R[2][1], R[2][2]) * rad2deg;
  44. g[2] = asinf ( R[2][0]) * rad2deg;
  45. if (g[0] < 0)
  46. g[0] += 360;
  47. *outEvent = event;
  48. outEvent->orientation.azimuth = g.x;
  49. outEvent->orientation.pitch = g.y;
  50. outEvent->orientation.roll = g.z;
  51. outEvent->orientation.status = SENSOR_STATUS_ACCURACY_HIGH;
  52. outEvent->sensor = '_ypr';
  53. outEvent->type = SENSOR_TYPE_ORIENTATION;
  54. return true;
  55. }
  56. }
  57. return false;
  58. }
  59. status_t OrientationSensor::activate(void* ident, bool enabled) {
  60. return mSensorFusion.activate(ident, enabled);
  61. }
  62. status_t OrientationSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
  63. return mSensorFusion.setDelay(ident, ns);
  64. }
  65. Sensor OrientationSensor::getSensor() const {
  66. sensor_t hwSensor;
  67. hwSensor.name = "Orientation Sensor";
  68. hwSensor.vendor = "AOSP";
  69. hwSensor.version = 1;
  70. hwSensor.handle = '_ypr';
  71. hwSensor.type = SENSOR_TYPE_ORIENTATION;
  72. hwSensor.maxRange = 360.0f;
  73. hwSensor.resolution = 1.0f/256.0f; // FIXME: real value here
  74. hwSensor.power = mSensorFusion.getPowerUsage();
  75. hwSensor.minDelay = mSensorFusion.getMinDelay();
  76. Sensor sensor(&hwSensor);
  77. return sensor;
  78. }
  79. // ---------------------------------------------------------------------------
  80. }; // namespace android