Robofun 機器人論壇

 找回密碼
 申請會員
搜索
熱搜: 活動 交友 discuz
查看: 5514|回復: 6
打印 上一主題 下一主題

陀螺儀積分的問題

[複製鏈接]
跳轉到指定樓層
1#
發表於 2012-11-21 20:33:08 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最後由 Bhai 於 2012-11-21 20:34 編輯

我目前在用 Arduino 做四軸,
我用的是 L3G4200D 的陀螺儀,
但是我在做積分時,陀螺儀靜止時候的角度也會一直加上去,
請問...這該怎麼解決?


下面是我用 L3G4200D 求角度的程式。

  1. #include <Wire.h>

  2. #define CTRL_REG1 0x20
  3. #define CTRL_REG2 0x21
  4. #define CTRL_REG3 0x22
  5. #define CTRL_REG4 0x23
  6. #define CTRL_REG5 0x24
  7. int L3G4200D_Address = 105; //I2C address of the L3G4200D

  8. int x;
  9. int y;
  10. int z;
  11. float angleG;
  12. float gyroSpeed;
  13. unsigned long o_timer, timer;

  14. void setup(){
  15.   Wire.begin();
  16.   Serial.begin(9600);

  17.   Serial.println("starting up L3G4200D");
  18.   setupL3G4200D(250); // Configure L3G4200  - 250, 500 or 2000 deg/sec
  19.   angleG = 0;
  20.   gyroSpeed = 0;
  21.   o_timer = 0;
  22.   timer = 0;
  23.   delay(1500); //wait for the sensor to be ready
  24.   timer = millis();
  25. }

  26. void loop()  {
  27.   long o_timer = timer;
  28.   getGyroValues();  // This will update x, y, and z with new values
  29.   timer = millis();
  30.   int dt = timer - o_timer;
  31.   gyroSpeed = x * 8.75 / 1000;
  32.   angleG = angleG + gyroSpeed * dt / 1000;
  33.   Serial.print("angle_x: ");
  34.   Serial.println(angleG,6);
  35.   
  36.   //delay(100); //Just here to slow down the serial to make it more readable
  37. }

  38. void getGyroValues(){
  39.   //(deg/sec)
  40.   byte xMSB = readRegister(L3G4200D_Address, 0x29);
  41.   byte xLSB = readRegister(L3G4200D_Address, 0x28);
  42.   x = ((xMSB << 8) | xLSB);

  43.   byte yMSB = readRegister(L3G4200D_Address, 0x2B);
  44.   byte yLSB = readRegister(L3G4200D_Address, 0x2A);
  45.   y = ((yMSB << 8) | yLSB);

  46.   byte zMSB = readRegister(L3G4200D_Address, 0x2D);
  47.   byte zLSB = readRegister(L3G4200D_Address, 0x2C);
  48.   z = ((zMSB << 8) | zLSB);
  49. }

  50. int setupL3G4200D(int scale){
  51.   //From  Jim Lindblom of Sparkfun's code

  52.   // Enable x, y, z and turn off power down:
  53.   writeRegister(L3G4200D_Address, CTRL_REG1, 0b00001111);

  54.   // If you'd like to adjust/use the HPF, you can edit the line below to configure CTRL_REG2:
  55.   writeRegister(L3G4200D_Address, CTRL_REG2, 0b00000000);

  56.   // Configure CTRL_REG3 to generate data ready interrupt on INT2
  57.   // No interrupts used on INT1, if you'd like to configure INT1
  58.   // or INT2 otherwise, consult the datasheet:
  59.   writeRegister(L3G4200D_Address, CTRL_REG3, 0b00001000);

  60.   // CTRL_REG4 controls the full-scale range, among other things:

  61.   if(scale == 250){
  62.     writeRegister(L3G4200D_Address, CTRL_REG4, 0b00000000);
  63.   }else if(scale == 500){
  64.     writeRegister(L3G4200D_Address, CTRL_REG4, 0b00010000);
  65.   }else{
  66.     writeRegister(L3G4200D_Address, CTRL_REG4, 0b00110000);
  67.   }

  68.   // CTRL_REG5 controls high-pass filtering of outputs, use it
  69.   // if you'd like:
  70.   writeRegister(L3G4200D_Address, CTRL_REG5, 0b00000000);
  71. }

  72. void writeRegister(int deviceAddress, byte address, byte val) {
  73.     Wire.beginTransmission(deviceAddress); // start transmission to device
  74.     Wire.write(address);       // send register address
  75.     Wire.write(val);         // send value to write
  76.     Wire.endTransmission();     // end transmission
  77. }

  78. int readRegister(int deviceAddress, byte address){

  79.     int v;
  80.     Wire.beginTransmission(deviceAddress);
  81.     Wire.write(address); // register to read
  82.     Wire.endTransmission();

  83.     Wire.requestFrom(deviceAddress, 1); // read a byte

  84.     v = Wire.read();
  85.     return v;
  86. }
複製代碼
2#
發表於 2012-11-21 22:15:38 | 只看該作者
姿態角的計算式是錯的~~歐耶~~
3#
 樓主| 發表於 2012-11-21 23:46:28 | 只看該作者
姿態角的計算式是錯的~~歐耶~~
g921002 發表於 2012-11-21 22:15



請問能夠詳細解釋嗎?
4#
 樓主| 發表於 2012-11-26 11:03:59 | 只看該作者
L3G4200D 的 X 乘上靈敏度,出來的結果就是 X 軸的角速度
得到角速度後再對時間積分,就能得到角度。

這是我在網路上查到的解釋,
請問是程式寫錯,還是理論錯了?
5#
發表於 2012-11-26 19:58:28 | 只看該作者
我們的物理空間是三維的吧?
6#
 樓主| 發表於 2012-11-27 14:27:40 | 只看該作者
是。

抱歉,我不太能夠理解您的意思,
您所指的是我還少考慮什麼嗎?
7#
發表於 2014-9-25 12:40:29 | 只看該作者
謝謝樓主無私的分享~!!!!
您需要登錄後才可以回帖 登錄 | 申請會員

本版積分規則

小黑屋|手機版|Archiver|機器人論壇 from 2005.07

GMT+8, 2024-6-26 20:18 , Processed in 0.380976 second(s), 8 queries , Apc On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表