• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Problem z MPU6050, zły odczyt

Object Storage Arubacloud
0 głosów
71 wizyt
pytanie zadane 25 czerwca 2019 w Mikrokontrolery przez Username Obywatel (1,350 p.)

Dzień dobry, 
Od razu zaznaczam że jest to mój drugi projekt który robię w swoim życiu. Wykonuje samo balansującego robota. No i wszystko dobrze gdyby nie to że MPU6050 źle odczytuje położenie. Np. Postawiłem robota prosto - MPU6050 wysyłała do Seriala zero - czyli tak jak powinno - jednak jeżeli przechylę o np. 5 stopni i wrócę do poprzedniego stanu to może być albo zero (jak powinno) albo np. 3. Nie zawsze to występuje jednak gdy robot często się przewraca to MPU6050 głupieje.

Biblioteki nie znam niestety, skopiowałem ją tydzień temu z jakiegoś filmiku bodajże a moja pamięć zawodzi.

Używam:
Arduino Nano
6v 4xAA
L293D
MPU6050


Mój kod:
 

#include <Wire.h>

//Variables
float elapsedTime, time, timePrev;        //Variables for time control
int gyro_error=0;                         //We use this variable to only calculate once the gyro data error
int16_t Gyr_rawX, Gyr_rawY, Gyr_rawZ;     //Here we store the raw data read 
float Gyro_angle_x, Gyro_angle_y;         //Here we store the angle value obtained with Gyro data
float Gyro_raw_error_x, Gyro_raw_error_y; //Here we store the initial gyro data error

int minustys;

/* Silniki */
const int leftBackward = 3;
const int leftForward = 4;
const int rightBackward = 5;
const int rightForward = 6;

bool tryb = true;

 
void setup(){
  Wire.begin();                           //begin the wire comunication  
  Wire.beginTransmission(0x68);           //begin, Send the slave adress (in this case 68)              
  Wire.write(0x6B);                       //make the reset (place a 0 into the 6B register)
  Wire.write(0x00);
  Wire.endTransmission(true);             //end the transmission
  
  Wire.beginTransmission(0x68);           //begin, Send the slave adress (in this case 68) 
  Wire.write(0x1B);                       //We want to write to the GYRO_CONFIG register (1B hex)
  Wire.write(0x10);                       //Set the register bits as 00010000 (1000dps full scale)
  Wire.endTransmission(true);             //End the transmission with the gyro

  Serial.begin(9600);                     //Remember to set this same baud rate to the serial monitor  
  time = millis();                        //Start counting time in milliseconds

/*Here we calculate the gyro data error before we start the loop
 * I make the mean of 200 values, that should be enough*/
  if(gyro_error==0)
  {
    for(int i=0; i<200; i++)
    {
      Wire.beginTransmission(0x68);            //begin, Send the slave adress (in this case 68) 
      Wire.write(0x43);                        //First adress of the Gyro data
      Wire.endTransmission(false);
      Wire.requestFrom(0x68,4,true);           //We ask for just 4 registers 
         
      Gyr_rawX=Wire.read()<<8|Wire.read();     //Once again we shif and sum
      Gyr_rawY=Wire.read()<<8|Wire.read();
   
      /*---X---*/
      Gyro_raw_error_x = Gyro_raw_error_x + (Gyr_rawX/32.8); 
      /*---Y---*/
      Gyro_raw_error_y = Gyro_raw_error_y + (Gyr_rawY/32.8);
      if(i==199)
      {
        Gyro_raw_error_x = Gyro_raw_error_x/200;
        Gyro_raw_error_y = Gyro_raw_error_y/200;
        gyro_error=1;
      }
    }
  }  

  minustys -= 1;
}
 
void loop(){
  timePrev = time;                        // the previous time is stored before the actual time read
    time = millis();                        // actual time read
    elapsedTime = (time - timePrev) / 1000; //divide by 1000 in order to obtain seconds

    Wire.beginTransmission(0x68);            //begin, Send the slave adress (in this case 68) 
    Wire.write(0x43);                        //First adress of the Gyro data
    Wire.endTransmission(false);
    Wire.requestFrom(0x68,4,true);           //We ask for just 4 registers
        
    Gyr_rawX=Wire.read()<<8|Wire.read();     //Once again we shif and sum
    Gyr_rawY=Wire.read()<<8|Wire.read();

    Gyr_rawX = (Gyr_rawX/32.8) - Gyro_raw_error_x; 
    Gyr_rawY = (Gyr_rawY/32.8) - Gyro_raw_error_y;
    
    Gyro_angle_x = Gyro_angle_x  + Gyr_rawX*elapsedTime;
    Gyro_angle_y = Gyro_angle_y  + Gyr_rawY*elapsedTime;
    Serial.print("GyroY angle: ");
    Serial.println(Gyro_angle_y);

    if(static_cast<int>(Gyro_angle_y)>3)
    {
      digitalWrite(leftBackward, HIGH);
      digitalWrite(leftForward, LOW);
      digitalWrite(rightForward, LOW); 
      digitalWrite(rightBackward, HIGH);
      Serial.print("1");
    }
    if (static_cast<int>(Gyro_angle_y)<minustys)
    {
      digitalWrite(leftBackward, LOW);
      digitalWrite(leftForward, HIGH);
      digitalWrite(rightForward, HIGH); 
      digitalWrite(rightBackward, LOW); 
      Serial.print("2");
    }
    if (static_cast<int>(Gyro_angle_y)>minustys && static_cast<int>(Gyro_angle_y)<3)
    {
      digitalWrite(leftForward, LOW);
      digitalWrite(rightForward, LOW);
      digitalWrite(leftBackward, LOW);
      digitalWrite(rightBackward, LOW); 
      Serial.print("3");
    }
}

 

Zaloguj lub zarejestruj się, aby odpowiedzieć na to pytanie.

Podobne pytania

0 głosów
0 odpowiedzi 68 wizyt
pytanie zadane 18 kwietnia 2019 w Mikrokontrolery przez karollewy123 Nowicjusz (120 p.)
+1 głos
1 odpowiedź 693 wizyt
pytanie zadane 27 września 2020 w Mikrokontrolery przez krzysieq18 Nowicjusz (230 p.)
0 głosów
1 odpowiedź 436 wizyt

92,575 zapytań

141,424 odpowiedzi

319,649 komentarzy

61,960 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...