Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void getAngles() {
- byte readed = getRegisters(MPU6050, MPU6050_ACCEL_XOUT_H, buffer, 14);
- if(readed == 14)
- {
- // gyro
- float gyro_x = (int)word(buffer[8], buffer[9]);
- float gyro_y = (int)word(buffer[10], buffer[11]);
- Serial.print(mapf(gyro_x, -32768, 32767, -250, 250)); Serial.print("\t");
- Serial.print(mapf(gyro_y, -32768, 32767, -250, 250)); Serial.println();
- gyro_x = mapf(gyro_x - gyro_x_offset, -32768, 32767, -250, 250);
- gyro_y = mapf(gyro_y - gyro_y_offset, -32768, 32767, -250, 250);
- // accelerometer
- float accel_x = (int)word(buffer[0], buffer[1]);
- float accel_y = (int)word(buffer[2], buffer[3]);
- float accel_z = (int)word(buffer[4], buffer[5]);
- accel_x = mapf(accel_x, -32768, 32767, -2, 2);
- accel_y = mapf(accel_y, -32768, 32767, -2, 2);
- accel_z = mapf(accel_z, -32768, 32767, -2, 2);
- float accel_x_angle = atan2(accel_y, accel_z) * 180 / PI;
- float accel_y_angle = atan2(accel_x, accel_z) * 180 / PI;
- // complementary filter
- /*
- angle_x = 0.98 * (angle_x + gyro_x * dt / 1000.0) + 0.02 * accel_x_angle;
- angle_y = 0.98 * (angle_y - gyro_y * dt / 1000.0) + 0.02 * accel_y_angle;
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement