Skip to content
This repository was archived by the owner on Jan 15, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 66 additions & 17 deletions flight-computer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@
#include "mosfet_igniter.h"
#include "adafruit_gps_api.h"
#include "definitions.h"
#include "SD.h"

LSM9DS1_API imu_sensor = LSM9DS1_API::getInstance();
BMP3XX_API altimeter = BMP3XX_API::getInstance();
Telemetry telemetry = Telemetry::getInstance();
MosfetIgniter igniter = MosfetIgniter::getInstance();
Adafruit_GPS_API gps = Adafruit_GPS_API::getInstance();
FSM *fsm;
static LSM9DS1_API* imu_sensor = nullptr;
static BMP3XX_API* altimeter = nullptr;
static Telemetry* telemetry = nullptr;
static MosfetIgniter* igniter = nullptr;
static Adafruit_GPS_API* gps = nullptr;
static FSM* fsm = nullptr;
// TODO: improve buzzer code
const int buzzer = 17;
bool sound = false;
uint8_t loop_frequency = 10; // Hz

void setup() {
#if SERIAL_DEBUG
Serial.begin(115200);
#endif
bool SetupSensorsAndCommunication();

#if BUZZER
pinMode(buzzer, OUTPUT);
tone(buzzer, 500);
#endif
void setup() {
#if SERIAL_DEBUG
Serial.begin(115200);
#endif

fsm = new FSM(&telemetry, &imu_sensor, &altimeter, &gps, &igniter, &loop_frequency);
if (!SetupSensorsAndCommunication()) {
Serial.println("Did not initialize sensors");
while(1);
}

#if BUZZER
noTone(buzzer);
Expand All @@ -36,8 +37,8 @@ void setup() {
void loop() {
int timerStart = millis();

if (telemetry.messageAvailable()) {
String message = telemetry.receiveMessage();
if (telemetry->messageAvailable()) {
String message = telemetry->receiveMessage();
if (message.substring(0, 5).equals("SPCMD")) {
int event;
if (sscanf(message.c_str(), "SPCMD:%i--", &event) == 1) {
Expand Down Expand Up @@ -69,3 +70,51 @@ void loop() {

delay(max(0, delayTime));
}

bool SetupSensorsAndCommunication() {
#if SD_LOGS
while(!SD.begin(SD_CS)) {
#if SERIAL_DEBUG
Serial.println("SD did not initialize");
#endif
}
#endif

#if BUZZER
pinMode(buzzer, OUTPUT);
tone(buzzer, 500);
#endif

imu_sensor = static_cast<LSM9DS1_API*>(malloc(sizeof(LSM9DS1_API)));
if (imu_sensor == nullptr) {
return false;
}
*imu_sensor = LSM9DS1_API::getInstance();

altimeter = static_cast<BMP3XX_API*>(malloc(sizeof(BMP3XX_API)));
if (altimeter == nullptr) {
return false;
}
*altimeter = BMP3XX_API::getInstance();

telemetry = static_cast<Telemetry*>(malloc(sizeof(Telemetry)));
if (telemetry == nullptr) {
return false;
}
*telemetry = Telemetry::getInstance();

igniter = static_cast<MosfetIgniter*>(malloc(sizeof(MosfetIgniter)));
if (igniter == nullptr) {
return false;
}
*igniter = MosfetIgniter::getInstance();

gps = static_cast<Adafruit_GPS_API*>(malloc(sizeof(Adafruit_GPS_API)));
if (gps == nullptr) {
return false;
}
*gps = Adafruit_GPS_API::getInstance();

fsm = new FSM(telemetry, imu_sensor, altimeter, gps, igniter, &loop_frequency);
return fsm != nullptr;
}
40 changes: 23 additions & 17 deletions libraries/adafruit_gps_api.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
#include <Adafruit_GPS.h>
#include "adafruit_gps_api.h"

#define GPSSerial Serial1

Adafruit_GPS_API::Adafruit_GPS_API() {};
static Adafruit_GPS_API* instance = nullptr;
static Adafruit_GPS* receiver = nullptr;
static unsigned long last_read = 0;

Adafruit_GPS Adafruit_GPS_API::receiver(&GPSSerial);
Adafruit_GPS_API::Adafruit_GPS_API() {};

Adafruit_GPS_API& Adafruit_GPS_API::getInstance() {
static Adafruit_GPS_API instance;
return instance;
if (instance == nullptr) {
instance = new Adafruit_GPS_API();
receiver = new Adafruit_GPS(&GPSSerial);
}
return *instance;
}

void Adafruit_GPS_API::setup() {
last_read = 0;

receiver.begin(9600);
receiver->begin(9600);

// turn on GPRMC and GGA.
receiver.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
receiver->sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

// Update every 1Hz.
receiver.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
receiver->sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);

// Possition fix update every 1Hz.
receiver.sendCommand(PMTK_API_SET_FIX_CTL_1HZ);
receiver->sendCommand(PMTK_API_SET_FIX_CTL_1HZ);
}

void Adafruit_GPS_API::readData() {
Expand All @@ -33,21 +39,21 @@ void Adafruit_GPS_API::readData() {
}

// Empty buffer.
while(receiver.read());
while(receiver->read());

last_read = millis();

// Parse data.
if (receiver.newNMEAreceived()) {
receiver.parse(receiver.lastNMEA());
if (receiver->newNMEAreceived()) {
receiver->parse(receiver->lastNMEA());
}
}

bool Adafruit_GPS_API::fix() {
// Check if last data is updated.
readData();

return receiver.fix;
return receiver->fix;
}

int32_t Adafruit_GPS_API::latitude() {
Expand All @@ -60,7 +66,7 @@ int32_t Adafruit_GPS_API::latitude() {
return 0;
}

return receiver.latitude_fixed;
return receiver->latitude_fixed;
}

int32_t Adafruit_GPS_API::longitude() {
Expand All @@ -73,7 +79,7 @@ int32_t Adafruit_GPS_API::longitude() {
return 0;
}

return receiver.longitude_fixed;
return receiver->longitude_fixed;
}

float Adafruit_GPS_API::altitude() {
Expand All @@ -86,12 +92,12 @@ float Adafruit_GPS_API::altitude() {
return 0;
}

return receiver.altitude;
return receiver->altitude;
}

int Adafruit_GPS_API::satellites() {
// Check if last data is updated.
readData();

return receiver.satellites;
}
return receiver->satellites;
}
10 changes: 0 additions & 10 deletions libraries/adafruit_gps_api.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include <Adafruit_GPS.h>
#include "gps_receiver.h"

class Adafruit_GPS_API: public GPSReceiver {
Expand Down Expand Up @@ -87,15 +86,6 @@ class Adafruit_GPS_API: public GPSReceiver {
*/
Adafruit_GPS_API();

static Adafruit_GPS receiver;

/**
* @brief <h1>Last read data time.</h1>
*
* Ensure data is read every 100ms.
*/
unsigned long last_read;

/**
* @brief <h1>Reads the latest data from the serial.</h1>
* Clear the GPS buffer and read the last data available from the GPS module.
Expand Down
20 changes: 13 additions & 7 deletions libraries/bmp280_api.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
#include "Adafruit_BMP280.h"
#include "bmp280_api.h"

#define BMP_SCK (13)
#define BMP_MISO (12) // SD0
#define BMP_MOSI (11) // SD1
#define BMP_CS (10)

Adafruit_BMP280 BMP280_API::sensor;
static BMP280_API* instance = nullptr;
static Adafruit_BMP280* sensor = nullptr;
static int32_t ground_level = 0;

BMP280_API& BMP280_API::getInstance() {
static BMP280_API instance;
return instance;
if (instance == nullptr) {
instance = new BMP280_API();
sensor = new Adafruit_BMP280();
}
return *instance;
}

int32_t BMP280_API::altitude_cm() {
return sensor.readAltitude() * 100;
return sensor->readAltitude() * 100;
}

int32_t BMP280_API::pressure() {
return sensor.readPressure();
return sensor->readPressure();
}

int32_t BMP280_API::aglCM() {
Expand All @@ -33,13 +39,13 @@ int32_t BMP280_API::getGroundLevelCM() {
}

void BMP280_API::setup() {
if (!sensor.begin()) {
if (!sensor->begin()) {
while(1); //Freeze
}
}

void BMP280_API::calibrate() {
sensor.setSampling(
sensor->setSampling(
Adafruit_BMP280::MODE_NORMAL,
Adafruit_BMP280::SAMPLING_X16,
Adafruit_BMP280::SAMPLING_X16,
Expand Down
8 changes: 0 additions & 8 deletions libraries/bmp280_api.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#include "altimeter.h"
#include "Adafruit_BMP280.h"
#include <stdint.h>

class BMP280_API: public Altimeter {
static Adafruit_BMP280 sensor;

public:
static BMP280_API& getInstance();

Expand Down Expand Up @@ -101,11 +98,6 @@ class BMP280_API: public Altimeter {
* @author themicp
*/
BMP280_API();

/**
* @brief <h1>Reference ground level.</h1>
*/
int32_t ground_level = 0;

void setGroundLevel();
};
31 changes: 20 additions & 11 deletions libraries/bmp3xx_api.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "Adafruit_BMP3XX.h"
#include "bmp3xx_api.h"

#define BMP_SCK (13)
Expand All @@ -8,11 +9,19 @@
#define BMP_REFRESH_RATE 50 // Hz
#define MAX_ALTITUDE_STEP 100000 // cm

Adafruit_BMP3XX BMP3XX_API::sensor;
static BMP3XX_API* instance = nullptr;
static Adafruit_BMP3XX* sensor = nullptr;
static unsigned long last_sensor_read = 0;
static int32_t cached_altitude_cm;
static int32_t ground_level = 0;
static bool broken_connection;

BMP3XX_API& BMP3XX_API::getInstance() {
static BMP3XX_API instance;
return instance;
if (instance == nullptr) {
instance = new BMP3XX_API();
sensor = new Adafruit_BMP3XX();
}
return *instance;
}


Expand All @@ -28,7 +37,7 @@ void BMP3XX_API::readSensorData() {
return;
}

int32_t reading = sensor.readAltitude(SEALEVELPRESSURE_HPA) * 100;
int32_t reading = sensor->readAltitude(SEALEVELPRESSURE_HPA) * 100;
if (abs(reading - cached_altitude_cm) >= MAX_ALTITUDE_STEP) {
// Ignore reading if difference is extreme. Probably problem with the sensor.
return;
Expand All @@ -43,11 +52,11 @@ int32_t BMP3XX_API::altitude_cm() {
}

int32_t BMP3XX_API::pressure() {
return sensor.pressure;
return sensor->pressure;
}

int16_t BMP3XX_API::temperature() {
return sensor.temperature * 100;
return sensor->temperature * 100;
}

int32_t BMP3XX_API::aglCM() {
Expand All @@ -63,18 +72,18 @@ int32_t BMP3XX_API::getGroundLevelCM() {
}

void BMP3XX_API::setup() {
if (!sensor.begin()) {
if (!sensor->begin()) {
while(1); //Freeze
}
}

void BMP3XX_API::calibrate() {
sensor.setPressureOversampling(BMP3_OVERSAMPLING_16X);
sensor.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_7);
sensor.setOutputDataRate(BMP3_ODR_100_HZ);
sensor->setPressureOversampling(BMP3_OVERSAMPLING_16X);
sensor->setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_7);
sensor->setOutputDataRate(BMP3_ODR_100_HZ);

// TODO: use something better than a constant here
while (sensor.readAltitude(SEALEVELPRESSURE_HPA) > 1700) {
while (sensor->readAltitude(SEALEVELPRESSURE_HPA) > 1700) {
delay(100);
}
delay(1000); // TODO: refine the value. New BMP needed more time
Expand Down
Loading