Hi, I am currently trying to get the angle pitch of 2 gyro sensors. The first one seems to be working fine, but the second one slowly increments itself by one even when idle. I have the arduino connected to a node MCU where the arduino sends the angles and the node MCU reads the serial data. If somebody could tell me what the problem is I would really appreciate it the code is below:
#include <SoftwareSerial.h>
#include <Wire.h>
#include "I2Cdev.h"
#include "MPU6050_6Axis_MotionApps20.h"
MPU6050 MPU1(0x68);
MPU6050 MPU2(0x69);
SoftwareSerial espSerial(5,6);
float str1;
float str2;
//Declaring some global variables
int gyro_x, gyro_y, gyro_z;
int gyro_x2, gyro_y2, gyro_z2;
long gyro_x_cal, gyro_y_cal, gyro_z_cal;
long gyro_x_cal2, gyro_y_cal2, gyro_z_cal2;
boolean set_gyro_angles;
boolean set_gyro_angles2;
long acc_x, acc_y, acc_z, acc_total_vector;
long acc_x2, acc_y2, acc_z2, acc_total_vector2;
float angle_roll_acc, angle_pitch_acc;
float angle_roll_acc2, angle_pitch_acc2;
float angle_pitch, angle_roll;
float angle_pitch2, angle_roll2;
int angle_pitch_buffer, angle_roll_buffer;
int angle_pitch_buffer2, angle_roll_buffer2;
float angle_pitch_output, angle_roll_output;
float angle_pitch_output2, angle_roll_output2;
int angle1;
int angle2;
long loop_timer;
long loop_timer2;
int temp;
int temp2;
String out1;
String out2;
void setup() {
Wire.begin();
MPU1.initialize();
MPU1.dmpInitialize();
MPU1.setXGyroOffset(220);
MPU1.setYGyroOffset(76);
MPU1.setZGyroOffset(-85);
MPU1.setZAccelOffset(1788); // 1688 factory default for my test chip
setup_mpu1_6050_registers(); //Setup the registers of the MPU-6050
for (int cal_int = 0; cal_int < 1000 ; cal_int ++){ //Read the raw acc and gyro data from the MPU-6050 for 1000 times
read_mpu1_6050_data();
gyro_x_cal += gyro_x; //Add the gyro x offset to the gyro_x_cal variable
gyro_y_cal += gyro_y; //Add the gyro y offset to the gyro_y_cal variable
gyro_z_cal += gyro_z; //Add the gyro z offset to the gyro_z_cal variable
delay(3); //Delay 3us to have 250Hz for-loop
}
gyro_x_cal /= 1000;
gyro_y_cal /= 1000;
gyro_z_cal /= 1000;
Serial.begin(115200);
loop_timer = micros();
Wire.begin();
MPU2.initialize();
MPU2.dmpInitialize();
MPU2.setXGyroOffset(220);
MPU2.setYGyroOffset(76);
MPU2.setZGyroOffset(-85);
MPU2.setZAccelOffset(1788);
setup_mpu2_6050_registers();
for (int cal_int2 = 0; cal_int2 < 1000 ; cal_int2 ++){ //Read the raw acc and gyro data from the MPU-6050 for 1000 times
read_mpu2_6050_data();
gyro_x_cal2 += gyro_x2; //Add the gyro x offset to the gyro_x_cal variable
gyro_y_cal2 += gyro_y2; //Add the gyro y offset to the gyro_y_cal variable
gyro_z_cal2 += gyro_z2; //Add the gyro z offset to the gyro_z_cal variable
delay(3);
}
// divide by 1000 to get avarage offset
gyro_x_cal2 /= 1000;
gyro_y_cal2 /= 1000;
gyro_z_cal2 /= 1000;
Serial.begin(115200);
espSerial.begin(115200);
loop_timer2 = micros(); //Reset the loop timer
}
void loop(){
// Serial.print("Angle1: ");
out1 = read_mpu1_6050_data();
// Serial.println(out1);
//Subtract the offset values from the raw gyro values
// Serial.print("Angle2: ");
out2 = read_mpu2_6050_data();
// Serial.println(out2);
espSerial.print("angle1: ");
espSerial.println(out1);
espSerial.print("angle2: ");
espSerial.println(out2);
}
void setup_mpu1_6050_registers(){
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x6B); //Send the requested starting register
Wire.write(0b00000000); //Set the requested starting register
Wire.endTransmission();
//Configure the accelerometer (+/-8g)
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x1B); //Send the requested starting register
Wire.write(0b00000000);; //Set the requested starting register
Wire.endTransmission();
//Configure the gyro (500dps full scale)
Wire.beginTransmission(0x68); //Start communicating with the MPU-6050
Wire.write(0x1C); //Send the requested starting register
Wire.write(0b00000000); //Set the requested starting register
Wire.endTransmission();
}
void setup_mpu2_6050_registers(){
Wire.beginTransmission(0x69); //Start communicating with the MPU-6050
Wire.write(0x6B); //Send the requested starting register
Wire.write(0b00000000); //Set the requested starting register
Wire.endTransmission();
Wire.beginTransmission(0x69); //Start communicating with the MPU-6050
Wire.write(0x1B); //Send the requested starting register
Wire.write(0b00000000); //Set the requested starting register
Wire.endTransmission();
//Configure the gyro (500dps full scale)
Wire.beginTransmission(0x69); //Start communicating with the MPU-6050
Wire.write(0x1C); //Send the requested starting register
Wire.write(0b00000000); //Set the requested starting register
Wire.endTransmission();
}