#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)
// Mock MPU6050 class
class MPU6050 {
public:
void initialize() {
Serial.println("MPU6050 initialized.");
}
bool testConnection() {
return true;
}
void update() {}
double getGyroZ() {
// Simulate 0.5 Hz roll about Z-axis (1 cycle every 2 seconds)
static double angle = 0;
double frequency = 0.5; // 0.5 Hz
angle += 360 * frequency / 100; // Increment angle for each loop
if (angle > 180) angle = -180; // Simulate angle wrap around
return angle;
}
};
Adafruit_BMP280 bmp;
MPU6050 mpu;
double gyroZ_bias = 0;
// Kalman filter variables for altitude estimation
double altitude_estimate = 0;
double velocity_estimate = 12.0; // 12 m/s upwards
double accel_bias = 0;
double P[2][2] = { {1, 0}, {0, 1} };
double Q[2][2] = { {0.001, 0}, {0, 0.003} };
double R = 0.03;
double K[2];
// Calibration function for MPU6050
void calibrateMPU6050() {
long gyroZ_sum = 0;
int samples = 1000;
for (int i = 0; i < samples; i++) {
mpu.update();
gyroZ_sum += mpu.getGyroZ();
delay(2);
}
gyroZ_bias = gyroZ_sum / samples;
}
// Kalman filter update function for altitude
void kalmanUpdate(double measured_altitude) {
// Predict step
altitude_estimate += velocity_estimate * 0.1; // dt = 0.1s
P[0][0] += P[1][1] + Q[0][0];
P[0][1] += P[1][1];
P[1][0] += P[1][1];
P[1][1] += Q[1][1];
// Update step
double y = measured_altitude - altitude_estimate;
double S = P[0][0] + R;
K[0] = P[0][0] / S;
K[1] = P[1][0] / S;
altitude_estimate += K[0] * y;
velocity_estimate += K[1] * y;
double P00_temp = P[0][0];
double P01_temp = P[0][1];
P[0][0] -= K[0] * P00_temp;
P[0][1] -= K[0] * P01_temp;
P[1][0] -= K[1] * P00_temp;
P[1][1] -= K[1] * P01_temp;
}
void setup() {
Serial.begin(115200);
Wire.begin();
if (!bmp.begin(0x77)) { // Assuming the I2C address is 0x76
Serial.println("Could not find a valid BMP280 sensor, check wiring!");
while (1);
}
mpu.initialize();
if (!mpu.testConnection()) {
Serial.println("MPU6050 connection failed!");
while (1);
}
Serial.println("Calibrating MPU6050...");
calibrateMPU6050();
Serial.print("Gyro Z Bias: ");
Serial.println(gyroZ_bias);
// BMP280 settings
bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,
Adafruit_BMP280::SAMPLING_X2,
Adafruit_BMP280::SAMPLING_X16,
Adafruit_BMP280::FILTER_X16,
Adafruit_BMP280::STANDBY_MS_1);
}
void loop() {
mpu.update();
double gyroZ = mpu.getGyroZ() - gyroZ_bias;
// Calculate roll angle based on gyroZ readings
static double roll_angle = 0;
double dt = 0.1; // Assume a fixed time step for simplicity
roll_angle += gyroZ * dt;
if (roll_angle > 180) roll_angle = -180;
// Simulate altitude change
double measured_altitude = bmp.readAltitude(1013.25) + velocity_estimate * dt; // Simulate altitude change
// Update Kalman filter for altitude
kalmanUpdate(measured_altitude);
Serial.print("Gyro Z: ");
Serial.print(gyroZ);
Serial.print(", Roll Angle: ");
Serial.print(roll_angle);
Serial.print(", Measured Altitude: ");
Serial.print(measured_altitude);
Serial.print(", Estimated Altitude: ");
Serial.println(altitude_estimate);
delay(100); // Adjust delay as needed
}