Skip to content
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
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
IndentRequires: true
PenaltyReturnTypeOnItsOwnLine: 1000
Cpp11BracedListStyle: false
BreakBeforeBinaryOperators: NonAssignment
AlignOperands: AlignAfterOperator
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ cert-*,
-cert-dcl16-c,
cppcoreguidelines-*,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-pro-type-union-access,
modernize-*,
-cppcoreguidelines-avoid-non-const-global-variables,
-modernize-use-nodiscard,
Expand Down
25 changes: 18 additions & 7 deletions include/accelerometer.hpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
#ifndef OBC_ACCELERATION_HPP
#define OBC_ACCELERATION_HPP

#include <SparkFun_MMA8452Q.h>
#include <Adafruit_MPU6050.h>

#include "error.hpp"
#include "result.hpp"

namespace obc {

struct Acceleration {
short x;
short y;
short z;
float x;
float y;
float z;
};

Result<Unit, Errc> init(MMA8452Q& accelerometer);
Result<Acceleration, Errc> measure(MMA8452Q& accelerometer);
void print(Acceleration acclr);
struct Gyro {
float x;
float y;
float z;
};

struct AcclrMeasurements {
Acceleration acclr_measurements;
Gyro gyro_measurements;
};

Result<Unit, Errc> init(Adafruit_MPU6050& accelerometer);
Result<AcclrMeasurements, Errc> measure(Adafruit_MPU6050& accelerometer);
void print(AcclrMeasurements data);

} // namespace obc

