BALA.ino 2.8 KB

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