极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 4773|回复: 0

關於gyro_accel文字庫驅動

[复制链接]
发表于 2016-3-21 15:28:34 | 显示全部楼层 |阅读模式
如題 小弟最近再測試別人的程式碼 可是在找庫的時候發現了個問題


  1. #include <Wire.h>
  2. #include "C:\Program Files\Arduino\libraries\gyro_accel.h"
  3. #include "PID_v1.h"

  4. // Defining constants
  5. #define dt 1            // time difference in milli seconds
  6. #define rad2degree 57.3  // radian to degree conversion
  7. #define Filter_gain 0.95  // e.g. angle = angle_gyro*Filter_gain + angle_accel*(1-Filter_gain)

  8. // Global Variables
  9. unsigned long t = 0;      // Time Variables
  10. float angle_x_gyro=0, angle_y_gyro=0, angle_z_gyro=0, angle_x_accel=0, angle_y_accel=0, angle_z_accel=0, angle_x=0, angle_y=0, angle_z=0;
  11. char val ;
  12. int maichong,lefta,leftaa,leftone,righta,rightaa,rightone;
  13. // DC motor driver with L298N
  14. const int motor1PWMPin = 11; // PWM Pin of Motor 1
  15. const int motor1Polarity1 = 10; // Polarity 1 Pin of Motor 1
  16. const int motor1Polarity2 = 9;  // Polarity 2 Pin of Motor 1
  17. const int motor2PWMPin = 6;  // PWM Pin of Motor 2
  18. const int motor2Polarity1 = 8; // Polarity 1 Pin of Motor 2
  19. const int motor2Polarity2 = 7;  // Polarity 2 Pin of Motor 2
  20. const int left=12; //码盘
  21. const int right=5;//码盘
  22. int ValM1 = 255;  // Initial Value for PWM Motor 1
  23. int ValM2 = 255;  // Initial Value for PWM Motor 2

  24. double Setpoint, Input, Output;

  25. //PID myPID(&Input, &Output, &Setpoint, 1, 0, -10, DIRECT);
  26. PID myPID(&Input, &Output, &Setpoint, 25, 4, -5.5, DIRECT);

  27. void setup(){
  28.   // MPU-6050
  29.   Serial.begin(115200);
  30.   Wire.begin();
  31.   MPU6050_ResetWake();
  32.   MPU6050_SetGains(0,1);  // Setting the lows scale
  33.   MPU6050_SetDLPF(0);    // Setting the DLPF to inf Bandwidth for calibration
  34.   MPU6050_OffsetCal();  // very important
  35.   MPU6050_SetDLPF(6);  // Setting the DLPF to lowest Bandwidth
  36.    
  37.   t = millis();
  38.    
  39.   // DC motor
  40.   pinMode(motor1PWMPin, OUTPUT);
  41.   pinMode(motor1Polarity1, OUTPUT);
  42.   pinMode(motor1Polarity2, OUTPUT);
  43.    
  44.   pinMode(motor2PWMPin, OUTPUT);
  45.   pinMode(motor2Polarity1, OUTPUT);
  46.   pinMode(motor2Polarity2, OUTPUT);
  47.   //0 1 zhongduan
  48.   attachInterrupt(left, jishu1, CHANGE);
  49.   attachInterrupt(right, jishu, CHANGE);
  50.   
  51.    
  52.   // set enablePin of motor 1 high so that motor 1 can turn on
  53.   digitalWrite(motor1PWMPin, HIGH);
  54.   digitalWrite(motor1Polarity1, HIGH);
  55.   digitalWrite(motor1Polarity2, LOW);

  56.   // set enablePin of motor 2 high so that motor 2 can turn on  
  57.   digitalWrite(motor2PWMPin, HIGH);
  58.   digitalWrite(motor2Polarity1, HIGH);
  59.   digitalWrite(motor2Polarity2, LOW);
  60.    
  61.   Input = 0.0;
  62.   Setpoint = 10.0;
  63.   
  64.   //myPID.SetSampleTime(100);
  65.   myPID.SetMode(AUTOMATIC);
  66.   
  67. }
  68. void jishu()
  69. {  
  70.     lefta++;
  71.     if(lefta==45)
  72.     {  
  73.       lefta=0;  //mai chong biao zhi qing ling
  74.       leftone++;//一圈
  75.     }  
  76. }
  77. void jishu1()
  78. {  
  79.     righta++;
  80.     if(righta==45)
  81.     {  
  82.       righta=0;  //mai chong biao zhi qing ling
  83.       rightone++;//一圈
  84.     }  
  85. }
  86. void rightz()//右轮正转
  87. {
  88.   digitalWrite(motor1PWMPin, HIGH);digitalWrite(motor1Polarity1, HIGH);digitalWrite(motor1Polarity2, LOW);
  89. }
  90. void rightf()//右轮反转
  91. {
  92.   digitalWrite(motor1PWMPin, HIGH);digitalWrite(motor1Polarity1, LOW);digitalWrite(motor1Polarity2, HIGH);
  93. }
  94. void leftz()
  95. {
  96.   digitalWrite(motor2PWMPin, HIGH);digitalWrite(motor2Polarity1, HIGH);digitalWrite(motor2Polarity2, LOW);
  97. }
  98. void leftf()
  99. {
  100.   digitalWrite(motor2PWMPin, HIGH);digitalWrite(motor2Polarity1, LOW);digitalWrite(motor2Polarity2, HIGH);
  101. }
  102. void st()//电机停
  103. {
  104.   digitalWrite(motor1PWMPin, HIGH);digitalWrite(motor1Polarity1, LOW);digitalWrite(motor1Polarity2, LOW);
  105.   digitalWrite(motor2PWMPin, HIGH);digitalWrite(motor2Polarity1, LOW);digitalWrite(motor2Polarity2, LOW);
  106. }
  107. void blue()//蓝牙
  108. {
  109.   if (Serial.available() > 0) {
  110.       val = Serial.read();
  111.      if(val == 'A') {
  112.         Serial.println(angle_x); //jiaodu
  113.       }
  114.       if(val == 'z') {
  115.        rightz();
  116.       }
  117.       if(val == 'x') {
  118.       rightf();
  119.       }
  120.       if(val == 'c') {
  121.        leftz();
  122.       }
  123.       if(val == 'v') {
  124.        leftf();
  125.       }
  126.       if(val == 'b') {
  127.        st();
  128.       }
  129. }
  130. }
  131. void loop(){
  132.   t = millis();
  133.   
  134.   MPU6050_ReadData();
  135.    
  136.   angle_x_gyro = (gyro_x_scalled*((float)dt/1000)+angle_x);
  137.   angle_y_gyro = (gyro_y_scalled*((float)dt/1000)+angle_y);
  138.   angle_z_gyro = (gyro_z_scalled*((float)dt/1000)+angle_z);

  139.   angle_z_accel = atan(accel_z_scalled/(sqrt(accel_y_scalled*accel_y_scalled+accel_x_scalled*accel_x_scalled)))*(float)rad2degree;
  140.   angle_y_accel = -atan(accel_x_scalled/(sqrt(accel_y_scalled*accel_y_scalled+accel_z_scalled*accel_z_scalled)))*(float)rad2degree;
  141.   angle_x_accel = atan(accel_y_scalled/(sqrt(accel_x_scalled*accel_x_scalled+accel_z_scalled*accel_z_scalled)))*(float)rad2degree;

  142.   angle_x = Filter_gain*angle_x_gyro+(1-Filter_gain)*angle_x_accel;
  143.   angle_y = Filter_gain*angle_y_gyro+(1-Filter_gain)*angle_y_accel;
  144.   angle_z = Filter_gain*angle_z_gyro+(1-Filter_gain)*angle_z_accel;
  145.    
  146.   digitalWrite(motor1PWMPin, HIGH);
  147.   digitalWrite(motor2PWMPin, HIGH);

  148. Serial.print("\n");Serial.print("    ");Serial.print("angle_x=");  Serial.print(angle_x);Serial.print("  ");Serial.print("angle_y=");Serial.print(angle_y);Serial.print("  ");Serial.print("angle_z=");Serial.print(angle_z);
  149. Serial.print("____");           //串口输出
  150.    
  151.   // change direction is very important   平衡
  152.   if(angle_x>0)
  153.   {
  154.     myPID.SetControllerDirection(REVERSE);
  155.      
  156.     // set enablePin of motor 1 high so that motor 1 can turn on
  157.     digitalWrite(motor1Polarity1, HIGH);
  158.     digitalWrite(motor1Polarity2, LOW);

  159.     // set enablePin of motor 2 high so that motor 2 can turn on  
  160.     digitalWrite(motor2Polarity1, HIGH);
  161.     digitalWrite(motor2Polarity2, LOW);
  162.   }
  163.   else
  164.   {
  165.    myPID.SetControllerDirection(DIRECT);
  166.     // set enablePin of motor 1 high so that motor 1 can turn on
  167.     digitalWrite(motor1Polarity1, LOW);
  168.     digitalWrite(motor1Polarity2, HIGH);

  169.     // set enablePin of motor 2 high so that motor 2 can turn on  
  170.     digitalWrite(motor2Polarity1, LOW);
  171.     digitalWrite(motor2Polarity2, HIGH);
  172.   }
  173.    
  174.   Input = angle_x;
  175.   Serial.print("Input=");Serial.print(Input);Serial.print("____");
  176.    
  177.   myPID.Compute();
  178.   Serial.print("Output=");Serial.print(Output);Serial.print("____");                        //Output 怎么会是0????
  179.    
  180.   analogWrite(motor1PWMPin, Output);
  181.   analogWrite(motor2PWMPin, Output);
  182.   
  183.   Serial.print("Setpoint=");Serial.print(Setpoint); Serial.print("\t");
  184.   Serial.print("left=");Serial.print(lefta);Serial.print("  ");Serial.print(leftone); Serial.print("\t");
  185.   Serial.print("right=");Serial.print(righta);Serial.print("  ");Serial.print(rightone); Serial.print("\t");   //串口输出
  186.    blue();//蓝牙
  187.    
  188.    if(rightone==1) //转动1圈标志
  189.        {
  190.          rightone=0;   
  191.          st();   // 停止转动
  192.        }
  193.       
  194.    
  195. //while((millis()-t) < dt)
  196.   {
  197.     // Do nothing
  198.   }
  199. }
