Hello,
I am currently working on rewriting my ongoing drone auto level program from an Arduino Uno to a faster Sparkfun Redboard Artemis. First step is to get the angle data from the MPU-6050(Accelerometer and gyroscope combined). So I got a working Arduino Code for testing and it works fine on my old Arduino Uno, but when I load the same code to the Redboard it outputs really weird data or “Nan”.
The MPU-6050 is attached to the sda and scl Pin.
Here is the testing programm:
#include <Wire.h>
int escm1, escm2, escm3, escm4;
int all_esc, e1, e2, e3, e4, es1, es2, es3, es4, emp;
int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;
long gyro_x_cal, gyro_y_cal, gyro_z_cal, angle_pitch_acc_cal, angle_roll_acc_cal;
float angle_pitch, angle_roll;
int angle_pitch_buffer, angle_roll_buffer;
boolean set_gyro_angles;
float angle_roll_acc, angle_pitch_acc;
float angle_pitch_output, angle_roll_output;
double pwm1, pwm2, pwm3, pwm4;
void setup()
{
Serial.begin(57600);
Wire.begin();
//Wire.setClock(400000);
setup_mpu_6050_registers();
Serial.print("MPU-6050 IMU");
Serial.println("\t");
delay(1500);
Serial.print("Kalibriere Gyro:");
for (int cal_int = 0; cal_int < 2000 ; cal_int ++){
if(cal_int % 125 == 0)Serial.print(".");
read_mpu_6050_data();
gyro_x_cal += gyro_x;
gyro_y_cal += gyro_y;
gyro_z_cal += gyro_z;
delay(3);
}
gyro_x_cal /= 2000;
gyro_y_cal /= 2000;
gyro_z_cal /= 2000;
}
void loop() {
read_mpu_6050_data();
gyro_x -= gyro_x_cal;
gyro_y -= gyro_y_cal;
gyro_z -= gyro_z_cal;
//Gyro angle calculations
//0.0000611 = 1 / (250Hz / 65.5)
angle_pitch += gyro_x * 0.0000611;
angle_roll += gyro_y * 0.0000611;
angle_pitch += angle_roll * sin(gyro_z * 0.000001066);
angle_roll -= angle_pitch * sin(gyro_z * 0.000001066);
acc_total_vector = sqrt((acc_x*acc_x)+(acc_y*acc_y)+(acc_z*acc_z));
angle_pitch_acc = asin((float)acc_y/acc_total_vector)* 57.296;
angle_roll_acc = asin((float)acc_x/acc_total_vector)* -57.296;
// acc Output auf gerader Fläche:
angle_pitch_acc -= 1.1 ;
angle_roll_acc -= -1,7 ;
if(set_gyro_angles){
angle_pitch = angle_pitch * 0.9996 + angle_pitch_acc * 0.0004;
angle_roll = angle_roll * 0.9996 + angle_roll_acc * 0.0004;
}
else{
angle_pitch = angle_pitch_acc;
angle_roll = angle_roll_acc;
set_gyro_angles = true;
}
angle_pitch_output = angle_pitch_output * 0.85 + angle_pitch * 0.15;
angle_roll_output = angle_roll_output * 0.85 + angle_roll * 0.15;
Serialprint();
}
void read_mpu_6050_data(){
Wire.beginTransmission(0x68);
Wire.write(0x3B);
Wire.endTransmission();
Wire.requestFrom(0x68,14);
while(Wire.available() < 14);
acc_x = Wire.read()<<8|Wire.read();
acc_y = Wire.read()<<8|Wire.read();
acc_z = Wire.read()<<8|Wire.read();
temperature = Wire.read()<<8|Wire.read();
gyro_x = Wire.read()<<8|Wire.read();
gyro_y = Wire.read()<<8|Wire.read();
gyro_z = Wire.read()<<8|Wire.read();
}
void setup_mpu_6050_registers(){
//Activate the MPU-6050
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission();
//Configure the accelerometer (+/-8g)
Wire.beginTransmission(0x68);
Wire.write(0x1C);
Wire.write(0x10);
Wire.endTransmission();
//Configure the gyro (500dps full scale)
Wire.beginTransmission(0x68);
Wire.write(0x1B);
Wire.write(0x08);
Wire.endTransmission();
}
void Serialprint(){
Serial.print("X:");
Serial.print(angle_roll_output);
Serial.print("\t");
Serial.print("\t");
Serial.print("Y:");
Serial.println(angle_pitch_output);
}
void acc_angle_calculations(){
acc_total_vector = sqrt((acc_x*acc_x)+(acc_y*acc_y)+(acc_z*acc_z));
angle_pitch_acc = asin((float)acc_y/acc_total_vector)* 57.296;
angle_roll_acc = asin((float)acc_x/acc_total_vector)* -57.296;
}
Here is the Arduino Output(as it should be):
And here the Redboard Ouput:
I would really appreciate the help
Thanks