eeprom_utils.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include <EEPROM.h>
  2. #include "MPU9250.h"
  3. const uint8_t EEPROM_SIZE = 1 + sizeof(float) * 3 * 4;
  4. extern MPU9250 mpu;
  5. enum EEP_ADDR
  6. {
  7. EEP_CALIB_FLAG = 0x00,
  8. EEP_ACC_BIAS = 0x01,
  9. EEP_GYRO_BIAS = 0x0D,
  10. EEP_MAG_BIAS = 0x19,
  11. EEP_MAG_SCALE = 0x25
  12. };
  13. void writeByte(int address, byte value){
  14. EEPROM.put(address, value);
  15. }
  16. void writeFloat(int address, float value){
  17. EEPROM.put(address, value);
  18. }
  19. byte readByte(int address){
  20. byte valueIn;
  21. EEPROM.get(address, valueIn);
  22. return valueIn;
  23. }
  24. float readFloat(int address){
  25. float valueIn;
  26. EEPROM.get(address, valueIn);
  27. return valueIn;
  28. }
  29. void clearCalibration()
  30. {
  31. writeByte(EEP_CALIB_FLAG, 0);
  32. }
  33. bool isCalibrated()
  34. {
  35. return (readByte(EEP_CALIB_FLAG) == 0x01);
  36. }
  37. void saveCalibration()
  38. {
  39. writeByte(EEP_CALIB_FLAG, 1);
  40. writeFloat(EEP_ACC_BIAS + 0, mpu.getAccBias(0));
  41. writeFloat(EEP_ACC_BIAS + 4, mpu.getAccBias(1));
  42. writeFloat(EEP_ACC_BIAS + 8, mpu.getAccBias(2));
  43. writeFloat(EEP_GYRO_BIAS + 0, mpu.getGyroBias(0));
  44. writeFloat(EEP_GYRO_BIAS + 4, mpu.getGyroBias(1));
  45. writeFloat(EEP_GYRO_BIAS + 8, mpu.getGyroBias(2));
  46. writeFloat(EEP_MAG_BIAS + 0, mpu.getMagBias(0));
  47. writeFloat(EEP_MAG_BIAS + 4, mpu.getMagBias(1));
  48. writeFloat(EEP_MAG_BIAS + 8, mpu.getMagBias(2));
  49. writeFloat(EEP_MAG_SCALE + 0, mpu.getMagScale(0));
  50. writeFloat(EEP_MAG_SCALE + 4, mpu.getMagScale(1));
  51. writeFloat(EEP_MAG_SCALE + 8, mpu.getMagScale(2));
  52. }
  53. void loadCalibration()
  54. {
  55. if (isCalibrated())
  56. {
  57. Serial.println("calibrated? : YES");
  58. Serial.println("load calibrated values");
  59. mpu.setAccBias(0, readFloat(EEP_ACC_BIAS + 0));
  60. mpu.setAccBias(1, readFloat(EEP_ACC_BIAS + 4));
  61. mpu.setAccBias(2, readFloat(EEP_ACC_BIAS + 8));
  62. mpu.setGyroBias(0, readFloat(EEP_GYRO_BIAS + 0));
  63. mpu.setGyroBias(1, readFloat(EEP_GYRO_BIAS + 4));
  64. mpu.setGyroBias(2, readFloat(EEP_GYRO_BIAS + 8));
  65. mpu.setMagBias(0, readFloat(EEP_MAG_BIAS + 0));
  66. mpu.setMagBias(1, readFloat(EEP_MAG_BIAS + 4));
  67. mpu.setMagBias(2, readFloat(EEP_MAG_BIAS + 8));
  68. mpu.setMagScale(0, readFloat(EEP_MAG_SCALE + 0));
  69. mpu.setMagScale(1, readFloat(EEP_MAG_SCALE + 4));
  70. mpu.setMagScale(2, readFloat(EEP_MAG_SCALE + 8));
  71. }
  72. else
  73. {
  74. Serial.println("calibrated? : NO");
  75. Serial.println("load default values");
  76. mpu.setAccBias(0, +0.005);
  77. mpu.setAccBias(1, -0.008);
  78. mpu.setAccBias(2, -0.001);
  79. mpu.setGyroBias(0, +1.5);
  80. mpu.setGyroBias(1, -0.5);
  81. mpu.setGyroBias(2, +0.7);
  82. mpu.setMagBias(0, +186.41);
  83. mpu.setMagBias(1, -197.91);
  84. mpu.setMagBias(2, -425.55);
  85. mpu.setMagScale(0, +1.07);
  86. mpu.setMagScale(1, +0.95);
  87. mpu.setMagScale(2, +0.99);
  88. }
  89. }
  90. void printCalibration()
  91. {
  92. Serial.println("< calibration parameters >");
  93. Serial.print("calibrated? : ");
  94. Serial.println(readByte(EEP_CALIB_FLAG) ? "YES" : "NO");
  95. Serial.print("acc bias x : ");
  96. Serial.println(readFloat(EEP_ACC_BIAS + 0) * 1000.f);
  97. Serial.print("acc bias y : ");
  98. Serial.println(readFloat(EEP_ACC_BIAS + 4) * 1000.f);
  99. Serial.print("acc bias z : ");
  100. Serial.println(readFloat(EEP_ACC_BIAS + 8) * 1000.f);
  101. Serial.print("gyro bias x : ");
  102. Serial.println(readFloat(EEP_GYRO_BIAS + 0));
  103. Serial.print("gyro bias y : ");
  104. Serial.println(readFloat(EEP_GYRO_BIAS + 4));
  105. Serial.print("gyro bias z : ");
  106. Serial.println(readFloat(EEP_GYRO_BIAS + 8));
  107. Serial.print("mag bias x : ");
  108. Serial.println(readFloat(EEP_MAG_BIAS + 0));
  109. Serial.print("mag bias y : ");
  110. Serial.println(readFloat(EEP_MAG_BIAS + 4));
  111. Serial.print("mag bias z : ");
  112. Serial.println(readFloat(EEP_MAG_BIAS + 8));
  113. Serial.print("mag scale x : ");
  114. Serial.println(readFloat(EEP_MAG_SCALE + 0));
  115. Serial.print("mag scale y : ");
  116. Serial.println(readFloat(EEP_MAG_SCALE + 4));
  117. Serial.print("mag scale z : ");
  118. Serial.println(readFloat(EEP_MAG_SCALE + 8));
  119. }
  120. void printBytes()
  121. {
  122. for (size_t i = 0; i < EEPROM_SIZE; ++i)
  123. Serial.println(readByte(i), HEX);
  124. }
  125. void setupEEPROM()
  126. {
  127. Serial.println("EEPROM start");
  128. if (!isCalibrated())
  129. {
  130. Serial.println("Need Calibration!!");
  131. }
  132. Serial.println("EEPROM calibration value is : ");
  133. printCalibration();
  134. Serial.println("Loaded calibration value is : ");
  135. loadCalibration();
  136. }