calibration_data.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 INT_PATH(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() { return complete; }
  43. /** Prepare to collect new calibration data. */
  44. void reset();
  45. /**
  46. * Retrieve the median gyroscope readings.
  47. *
  48. * @return Three-axis median vector.
  49. */
  50. Vector getMedian() { return median; }
  51. /**
  52. * Retrieve the mean gyroscope readings.
  53. *
  54. * @return Three-axis mean vector.
  55. */
  56. Vector getMean() { return mean; }
  57. /**
  58. * Retrieve the standard deviation of gyroscope readings.
  59. *
  60. * @return Three-axis standard deviation vector.
  61. */
  62. Vector getSigma() { return sigma; }
  63. /**
  64. * Retrieve the confidence interval size of gyroscope readings.
  65. *
  66. * @return Three-axis confidence interval size vector.
  67. */
  68. Vector getDelta() { return delta; }
  69. /**
  70. * Add a new gyroscope reading to the stats.
  71. *
  72. * @param data gyroscope values vector.
  73. * @return {@code true} if we now have enough data for calibration, or {@code false} otherwise.
  74. */
  75. bool add(Vector& data);
  76. private:
  77. // Calculates the confidence interval (mean +- delta) and some other related values, like
  78. // standard deviation, etc. See https://en.wikipedia.org/wiki/Student%27s_t-distribution
  79. void calcDelta();
  80. int count;
  81. bool complete;
  82. Vector sum;
  83. Vector sumSq;
  84. Vector mean;
  85. Vector median;
  86. Vector sigma;
  87. Vector delta;
  88. std::vector<double> xData;
  89. std::vector<double> yData;
  90. std::vector<double> zData;
  91. };