Expand Down
4 changes: 2 additions & 2 deletions include/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace obc {

struct Packet {
Acceleration acclr_measurements;
AcclrMeasurements acclr_measurements;
BmpMeasurements bmp_measurements;
GpsTime time;
GpsDate date;
Expand Down Expand Up @@ -46,7 +46,7 @@ inline void log_error_and_panic(
}

void serialize_into(String &buf, const BmpMeasurements &data);
void serialize_into(String &buf, const Acceleration &data);
void serialize_into(String &buf, const AcclrMeasurements &data);
void serialize_into(String &buf, const Packet &data);
void serialize_into(String &buf, const GpsTime &data);
void serialize_into(String &buf, const GpsPosition &data);
Expand Down
8 changes: 4 additions & 4 deletions include/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ struct ResultBase {
bool is_ok_;

// NOLINTNEXTLINE(hicpp-explicit-conversions)
ResultBase(Ok<T>&& ok) : ok{std::move(ok)}, is_ok_{true} {}
ResultBase(Ok<T>&& ok) : ok{ std::move(ok) }, is_ok_{ true } {}

// NOLINTNEXTLINE(hicpp-explicit-conversions)
ResultBase(Err<E>&& err) : err{std::move(err)}, is_ok_{false} {}
ResultBase(Err<E>&& err) : err{ std::move(err) }, is_ok_{ false } {}

ResultBase(const ResultBase&) = default;
ResultBase& operator=(const ResultBase&) = default;
Expand All @@ -115,10 +115,10 @@ struct ResultBase<T, E, false> {
bool is_ok_;

// NOLINTNEXTLINE(hicpp-explicit-conversions)
ResultBase(Ok<T>&& ok) : ok{std::move(ok)}, is_ok_{true} {}
ResultBase(Ok<T>&& ok) : ok{ std::move(ok) }, is_ok_{ true } {}

// NOLINTNEXTLINE(hicpp-explicit-conversions)
ResultBase(Err<E>&& err) : err{std::move(err)}, is_ok_{false} {}
ResultBase(Err<E>&& err) : err{ std::move(err) }, is_ok_{ false } {}

ResultBase(const ResultBase&) = delete;
ResultBase& operator=(const ResultBase&) = delete;
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ build_flags = -std=gnu++17
build_src_flags = -Wall -Wextra -Wpedantic -Wconversion -Werror -fconcepts -Waddress-of-packed-member -Wdeprecated-declarations
extra_scripts = script/replace_inc_flag.py
pre:script/framework_unflags.py
#upload_protocol = dfu
upload_protocol = dfu
lib_deps =
adafruit/Adafruit GPS Library @ ^1.7.2
mahfuz195/BMP280 @ ^1.0.0
sparkfun/SparkFun_MMA8452Q @ ^1.4.0
adafruit/Adafruit MPU6050 @ ^2.2.4
arduino-libraries/SD @ ^1.2.4
agdl/Base64 @ ^1.0.0
check_tool = clangtidy
Expand Down
44 changes: 31 additions & 13 deletions src/accelerometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,47 @@

namespace obc {

Result<Unit, Errc> init(MMA8452Q& accelerometer)
Result<Unit, Errc> init(Adafruit_MPU6050& accelerometer)
{
Wire.begin();
if (not accelerometer.begin()) { return Err{Errc::Busy}; }
return Ok{Unit{}};
if (not accelerometer.begin()) { return Err{ Errc::Busy }; }
accelerometer.setAccelerometerRange(MPU6050_RANGE_8_G);
accelerometer.setGyroRange(MPU6050_RANGE_500_DEG);
accelerometer.setFilterBandwidth(MPU6050_BAND_21_HZ);
return Ok{ Unit{} };
}

Result<Acceleration, Errc> measure(MMA8452Q& accelerometer)
Result<AcclrMeasurements, Errc> measure(Adafruit_MPU6050& accelerometer)
{
if (accelerometer.available() == 0) { return Err{Errc::Busy}; }
return Ok{Acceleration{
accelerometer.getX(),
accelerometer.getY(),
accelerometer.getZ()}};
sensors_event_t acclr;
sensors_event_t gyro;
sensors_event_t temp;

if (not accelerometer.getEvent(&acclr, &gyro, &temp)) {
return Err{ Errc::Busy };
}

AcclrMeasurements measurements{
{ acclr.acceleration.x, acclr.acceleration.y, acclr.acceleration.z },
{ gyro.gyro.x, gyro.gyro.y, gyro.gyro.z }
};

return Ok{ measurements };
}

void print(Acceleration acclr)
void print(AcclrMeasurements data)
{
Serial.print(acclr.x);
Serial.print(data.gyro_measurements.x, 2);
Serial.print("\t");
Serial.print(data.gyro_measurements.y, 2);
Serial.print("\t");
Serial.print(data.gyro_measurements.z, 2);
Serial.print("\t");
Serial.print(data.acclr_measurements.x, 2);
Serial.print("\t");
Serial.print(acclr.y);
Serial.print(data.acclr_measurements.y, 2);
Serial.print("\t");
Serial.print(acclr.z);
Serial.print(data.acclr_measurements.z, 2);
Serial.println();
}

Expand Down
12 changes: 6 additions & 6 deletions src/barometer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ constexpr double ground_lvl_pressure = 1013.25;

Result<Unit, Errc> init(BMP280& bmp)
{
if (bmp.begin() == 0) { return Err{Errc::Busy}; }
if (bmp.begin() == 0) { return Err{ Errc::Busy }; }
bmp.setOversampling(4);
return Ok{Unit{}};
return Ok{ Unit{} };
}

Result<BmpMeasurements, Errc> measure(BMP280& bmp)
{
char result = bmp.startMeasurment();
BmpMeasurements temp = {0, 0, 0};
BmpMeasurements temp = { 0, 0, 0 };

if (result == 0) { return Err{Errc::Busy}; }
if (result == 0) { return Err{ Errc::Busy }; }
result = bmp.getTemperatureAndPressure(temp.temperature, temp.pressure);

if (result == 0) { return Err{Errc::Busy}; }
if (result == 0) { return Err{ Errc::Busy }; }
temp.altitude = bmp.altitude(temp.pressure, ground_lvl_pressure);

return Ok{temp};
return Ok{ temp };
}

void print(BmpMeasurements measurements)
Expand Down
37 changes: 19 additions & 18 deletions src/devices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <Arduino.h>

extern MMA8452Q accelerometer;
extern Adafruit_MPU6050 accelerometer;
extern BMP280 bmp;
extern Adafruit_GPS gps;

Expand All @@ -20,29 +20,30 @@ void init()
pinMode(custom_buzzer_pin, OUTPUT);
sd_init().expect("SD init failure");

if (auto result = init(accelerometer); result.is_err()) {
if (auto result = init_lora(); result.is_err()) {
log_error_and_panic(
String("Accelerometer not initialized properly, errc: ")
String("Lora not initialized properly, errc: ")
+ utl::to_underlying(result.unwrap_err()));
}
log_boot("Lora Init --- [OK]");

if (auto result = init(bmp); result.is_err()) {
log_error_and_panic(
String("Barometer not initialized properly, errc: ")
+ utl::to_underlying(result.unwrap_err()));
}
constexpr auto initialize_device = [&](auto& device, const String& name) {
if (auto result = init(device); result.is_err()) {
send_packet(String(name + " [INIT ERROR]"));
log_error_and_panic(
String(name + " not initialized properly, errc: ")
+ utl::to_underlying(result.unwrap_err()));
}
log_boot(String(name + " Init --- [OK]"));

if (auto result = init(gps); result.is_err()) {
log_error_and_panic(
String("GPS not initialized properly, errc: ")
+ utl::to_underlying(result.unwrap_err()));
}
send_packet(String(name + " [OK]"));

if (auto result = init_lora(); result.is_err()) {
log_error_and_panic(
String("Lora not initialized properly, errc: ")
+ utl::to_underlying(result.unwrap_err()));
}
delay(10000);
};

initialize_device(accelerometer, "Accelerometer");
initialize_device(bmp, "BMP");
initialize_device(gps, "GPS");

log_boot("Devices initialized properly.");
}
Expand Down
30 changes: 13 additions & 17 deletions src/gps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,57 +15,53 @@ constexpr std::array<const char, airborne_hex_array_size> airborne_mode_set = {
0x00, 0x00, 0x00, 0x10, 0x27, 0x00, 0x00, 0x05, 0x00, 0xfa, 0x00,
0xfa, 0x00, 0x64, 0x00, 0x2c, 0x01, 0x00, 0x3c, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x52, 0xe9,
0xb5, 0x62, 0x06, 0x24, 0x00, 0x00, 0x2a, 0x84};
0xb5, 0x62, 0x06, 0x24, 0x00, 0x00, 0x2a, 0x84
};

} // namespace

Result<Unit, Errc> init(Adafruit_GPS& gps)
{
if (not gps.begin(baud_rate)) { return Err{Errc::Busy}; }
if (not gps.begin(baud_rate)) { return Err{ Errc::Busy }; }
gps.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
gps.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);
gps.sendCommand(PGCMD_ANTENNA);
Serial2.println(PMTK_Q_RELEASE);
Serial2.write(airborne_mode_set.data(), airborne_mode_set.size());
Serial2.write("\0");

return Ok{Unit{}};
return Ok{ Unit{} };
}

Result<Unit, Errc> measure(Adafruit_GPS& gps)
{
gps.read();
if (gps.newNMEAreceived()) {
if (not gps.parse(gps.lastNMEA())) { return Err{Errc::Busy}; }
if (not gps.parse(gps.lastNMEA())) { return Err{ Errc::Busy }; }
}
return Ok{Unit{}};
return Ok{ Unit{} };
}

GpsDate read_date(Adafruit_GPS& gps)
{
return GpsDate{gps.year, gps.month, gps.day};
return GpsDate{ gps.year, gps.month, gps.day };
}

GpsTime read_time(Adafruit_GPS& gps)
{
return GpsTime{gps.hour, gps.minute, gps.seconds, gps.milliseconds};
return GpsTime{ gps.hour, gps.minute, gps.seconds, gps.milliseconds };
}

GpsPosition read_position(Adafruit_GPS& gps)
{
if (gps.fix) {
return GpsPosition{
gps.fix,
gps.fixquality,
gps.longitudeDegrees,
gps.lon,
gps.latitudeDegrees,
gps.lat,
gps.altitude,
gps.speed,
gps.satellites};
gps.fix, gps.fixquality, gps.longitudeDegrees,
gps.lon, gps.latitudeDegrees, gps.lat,
gps.altitude, gps.speed, gps.satellites
};
}
return GpsPosition{false, 0, 0, 0, 0, 0, 0, 0, 0};
return GpsPosition{ false, 0, 0, 0, 0, 0, 0, 0, 0 };
}

void print(GpsTime time)
Expand Down
20 changes: 13 additions & 7 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ void file_appendln(const char* file_name, const char* data)

Result<Unit, Errc> sd_init()
{
if (not SD.begin(sd_chip_select)) { return Err{Errc::Busy}; }
if (not SD.begin(sd_chip_select)) { return Err{ Errc::Busy }; }

init_flight_path_folder();
if (not SD.mkdir(flight_path_folder)) { return Err{Errc::Busy}; }
if (not SD.mkdir(flight_path_folder)) { return Err{ Errc::Busy }; }

log_boot("Booting time: " + String(millis()) + "ms");

Expand All @@ -63,7 +63,7 @@ Result<Unit, Errc> sd_init()

log_data(logs_legend);

return Ok{Unit{}};
return Ok{ Unit{} };
}

void log_boot(const char* msg)
Expand Down Expand Up @@ -157,13 +157,19 @@ void serialize_into(String& buf, const BmpMeasurements& data)
buf += "\t";
}

void serialize_into(String& buf, const Acceleration& data)
void serialize_into(String& buf, const AcclrMeasurements& data)
{
buf += data.x;
buf += data.gyro_measurements.x;
buf += "\t";
buf += data.y;
buf += data.gyro_measurements.y;
buf += "\t";
buf += data.z;
buf += data.gyro_measurements.z;
buf += "\t";
buf += data.acclr_measurements.x;
buf += "\t";
buf += data.acclr_measurements.x;
buf += "\t";
buf += data.acclr_measurements.x;
buf += "\t";
}

Expand Down
Loading