|
| 1 | +#include <Altimeter/altimeter.h> |
| 2 | +#include <Adafruit_BMP3XX.h> |
| 3 | +// #include "MovingAverageFilter.h" |
| 4 | +#include <Chrono.h> |
| 5 | + |
| 6 | +#include <global.h> |
| 7 | +#include <config.h> |
| 8 | + |
| 9 | +#define SEALEVELPRESSURE_HPA (1013.25) |
| 10 | + |
| 11 | +Adafruit_BMP3XX bmp; |
| 12 | +// MovingAverageFilter MAF_altitude(20); |
| 13 | + |
| 14 | +Chrono apogeeTimer; |
| 15 | + |
| 16 | +bool Altimeter::begin() |
| 17 | +{ |
| 18 | + // Start sensor on I2C |
| 19 | + if (bmp.begin_I2C()) |
| 20 | + { |
| 21 | +#if is_DEBUG |
| 22 | + Serial.println("Starting BMP --> OK!"); |
| 23 | +#endif |
| 24 | + // Set up oversampling and filter initialization |
| 25 | + bmp.setTemperatureOversampling(BMP3_OVERSAMPLING_8X); |
| 26 | + bmp.setPressureOversampling(BMP3_OVERSAMPLING_8X); |
| 27 | + bmp.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3); |
| 28 | + bmp.setOutputDataRate(BMP3_ODR_200_HZ); |
| 29 | + |
| 30 | + // KalmanInit(); // start kalman filter for the Altitude readings // KALMAN FILTER ON main.cpp |
| 31 | + |
| 32 | +#if is_DEBUG |
| 33 | + Serial.print("Calculating Relative Altitude"); |
| 34 | +#endif |
| 35 | + |
| 36 | + for (int i = 0; i < 50; i++) // let the sensor make a few readings to get it started |
| 37 | + { |
| 38 | + bmp.readPressure(); |
| 39 | + bmp.readTemperature(); |
| 40 | + } |
| 41 | + |
| 42 | + // Just take the average for (relative Altitude determination) |
| 43 | + for (int i = 0; i < startingAltitudeReadings; i++) |
| 44 | + { |
| 45 | + sealevel_offset += bmp.readAltitude(SEALEVELPRESSURE_HPA); |
| 46 | + |
| 47 | + // ground_pressure += bmp.readPressure(); |
| 48 | + |
| 49 | +#if is_DEBUG |
| 50 | + Serial.print("."); |
| 51 | +#endif |
| 52 | + } |
| 53 | + sealevel_offset /= startingAltitudeReadings; |
| 54 | + |
| 55 | + // ground_pressure /= 100; // Pascal to hPa |
| 56 | + // ground_pressure /= startingAltitudeReadings; // Avg to get relative ground pressure (set my AGL to 0m) |
| 57 | + |
| 58 | + // Equation taken from BMP180 datasheet (page 16): |
| 59 | + // http://www.adafruit.com/datasheets/BST-BMP180-DS000-09.pdf |
| 60 | + // current_altitude = 44330.0 * (1.0 - pow((bmp.pressure / 100.0) / ground_pressure, 0.1903)); // /100 Pascal to hPa |
| 61 | + |
| 62 | + current_altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA) - sealevel_offset; |
| 63 | + data.altimeter.altitude = current_altitude; |
| 64 | + data.altimeter.temperature = bmp.readTemperature(); |
| 65 | + previous_altitude = 0; // make sure this is 0 |
| 66 | + |
| 67 | +#if is_DEBUG |
| 68 | + Serial.println(); |
| 69 | + Serial.println("LAUNCH ALTITUDE: " + String(data.altimeter.altitude) + " TEMP: " + String(data.altimeter.temperature)); |
| 70 | +#endif |
| 71 | + |
| 72 | +#if is_DEBUG |
| 73 | + Serial.println("PYRO CHANNELS --> OK!"); |
| 74 | +#endif |
| 75 | + return true; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | +#if is_DEBUG |
| 80 | + Serial.println("Starting BMP --> FAILED!"); |
| 81 | +#endif |
| 82 | + return false; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +bool Altimeter::update() |
| 87 | +{ |
| 88 | + if (bmp.performReading()) |
| 89 | + { |
| 90 | + current_altitude = bmp.readAltitude(SEALEVELPRESSURE_HPA) - sealevel_offset; |
| 91 | + data.altimeter.temperature = bmp.temperature; |
| 92 | + } |
| 93 | + |
| 94 | + // current_altitude = MAF_altitude.process(current_altitude); // when used the Moving average filter |
| 95 | + data.altimeter.altitude = current_altitude; |
| 96 | + |
| 97 | + // Record the max altitude |
| 98 | + if (current_altitude > max_altitude) |
| 99 | + { |
| 100 | + max_altitude = data.altimeter.altitude; |
| 101 | + } |
| 102 | + |
| 103 | + // Replace the previous_altitude with the current altitude for next loop |
| 104 | + previous_altitude = current_altitude; |
| 105 | + return true; |
| 106 | +} |
| 107 | + |
| 108 | +bool Altimeter::detectApogge() |
| 109 | +{ |
| 110 | + if ((current_altitude < max_altitude)) |
| 111 | + { |
| 112 | + if (apogeeTimer.hasPassed(10)) |
| 113 | + { |
| 114 | +#if is_DEBUG |
| 115 | + Serial.println("---- APOGGE! ---- Max altitude: "); |
| 116 | + Serial.print(max_altitude); |
| 117 | +#endif |
| 118 | + return true; |
| 119 | + } |
| 120 | + } |
| 121 | + else |
| 122 | + { |
| 123 | + return false; |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +float Altimeter::getVerticalVelocity() |
| 128 | +{ |
| 129 | + return (current_altitude - previous_altitude) / data.delta_t; // check data struct for the kalman derived velocity |
| 130 | +} |
0 commit comments