calibration.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #define M5STACK_MPU6886
  2. #include <Preferences.h>
  3. #include "M5Stack.h"
  4. Preferences preferences;
  5. void GetGyroOffset(uint16_t times, int16_t* x_offset, int16_t* y_offset,
  6. int16_t* z_offset) {
  7. int64_t x = 0, y = 0, z = 0;
  8. int16_t gyro_x, gyro_y, gyro_z;
  9. for (size_t i = 0; i < times; i++) {
  10. M5.IMU.getGyroAdc(&gyro_x, &gyro_y, &gyro_z);
  11. x += gyro_x;
  12. y += gyro_y;
  13. z += gyro_z;
  14. }
  15. *x_offset = x / times;
  16. *y_offset = y / times;
  17. *z_offset = z / times;
  18. }
  19. void calibrationInit() { preferences.begin("Bala2Cal", false); }
  20. void calibrationGet(int16_t* gyro_x_offset, int16_t* gyro_y_offset,
  21. 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. }