RotationVectorSensor.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2010 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 "RotationVectorSensor.h"
  22. namespace android {
  23. // ---------------------------------------------------------------------------
  24. RotationVectorSensor::RotationVectorSensor()
  25. : mSensorDevice(SensorDevice::getInstance()),
  26. mSensorFusion(SensorFusion::getInstance())
  27. {
  28. }
  29. bool RotationVectorSensor::process(sensors_event_t* outEvent,
  30. const sensors_event_t& event)
  31. {
  32. if (event.type == SENSOR_TYPE_ACCELEROMETER) {
  33. if (mSensorFusion.hasEstimate()) {
  34. const vec4_t q(mSensorFusion.getAttitude());
  35. *outEvent = event;
  36. outEvent->data[0] = q.x;
  37. outEvent->data[1] = q.y;
  38. outEvent->data[2] = q.z;
  39. outEvent->data[3] = q.w;
  40. outEvent->sensor = '_rov';
  41. outEvent->type = SENSOR_TYPE_ROTATION_VECTOR;
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. status_t RotationVectorSensor::activate(void* ident, bool enabled) {
  48. return mSensorFusion.activate(ident, enabled);
  49. }
  50. status_t RotationVectorSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
  51. return mSensorFusion.setDelay(ident, ns);
  52. }
  53. Sensor RotationVectorSensor::getSensor() const {
  54. sensor_t hwSensor;
  55. hwSensor.name = "Rotation Vector Sensor";
  56. hwSensor.vendor = "AOSP";
  57. hwSensor.version = 3;
  58. hwSensor.handle = '_rov';
  59. hwSensor.type = SENSOR_TYPE_ROTATION_VECTOR;
  60. hwSensor.maxRange = 1;
  61. hwSensor.resolution = 1.0f / (1<<24);
  62. hwSensor.power = mSensorFusion.getPowerUsage();
  63. hwSensor.minDelay = mSensorFusion.getMinDelay();
  64. Sensor sensor(&hwSensor);
  65. return sensor;
  66. }
  67. // ---------------------------------------------------------------------------
  68. GyroDriftSensor::GyroDriftSensor()
  69. : mSensorDevice(SensorDevice::getInstance()),
  70. mSensorFusion(SensorFusion::getInstance())
  71. {
  72. }
  73. bool GyroDriftSensor::process(sensors_event_t* outEvent,
  74. const sensors_event_t& event)
  75. {
  76. if (event.type == SENSOR_TYPE_ACCELEROMETER) {
  77. if (mSensorFusion.hasEstimate()) {
  78. const vec3_t b(mSensorFusion.getGyroBias());
  79. *outEvent = event;
  80. outEvent->data[0] = b.x;
  81. outEvent->data[1] = b.y;
  82. outEvent->data[2] = b.z;
  83. outEvent->sensor = '_gbs';
  84. outEvent->type = SENSOR_TYPE_ACCELEROMETER;
  85. return true;
  86. }
  87. }
  88. return false;
  89. }
  90. status_t GyroDriftSensor::activate(void* ident, bool enabled) {
  91. return mSensorFusion.activate(ident, enabled);
  92. }
  93. status_t GyroDriftSensor::setDelay(void* ident, int /*handle*/, int64_t ns) {
  94. return mSensorFusion.setDelay(ident, ns);
  95. }
  96. Sensor GyroDriftSensor::getSensor() const {
  97. sensor_t hwSensor;
  98. hwSensor.name = "Gyroscope Bias (debug)";
  99. hwSensor.vendor = "AOSP";
  100. hwSensor.version = 1;
  101. hwSensor.handle = '_gbs';
  102. hwSensor.type = SENSOR_TYPE_ACCELEROMETER;
  103. hwSensor.maxRange = 1;
  104. hwSensor.resolution = 1.0f / (1<<24);
  105. hwSensor.power = mSensorFusion.getPowerUsage();
  106. hwSensor.minDelay = mSensorFusion.getMinDelay();
  107. Sensor sensor(&hwSensor);
  108. return sensor;
  109. }
  110. // ---------------------------------------------------------------------------
  111. }; // namespace android