Skip to content

Commit

Permalink
Initial implementation of downlink windows
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanjma committed Mar 27, 2024
1 parent a4be3e0 commit e77302a
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 45 deletions.
65 changes: 35 additions & 30 deletions src/ControlTasks/RadioControlTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,24 @@ void RadioControlTask::execute()
#endif
init();
if (sfr::radio::init_mode == sensor_init_mode_type::complete) {
sfr::radio::mode = radio_mode_type::waiting;
sfr::radio::mode = radio_mode_type::downlink;
sfr::radio::listen_period_start = millis();
sfr::radio::downlink_period_start = millis();
downlinkSettings();
}
break;
}
case radio_mode_type::waiting: {
case radio_mode_type::downlink: {
#ifdef VERBOSE
Serial.println(F("Radio: Waiting State"));
Serial.println(F("Radio: Downlink State"));
#endif
if (millis() - sfr::radio::listen_period_start >= sfr::radio::listen_period) {
// downlink when slot reached
if (!sfr::radio::downlinked_in_slot && millis() - sfr::radio::downlink_window_start >= constants::radio::transmit_slot_length * sfr::radio::downlink_slot) {
executeDownlink();
sfr::radio::downlinked_in_slot = true;
}

// go into listen mode if listen period reached
if (millis() - sfr::radio::listen_period_start >= constants::radio::listen_period) {
sfr::radio::mode = radio_mode_type::listen;
sfr::radio::command_wait_start = millis();

Expand All @@ -223,20 +230,10 @@ void RadioControlTask::execute()
#endif
// downlink with listen flag = true
normalReportDownlink();
} else if (millis() - sfr::radio::downlink_period_start >= sfr::radio::downlink_period) {
sfr::radio::mode = radio_mode_type::downlink;
}
break;
}
case radio_mode_type::downlink: {
#ifdef VERBOSE
Serial.println(F("Radio: Downlink State"));
#endif
bool transmit_success = executeDownlink();
sfr::radio::mode = radio_mode_type::waiting;
// reset downlink period if downlink successful
if (transmit_success) {
sfr::radio::downlink_period_start = millis();
// reset window and choose slot for next downlink
else if (millis() - sfr::radio::downlink_window_start >= sfr::radio::downlink_window_length) {
downlinkSettings();
}
break;
}
Expand All @@ -247,27 +244,34 @@ void RadioControlTask::execute()
// built in timeout is 100 LoRa symbols
bool receive_success = receive();
if (receive_success) {
#ifdef VERBOSE
Serial.println(F("Receive Success"));
#endif
processUplink();
} else {
#ifdef VERBOSE
Serial.println(F("Receive Failed"));
#endif
}

if (receive_success || millis() - sfr::radio::command_wait_start >= sfr::radio::command_wait_period) {
sfr::radio::mode = radio_mode_type::waiting;
if (receive_success || millis() - sfr::radio::command_wait_start >= constants::radio::command_wait_period) {
sfr::radio::mode = radio_mode_type::downlink;
sfr::radio::listen_period_start = millis();
downlinkSettings();
}
break;
}
}
}

void RadioControlTask::downlinkSettings()
{
#ifdef VERBOSE
Serial.println(F("Downlink Window Start"));
#endif
// Reset window start time and pick random slot
sfr::radio::downlink_window_start = millis();
int num_slots = (sfr::radio::downlink_window_length / constants::radio::transmit_slot_length) - 1;
sfr::radio::downlink_slot = random(0, num_slots + 1); // max is exclusive
sfr::radio::downlinked_in_slot = false;
}

bool RadioControlTask::executeDownlink()
{
// Downlink callsign report once in a while
if (millis() - sfr::radio::last_callsign_time < constants::radio::callsign_interval) {
return normalReportDownlink();
} else {
Expand All @@ -283,6 +287,7 @@ bool RadioControlTask::executeDownlink()

bool RadioControlTask::normalReportDownlink()
{
// see https://github.com/Alpha-CubeSat/oop-chipsat-code/wiki/2.-Telemetry for more info
uint16_t lat = sfr::gps::latitude * 10;
uint16_t lon = sfr::gps::longitude * 10;
uint16_t alt = sfr::gps::altitude / 10;
Expand Down Expand Up @@ -328,17 +333,17 @@ uint8_t RadioControlTask::map_range(float value, int min_val, int max_val)

void RadioControlTask::processUplink()
{

// see https://github.com/Alpha-CubeSat/oop-chipsat-code/wiki/3.-Commands for more info
switch (received[0]) {
case constants::opcodes::no_op: // No-Op
#ifdef VERBOSE
Serial.println(F("No-Op Command"));
#endif
sfr::radio::valid_uplinks++;
break;
case constants::opcodes::change_downlink_period: { // Change downlink period
case constants::opcodes::change_downlink_window: { // Change downlink window length
uint16_t combined = ((uint16_t)received[1] << 8) | received[2];
sfr::radio::downlink_period = combined * constants::time::one_second;
sfr::radio::downlink_window_length = combined * constants::time::one_second;
sfr::radio::valid_uplinks++;
#ifdef VERBOSE
Serial.print(F("Change Downlink Command: "));
Expand Down
1 change: 1 addition & 0 deletions src/ControlTasks/RadioControlTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class RadioControlTask
constants::radio::radio_rst_pin, constants::radio::radio_busy_pin);
int16_t code;
void init();
void downlinkSettings();
bool transmit(uint8_t *packet, uint8_t size);
bool receive();
bool executeDownlink();
Expand Down
3 changes: 3 additions & 0 deletions src/MainControlLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ void MainControlLoop::execute()
Serial.print("UTC Time: ");
Serial.println(sfr::gps::utc_time);

Serial.print("Downlink Slot: ");
Serial.println(sfr::radio::downlink_slot);

#endif

temp_monitor.execute();
Expand Down
3 changes: 0 additions & 3 deletions src/MainControlLoop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ class MainControlLoop
public:
MainControlLoop();
void execute();

private:
float val;
};

#endif
1 change: 0 additions & 1 deletion src/Modes/radio_mode_type.enum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
enum class radio_mode_type : unsigned char
{
init = 0,
waiting,
downlink,
listen
};
Expand Down
4 changes: 2 additions & 2 deletions src/Monitors/GPSMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GPSMonitor::GPSMonitor()
{
}

void GPSMonitor::init()
void GPSMonitor::init()
{
digitalWrite(constants::gps::reset_pin, LOW);
sfr::gps::on = true;
Expand Down Expand Up @@ -159,7 +159,7 @@ void GPSMonitor::execute()
init();
} else {
return;
}
}
}

// Check to see if anything is available in the serial receive buffer
Expand Down
6 changes: 5 additions & 1 deletion src/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ namespace constants {
constexpr uint8_t id = 0;
#endif

constexpr uint32_t listen_period = 30 * constants::time::one_second;
constexpr uint32_t command_wait_period = 30 * constants::time::one_second;
constexpr uint32_t callsign_interval = 10 * constants::time::one_minute;

constexpr uint32_t transmit_slot_length = 2 * constants::time::one_second;

} // namespace radio
namespace imu {
constexpr int gyro_min = -245;
Expand Down Expand Up @@ -58,7 +62,7 @@ namespace constants {
} // namespace gps
namespace opcodes {
constexpr uint8_t no_op = 0x00;
constexpr uint8_t change_downlink_period = 0x11;
constexpr uint8_t change_downlink_window = 0x11;
} // namespace opcodes

}; // namespace constants
Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ void setup()
pinMode(constants::gps::reset_pin, OUTPUT);
digitalWrite(constants::gps::reset_pin, HIGH);
sfr::gps::boot_time = millis();

// seed random number gen with noise from unconnected analog pin
randomSeed(analogRead(A0));
}

void loop()
Expand Down
9 changes: 5 additions & 4 deletions src/sfr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ namespace sfr {
radio_mode_type mode = radio_mode_type::init;
uint8_t start_progress = 0;

uint32_t downlink_period = 10 * constants::time::one_second;
uint32_t listen_period = 30 * constants::time::one_second;
uint32_t command_wait_period = 30 * constants::time::one_second;
uint32_t downlink_period_start;
uint32_t downlink_window_length = 10 * constants::time::one_second;
uint32_t downlink_window_start;
uint32_t listen_period_start;
uint32_t command_wait_start;
uint32_t last_callsign_time;

uint8_t downlink_slot;
bool downlinked_in_slot = false;

uint8_t valid_uplinks = 0;
uint8_t invalid_uplinks = 0;

Expand Down
9 changes: 5 additions & 4 deletions src/sfr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ namespace sfr {
extern uint8_t start_progress;
extern radio_mode_type mode;

extern uint32_t downlink_period;
extern uint32_t listen_period;
extern uint32_t command_wait_period;
extern uint32_t downlink_period_start;
extern uint32_t downlink_window_length;
extern uint32_t downlink_window_start;
extern uint32_t listen_period_start;
extern uint32_t command_wait_start;
extern uint32_t last_callsign_time;

extern uint8_t downlink_slot;
extern bool downlinked_in_slot;

extern uint8_t valid_uplinks;
extern uint8_t invalid_uplinks;

Expand Down

0 comments on commit e77302a

Please sign in to comment.