Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added handle_mode() #10

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
Checks: '
-*,
bugprone-*,
-bugprone-easily-swappable-parameters
cert-*,
-cert-err58-cpp,
-cert-dcl16-c,
cppcoreguidelines-*,
-cppcoreguidelines-avoid-magic-numbers,
modernize-*,
-cppcoreguidelines-avoid-non-const-global-variables,
modernize-*,
-modernize-use-nodiscard,
-modernize-avoid-c-arrays,
-modernize-use-trailing-return-type,
Expand All @@ -24,5 +25,4 @@ hicpp-*,-hicpp-special-member-functions,
-hicpp-no-array-decay,
-hicpp-noexcept-move,
-hicpp-uppercase-literal-suffix,
-bugprone-easily-swappable-parameters
'
10 changes: 10 additions & 0 deletions include/utils.hpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
#ifndef OBC_UTILS_HPP
#define OBC_UTILS_HPP

#include <Arduino.h>

#include <type_traits>

namespace utl {

enum class Logger { Debug, Flight };

enum class Modes { Idle, Measurements, Experiment, Uplink };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Czym różni się Measurements od Experiment? Chyba czuje, różnice, ale czy to oznacza, że gdy odpalimy eksperyment to nie wykonujemy pomiarów z samego obc? (w sensie, czy mode Measurements pokrywa się jakoś z Experiment zadaniami).

Ogólnie to szczegół, przy takiej implementacji chyba łatwo będzie zedytować te stany/zdefiniować nowe. O to chodziło.


template <typename Enum>
constexpr auto to_underlying(Enum e)
{
return static_cast<std::underlying_type_t<Enum>>(e);
}

void handle_mode(Modes mode);
void handle_idle();
void handle_measurements();
void handle_experiment();
void handle_uplink();

} // namespace utl

#endif
6 changes: 4 additions & 2 deletions src/buzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

#include <Arduino.h>

constexpr auto buzzer_pin = LED_BUILTIN; // PC13

namespace obc {

void buzzer(std::size_t quantity)
{
for (std::size_t i = 0; i < quantity; ++i) {
digitalWrite(D6, 255);
digitalWrite(buzzer_pin, HIGH);
delay(100);
digitalWrite(D6, LOW);
digitalWrite(buzzer_pin, LOW);
delay(200);
}
}
Expand Down
17 changes: 13 additions & 4 deletions src/devices.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@

#include <Arduino.h>

extern MMA8452Q accelerometer;
extern BMP280 bmp;
extern Adafruit_GPS gps;
HardwareSerial Serial2{PA3, PA2};
Adafruit_GPS gps{&Serial2};
MMA8452Q accelerometer;
BMP280 bmp;

constexpr auto custom_sda = PB9;
constexpr auto custom_scl = PB8;
constexpr auto custom_buzzer_pin = LED_BUILTIN; // PC13

namespace obc {

void init()
{
pinMode(D6, OUTPUT);
Wire.setSDA(custom_sda);
Wire.setSCL(custom_scl);

pinMode(custom_buzzer_pin, OUTPUT);
sd_init().expect("SD init failure");

if (auto result = init(accelerometer); result.is_err()) {
Expand Down Expand Up @@ -38,6 +46,7 @@ void init()
}

log_boot("Devices initialized properly.");
obc::buzzer(5);
}

} // namespace obc
2 changes: 1 addition & 1 deletion src/lora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void send_packet(const String &packet)
{
auto encoded_logs = encode(packet);
String encoded = String("AT+MSG=") + "\"" + encoded_logs + "\"";
Serial5.print(encoded);
Serial5.println(encoded);
}

} // namespace obc
19 changes: 11 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#include <Arduino.h>
#include <IWatchdog.h>

#include "accelerometer.hpp"
#include "barometer.hpp"
#include "gps.hpp"
#include "logger.hpp"
#include "devices.hpp"
#include "utils.hpp"

HardwareSerial Serial2{PA3, PA2};
constexpr auto baud_rate = 9600l;
constexpr auto watchdog_interval = 10000000;

Adafruit_GPS gps{&Serial2};
auto current_mode = utl::Modes::Measurements;

void setup()
{
// put your setup code here, to run once:
Serial.begin(baud_rate);
IWatchdog.begin(watchdog_interval);
obc::init();
}

void loop()
{
// put your main code here, to run repeatedly:
utl::handle_mode(current_mode);
IWatchdog.reload();
}
83 changes: 83 additions & 0 deletions src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "utils.hpp"

#include "accelerometer.hpp"
#include "barometer.hpp"
#include "buzzer.hpp"
#include "devices.hpp"
#include "gps.hpp"
#include "logger.hpp"
#include "lora.hpp"

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

namespace utl {

namespace {
constexpr auto logs_interval = 2000;
constexpr auto lora_interval = 60000;
constexpr std::size_t buzzer_ind_fix_not_fetched = 1;

uint32_t timer = millis();
uint32_t lora_timer = millis();

bool is_date_appended = false;

} // namespace

void handle_mode(Modes mode)
{
switch (mode) {
case Modes::Idle:
return handle_idle();
case Modes::Measurements:
return handle_measurements();
case Modes::Experiment:
return handle_experiment();
case Modes::Uplink:
return handle_uplink();
default:
break;
}
}

void handle_idle() {}

void handle_measurements()
{
if (obc::measure(gps).is_ok() and millis() - timer > logs_interval) {
obc::Packet logs = {{}, {}, {}, {}, {}};
const auto acclr = obc::measure(accelerometer);
const auto bmp_measurements = obc::measure(bmp);
logs.time = obc::read_time(gps);
logs.position = obc::read_position(gps);

if (acclr.is_ok()) { logs.acclr_measurements = acclr.unwrap(); }
if (bmp_measurements.is_ok()) {
logs.bmp_measurements = bmp_measurements.unwrap();
}

if (not is_date_appended) {
obc::log_boot(obc::serialize(obc::read_date(gps)));
is_date_appended = true;
}

obc::log_data(obc::serialize(logs));

if (millis() - lora_timer > lora_interval) {
obc::send_packet(obc::lora_packet(logs));
lora_timer = millis();
}

if (not logs.position.fix) { obc::buzzer(buzzer_ind_fix_not_fetched); }

timer = millis();
}
}

void handle_experiment() {}

void handle_uplink() {}

} // namespace utl