calibration.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #define M5STACK_MPU6886
  2. #include "M5Stack.h"
  3. #include <Preferences.h>
  4. Preferences preferences;
  5. void GetGyroOffset(uint16_t times, int16_t* x_offset, int16_t* y_offset, int16_t* z_offset) {
  6. int64_t x = 0, y = 0, z = 0;
  7. int16_t gyro_x, gyro_y, gyro_z;
  8. for (size_t i = 0; i < times; i++) {
  9. M5.IMU.getGyroAdc(&gyro_x, &gyro_y, &gyro_z);
  10. x += gyro_x;
  11. y += gyro_y;
  12. z += gyro_z;
  13. }
  14. *x_offset = x / times;
  15. *y_offset = y / times;
  16. *z_offset = z / times;
  17. }
  18. void calibrationInit() {
  19. preferences.begin("Bala2Cal", false);
  20. }
  21. void calibrationGet(int16_t* gyro_x_offset, int16_t* gyro_y_offset, int16_t* gyro_z_offset, float *angle_center) {
  22. *gyro_x_offset = preferences.getInt("gryo_x", 0);
  23. *gyro_y_offset = preferences.getInt("gryo_y", 0);
  24. *gyro_z_offset = preferences.getInt("gryo_z", 0);
  25. *angle_center = preferences.getFloat("angle", 0.0);
  26. }
  27. void calibrationGryo() {
  28. M5.Lcd.fillScreen(BLACK);
  29. M5.Lcd.setTextFont(2);
  30. M5.Lcd.setTextColor(GREEN);
  31. M5.Lcd.printf("Start gryo calibration\r\n");
  32. delay(1000);
  33. M5.Lcd.printf("Please keep BALA2 still for 2 seconds\r\n");
  34. int16_t x_offset = 0;
  35. int16_t y_offset = 0;
  36. int16_t z_offset = 0;
  37. GetGyroOffset(2000, &x_offset, &y_offset, &z_offset);
  38. M5.Lcd.printf("Finish calibration !!!\r\n");
  39. preferences.putInt("gryo_x", x_offset);
  40. preferences.putInt("gryo_y", y_offset);
  41. preferences.putInt("gryo_z", z_offset);
  42. delay(1000);
  43. }
  44. void calibrationSaveCenterAngle(float angle) {
  45. preferences.putFloat("angle", angle);
  46. }