Skip to content

Commit

Permalink
file received on DUT
Browse files Browse the repository at this point in the history
  • Loading branch information
bytecod3 committed Nov 9, 2024
1 parent 2c73609 commit 352e900
Showing 1 changed file with 69 additions and 27 deletions.
96 changes: 69 additions & 27 deletions n4-flight-software/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ 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 /////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -83,6 +82,7 @@ int16_t serial_index = 0;

// buffer to store the CSV test data
char test_data_buffer[MAX_CSV_LENGTH];
char data_buffer_formatted[MAX_CSV_LENGTH];
int16_t test_data_serial_index = 0;

// pin definitions
Expand Down Expand Up @@ -252,7 +252,7 @@ void initSD() {
}

// initialize test data file
File file = SD.open("/data.csv", FILE_WRITE); // TODO: change file name to const char*
File file = SD.open("/data.txt", FILE_WRITE); // TODO: change file name to const char*

if (!file) {
debugln("[File does not exist. Creating file]");
Expand Down Expand Up @@ -410,6 +410,7 @@ void ParseSerialBuffer(char *buffer) {
current_test_state = TEST_STATE::RECEIVE_TEST_DATA;
SwitchLEDs(0, 1);


} else {
debugln("Unknown");
}
Expand All @@ -424,7 +425,7 @@ void ParseSerialNumeric(int value) {
debug("Receive val: ");
debugln(value);

if (value == 1) // SOH: numeric 1
if (value == 1) // SOH: numeric 1 -> ready to receive data
{
debugln("<Start of transmission>");
SOH_recvd_flag = 1;
Expand All @@ -446,7 +447,7 @@ void ParseSerialNumeric(int value) {
*******************************************************************************/
void handshakeSerialEvent() {
SwitchLEDs(1, 0);
while (Serial.available()) {
if (Serial.available()) {
char ch = Serial.read();

if (isDigit(ch)) { // character between 0 an 9
Expand Down Expand Up @@ -480,33 +481,32 @@ void handshakeSerialEvent() {
*
*******************************************************************************/
void receiveTestDataSerialEvent() {
while (Serial.available()) {
// initialize test data file

// File file = SD.open("/data.txt", FILE_APPEND); // TODO: change file name to const char*

if (Serial.available()) {
char ch = Serial.read();
Serial.write(ch);
// Serial.write(ch);

// each CSV string ends with a newline
if (test_data_serial_index < MAX_CSV_LENGTH && (ch != '\n')) {
if (ch != '\n') { // TODO: check for string length
test_data_buffer[test_data_serial_index++] = ch;

} else {
// buffer is full or newline is received
// newline is received

debugln(test_data_buffer);
test_data_buffer[test_data_serial_index] = 0; // NUL terminator
test_data_serial_index = 0;

// HERE - LOG THE CSV STRING TO EXTERNAL FLASH MEMORY
//debugln(test_data_buffer);
// open file in append mode
File data_file = SPIFFS.open(test_data_file, "a");
if (data_file) {
data_file.print(test_data_buffer);
data_file.println(); // start a new line
data_file.close();
} else {
debugln("<Failed to write to file>");
}
}

sprintf(data_buffer_formatted, "%s\n", test_data_buffer);
appendFile(SD, "/data.txt", (const char*) data_buffer_formatted);

test_data_serial_index = 0;
}
}

// file.close(); // close the file after receiving the test data is completed
}

/**
Expand All @@ -516,6 +516,17 @@ void receiveTestDataSerialEvent() {
* confirm the actual data
*/
void prepareForDataReceive() {

// switch (current_test_state) {
// case TEST_STATE::HANDSHAKE:
// debugln("HANDSHAKE");
// break;

// case TEST_STATE::RECEIVE_TEST_DATA:
// debugln("RECEIVE_TEST_DATA");
// break;
// }

if(TEST_MODE) {
// we are in test mode
switch (current_test_state)
Expand All @@ -535,6 +546,7 @@ void prepareForDataReceive() {

// this state receives data sent from the transmitting PC
case TEST_STATE::RECEIVE_TEST_DATA:
// debugln("RECEIVE_TEST_DATA");
receiveTestDataSerialEvent();
break;

Expand All @@ -544,7 +556,8 @@ void prepareForDataReceive() {
readTestDataFile();
break;
}
}
} // end of test mode

}


Expand Down Expand Up @@ -1261,7 +1274,22 @@ void MQTTInit(const char* broker_IP, int broker_port) {
*
*******************************************************************************/
void drogueChuteDeploy() {
debugln("DROGUE CHUTE DEPLOYED");

// // check for drogue chute deploy conditions

// //if the drogue deploy pin is HIGH, there is an error
// if(digitalRead(DROGUE_PIN)) {
// // error
// } else {
// // pulse the drogue pin for a number ofseceonds - determined from pop tests
// digitalWrite(DROGUE_PIN, HIGH);
// delay(PYRO_CHARGE_TIME); // TODO- Make this delay non-blocking

// // update the drogue deployed telemetry variable
// DROGUE_DEPLOY_FLAG = 1;
// debugln("DROGUE CHUTE DEPLOYED");
// }

}

/*!****************************************************************************
Expand All @@ -1272,7 +1300,20 @@ void drogueChuteDeploy() {
*
*******************************************************************************/
void mainChuteDeploy() {
debugln("MAIN CHUTE DEPLOYED");
// // check for drogue chute deploy conditions

// //if the drogue deploy pin is HIGH, there is an error
// if(digitalRead(MAIN_CHUTE_EJECT_PIN)) { // NOT NECESSARY - TEST WITHOUT
// // error
// } else {
// // pulse the drogue pin for a number of seconds - determined from pop tests
// digitalWrite(MAIN_CHUTE_EJECT_PIN, HIGH);
// delay(MAIN_DESCENT_PYRO_CHARGE_TIME); // TODO- Make this delay non-blocking

// // update the drogue deployed telemetry variable
// MAIN_CHUTE_EJECT_FLAG = 1;
// debugln("MAIN CHUTE DEPLOYED");
// }
}


Expand All @@ -1291,6 +1332,7 @@ void setup(){
//////////////////////////////////////////////////////////////////////////////////////////////

// check whether we are in test mode or running mode
initSD();
initTestGPIO();
checkRunTestToggle();

Expand Down Expand Up @@ -1345,7 +1387,7 @@ void setup(){
debugln(F("=============================================="));
debugln(F("===== INITIALIZING DATA LOGGING SYSTEM ======="));
debugln(F("=============================================="));
initSD();

data_logger.loggerInit();

uint8_t app_id = xPortGetCoreID();
Expand Down Expand Up @@ -1567,7 +1609,7 @@ void loop(){
prepareForDataReceive();

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// END OFFLIGHT COMPUTER TESTING SYSTEM ////////////////////////////
//////////////////////////// END OF FLIGHT COMPUTER TESTING SYSTEM ////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////


Expand Down

0 comments on commit 352e900

Please sign in to comment.