IMU.ino 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/core/gray
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
  8. *
  9. * Describe: MPU6886 example. 惯性传感器
  10. * Date: 2021/7/21
  11. *******************************************************************************
  12. */
  13. #define M5STACK_MPU6886
  14. #include <M5Stack.h>
  15. float accX = 0.0F; // Define variables for storing inertial sensor data
  16. float accY = 0.0F; //定义存储惯性传感器相关数据的相关变量
  17. float accZ = 0.0F;
  18. float gyroX = 0.0F;
  19. float gyroY = 0.0F;
  20. float gyroZ = 0.0F;
  21. float pitch = 0.0F;
  22. float roll = 0.0F;
  23. float yaw = 0.0F;
  24. float temp = 0.0F;
  25. /* After M5Core is started or reset
  26. the program in the setUp () function will be run, and this part will only be run
  27. once. 在 M5Core
  28. 启动或者复位后,即会开始执行setup()函数中的程序,该部分只会执行一次。 */
  29. void setup() {
  30. M5.begin(); // Init M5Core. 初始化 M5Core
  31. M5.Power.begin(); // Init Power module. 初始化电源
  32. M5.IMU.Init(); // Init IMU sensor. 初始化惯性传感器
  33. M5.Lcd.fillScreen(BLACK); // Set the screen background color to black.
  34. // 设置屏幕背景色为黑色
  35. M5.Lcd.setTextColor(
  36. GREEN, BLACK); // Sets the foreground color and background color of the
  37. // displayed text. 设置显示文本的前景颜色和背景颜色
  38. M5.Lcd.setTextSize(2); // Set the font size. 设置字体大小
  39. }
  40. /* After the program in setup() runs, it runs the program in loop()
  41. The loop() function is an infinite loop in which the program runs repeatedly
  42. 在setup()函数中的程序执行完后,会接着执行loop()函数中的程序
  43. loop()函数是一个死循环,其中的程序会不断的重复运行 */
  44. void loop() {
  45. // Stores the triaxial gyroscope data of the inertial sensor to the relevant
  46. // variable 将惯性传感器的三轴陀螺仪数据存储至相关变量
  47. M5.IMU.getGyroData(&gyroX, &gyroY, &gyroZ);
  48. M5.IMU.getAccelData(
  49. &accX, &accY,
  50. &accZ); // Stores the triaxial accelerometer. 存储三轴加速度计数据
  51. M5.IMU.getAhrsData(
  52. &pitch, &roll,
  53. &yaw); // Stores the inertial sensor attitude. 存储惯性传感器的姿态
  54. M5.IMU.getTempData(&temp); // Stores the inertial sensor temperature to
  55. // temp. 存储惯性传感器的温度
  56. /* The M5Core screen is 320x240 pixels, starting at the top left corner of
  57. the screen (0,0). gyroscope output related M5Stack屏幕像素为
  58. 320x240,以屏幕左上角为原点 (0,0)*/
  59. // gyroscope output related. 陀螺仪输出相关
  60. M5.Lcd.setCursor(
  61. 0, 20); // Move the cursor position to (x,y). 移动光标位置到(x,y)处
  62. M5.Lcd.printf("gyroX, gyroY, gyroZ"); // Screen printingformatted string.
  63. // 输出格式化字符串
  64. M5.Lcd.setCursor(0, 42);
  65. M5.Lcd.printf("%6.2f %6.2f%6.2f o/s", gyroX, gyroY, gyroZ);
  66. // Accelerometer output is related
  67. //加速度计输出相关
  68. M5.Lcd.setCursor(0, 70);
  69. M5.Lcd.printf("accX, accY, accZ");
  70. M5.Lcd.setCursor(0, 92);
  71. M5.Lcd.printf("%5.2f %5.2f %5.2f G", accX, accY, accZ);
  72. // Pose output is related
  73. //姿态输出相关
  74. M5.Lcd.setCursor(0, 120);
  75. M5.Lcd.printf("pitch, roll, yaw");
  76. M5.Lcd.setCursor(0, 142);
  77. M5.Lcd.printf("%5.2f %5.2f %5.2f deg", pitch, roll, yaw);
  78. // Inertial sensor temperature output related
  79. //惯性传感器温度输出相关
  80. M5.Lcd.setCursor(0, 175);
  81. M5.Lcd.printf("Temperature : %.2f C", temp);
  82. delay(10); // Delay 10ms //延迟10ms
  83. }