#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include <Adafruit_Sensor.h>
#define BMP_SCK (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS (10)
// Initialize BMP280 sensor (hardware SPI)
Adafruit_BMP280 bmp(BMP_CS);
// Mock MPU6050 class
class MPU6050 {
public:
void initialize() {
Serial.println("MPU6050 initialized.");
}
bool testConnection() {
return true;
}
void update() {}
double getGyroZ() {
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;
return angle;
}
};
MPU6050 mpu;
double gyroZ_bias = 0;
// Kalman filter variables for altitude estimation
double altitude_estimate = 0;
double velocity_estimate = 2.0;
double accel_bias = 0;
double P[2][2] = { {1, 0}, {0, 1} };
double Q[2][2] = { {0.01, 0}, {0, 0.02} };
double R = 0.01;
double K[2];
// Approximate thrust data derived from the simulation graph
const int thrust_data_length = 240;
double thrust_data[thrust_data_length];
// Function to approximate thrust data from the graph
void initializeThrustData() {
for (int i = 0; i < thrust_data_length; i++) {
if (i < 15) {
thrust_data[i] = 200 - (i * 10); // Example values for initial thrust
} else {
thrust_data[i] = -9.8; // Gravity after burnout
}
}
}
// Rocket and simulation parameters
double rocket_mass = 50.0; // Mass of the rocket in kg
double time_step = 1.0; // Time step for the simulation in seconds
int thrust_index = 0;
void setup() {
Serial.begin(115200);
Wire.begin();
if (!bmp.begin(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);
initializeThrustData();
}
void loop() {
mpu.update();
double gyroZ = mpu.getGyroZ() - gyroZ_bias;
static double roll_angle = 0;
double dt = time_step;
roll_angle += gyroZ * dt;
if (roll_angle > 180) roll_angle = -180;
static double simulated_altitude = 0;
static double simulated_velocity = 0;
if (thrust_index < thrust_data_length) {
double thrust = thrust_data[thrust_index];
double acceleration = thrust / rocket_mass;
simulated_velocity += acceleration * dt;
simulated_altitude += simulated_velocity * dt;
thrust_index++;
}
double measured_altitude = simulated_altitude;
kalmanUpdate(measured_altitude);
Serial.print("Simulated Altitude: ");
Serial.print(simulated_altitude);
Serial.print(", Estimated Altitude: ");
Serial.println(altitude_estimate);
delay(100);
}
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;
}
void kalmanUpdate(double measured_altitude) {
altitude_estimate += velocity_estimate * 1.0; // dt = 1.0s
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];
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;
}