BALA.ino 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Description: This code only for mpu6886! first init need press BtnC and
  3. reboot to calibrate !
  4. */
  5. #include <M5Stack.h>
  6. #include <Preferences.h>
  7. #include <Wire.h>
  8. #include "M5Bala.h"
  9. #include "imuCalibration.h"
  10. Preferences preferences;
  11. M5Bala m5bala(Wire);
  12. // ================ Draw Angle Wavefrom =================
  13. void draw_waveform() {
  14. #define MAX_LEN 120
  15. #define X_OFFSET 0
  16. #define Y_OFFSET 100
  17. #define X_SCALE 3
  18. static int16_t val_buf[MAX_LEN] = {0};
  19. static int16_t pt = MAX_LEN - 1;
  20. val_buf[pt] = constrain((int16_t)(m5bala.getAngle() * X_SCALE), -80, 80);
  21. if (--pt < 0) {
  22. pt = MAX_LEN - 1;
  23. }
  24. for (int i = 1; i < (MAX_LEN); i++) {
  25. uint16_t now_pt = (pt + i) % (MAX_LEN);
  26. M5.Lcd.drawLine(i, val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET, i + 1,
  27. val_buf[(now_pt + 2) % MAX_LEN] + Y_OFFSET, TFT_BLACK);
  28. if (i < MAX_LEN - 1)
  29. M5.Lcd.drawLine(i, val_buf[now_pt] + Y_OFFSET, i + 1,
  30. val_buf[(now_pt + 1) % MAX_LEN] + Y_OFFSET,
  31. TFT_GREEN);
  32. }
  33. }
  34. // ================ GYRO offset param ==================
  35. void auto_tune_gyro_offset() {
  36. M5.Speaker.tone(500, 200);
  37. delay(300);
  38. M5.update();
  39. M5.Lcd.println("Start IMU calculate gyro offsets");
  40. M5.Lcd.println("DO NOT MOVE A MPU6050...");
  41. delay(2000);
  42. imu_calcGyroOffsets();
  43. float gyroXoffset = imu_getOffsetX();
  44. M5.Lcd.println("Done!!!");
  45. M5.Lcd.print("X : ");
  46. M5.Lcd.println(gyroXoffset);
  47. M5.Lcd.println("Program will start after 3 seconds");
  48. M5.Lcd.print("========================================");
  49. // Save
  50. preferences.putFloat("gyroXoffset", gyroXoffset);
  51. preferences.end();
  52. }
  53. void setup() {
  54. // Power ON Stabilizing...
  55. M5.begin();
  56. M5.Power.begin();
  57. Wire.begin();
  58. Wire.setClock(400000UL); // Set I2C frequency to 400kHz
  59. // Display info
  60. M5.Lcd.setTextFont(2);
  61. M5.Lcd.setTextColor(WHITE, BLACK);
  62. M5.Lcd.println("M5Stack Balance Mode start");
  63. // Init M5Bala
  64. m5bala.begin();
  65. m5bala.setAngleOffset(-2.2);
  66. // Loading the IMU parameters
  67. if (M5.BtnC.isPressed()) {
  68. preferences.begin("m5bala-cfg", false);
  69. auto_tune_gyro_offset();
  70. } else {
  71. preferences.begin("m5bala-cfg", true);
  72. imu_setOffsetX(preferences.getFloat("gyroXoffset"));
  73. }
  74. }
  75. void loop() {
  76. // LCD display
  77. static uint32_t print_interval = millis() + 30;
  78. if (millis() > print_interval) {
  79. print_interval = millis() + 100;
  80. M5.Lcd.setCursor(0, 190);
  81. M5.Lcd.printf("Input Encoer0: %+4d Encoer1: %+4d \r\n",
  82. m5bala.getSpeed0(), m5bala.getSpeed1());
  83. M5.Lcd.printf("Output PWM0: %+4d PWM1: %+4d \r\n",
  84. m5bala.getOut0(), m5bala.getOut1());
  85. M5.Lcd.printf("AngleX: %+05.2f\r\n", m5bala.getAngle());
  86. }
  87. // Draw the waveform
  88. static uint32_t draw_interval = millis() + 5;
  89. if (millis() > draw_interval) {
  90. draw_interval = millis() + 20;
  91. draw_waveform();
  92. }
  93. // M5Bala balance run
  94. m5bala.run();
  95. // M5 Loop
  96. M5.update();
  97. }