Hallo, ich arbeite momentan an einem Schulprojekt, indem ich eine Drohne bauen möchte.
Doch momentan stecke ich hier fest. Die daten sollten relativ genau sein, und vor allem in ihre ursprungszustände zurückgehen. Ist das überhaupt so möglich?
Ich bin für jede Antwort und jede Hilfe sehr dankbar.
Hinzuzufügen ist noch, dass ich offensichtlich kein Profi bin, und entschuldige mich schonmal im voraus auf den hässlichen und "komplizierten" Code
#include <Wire.h>
#define MPU 0x68 // Alternatively set AD0 to HIGH --> Address = 0x69
#define MPU6050_GYRO_CONFIG 0x1B
#define MPU6050_ACCEL_CONFIG 0x1C // Accelerometer Configuration Register
#define MPU6050_PWR_MGT_1 0x6B // Power Management 1 Register
#define MPU6050_INT_PIN_CFG 0x37 // Interrupt Pin / Bypass Enable Configuration Register
#define MPU6050_INT_ENABLE 0x38 // Interrupt Enable Register
#define MPU6050_LATCH_INT_EN 0x05 // Latch Enable Bit for Interrupt
#define MPU6050_ACTL 0x07 // Active-Low Enable Bit
#define MPU6050_WOM_EN 0x06 // Wake on Motion Enable bit
#define MPU6050_WOM_THR 0x1F // Wake on Motion Threshold Register
#define MPU6050_MOT_DUR 0x20 // Motion Detection Duration Register
#define MPU6050_ACCEL_INTEL_CTRL 0x69 // Accelaration Interrupt Control Register
#define MPU6050_SIGNAL_PATH_RESET 0x68 // Signal Path Reset Register
unsigned long startTime;
unsigned long endTime;
unsigned long elapsedTime;
int loopcontrol1=1;
double AccX,AccY,AccZ,Temp, ProcessAccX, ProcessAccY, ProcessAccZ,ProcessGyroX, ProcessGyroY, ProcessGyroZ,GyroX,GyroY,GyroZ;
//int ProcessGyroX, ProcessGyroY, ProcessGyroZ,GyroX,GyroY,GyroZ;
float FertigGyroX,FertigGyroX2;
float TestA,TestB,TestC, GyroreadingsinGradX, GyroreadingsinGradY, GyroreadingsinGradZ ;
float GyroreadingsinGradX1, GyroreadingsinGradY1, GyroreadingsinGradZ1 ;
float OffsetGyroX, OffsetGyroY, OffsetGyroZ, OffsetGyroX1, OffsetGyroY1, OffsetGyroZ1;
float OffsetGyroXinsg,OffsetGyroYinsg,OffsetGyroZinsg;
int gyroZaehler=0;
int ACCData,C,GYROData;
int accregisterconf;
int interruptPin = 2;
int LIM=100;
volatile bool accEvent = false;
float GyroXOfset, GyroYOfset, GyroZOfset;
byte ledPin=4;
float GyroreadingsinGradX2=0;
void setup() {
ACCData = 16384;
GYROData = 10;//16384;
Wire.begin();
Serial.begin(115200);
//accConfG();
//pinmodes
pinMode(ledPin, OUTPUT);
pinMode(interruptPin, INPUT);
//
}
void loop(){
delay(100);
startTime = millis();
//accTest();
//accReadings();
gyroReadings();
//accReadings();
//interruptloop();
endTime = millis();
}
void accConfG(){
Wire.begin();
Serial.println("Starte Accelerometer konfiguration");
Wire.beginTransmission(MPU);
Wire.write(MPU6050_PWR_MGT_1);
Wire.write(0x00);
Serial.println("powermanagement RESET");
delay(1000);
Wire.endTransmission(true);
Wire.beginTransmission(MPU);
Wire.write(MPU6050_ACCEL_CONFIG);
Wire.write(0b00000000);//Accelerometer auf +-2g einstellen(Bit 4 und 5)
Serial.println("Accelerometer eingestellt");
delay(1000);
gyroConfig();
}
void gyroConfig(){
Wire.begin(MPU);
Serial.println("Starte Gyroskop konfiguration");
Wire.beginTransmission(MPU);
Wire.write(MPU6050_GYRO_CONFIG);
Wire.write(0b00000000);//gyro auf +- 250 gard pro sekunde )
Serial.println("Accelerometer eingestellt");
delay(1000);
}
void accReadings(){
Wire.beginTransmission(MPU);
Wire.write(59);
Wire.requestFrom(MPU,6,true);//frägt nach 14 registern also von 59 nach 73 weil 59 und 60 ACC X sind 60,61 AccY usw..
Wire.endTransmission(false);
AccX=Wire.read()<<8;Wire.read();
AccY=Wire.read()<<8;Wire.read();
AccZ=Wire.read()<<8;Wire.read();
ProcessAccX=AccX/ACCData;
ProcessAccY=AccY/ACCData;
ProcessAccZ=AccZ/ACCData;
Serial.print("AccX = "); Serial.print(ProcessAccX);
Serial.print(" || AccY = "); Serial.print(ProcessAccY);
Serial.print(" || AccZ = "); Serial.print(ProcessAccZ);
//accelerationTRI(ProcessAccX,ProcessAccY,ProcessAccZ);
}
void accelerationTRI(){
}
double X2, Y2;
double DriftX, DriftY, DriftZ;
double DriftXges, DriftYges, DriftZges;
double gyroXGes, gyroYGes;
int offsetcount;
int upprbound=50;
int lowrbound=10;
int extratrim=3;
int ff;
bool allesneu = false;
int gyroReadings(){
offsetcount++;
Wire.beginTransmission(MPU);
Wire.write(67);
Wire.requestFrom(MPU,4,true);//frägt nach 14 registern also von 59 nach 73 weil 59 und 60 ACC X sind 60,61 AccY usw..
Wire.endTransmission(false);
GyroX=Wire.read()<<8;Wire.read();
GyroY=Wire.read()<<8;Wire.read();
if(!allesneu){
ProcessGyroX=GyroX/GYROData;
gyroXGes=gyroXGes+ProcessGyroX;
ProcessGyroY=GyroY/GYROData;
gyroYGes=gyroYGes+ProcessGyroY;
}else{
ProcessGyroX=GyroX/GYROData;
ProcessGyroY=GyroY/GYROData;
gyroXGes=gyroXGes+(ProcessGyroX+DriftXges);
gyroYGes=gyroYGes+(ProcessGyroY+DriftYges);
Serial.print(" gyroXGes ");
Serial.print(gyroXGes);
Serial.print(" gyroYGes ");
Serial.println(gyroYGes);
}
drift(gyroXGes,gyroYGes);
}
void drift(double gyroXges, double gyroYges){
if(offsetcount<=upprbound && offsetcount > lowrbound){
ff++;
if(offsetcount==lowrbound+extratrim){
DriftYges=0;
DriftXges=0;
}
Serial.print("DriftY ");
Serial.println(DriftY);
DriftY=gyroYGes-Y2;
DriftX=gyroXGes-X2;
Y2=gyroYGes;
X2=gyroXGes;
DriftXges=DriftXges+DriftX;
DriftYges=DriftYges+DriftY;
if(offsetcount==upprbound){
allesneu = true;
DriftXges=DriftXges/(upprbound-lowrbound-extratrim+1);
DriftXges=abs(DriftXges);
DriftYges=DriftYges/(upprbound-lowrbound-extratrim+1);
DriftYges=abs(DriftYges);
gyroXGes=0;
gyroYGes=0;
}
}
}