Arduino爱好者

 找回密码
 立即注册

QQ登录

只需一步,快速开始

楼主: eagler8

[经验] 【Arduino】168种传感器模块系列实验(112)---GY-521三轴模块

[复制链接]
 楼主| 发表于 2022-3-7 15:53 | 显示全部楼层
flyccie 发表于 2022-3-7 10:16
楼主,Thank you,给你点赞!

不客气,有空多交流
发表于 2022-3-8 15:51 | 显示全部楼层
太棒了,找到了这个宝藏的教程,我的毕业设计就是基于arduino测量电子陀螺仪数据的,请楼主赐给我一份库文件,我的邮箱是1642815922@qq.com,不胜感激!!!
 楼主| 发表于 2022-3-8 19:28 | 显示全部楼层
hqs9527 发表于 2022-3-8 15:51
太棒了,找到了这个宝藏的教程,我的毕业设计就是基于arduino测量电子陀螺仪数据的,请楼主赐给我一份库文 ...

已经发送,请查收
发表于 2022-3-13 10:57 | 显示全部楼层
eagler8 发表于 2020-7-7 19:24
已发送,请查收

楼主能给我一下这个的所有资料的嘛  
1104989082@qq.com
感谢
发表于 2022-3-13 10:58 | 显示全部楼层
楼主能给我一下这个的所有资料的嘛  
1104989082@qq.com
感谢
 楼主| 发表于 2022-3-13 12:26 | 显示全部楼层
liubosen 发表于 2022-3-13 10:58
楼主能给我一下这个的所有资料的嘛  

感谢

已经发送,请查收
发表于 2022-4-3 19:52 | 显示全部楼层
楼主,这一块编译错误是怎么回事呀
Arduino:1.8.19 (Windows 10), 开发板:"Arduino Uno"

YPR:45:30: error: missing terminating ' character

uint8_t teapotPacket[14] = { '

                              ^
YPR:46:1: error: expected primary-expression before ',' token

, 0x02, 0,0, 0,0, 0,0, 0,0, 0x00, 0x00, '\r', '\n' };

^

exit status 1

missing terminating ' character



在文件 -> 首选项开启
“编译过程中显示详细输出”选项
这份报告会包含更多信息。


QQ图片20220403194823.png
 楼主| 发表于 2022-4-3 20:28 | 显示全部楼层
hqs9527 发表于 2022-4-3 19:52
楼主,这一块编译错误是怎么回事呀
Arduino:1.8.19 (Windows 10), 开发板:"Arduino Uno"

可以通过编译

z.jpg
 楼主| 发表于 2022-4-3 20:29 | 显示全部楼层
