calibration_data.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include <toolbox/saved_struct.h>
  3. #include <storage/storage.h>
  4. #include <vector>
  5. #include "util/vector.h"
  6. #define CALIBRATION_DATA_VER (1)
  7. #define CALIBRATION_DATA_FILE_NAME "calibration.data"
  8. #define CALIBRATION_DATA_PATH EXT_PATH("apps_data/air_mouse/" CALIBRATION_DATA_FILE_NAME)
  9. #define CALIBRATION_DATA_MAGIC (0x23)
  10. #define CALIBRATION_DATA_SAVE(x) \
  11. saved_struct_save( \
  12. CALIBRATION_DATA_PATH, \
  13. (x), \
  14. sizeof(CalibrationMedian), \
  15. CALIBRATION_DATA_MAGIC, \
  16. CALIBRATION_DATA_VER)
  17. #define CALIBRATION_DATA_LOAD(x) \
  18. saved_struct_load( \
  19. CALIBRATION_DATA_PATH, \
  20. (x), \
  21. sizeof(CalibrationMedian), \
  22. CALIBRATION_DATA_MAGIC, \
  23. CALIBRATION_DATA_VER)
  24. typedef struct {
  25. double x;
  26. double y;
  27. double z;
  28. } CalibrationMedian;
  29. typedef cardboard::Vector3 Vector;
  30. /**
  31. * Helper class to gather some stats and store the calibration data. Right now it calculates a lot
  32. * more stats than actually needed. Some of them are used for logging the sensors quality (and
  33. * filing bugs), other may be required in the future, e.g. for bias.
  34. */
  35. class CalibrationData {
  36. public:
  37. /**
  38. * Check if the sensors were calibrated before.
  39. *
  40. * @return {@code true} if calibration data is available, or {@code false} otherwise.
  41. */
  42. bool isComplete() {
  43. return complete;
  44. }
  45. /** Prepare to collect new calibration data. */
  46. void reset();
  47. /**
  48. * Retrieve the median gyroscope readings.
  49. *
  50. * @return Three-axis median vector.
  51. */
  52. Vector getMedian() {
  53. return median;
  54. }
  55. /**
  56. * Retrieve the mean gyroscope readings.
  57. *
  58. * @return Three-axis mean vector.
  59. */
  60. Vector getMean() {
  61. return mean;
  62. }
  63. /**
  64. * Retrieve the standard deviation of gyroscope readings.
  65. *
  66. * @return Three-axis standard deviation vector.
  67. */
  68. Vector getSigma() {
  69. return sigma;
  70. }
  71. /**
  72. * Retrieve the confidence interval size of gyroscope readings.
  73. *
  74. * @return Three-axis confidence interval size vector.
  75. */
  76. Vector getDelta() {
  77. return delta;
  78. }
  79. /**
  80. * Add a new gyroscope reading to the stats.
  81. *
  82. * @param data gyroscope values vector.
  83. * @return {@code true} if we now have enough data for calibration, or {@code false} otherwise.
  84. */
  85. bool add(Vector& data);
  86. private:
  87. // Calculates the confidence interval (mean +- delta) and some other related values, like
  88. // standard deviation, etc. See https://en.wikipedia.org/wiki/Student%27s_t-distribution
  89. void calcDelta();
  90. int count;
  91. bool complete;
  92. Vector sum;
  93. Vector sumSq;
  94. Vector mean;
  95. Vector median;
  96. Vector sigma;
  97. Vector delta;
  98. std::vector<double> xData;
  99. std::vector<double> yData;
  100. std::vector<double> zData;
  101. };