-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarometer.cpp
65 lines (51 loc) · 1.92 KB
/
barometer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// STAY: Solid fuel propelled rocket
// Copyright (C) 2024 Liftorbit
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <Arduino.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SPI.h>
#include "barometer.h"
const int BMP_SCL = 13; // SCK
const int BMP_SDO = 12; // MISO
const int BMP_SDA = 14; // MOSI
const int BMP_CSB = 27; // CS
Adafruit_BMP280 bmp280(BMP_CSB, BMP_SDA, BMP_SDO, BMP_SCL);
bool Barometer::begin() {
return bmp280.begin();
}
float Barometer::getAltitude() {
return bmp280.readAltitude(1013.25);
};
float Barometer::getPressure() {
return bmp280.readPressure();
};
float Barometer::getTemperature() {
return bmp280.readTemperature();
};
float Barometer::getGroundDistance() {
float currentAltitude = this->getAltitude();
float distance = currentAltitude - this->groundAltitude;
distance = (distance < 0) ? 0 : distance;
return distance;
};
void Barometer::saveGroundAltitude() {
this->groundAltitude = this->getAltitude();
};
float Barometer::getAverageSpeed(int periodOfTime) {
float timeInSeconds = periodOfTime / 1000;
float initialAltitude = this->getAltitude();
delay(periodOfTime);
float finalAltitude = this->getAltitude();
float averageSpeed = (finalAltitude - initialAltitude) / timeInSeconds;
return averageSpeed;
};