代码

  1. /*
  2.   【Arduino】108种传感器模块系列实验(资料+代码+图形+仿真)
  3.   实验一百一十二: MPU-6050模块 三轴加速度 电子陀螺仪6DOF GY-521传感器
  4.   项目:测试MPU6050示例
  5.   Arduino------MPU 6050
  6.   5V-------------VCC
  7.   GND-----------GND
  8.   A4-----------SDA IIC 数据线
  9.   A5-----------SCL  IIC 时钟线
  10.   D2-----------INT 中断脚
  11. */

  12. // I2Cdev and MPU6050 must be installed as libraries, or else the .cpp/.h files
  13. // for both classes must be in the include path of your project
  14. #include "I2Cdev.h"
  15. #include "MPU6050.h"

  16. // Arduino Wire library is required if I2Cdev I2CDEV_ARDUINO_WIRE implementation
  17. // is used in I2Cdev.h
  18. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  19. #include "Wire.h"
  20. #endif

  21. // class default I2C address is 0x68
  22. // specific I2C addresses may be passed as a parameter here
  23. // AD0 low = 0x68 (default for InvenSense evaluation board)
  24. // AD0 high = 0x69
  25. MPU6050 accelgyro;
  26. //MPU6050 accelgyro(0x69); // <-- use for AD0 high


  27. const char LBRACKET = '[';
  28. const char RBRACKET = ']';
  29. const char COMMA    = ',';
  30. const char BLANK    = ' ';
  31. const char PERIOD   = '.';

  32. const int iAx = 0;
  33. const int iAy = 1;
  34. const int iAz = 2;
  35. const int iGx = 3;
  36. const int iGy = 4;
  37. const int iGz = 5;

  38. const int usDelay = 3150;   // empirical, to hold sampling to 200 Hz
  39. const int NFast =  1000;    // the bigger, the better (but slower)
  40. const int NSlow = 10000;    // ..
  41. const int LinesBetweenHeaders = 5;
  42. int LowValue[6];
  43. int HighValue[6];
  44. int Smoothed[6];
  45. int LowOffset[6];
  46. int HighOffset[6];
  47. int Target[6];
  48. int LinesOut;
  49. int N;

  50. void ForceHeader()
  51. {
  52.   LinesOut = 99;
  53. }

  54. void GetSmoothed()
  55. { int16_t RawValue[6];
  56.   int i;
  57.   long Sums[6];
  58.   for (i = iAx; i <= iGz; i++)
  59.   {
  60.     Sums[i] = 0;
  61.   }
  62.   //    unsigned long Start = micros();

  63.   for (i = 1; i <= N; i++)
  64.   { // get sums
  65.     accelgyro.getMotion6(&RawValue[iAx], &RawValue[iAy], &RawValue[iAz],
  66.                          &RawValue[iGx], &RawValue[iGy], &RawValue[iGz]);
  67.     if ((i % 500) == 0)
  68.       Serial.print(PERIOD);
  69.     delayMicroseconds(usDelay);
  70.     for (int j = iAx; j <= iGz; j++)
  71.       Sums[j] = Sums[j] + RawValue[j];
  72.   } // get sums
  73.   //    unsigned long usForN = micros() - Start;
  74.   //    Serial.print(" reading at ");
  75.   //    Serial.print(1000000/((usForN+N/2)/N));
  76.   //    Serial.println(" Hz");
  77.   for (i = iAx; i <= iGz; i++)
  78.   {
  79.     Smoothed[i] = (Sums[i] + N / 2) / N ;
  80.   }
  81. } // GetSmoothed

  82. void Initialize()
  83. {
  84.   // join I2C bus (I2Cdev library doesn't do this automatically)
  85. #if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
  86.   Wire.begin();
  87. #elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
  88.   Fastwire::setup(400, true);
  89. #endif

  90.   Serial.begin(9600);

  91.   // initialize device
  92.   Serial.println("Initializing I2C devices...");
  93.   accelgyro.initialize();

  94.   // verify connection
  95.   Serial.println("Testing device connections...");
  96.   Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  97.   Serial.println("PID tuning Each Dot = 100 readings");
  98.   /*A tidbit on how PID (PI actually) tuning works.
  99.     When we change the offset in the MPU6050 we can get instant results. This allows us to use Proportional and
  100.     integral of the PID to discover the ideal offsets. Integral is the key to discovering these offsets, Integral
  101.     uses the error from set-point (set-point is zero), it takes a fraction of this error (error * ki) and adds it
  102.     to the integral value. Each reading narrows the error down to the desired offset. The greater the error from
  103.     set-point, the more we adjust the integral value. The proportional does its part by hiding the noise from the
  104.     integral math. The Derivative is not used because of the noise and because the sensor is stationary. With the
  105.     noise removed the integral value lands on a solid offset after just 600 readings. At the end of each set of 100
  106.     readings, the integral value is used for the actual offsets and the last proportional reading is ignored due to
  107.     the fact it reacts to any noise.
  108.   */
  109.   accelgyro.CalibrateAccel(6);
  110.   accelgyro.CalibrateGyro(6);
  111.   Serial.println("\nat 600 Readings");
  112.   accelgyro.PrintActiveOffsets();
  113.   Serial.println();
  114.   accelgyro.CalibrateAccel(1);
  115.   accelgyro.CalibrateGyro(1);
  116.   Serial.println("700 Total Readings");
  117.   accelgyro.PrintActiveOffsets();
  118.   Serial.println();
  119.   accelgyro.CalibrateAccel(1);
  120.   accelgyro.CalibrateGyro(1);
  121.   Serial.println("800 Total Readings");
  122.   accelgyro.PrintActiveOffsets();
  123.   Serial.println();
  124.   accelgyro.CalibrateAccel(1);
  125.   accelgyro.CalibrateGyro(1);
  126.   Serial.println("900 Total Readings");
  127.   accelgyro.PrintActiveOffsets();
  128.   Serial.println();
  129.   accelgyro.CalibrateAccel(1);
  130.   accelgyro.CalibrateGyro(1);
  131.   Serial.println("1000 Total Readings");
  132.   accelgyro.PrintActiveOffsets();
  133.   Serial.println("\n\n Any of the above offsets will work nice \n\n Lets proof the PID tuning using another method:");
  134. } // Initialize

  135. void SetOffsets(int TheOffsets[6])
  136. { accelgyro.setXAccelOffset(TheOffsets [iAx]);
  137.   accelgyro.setYAccelOffset(TheOffsets [iAy]);
  138.   accelgyro.setZAccelOffset(TheOffsets [iAz]);
  139.   accelgyro.setXGyroOffset (TheOffsets [iGx]);
  140.   accelgyro.setYGyroOffset (TheOffsets [iGy]);
  141.   accelgyro.setZGyroOffset (TheOffsets [iGz]);
  142. } // SetOffsets

  143. void ShowProgress()
  144. { if (LinesOut >= LinesBetweenHeaders)
  145.   { // show header
  146.     Serial.println("\tXAccel\t\t\tYAccel\t\t\t\tZAccel\t\t\tXGyro\t\t\tYGyro\t\t\tZGyro");
  147.     LinesOut = 0;
  148.   } // show header
  149.   Serial.print(BLANK);
  150.   for (int i = iAx; i <= iGz; i++)
  151.   { Serial.print(LBRACKET);
  152.     Serial.print(LowOffset[i]),
  153.                  Serial.print(COMMA);
  154.     Serial.print(HighOffset[i]);
  155.     Serial.print("] --> [");
  156.     Serial.print(LowValue[i]);
  157.     Serial.print(COMMA);
  158.     Serial.print(HighValue[i]);
  159.     if (i == iGz)
  160.     {
  161.       Serial.println(RBRACKET);
  162.     }
  163.     else
  164.     {
  165.       Serial.print("]\t");
  166.     }
  167.   }
  168.   LinesOut++;
  169. } // ShowProgress

  170. void PullBracketsIn()
  171. { boolean AllBracketsNarrow;
  172.   boolean StillWorking;
  173.   int NewOffset[6];

  174.   Serial.println("\nclosing in:");
  175.   AllBracketsNarrow = false;
  176.   ForceHeader();
  177.   StillWorking = true;
  178.   while (StillWorking)
  179.   { StillWorking = false;
  180.     if (AllBracketsNarrow && (N == NFast))
  181.     {
  182.       SetAveraging(NSlow);
  183.     }
  184.     else
  185.     {
  186.       AllBracketsNarrow = true;  // tentative
  187.     }
  188.     for (int i = iAx; i <= iGz; i++)
  189.     { if (HighOffset[i] <= (LowOffset[i] + 1))
  190.       {
  191.         NewOffset[i] = LowOffset[i];
  192.       }
  193.       else
  194.       { // binary search
  195.         StillWorking = true;
  196.         NewOffset[i] = (LowOffset[i] + HighOffset[i]) / 2;
  197.         if (HighOffset[i] > (LowOffset[i] + 10))
  198.         {
  199.           AllBracketsNarrow = false;
  200.         }
  201.       } // binary search
  202.     }
  203.     SetOffsets(NewOffset);
  204.     GetSmoothed();
  205.     for (int i = iAx; i <= iGz; i++)
  206.     { // closing in
  207.       if (Smoothed[i] > Target[i])
  208.       { // use lower half
  209.         HighOffset[i] = NewOffset[i];
  210.         HighValue[i] = Smoothed[i];
  211.       } // use lower half
  212.       else
  213.       { // use upper half
  214.         LowOffset[i] = NewOffset[i];
  215.         LowValue[i] = Smoothed[i];
  216.       } // use upper half
  217.     } // closing in
  218.     ShowProgress();
  219.   } // still working

  220. } // PullBracketsIn

  221. void PullBracketsOut()
  222. { boolean Done = false;
  223.   int NextLowOffset[6];
  224.   int NextHighOffset[6];

  225.   Serial.println("expanding:");
  226.   ForceHeader();

  227.   while (!Done)
  228.   { Done = true;
  229.     SetOffsets(LowOffset);
  230.     GetSmoothed();
  231.     for (int i = iAx; i <= iGz; i++)
  232.     { // got low values
  233.       LowValue[i] = Smoothed[i];
  234.       if (LowValue[i] >= Target[i])
  235.       { Done = false;
  236.         NextLowOffset[i] = LowOffset[i] - 1000;
  237.       }
  238.       else
  239.       {
  240.         NextLowOffset[i] = LowOffset[i];
  241.       }
  242.     } // got low values

  243.     SetOffsets(HighOffset);
  244.     GetSmoothed();
  245.     for (int i = iAx; i <= iGz; i++)
  246.     { // got high values
  247.       HighValue[i] = Smoothed[i];
  248.       if (HighValue[i] <= Target[i])
  249.       { Done = false;
  250.         NextHighOffset[i] = HighOffset[i] + 1000;
  251.       }
  252.       else
  253.       {
  254.         NextHighOffset[i] = HighOffset[i];
  255.       }
  256.     } // got high values
  257.     ShowProgress();
  258.     for (int i = iAx; i <= iGz; i++)
  259.     { LowOffset[i] = NextLowOffset[i];   // had to wait until ShowProgress done
  260.       HighOffset[i] = NextHighOffset[i]; // ..
  261.     }
  262.   } // keep going
  263. } // PullBracketsOut

  264. void SetAveraging(int NewN)
  265. { N = NewN;
  266.   Serial.print("averaging ");
  267.   Serial.print(N);
  268.   Serial.println(" readings each time");
  269. } // SetAveraging

  270. void setup()
  271. { Initialize();
  272.   for (int i = iAx; i <= iGz; i++)
  273.   { // set targets and initial guesses
  274.     Target[i] = 0; // must fix for ZAccel
  275.     HighOffset[i] = 0;
  276.     LowOffset[i] = 0;
  277.   } // set targets and initial guesses
  278.   Target[iAz] = 16384;
  279.   SetAveraging(NFast);

  280.   PullBracketsOut();
  281.   PullBracketsIn();

  282.   Serial.println("-------------- done --------------");
  283. } // setup

  284. void loop()
  285. {
  286. } // loop
复制代码


发表于 2022-4-3 20:40 | 显示全部楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino爱好者

GMT+8, 2022-12-10 01:02 , Processed in 0.071709 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表