diff --git a/mainpage.txt b/mainpage.txt index ba08725..803c19c 100644 --- a/mainpage.txt +++ b/mainpage.txt @@ -83,7 +83,17 @@ * @section step14 Integration and Testing Procedures * @note In development - * @section step15 Contributors + * @section step15 Pre-flight software checks + * The following checks MUST be performed pre-flight to ensure the software operates as designed: + 1. test mode pin toggle + 2. telemetry protocol selection + 3. clientID (rocket ID) + 4. arming check + 5. DEBUG_to_terminal check - defs.h + 6. LOG_TO_MEMORY - defs.h + + + * @section step16 Contributors * Our thanks go to the following current and previous avionics team members for their contribution: 1. Edwin Mwiti 2. Junn Hope diff --git a/n4-flight-software/include/defs.h b/n4-flight-software/include/defs.h index 6007356..612b689 100644 --- a/n4-flight-software/include/defs.h +++ b/n4-flight-software/include/defs.h @@ -8,14 +8,14 @@ #define XBEE 1 // set to 1 if using XBEE for telemetry transfer #define GPS_BAUD_RATE 9600 -#define XBEE_BAUD_RATE // TODO: set to XBEE baud rate +#define XBEE_BAUD_RATE 9600 // TODO: set to XBEE baud rate /* debug parameters for use during testing - set to 0 for production */ -#define DEBUG 1 -#define LOG_TO_MEMORY 0 // set to 1 during live testing +#define DEBUGGING 1 +#define LOG_TO_MEMORY 0 // set to 1 during live testing #define DEBUG_TO_TERMINAL 1 // set to 0 for production -#if DEBUG +#if DEBUGGING #define debug(x) Serial.print(x) #define debugln(x) Serial.println(x) #define debugf(x, y) Serial.printf(x, y) diff --git a/n4-flight-software/src/custom-time.cpp b/n4-flight-software/src/custom-time.cpp index f4d9b5f..a191d96 100644 --- a/n4-flight-software/src/custom-time.cpp +++ b/n4-flight-software/src/custom-time.cpp @@ -11,7 +11,7 @@ char tstamp[50]; // to hold a timestamp int minute=0, sec=0, msec=0; /*!**************************************************************************** - * @brief convert time in millisecsonds to minutes, seconds and time that are human readable + * @brief convert time in milliseconds to minutes, seconds and time that are human readable * @param msec time in milliseconds, got from millis() function *******************************************************************************/ char* convertTimestamp(unsigned long msec) { diff --git a/n4-flight-software/src/main.cpp b/n4-flight-software/src/main.cpp index f5a857a..2d4e6bc 100644 --- a/n4-flight-software/src/main.cpp +++ b/n4-flight-software/src/main.cpp @@ -28,13 +28,15 @@ #include "data-types.h" #include "custom-time.h" #include "states.h" +#include "system_logger.h" +#include "system_log_levels.h" /* function prototypes definition */ void drogueChuteDeploy(); void mainChuteDeploy(); /* state machine variables*/ -uint8_t operation_mode = 0; /*!< Tells whether software is in safe or flight mode - FLIGHT_MODE=1, SAFE_MODE=0 */ +uint8_t operation_mode = 0; /*!< Tells whether software is in safe or flight mode - FLIGHT_MODE=1, SAFE_MODE=0 */ uint8_t current_state = FLIGHT_STATE::PRE_FLIGHT_GROUND; /*!< The starting state - we start at PRE_FLIGHT_GROUND state */ /* create Wi-Fi Client */ @@ -46,6 +48,12 @@ PubSubClient mqtt_client(wifi_client); /* GPS object */ TinyGPSPlus gps; +/* system logger */ +SystemLogger system_logger; +const char* system_log_file = "/sys_log.log"; +LOG_LEVEL level = INFO; +const char* rocket_ID = "rocket-1"; /*!< Unique ID of the rocket. Change to the needed rocket name before uploading */ + ////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////// FLIGHT COMPUTER TESTING SYSTEM ///////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////// @@ -1203,7 +1211,7 @@ void mainChuteDeploy() { /*!**************************************************************************** - * @brief Setup - perfom initialization of all hardware subsystems, create queues, create queue handles + * @brief Setup - perform initialization of all hardware subsystems, create queues, create queue handles * initialize system check table * *******************************************************************************/ @@ -1212,6 +1220,10 @@ void setup(){ Serial.begin(BAUDRATE); delay(100); + /* initialize the system logger */ + InitSPIFFS(); + system_logger.logToFile(SPIFFS, 0, rocket_ID, level, system_log_file, "Game Time!"); + debugln(); debugln(F("==============================================")); debugln(F("========= INITIALIZING PERIPHERALS ===========")); diff --git a/n4-flight-software/system-logger/SystemLogLevels.h b/n4-flight-software/src/system_log_levels.h similarity index 77% rename from n4-flight-software/system-logger/SystemLogLevels.h rename to n4-flight-software/src/system_log_levels.h index fa2dc6e..17745f8 100644 --- a/n4-flight-software/system-logger/SystemLogLevels.h +++ b/n4-flight-software/src/system_log_levels.h @@ -7,15 +7,15 @@ * */ -#ifndef LEVELS_H -#define LEVELS_H +#ifndef SYSTEM_LOG_LEVELS_H +#define SYSTEM_LOG_LEVELS_H -enum LOG_LEVEL { +typedef enum{ DEBUG = 0, INFO, WARNING, CRITICAL, ERROR -}; +} LOG_LEVEL; #endif diff --git a/n4-flight-software/src/system_logger.cpp b/n4-flight-software/src/system_logger.cpp new file mode 100644 index 0000000..1ca8cf7 --- /dev/null +++ b/n4-flight-software/src/system_logger.cpp @@ -0,0 +1,121 @@ +#include "system_logger.h" + +void SystemLogger::logToFile (fs::FS &fs, uint8_t mode, const char* client, uint8_t log_level, const char* file, const char* msg) { + char log_buffer[100]; + // get the timestamp + uint32_t timestamp = millis(); + + // construct the log message + // timestamp clientID log_level msg + sprintf(log_buffer, + "%d %s %s %s\n", + timestamp, + client, + this->getLogLevelString(log_level), + msg + ); + + // open the file in the specified mode + if(mode == 0) { + // clear the file contents + // this is used just before flight to make sure we do not have previous data + // on the log file + File f = fs.open(file, FILE_WRITE); + if(!f) { + Serial.println("Failed to open file "); + return; + } + + // log to file + if(f.print(log_buffer)) { + Serial.println("- file written"); + } else { + Serial.println("- write failed"); + } + + // close file + f.close(); + + } else if(mode == 1) { + File f = fs.open(file, FILE_APPEND); + if(!f) { + Serial.println("Failed to open file "); + return; + } + + // log to file + if(f.print(log_buffer)) { + Serial.println("- file written"); + } else { + Serial.println("- write failed"); + } + + // close file + f.close(); + + } +} + +/** + * @brief read log file to console + */ + void SystemLogger::readLogFile(fs::FS &fs, const char* file) { + Serial.printf("Reading file: %s\r\n", file); + File f = fs.open(file); + + if(!f || f.isDirectory()){ + Serial.println("- failed to open file for reading"); + return; + } + + Serial.println("- read from file:"); + while(f.available()){ + Serial.write(f.read()); + } + Serial.println(); + f.close(); + } + +/** + * + * @brief convert the log level to string + * + */ +const char* SystemLogger::getLogLevelString(uint8_t log_level) { + static const char* debug = "DEBUG"; + static const char* info = "INFO"; + static const char* warning = "WARNING"; + static const char* critical = "CRITICAL"; + static const char* error = "ERROR"; + static const char* unknown = "UNKNOWN"; + + switch (log_level) { + case 0: + return debug; + break; + + case 1: + return info; + break; + + case 2: + return warning; + break; + + case 3: + return critical; + break; + + case 4: + return error; + break; + + default: + return unknown; + break; + } + +} + + + diff --git a/n4-flight-software/src/system_logger.h b/n4-flight-software/src/system_logger.h new file mode 100644 index 0000000..c2ebe32 --- /dev/null +++ b/n4-flight-software/src/system_logger.h @@ -0,0 +1,16 @@ +#ifndef SYSTEMLOGGER_H +#define SYSTEMLOGGER_H + +#include +#include +#include + +class SystemLogger { + public: + const char* getLogLevelString(uint8_t log_level); + void logToConsole (const uint32_t timestamp, const char* client, uint8_t log_level, const char* msg); + void logToFile (fs::FS &fs, uint8_t mode, const char* client, uint8_t log_level, const char* file, const char* msg); + void readLogFile(fs::FS &fs, const char* file); +}; + +#endif diff --git a/n4-flight-software/system-logger/SystemLogger.cpp b/n4-flight-software/system-logger/SystemLogger.cpp deleted file mode 100644 index ab2a93a..0000000 --- a/n4-flight-software/system-logger/SystemLogger.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include "SystemLogger.h" - -/** - * - * @brief convert the log level to string - * - */ -const char* SystemLogger::getLogLevelString(uint8_t log_level) { - static const char* debug = "DEBUG"; - static const char* info = "INFO"; - static const char* warning = "WARNING"; - static const char* critical = "CRITICAL"; - static const char* error = "ERROR"; - static const char* unknown = "UNKNOWN"; - - switch (log_level) { - case 0: - return debug; - break; - - case 1: - return info; - break; - - case 2: - return warning; - break; - - case 3: - return critical; - break; - - case 4: - return error; - break; - - default: - return unknown; - break; - } - -} - - - diff --git a/n4-flight-software/system-logger/SystemLogger.h b/n4-flight-software/system-logger/SystemLogger.h deleted file mode 100644 index 091f0df..0000000 --- a/n4-flight-software/system-logger/SystemLogger.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef SYSTEMLOGGER_H -#define SYSTEMLOGGER_H - -#include - - -class SystemLogger { - public: - void writeToConsole (const uint32_t timestamp, const char* client, uint8_t log_level, const char* msg); - const char* getLogLevelString(uint8_t log_level); -}; - -#endif diff --git a/n4-flight-software/system-logger/logger-arduino/logger-arduino.ino b/n4-flight-software/system-logger/logger-arduino/logger-arduino.ino deleted file mode 100644 index 13d06c0..0000000 --- a/n4-flight-software/system-logger/logger-arduino/logger-arduino.ino +++ /dev/null @@ -1,28 +0,0 @@ - -char tstamp[50]; // to hold a timestamp -int minute=0, sec=0, msec=0; - -void getTimeStamp(unsigned long m) { - // convert time to mins, secs, msecs - - minute = ((m/1000)/60) % 60; - sec = (m/1000) % 60; - msec = m%1000; - - sprintf(tstamp, "%d:%d:%ul", minute, sec, msec); - -} - -void setup() { - Serial.begin(115200); - -} - -void loop() { - - unsigned long x = millis(); - getTimeStamp(x); - - Serial.println(tstamp); - -} diff --git a/n4-flight-software/system-logger/logger-console.cpp b/n4-flight-software/system-logger/logger-console.cpp deleted file mode 100644 index c399290..0000000 --- a/n4-flight-software/system-logger/logger-console.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include -#include -#include "SystemLogger.h" -#include "SystemLogLevels.h" - -// DRIVER CODE FOR CONSOLE TESTING - -// LOG MESSAGE STRUCTURE -// [TIMESTAMP]:[CLIENT_ID]:[LOG-LEVEL]:MESSAGE -// CLIENT_ID tells which ESP - flight computer(FC) or ground-station(GS) - this is an ID xtracted from the ESP's core -// TIMESTAMP STRUCTURE: [HOUR/MIN/SEC/DAY/MONTH/YR] - -class LoggerConsole : public SystemLogger { - public: - void writeToConsole(const time_t timestamp, const char* client, uint8_t log_level, const char* msg) { - char log_buffer[128]; - - // get verbose log levels - returns int converted to string to tell the log level - const char* log_level_str = getLogLevelString(log_level); - - // package the log message structure - // int chars = sprintf(log_buffer, "[%s]:[%s]:[%s]:%s\n", timestamp, client, log_level_str, msg); - sprintf(log_buffer, "[%s]:[%s]:[%s]",client, log_level_str, msg); - - // print to console - std::cout << log_buffer; - - } -}; - -int main() { - LoggerConsole syslogger; - - // fake message - timestamp, client_id, log_level, message - time_t timestamp = 19400; - const char* client = "123EDFE"; - uint8_t log_level = LOG_LEVEL::DEBUG; - const char* msg = "Testing 1..2"; - - std::cout<<"\n"; - - // call the logger - syslogger.writeToConsole(timestamp, client, log_level, msg); - - - - return 0; -}