From 664809beacc6a3ac44c34511cfb3c60ec2c98e4c Mon Sep 17 00:00:00 2001 From: rvbnsk <58656716+rvbnsk@users.noreply.github.com> Date: Tue, 13 Jun 2023 20:27:03 +0200 Subject: [PATCH 1/2] Changed Accelerometer to MPU6050 --- include/accelerometer.hpp | 6 +++--- platformio.ini | 2 +- src/accelerometer.cpp | 10 ++++++---- src/devices.cpp | 37 +++++++++++++++++++------------------ src/main.cpp | 4 ++-- 5 files changed, 31 insertions(+), 28 deletions(-) diff --git a/include/accelerometer.hpp b/include/accelerometer.hpp index 379abf4..ba72874 100644 --- a/include/accelerometer.hpp +++ b/include/accelerometer.hpp @@ -1,7 +1,7 @@ #ifndef OBC_ACCELERATION_HPP #define OBC_ACCELERATION_HPP -#include +#include #include "error.hpp" #include "result.hpp" @@ -14,8 +14,8 @@ struct Acceleration { short z; }; -Result init(MMA8452Q& accelerometer); -Result measure(MMA8452Q& accelerometer); +Result init(Adafruit_MPU6050& accelerometer); +Result measure(Adafruit_MPU6050& accelerometer); void print(Acceleration acclr); } // namespace obc diff --git a/platformio.ini b/platformio.ini index 7969cb5..33d0843 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ extra_scripts = script/replace_inc_flag.py 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 diff --git a/src/accelerometer.cpp b/src/accelerometer.cpp index a889263..20b8c56 100644 --- a/src/accelerometer.cpp +++ b/src/accelerometer.cpp @@ -4,20 +4,22 @@ namespace obc { -Result init(MMA8452Q& accelerometer) +Result init(Adafruit_MPU6050& accelerometer) { Wire.begin(); if (not accelerometer.begin()) { return Err{Errc::Busy}; } return Ok{Unit{}}; } -Result measure(MMA8452Q& accelerometer) +Result measure(Adafruit_MPU6050& accelerometer) { - if (accelerometer.available() == 0) { return Err{Errc::Busy}; } + accelerometer.getClock(); + /*if (accelerometer.available() == 0) { return Err{Errc::Busy}; } return Ok{Acceleration{ accelerometer.getX(), accelerometer.getY(), - accelerometer.getZ()}}; + accelerometer.getZ()}};*/ + return Ok{Acceleration{0, 0, 0}}; } void print(Acceleration acclr) diff --git a/src/devices.cpp b/src/devices.cpp index 3a2f147..f142bf2 100644 --- a/src/devices.cpp +++ b/src/devices.cpp @@ -2,7 +2,7 @@ #include -extern MMA8452Q accelerometer; +extern Adafruit_MPU6050 accelerometer; extern BMP280 bmp; extern Adafruit_GPS gps; @@ -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."); } diff --git a/src/main.cpp b/src/main.cpp index 71b4992..0839daf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ bool is_date_appended = false; HardwareSerial Serial2{PA3, PA2}; Adafruit_GPS gps{&Serial2}; -MMA8452Q accelerometer; +Adafruit_MPU6050 accelerometer; BMP280 bmp; uint32_t timer = millis(); @@ -48,7 +48,7 @@ void loop() logs.bmp_measurements = bmp_measurements.unwrap(); } - if (not is_date_appended) { + if (not is_date_appended and logs.position.fix) { obc::log_boot(obc::serialize(obc::read_date(gps))); is_date_appended = true; } From e0827b85c6d5a92923be339cdff09cf72d6d18e7 Mon Sep 17 00:00:00 2001 From: rvbnsk <58656716+rvbnsk@users.noreply.github.com> Date: Fri, 16 Jun 2023 18:45:07 +0200 Subject: [PATCH 2/2] Added measuring gyro and acclr data --- .clang-format | 1 + .clang-tidy | 1 + include/accelerometer.hpp | 21 ++++++++++++++----- include/logger.hpp | 4 ++-- include/result.hpp | 8 +++---- platformio.ini | 2 +- src/accelerometer.cpp | 44 ++++++++++++++++++++++++++------------- src/barometer.cpp | 12 +++++------ src/gps.cpp | 30 ++++++++++++-------------- src/logger.cpp | 20 +++++++++++------- src/lora.cpp | 11 ++++------ src/main.cpp | 6 +++--- 12 files changed, 94 insertions(+), 66 deletions(-) diff --git a/.clang-format b/.clang-format index e36b38c..8ef6e0b 100644 --- a/.clang-format +++ b/.clang-format @@ -13,5 +13,6 @@ AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None IndentRequires: true PenaltyReturnTypeOnItsOwnLine: 1000 +Cpp11BracedListStyle: false BreakBeforeBinaryOperators: NonAssignment AlignOperands: AlignAfterOperator \ No newline at end of file diff --git a/.clang-tidy b/.clang-tidy index e6f8d5b..23eeaa1 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -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, diff --git a/include/accelerometer.hpp b/include/accelerometer.hpp index ba72874..4dd9a96 100644 --- a/include/accelerometer.hpp +++ b/include/accelerometer.hpp @@ -9,14 +9,25 @@ namespace obc { struct Acceleration { - short x; - short y; - short z; + float x; + float y; + float z; +}; + +struct Gyro { + float x; + float y; + float z; +}; + +struct AcclrMeasurements { + Acceleration acclr_measurements; + Gyro gyro_measurements; }; Result init(Adafruit_MPU6050& accelerometer); -Result measure(Adafruit_MPU6050& accelerometer); -void print(Acceleration acclr); +Result measure(Adafruit_MPU6050& accelerometer); +void print(AcclrMeasurements data); } // namespace obc diff --git a/include/logger.hpp b/include/logger.hpp index b55e460..7c4d4b8 100644 --- a/include/logger.hpp +++ b/include/logger.hpp @@ -16,7 +16,7 @@ namespace obc { struct Packet { - Acceleration acclr_measurements; + AcclrMeasurements acclr_measurements; BmpMeasurements bmp_measurements; GpsTime time; GpsDate date; @@ -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); diff --git a/include/result.hpp b/include/result.hpp index 927cfab..1cbce84 100644 --- a/include/result.hpp +++ b/include/result.hpp @@ -92,10 +92,10 @@ struct ResultBase { bool is_ok_; // NOLINTNEXTLINE(hicpp-explicit-conversions) - ResultBase(Ok&& ok) : ok{std::move(ok)}, is_ok_{true} {} + ResultBase(Ok&& ok) : ok{ std::move(ok) }, is_ok_{ true } {} // NOLINTNEXTLINE(hicpp-explicit-conversions) - ResultBase(Err&& err) : err{std::move(err)}, is_ok_{false} {} + ResultBase(Err&& err) : err{ std::move(err) }, is_ok_{ false } {} ResultBase(const ResultBase&) = default; ResultBase& operator=(const ResultBase&) = default; @@ -115,10 +115,10 @@ struct ResultBase { bool is_ok_; // NOLINTNEXTLINE(hicpp-explicit-conversions) - ResultBase(Ok&& ok) : ok{std::move(ok)}, is_ok_{true} {} + ResultBase(Ok&& ok) : ok{ std::move(ok) }, is_ok_{ true } {} // NOLINTNEXTLINE(hicpp-explicit-conversions) - ResultBase(Err&& err) : err{std::move(err)}, is_ok_{false} {} + ResultBase(Err&& err) : err{ std::move(err) }, is_ok_{ false } {} ResultBase(const ResultBase&) = delete; ResultBase& operator=(const ResultBase&) = delete; diff --git a/platformio.ini b/platformio.ini index 33d0843..d304f51 100644 --- a/platformio.ini +++ b/platformio.ini @@ -7,7 +7,7 @@ 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 diff --git a/src/accelerometer.cpp b/src/accelerometer.cpp index 20b8c56..5750e61 100644 --- a/src/accelerometer.cpp +++ b/src/accelerometer.cpp @@ -7,28 +7,44 @@ namespace obc { Result 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 measure(Adafruit_MPU6050& accelerometer) +Result measure(Adafruit_MPU6050& accelerometer) { - accelerometer.getClock(); - /*if (accelerometer.available() == 0) { return Err{Errc::Busy}; } - return Ok{Acceleration{ - accelerometer.getX(), - accelerometer.getY(), - accelerometer.getZ()}};*/ - return Ok{Acceleration{0, 0, 0}}; + 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(); } diff --git a/src/barometer.cpp b/src/barometer.cpp index ccab571..44c8b6c 100644 --- a/src/barometer.cpp +++ b/src/barometer.cpp @@ -12,23 +12,23 @@ constexpr double ground_lvl_pressure = 1013.25; Result 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 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) diff --git a/src/gps.cpp b/src/gps.cpp index 7cafa1f..0148b18 100644 --- a/src/gps.cpp +++ b/src/gps.cpp @@ -15,13 +15,14 @@ constexpr std::array 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 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); @@ -29,43 +30,38 @@ Result init(Adafruit_GPS& gps) Serial2.write(airborne_mode_set.data(), airborne_mode_set.size()); Serial2.write("\0"); - return Ok{Unit{}}; + return Ok{ Unit{} }; } Result 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) diff --git a/src/logger.cpp b/src/logger.cpp index 973fc2b..09f2baf 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -49,10 +49,10 @@ void file_appendln(const char* file_name, const char* data) Result 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"); @@ -63,7 +63,7 @@ Result sd_init() log_data(logs_legend); - return Ok{Unit{}}; + return Ok{ Unit{} }; } void log_boot(const char* msg) @@ -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"; } diff --git a/src/lora.cpp b/src/lora.cpp index aead319..3ed80c6 100644 --- a/src/lora.cpp +++ b/src/lora.cpp @@ -21,20 +21,17 @@ Result init_lora() // NOLINTNEXTLINE(cppcoreguidelines-init-variables) constexpr std::array commands{ - "AT+DR=EU868", - "AT+CH=NUM,0-2", - "AT+MODE=LWOTAA", - "AT+JOIN", - "AT+UART=TIMEOUT,2000", + "AT+DR=EU868", "AT+CH=NUM,0-2", "AT+MODE=LWOTAA", + "AT+JOIN", "AT+UART=TIMEOUT,2000", }; for (const auto &command : commands) { Serial5.println(command); delay(10000); String payload = Serial5.readString(); - if (payload.startsWith("ERR")) { return Err{Errc::Busy}; } + if (payload.startsWith("ERR")) { return Err{ Errc::Busy }; } } - return Ok{Unit{}}; + return Ok{ Unit{} }; } String lora_packet(const Packet &data) diff --git a/src/main.cpp b/src/main.cpp index 0839daf..6dae22e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,8 +18,8 @@ constexpr std::size_t buzzer_ind_fix_not_fetched = 1; bool is_date_appended = false; -HardwareSerial Serial2{PA3, PA2}; -Adafruit_GPS gps{&Serial2}; +HardwareSerial Serial2{ PA3, PA2 }; +Adafruit_GPS gps{ &Serial2 }; Adafruit_MPU6050 accelerometer; BMP280 bmp; @@ -37,7 +37,7 @@ void setup() void loop() { if (obc::measure(gps).is_ok() and millis() - timer > logs_interval) { - obc::Packet logs = {{}, {}, {}, {}, {}}; + obc::Packet logs = { {}, {}, {}, {}, {} }; const auto acclr = obc::measure(accelerometer); const auto bmp_measurements = obc::measure(bmp); logs.time = obc::read_time(gps);