Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
cameron-goddard committed Dec 24, 2024
1 parent 742b267 commit 4242e18
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 60 deletions.
36 changes: 18 additions & 18 deletions src/ControlTasks/RadioControlTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ void RadioControlTask::init()
#ifdef VERBOSE
Serial.println(F("Radio: Initializing ... "));
#endif
// initialize SX1278 with default settings
// Initialize SX1278 with default settings
code = radio.begin(constants::radio::freq, constants::radio::bw, constants::radio::sf, constants::radio::cr,
constants::radio::sw, constants::radio::pwr, constants::radio::pl, constants::radio::gn);
if (code == RADIOLIB_ERR_NONE) {
Expand All @@ -28,7 +28,7 @@ void RadioControlTask::init()
#ifdef VERBOSE
Serial.println(F("Radio: Setting CRC parameter ... "));
#endif
// set CRC parameter to true so it matches the CRC parameter on the TinyGS side
// Set CRC parameter to true so it matches the CRC parameter on the TinyGS side
code = radio.setCRC(true);
if (code == RADIOLIB_ERR_NONE) {
sfr::radio::start_progress++;
Expand All @@ -43,7 +43,7 @@ void RadioControlTask::init()
#ifdef VERBOSE
Serial.println(F("Radio: Setting forceLDRO parameter ... "));
#endif
// set forceLDRO parameter to true so it matches the forceLDRO parameter on the TinyGS side
// Set forceLDRO parameter to true so it matches the forceLDRO parameter on the TinyGS side
code = radio.forceLDRO(true);
if (code == RADIOLIB_ERR_NONE) {
sfr::radio::initialized = true;
Expand All @@ -60,7 +60,7 @@ void RadioControlTask::init()

bool RadioControlTask::transmit(uint8_t *packet, uint8_t size)
{
// blink LED during transmit
// Blink LED during transmit
if (millis() - sfr::gps::boot_time > constants::led::led_on_time) {
digitalWrite(constants::led::led_pin, HIGH);
}
Expand All @@ -82,7 +82,7 @@ bool RadioControlTask::transmit(uint8_t *packet, uint8_t size)
#endif

if (code == RADIOLIB_ERR_NONE) {
// the packet was successfully transmitted
// The packet was successfully transmitted
#ifdef VERBOSE
Serial.println(F("success!"));
Serial.print(F("[SX1278] Datarate:\t"));
Expand All @@ -93,10 +93,10 @@ bool RadioControlTask::transmit(uint8_t *packet, uint8_t size)
} else {
#ifdef VERBOSE
if (code == RADIOLIB_ERR_TX_TIMEOUT) {
// timeout occurred while transmitting packet
// Timeout occurred while transmitting packet
Serial.println(F("timeout!"));
} else {
// some other error occurred
// Some other error occurred
Serial.print(F("failed, code "));
Serial.println(code);
}
Expand Down Expand Up @@ -131,13 +131,13 @@ bool RadioControlTask::receive()
} else {
#ifdef VERBOSE
if (code == RADIOLIB_ERR_RX_TIMEOUT) {
// timeout occurred while waiting for a packet
// Timeout occurred while waiting for a packet
Serial.println(F("timeout!"));
} else if (code == RADIOLIB_ERR_CRC_MISMATCH) {
// packet was received, but is malformed
// Packet was received, but is malformed
Serial.println(F("CRC error!"));
} else {
// some other error occurred
// Some other error occurred
Serial.print(F("failed, code "));
Serial.println(code);
}
Expand All @@ -148,7 +148,7 @@ bool RadioControlTask::receive()

void RadioControlTask::execute()
{
// implements the state machine described in: https://github.com/Alpha-CubeSat/oop-chipsat-code/wiki
// Implement the state machine described in https://github.com/Alpha-CubeSat/oop-chipsat-code/wiki
switch (sfr::radio::mode) {
case radio_mode_type::init: {
#ifdef VERBOSE
Expand Down Expand Up @@ -177,24 +177,24 @@ void RadioControlTask::execute()
#ifdef VERBOSE
Serial.println(F("Radio: Downlink State"));
#endif
// downlink when slot reached
// 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
// 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;

#ifdef VERBOSE
Serial.println(F("Radio: Listen Flag Downlink"));
#endif
// downlink with listen flag = true
// Downlink with listen flag = true
normalReportDownlink();
sfr::radio::command_wait_start = millis();
}
// reset window and choose slot for next downlink
// Reset window and choose slot for next downlink
else if (millis() - sfr::radio::downlink_window_start >= sfr::radio::downlink_window_length) {
downlinkSettings();
}
Expand All @@ -204,7 +204,7 @@ void RadioControlTask::execute()
#ifdef VERBOSE
Serial.println(F("Radio: Listen State"));
#endif
// built in timeout is 100 LoRa symbols
// Built in timeout is 100 LoRa symbols
bool receive_success = receive();
if (receive_success) {
processUplink();
Expand Down Expand Up @@ -250,7 +250,7 @@ bool RadioControlTask::executeDownlink()

bool RadioControlTask::normalReportDownlink()
{
// see https://github.com/Alpha-CubeSat/oop-chipsat-code/wiki/2.-Telemetry for more info
// See https://github.com/Alpha-CubeSat/oop-chipsat-code/wiki/2.-Telemetry for more info
uint16_t lat = sfr::gps::latitude * 100;
uint16_t lon = sfr::gps::longitude * 100;
uint16_t alt = sfr::gps::altitude / 10;
Expand Down Expand Up @@ -300,7 +300,7 @@ 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
// 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
Expand Down
2 changes: 1 addition & 1 deletion src/ControlTasks/RadioControlTask.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class RadioControlTask
void init();

/**
* Resets the transmit window start time and picks a new slot
* @brief Resets the transmit window start time and picks a new slot
*/
void downlinkSettings();

Expand Down
2 changes: 1 addition & 1 deletion src/MainControlLoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void MainControlLoop::execute()
gps_monitor.execute();
radio_control_task.execute();

// turn off LED 5 seconds after boot
// Turn off LED 5 seconds after boot
if (millis() - sfr::gps::boot_time > constants::led::led_on_time) {
digitalWrite(constants::led::led_pin, LOW);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Monitors/GPSMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ void GPSMonitor::init()
digitalWrite(constants::gps::reset_pin, LOW);
sfr::gps::on = true;

// writes NMEA output message type to SRAM and Flash
// Writes NMEA output message type to SRAM and Flash
uint8_t SetNMEA[] = {
0xA0, 0xA1, 0x00, 0x03, 0x09, 0x01, 0x01, 0x09, 0x0D, 0x0A};

Expand All @@ -19,7 +19,7 @@ void GPSMonitor::init()

void GPSMonitor::execute()
{
// turn on GPS after boot mode is over (30 seconds)
// Turn on GPS after boot mode is over (10 seconds)
if (!sfr::gps::on) {
if (millis() - sfr::gps::boot_time > constants::gps::boot_time) {
init();
Expand All @@ -35,7 +35,7 @@ void GPSMonitor::execute()
serial_reads++;
}

// read GPS values if they are valid
// Read GPS values if they are valid
if (gps.location.isUpdated() && gps.location.isValid()) {
sfr::gps::latitude = gps.location.lat();
sfr::gps::longitude = gps.location.lng();
Expand Down
2 changes: 1 addition & 1 deletion src/Monitors/GPSMonitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GPSMonitor
SoftwareSerial ss = SoftwareSerial(constants::gps::rx_pin, constants::gps::tx_pin);

/**
* Begins the software serial instance and configures the GPS receiver
* @brief Begins the software serial instance and configures the GPS receiver
*/
void init();
};
Expand Down
9 changes: 4 additions & 5 deletions src/Monitors/IMUMonitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,32 @@ void IMUMonitor::execute()
} else {
sfr::imu::initialized = true;
}

} else {
capture_imu_values();
}
}

void IMUMonitor::capture_imu_values()
{
// check if the gyroscope, accelerometer, or magnetometer has new data available
// Check if the gyroscope, accelerometer, or magnetometer has new data available
if (IMU.gyroscopeAvailable()) {
IMU.readGyroscope(
sfr::imu::gyro_x,
sfr::imu::gyro_y,
sfr::imu::gyro_z); // data is in degrees/s
sfr::imu::gyro_z); // Data is in degrees/s
}

if (IMU.accelerationAvailable()) {
IMU.readAcceleration(
sfr::imu::acc_x,
sfr::imu::acc_y,
sfr::imu::acc_z); // data is in m/s^2
sfr::imu::acc_z); // Data is in m/s^2
}

if (IMU.magneticFieldAvailable()) {
IMU.readMagneticField(
sfr::imu::mag_x,
sfr::imu::mag_y,
sfr::imu::mag_z); // data in uT
sfr::imu::mag_z); // Data in uT
}
}
30 changes: 14 additions & 16 deletions src/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ namespace constants {
constexpr int radio_di0_pin = 2;
constexpr int radio_rst_pin = 5;
constexpr int radio_busy_pin = 16;
// TODO: Document units
constexpr float freq = 437.4;
constexpr float bw = 62.5;
constexpr int sf = 10;
constexpr int cr = 5;
constexpr int sw = 18;
constexpr int pwr = 20;
constexpr int pl = 8;
constexpr int gn = 0;

constexpr float freq = 437.4; // MHz
constexpr float bw = 62.5; // kHz
constexpr int sf = 10; // Between 7 and 12
constexpr int cr = 5; // Between 5 and 8. 4/8 coding ratio. One redundancy bit for every data bit
constexpr int sw = 18; // Sync-word (defines network). Default is 0d18
constexpr int pwr = 20; // Between 2 and 17, or 20 for max power
constexpr int pl = 8; // Payload length
constexpr int gn = 0; // Gain

constexpr uint8_t max_alive_signal_dlinks = 3;

Expand All @@ -35,14 +35,14 @@ namespace constants {
constexpr uint32_t callsign_interval = 10 * constants::time::one_minute;

constexpr uint32_t transmit_slot_length = 700; // ms
// TODO: Verify sensor data on alive signal
// TODO: Verify sensor data on alive signal
} // namespace radio
namespace imu {
constexpr int gyro_min = -245; // default gyro range is +/- 245 dps
constexpr int gyro_min = -245; // Default gyro range is +/- 245 dps
constexpr int gyro_max = 245;
constexpr int acc_min = -20; // default accel range is +/- 2 G
constexpr int acc_min = -20; // Default accel range is +/- 2 G
constexpr int acc_max = 20;
constexpr int mag_min = -100; // default mag range is +/- 4 guass -> 100 uT
constexpr int mag_min = -100; // Default mag range is +/- 4 guass -> 100 uT
constexpr int mag_max = 100;
} // namespace imu
namespace temperature {
Expand All @@ -63,12 +63,10 @@ namespace constants {
constexpr uint8_t no_op = 0x00;
constexpr uint8_t change_downlink_window = 0x11;
} // namespace opcodes

namespace led {
constexpr int led_pin = 9;
constexpr uint32_t led_on_time = 5 * constants::time::one_second;
} // namespace led

}; // namespace constants
}; // namespace constants

#endif
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ MainControlLoop mcl;

void setup()
{
// configure watchdog timer
// Set up watchdog timer
wdt_disable();
wdt_enable(WDTO_8S);
#ifdef VERBOSE
Expand All @@ -14,19 +14,19 @@ void setup()

sfr::gps::boot_time = millis();

// sets ChipSat LED high for debugging
// Set ChipSat LED high for debugging
pinMode(constants::led::led_pin, OUTPUT);
digitalWrite(constants::led::led_pin, HIGH);

// cuts off power to GPS
// Cut off power to GPS
pinMode(constants::gps::reset_pin, OUTPUT);
digitalWrite(constants::gps::reset_pin, HIGH);

// prevents current from leaking over GPS serial line (~79mA -> ~22mA)
// Prevents current from leaking over GPS serial line (~79mA -> ~22mA)
pinMode(constants::gps::tx_pin, OUTPUT);
digitalWrite(constants::gps::tx_pin, LOW);

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

Expand Down
9 changes: 2 additions & 7 deletions src/sfr.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#include "sfr.hpp"

namespace sfr {

namespace imu {
bool initialized = false;

float gyro_x, gyro_y, gyro_z;
float acc_x, acc_y, acc_z;
float mag_x, mag_y, mag_z;

bool initialized = false;

} // namespace imu

namespace temperature {
float temp_c;
} // namespace temperature
Expand All @@ -31,7 +28,6 @@ namespace sfr {
uint8_t alive_signal_dlinks = 0;

// This must be a multiple of constants::radio::transmit_slot_length
// Ideally window should be 10x the slot length
uint32_t downlink_window_length = 3500; // ms
uint32_t downlink_window_start;
uint32_t listen_period_start;
Expand All @@ -45,5 +41,4 @@ namespace sfr {
uint8_t invalid_uplinks = 0;

} // namespace radio

}; // namespace sfr
}; // namespace sfr
6 changes: 3 additions & 3 deletions src/sfr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace sfr {
namespace imu {
extern bool initialized;

extern float gyro_x;
extern float gyro_y;
extern float gyro_z;
Expand All @@ -19,7 +21,6 @@ namespace sfr {
extern float mag_y;
extern float mag_z;

extern bool initialized;
} // namespace imu
namespace temperature {
extern float temp_c;
Expand Down Expand Up @@ -56,7 +57,6 @@ namespace sfr {

extern uint32_t boot_time;
} // namespace gps

}; // namespace sfr
}; // namespace sfr

#endif

0 comments on commit 4242e18

Please sign in to comment.