复制代码


這是那位平衡小車大能做出來的程式碼 小弟最近裝了碼盤想測來看看是否能作用...

PID_v1.h 我已經有了....
至於gyro_accel我去載了GitHub的gyro_accel的代碼
由於沒辦法呼叫 所以就強行呼叫發生了個很麻煩的問題....

好像少了定義


  1. C:\DOCUME~1\pig\LOCALS~1\Temp\build6679638715162739097.tmp\blance_car4.cpp.o: In function `setup':
  2. C:\Program Files\Arduino/blance_car4.ino:36: undefined reference to `MPU6050_ResetWake()'
  3. C:\Program Files\Arduino/blance_car4.ino:37: undefined reference to `MPU6050_SetGains(int, int)'
  4. C:\Program Files\Arduino/blance_car4.ino:38: undefined reference to `MPU6050_SetDLPF(int)'
  5. C:\Program Files\Arduino/blance_car4.ino:39: undefined reference to `MPU6050_OffsetCal()'
  6. C:\Program Files\Arduino/blance_car4.ino:40: undefined reference to `MPU6050_SetDLPF(int)'
  7. C:\DOCUME~1\pig\LOCALS~1\Temp\build6679638715162739097.tmp\blance_car4.cpp.o: In function `loop':
  8. C:\Program Files\Arduino/blance_car4.ino:140: undefined reference to `MPU6050_ReadData()'
  9. C:\Program Files\Arduino/blance_car4.ino:142: undefined reference to `gyro_x_scalled'
  10. C:\Program Files\Arduino/blance_car4.ino:142: undefined reference to `gyro_x_scalled'
  11. C:\Program Files\Arduino/blance_car4.ino:142: undefined reference to `gyro_x_scalled'
  12. C:\Program Files\Arduino/blance_car4.ino:142: undefined reference to `gyro_x_scalled'
  13. C:\Program Files\Arduino/blance_car4.ino:143: undefined reference to `gyro_y_scalled'
  14. C:\Program Files\Arduino/blance_car4.ino:143: undefined reference to `gyro_y_scalled'
  15. C:\Program Files\Arduino/blance_car4.ino:143: undefined reference to `gyro_y_scalled'
  16. C:\Program Files\Arduino/blance_car4.ino:143: undefined reference to `gyro_y_scalled'
  17. C:\Program Files\Arduino/blance_car4.ino:144: undefined reference to `gyro_z_scalled'
  18. C:\Program Files\Arduino/blance_car4.ino:144: undefined reference to `gyro_z_scalled'
  19. C:\Program Files\Arduino/blance_car4.ino:144: undefined reference to `gyro_z_scalled'
  20. C:\Program Files\Arduino/blance_car4.ino:144: undefined reference to `gyro_z_scalled'
  21. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_z_scalled'
  22. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_z_scalled'
  23. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_z_scalled'
  24. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_z_scalled'
  25. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_y_scalled'
  26. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_y_scalled'
  27. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_y_scalled'
  28. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_y_scalled'
  29. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_x_scalled'
  30. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_x_scalled'
  31. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_x_scalled'
  32. C:\Program Files\Arduino/blance_car4.ino:146: undefined reference to `accel_x_scalled'
  33. collect2.exe: error: ld returned 1 exit status
复制代码


這是錯誤的訊息

我特別打開了gyro_accel.h和gyro_accel.cpp的程式碼

gyro_accel.h:


  1. #ifndef gyro_accel.h
  2.   #define gyro_accel.h
  3.   
  4.   extern int accel_x_OC, accel_y_OC, accel_z_OC, gyro_x_OC ,gyro_y_OC, gyro_z_OC; // offset variables
  5.   extern float temp_scalled,accel_x_scalled,accel_y_scalled,accel_z_scalled,gyro_x_scalled,gyro_y_scalled,gyro_z_scalled; //Scalled Data varaibles

  6.   
  7.   void MPU6050_ReadData();
  8.   void MPU6050_ResetWake();
  9.   void MPU6050_SetDLPF(int BW);
  10.   void MPU6050_SetGains(int gyro,int accel);
  11.   void MPU6050_OffsetCal();

  12. #endif

  13. /* Author = helscream (Omer Ikram ul Haq)
  14. Last edit date = 2014-06-22
  15. Website: [url]http://hobbylogs.me.pn/?p=47[/url]
  16. Location: Pakistan
  17. Ver: 0.1 beta --- Start
  18. */
复制代码


gyro_accel.cpp:


  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include "gyro_accel.h"


  4. // Defining the important registers

  5. #define MPU6050_address 0x68

  6. // ------ SELF Test Trim Factors ----------------------
  7. #define MPU6050_self_test_x 13       // R/W
  8. #define MPU6050_self_test_y 14       // R/W
  9. #define MPU6050_self_test_z 15       // R/W
  10. #define MPU6050_self_test_A 16       // R/W

  11. // ----- Sample Divider -------------------------------
  12. #define MPU6050_sample_div 25        // R/W
  13. /*       Sample Divider Discription
  14.   Sample rate = 8/(1 + Sample rate divider) [kHz]  if DLPF is disabled
  15.   Sample rate = 1/(1 + Sample rate divider) [kHz]  if DLPF is enabled
  16. */

  17. // ----- Configration ---------------------------------
  18. #define MPU6050_config 26            // R/W
  19. #define MPU6050_gyro_config 27       // R/W
  20. #define MPU6050_accel_config 28      // R/W

  21. // ----- Data ---------------------------------------------
  22. #define MPU6050_data_start 59



  23. // ----- Power Management ---------------------------------
  24. #define MPU6050_PWR1 107
  25. #define MPU6050_PWR2 108

  26. // ----- Defining Constant --------------------------------
  27. #define g 9.81                       // Gravitational accelration




  28. // Variables
  29. int temp=0, accel_x=0, accel_y=0, accel_z=0, gyro_x=0, gyro_y=0, gyro_z=0; // Raw values varaibles
  30. int accel_x_OC=0, accel_y_OC=0, accel_z_OC=0, gyro_x_OC=0 ,gyro_y_OC=0, gyro_z_OC=0; // offset variables
  31. float temp_scalled,accel_x_scalled,accel_y_scalled,accel_z_scalled,gyro_x_scalled,gyro_y_scalled,gyro_z_scalled; //Scalled Data varaibles
  32. float accel_scale_fact = 1, gyro_scale_fact = 1; // Scale factor variables


  33. // **************************************************************************
  34. //          Functions for MPU6050
  35. // **************************************************************************


  36. void MPU6050_ReadData(){
  37.   
  38.   
  39.   Wire.beginTransmission(MPU6050_address);
  40.   Wire.write(MPU6050_data_start);
  41.   Wire.endTransmission();

  42.   int read_bytes = 14;

  43.   Wire.requestFrom(MPU6050_address,read_bytes);

  44.   if(Wire.available() == read_bytes){
  45.   
  46.     accel_x = Wire.read()<<8 | Wire.read();
  47.     accel_y = Wire.read()<<8 | Wire.read();
  48.     accel_z = Wire.read()<<8 | Wire.read();
  49.   
  50.     temp = Wire.read()<<8 | Wire.read();
  51.   
  52.     gyro_x = Wire.read()<<8 | Wire.read();
  53.     gyro_y = Wire.read()<<8 | Wire.read();
  54.     gyro_z = Wire.read()<<8 | Wire.read();
  55.   
  56.   }
  57.   
  58.   
  59.   
  60.   accel_x_scalled = (float)(accel_x-accel_x_OC)*accel_scale_fact/1000; // divided by 1000 as the Scale factor is in milli units
  61.   accel_y_scalled = (float)(accel_y-accel_y_OC)*accel_scale_fact/1000;
  62.   accel_z_scalled = (float)(accel_z-accel_z_OC)*accel_scale_fact/1000;
  63.   
  64.   gyro_x_scalled = (float)(gyro_x-gyro_x_OC)*gyro_scale_fact/1000;
  65.   gyro_y_scalled = (float)(gyro_y-gyro_y_OC)*gyro_scale_fact/1000;
  66.   gyro_z_scalled = ((float)(gyro_z-gyro_z_OC)*gyro_scale_fact/1000);
  67.   
  68.   temp_scalled = (float)temp/340+36.53;
  69. }



  70. // ------------------------------------------------
  71. //       Reset and wake up the MPU6050
  72. // ------------------------------------------------
  73. void MPU6050_ResetWake(){
  74.   
  75.   Serial.println("Resetting MPU6050 and waking it up.....");
  76.   Wire.beginTransmission(MPU6050_address);
  77.   Wire.write(MPU6050_PWR1);
  78.   Wire.write(0b10000000);
  79.   Wire.endTransmission();
  80.   
  81.   delay(100); // Waiting for the reset to complete
  82.    
  83.   Wire.beginTransmission(MPU6050_address);
  84.   Wire.write(MPU6050_PWR1);

  85.   Wire.write(0b00000000);
  86.   Wire.endTransmission();
  87.   
  88. }



  89. // ------------------------------------------------
  90. //       Setting up the DLFP
  91. // ------------------------------------------------

  92. void MPU6050_SetDLPF(int BW)
  93.   {
  94.     if (BW < 0 || BW > 6){
  95.       BW = 0;
  96.     }
  97.   Wire.beginTransmission(MPU6050_address);
  98.   Wire.write(MPU6050_config); // Address to the configuration register
  99. /*       config Discription ---- x x 0 0 0 F2 F1 F0
  100.   I am only intrested in the Digital Low Pass Filter (DLPF)
  101.   F2 F1 F0    Bandwidth [Hz]
  102.   0  0  0        
  103.   0  0  1      184
  104.   0  1  0      94
  105.   0  1  1      44
  106.   1  0  0      21  
  107.   1  0  1      10  
  108.   1  1  0      5
  109. */

  110.   Wire.write(BW);
  111.   Wire.endTransmission();
  112.   }
  113.   
  114. // ------------------------------------------------
  115. //       Setting up the Accelrometer and Gyro Gains
  116. // ------------------------------------------------  

  117. void MPU6050_SetGains(int gyro,int accel)
  118.   {
  119.     byte gyro_byte,accel_byte;
  120.    
  121.     // Setting up Gyro
  122.     Wire.beginTransmission(MPU6050_address);
  123.     Wire.write(MPU6050_gyro_config); // Address to the configuration register
  124.     if (gyro==0)
  125.     {
  126.       gyro_scale_fact =(float)250*0.0305; // each data is of 16 bits that means, 250 is divided along 2^(15)-1 = 32767 so for milli degree/s 0.0305 = 1000/32767
  127.       gyro_byte = 0b00000000;
  128.     }else if (gyro == 1)
  129.     {
  130.       gyro_scale_fact = 500*0.0305; // each data is of 16 bits that means, 500 is divided along 2^(15)-1 = 32767 so for milli degree/s 0.0305 = 1000/32767
  131.       gyro_byte = 0b00001000;
  132.     }else if (gyro == 2)
  133.     {
  134.       gyro_scale_fact = 1000*0.0305;// each data is of 16 bits that means, 1000 is divided along 2^(15)-1 = 32767 so for milli degree/s 0.0305 = 1000/32767
  135.       gyro_byte = 0b00010000;
  136.     }else if (gyro == 3)
  137.     {
  138.       gyro_scale_fact = 2000*0.0305;  // each data is of 16 bits that means, 2000 is divided along 2^(15)-1 = 32767 so for milli degree/s 0.0305 = 1000/32767
  139.       gyro_byte = 0b00011000;
  140.     }else
  141.     {
  142.       gyro_scale_fact = 1;
  143.     }  
  144.       
  145.     Wire.write(gyro_byte);
  146.     Wire.endTransmission();
  147.     Serial.print("The gyro scale is set to ");
  148.     Serial.print(gyro_scale_fact);
  149.     Serial.println(" milli Degree/s");
  150.    
  151.    
  152.     // Setting up Accel
  153.     Wire.beginTransmission(MPU6050_address);
  154.     Wire.write(MPU6050_accel_config); // Address to the configuration register
  155.     if (accel==0)
  156.     {
  157.       accel_scale_fact =(float)2*g*0.0305; // each data is of 16 bits that means, 2g is divided along 2^(15)-1 = 32767 so for milli m/s^2 0.0305 = 1000/32767      
  158.       accel_byte = 0b00000000;
  159.     }else if (accel == 1)
  160.     {
  161.       accel_scale_fact = 4*g*0.0305; // each data is of 16 bits that means, 4g is divided along 2^(15)-1 = 32767 so for milli m/s^2 0.0305 = 1000/32767
  162.       accel_byte = 0b00001000;
  163.     }else if (accel == 2)
  164.     {
  165.       accel_scale_fact = 8*g*0.0305;// each data is of 16 bits that means, 8g is divided along 2^(15)-1 = 32767 so for milli m/s^2 0.0305 = 1000/32767
  166.       accel_byte = 0b00010000;
  167.     }else if (accel == 3)
  168.     {
  169.       accel_scale_fact = 16*g*0.0305; // each data is of 16 bits that means, 16g is divided along 2^(15)-1 = 32767 so for milli m/s^2 0.0305 = 1000/32767
  170.       accel_byte = 0b00011000;
  171.     }else
  172.     {
  173.       accel_scale_fact = 1;
  174.     }  
  175.       
  176.     Wire.write(accel_byte);
  177.     Wire.endTransmission();
  178.     Serial.print("The accel scale is set to ");
  179.     Serial.print(accel_scale_fact);
  180.     Serial.println(" milli m/s^2");
  181.    
  182.   }
  183.   
  184.   
  185. // ------------------------------------------------
  186. //       offset calibration
  187. // ------------------------------------------------  

  188. void MPU6050_OffsetCal(){
  189.   Serial.println("Calibrating gyroscope .... dont move the hardware ..........");
  190.   
  191.   int x=0,y=0,z=0,i;
  192.   
  193.   MPU6050_ReadData();
  194.   MPU6050_ReadData();
  195.   
  196.   // Gyro Offset Calculation
  197.   x=gyro_x;
  198.   y=gyro_y;
  199.   z=gyro_z;
  200.    
  201.   for (i=1;i<=1000;i++){
  202.     MPU6050_ReadData();
  203.     x=(x+gyro_x)/2;
  204.     y=(y+gyro_y)/2;
  205.     z=(z+gyro_z)/2;
  206.     Serial.print(".");
  207.   }
  208.   Serial.println(".");
  209.   gyro_x_OC=x;
  210.   gyro_y_OC=y;
  211.   gyro_z_OC=z;
  212.   
  213.   Serial.print("gyro_x register offset = ");
  214.   Serial.println(x);

  215.   
  216.   Serial.print("gyro_y register offect = ");
  217.   Serial.println(y);

  218.   
  219.   Serial.print("gyro_z register offset = ");
  220.   Serial.println(z);

  221.   
  222.   
  223.   // Accel Offset Calculation
  224.   Serial.println("Calibrating accelrometer .... dont move the hardware ..........");
  225.   x=accel_x;
  226.   y=accel_y;
  227.   z=accel_z;
  228.    
  229.   for (i=1;i<=1000;i++){
  230.     MPU6050_ReadData();
  231.     x=(x+accel_x)/2;
  232.     y=(y+accel_y)/2;
  233.     z=(z+accel_z)/2;
  234.     Serial.print(".");
  235.   }
  236.   Serial.println(".");
  237.   accel_x_OC=x;
  238.   accel_y_OC=y;
  239.   accel_z_OC=z-(float)g*1000/accel_scale_fact;
  240.   
  241.   Serial.print("Accel_x register offset = ");
  242.   Serial.println(x);
  243.   
  244.   
  245.   Serial.print("Accel_y register offect = ");
  246.   Serial.println(y);
  247.   
  248.   
  249.   Serial.print("Accel_z register offset = ");
  250.   Serial.println(z);
  251.   
  252.   
  253.   

  254. }
  255. /* Author = helscream (Omer Ikram ul Haq)
  256. Last edit date = 2014-06-22
  257. Website: [url]http://hobbylogs.me.pn/?p=47[/url]
  258. Location: Pakistan
  259. Ver: 0.1 beta --- Start
  260. */

复制代码


雖然複雜 還想請問一下版上 有沒有大能還有其它的gyro_accel.h文字庫 小弟找遍了都沒看到有...

不然gyro_accel.cpp 或gyro_accel.h 程式碼 需要加哪些字 要填到直到可以動為止

目前看一些應該有的東西都有在裡面 可是好像少了啥麼 跟那些少了的算法跟程式碼

怕沒寫好會出事情 麻煩請大人指點讓小弟我完成這程式碼....強行呼叫也是我從朋友那學的

可惜朋友不會寫文字庫

希望大大能伸出援手讓小弟完成他 就算給我gyro_accel.h檔 我也會想辦法做些比較

畢竟我這邊光mpu6050.h寫法就有10幾種寫法 多少都有研究

看完只有"崇拜"兩個字可以形容
雖然有很多大同小異
可是都各有風格...算法最強的就是整個數學系的數學....

小弟束手無策了 麻煩各位大大解惑
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-17 06:52 , Processed in 0.061829 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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