Skip to content

Test/state machine #44

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
FIXED SPIFFS FILE ASYNCHRONOUS WRITES
  • Loading branch information
DennisY888 committed May 18, 2025
commit c274a9603acb6b30d9e76fcc1ec0fcfa66eb8848
10 changes: 2 additions & 8 deletions Avionics/Firmware/src/IMU_Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void configIMU(){
calibrateGyroAccel();
}


void calibrateGyroAccel() {
int num_samples = 400;
sensors_event_t gyro_event;
Expand All @@ -80,16 +81,9 @@ void calibrateGyroAccel() {
accel_x_offset /= num_samples;
accel_y_offset /= num_samples;
accel_z_offset /= num_samples;

// Serial.print("\nGyro offsets - X: "); Serial.print(gyro_x_offset);
// Serial.print(", Y: "); Serial.print(gyro_y_offset);
// Serial.print(", Z: "); Serial.println(gyro_z_offset);

// Serial.print("\nAccel offsets - X: "); Serial.print(accel_x_offset);
// Serial.print(", Y: "); Serial.print(accel_y_offset);
// Serial.print(", Z: "); Serial.println(accel_z_offset);
}


void setLowNoiseMode(){
imu.writeExternalRegister(ICM_ADDR, GYRO_CONFIG_1, 0x01);
imu.writeExternalRegister(ICM_ADDR, ACCEL_CONFIG, 0x01);
Expand Down
2 changes: 1 addition & 1 deletion Avionics/Firmware/src/Wifi_Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void remoteControl(void (*beginFlight)()) {
// Wait for encrypted command
Serial.println("Waiting for encrypted command...");
unsigned long cmdStartTime = millis();
while(!client.available() && millis() - cmdStartTime < 5000) { // 5-second timeout
while(!client.available() && millis() - cmdStartTime < 20000) { // 20-second timeout
delay(10);
}

Expand Down
106 changes: 74 additions & 32 deletions Avionics/Firmware/src/flightdata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ unsigned long startTime = 0;
FlightData currentData;
extern bool done; // Declared in Wifi_Control.cpp


FlightData::FlightData() {
acceleration = {0.0, 0.0, 0.0};
gyroscope = {0.0, 0.0, 0.0};
Expand All @@ -20,18 +21,22 @@ FlightData::FlightData() {
time = 0;
}


sensors_vec_t FlightData::getAccel() const {
return acceleration;
}


sensors_vec_t FlightData::getGyro() const {
return gyroscope;
}


sensors_vec_t FlightData::getMag() const {
return magnetic;
}


float FlightData::getTemp() const {
return temperature;
}
Expand Down Expand Up @@ -93,46 +98,71 @@ void FlightData::save_values() {


bool initialize_csv() {
Serial.println("Starting CSV initialization...");

file = SPIFFS.open("/data.csv", FILE_WRITE);
if (!file) {
Serial.println("Failed to open file for initializing. Formatting...");
SPIFFS.format();

if (!SPIFFS.begin()) {
Serial.println("Failed to mount SPIFFS during formatting.");
return false;
}
// Try to open the file again after formatting
file = SPIFFS.open("/data.csv", FILE_WRITE);

if (!file) {
Serial.println("Failed to open file for initializing. Terminating...");
return false;
}
Serial.println("Failed to open file for initializing. Formatting...");
SPIFFS.format();

if (!SPIFFS.begin()) {
Serial.println("Failed to mount SPIFFS during formatting.");
return false;
}

// Try to open the file again after formatting
file = SPIFFS.open("/data.csv", FILE_WRITE);

if (!file) {
Serial.println("Failed to open file for initializing. Terminating...");
return false;
}
}

Serial.println("Opened file for initializing");

bool headerWriteSuccess = true;
headerWriteSuccess &= file.print("Time (ms)") && file.print(",");
headerWriteSuccess &= file.print("Accel x (+/- 0.1 m/s^2)") && file.print(",");
headerWriteSuccess &= file.print("Accel y (+/- 0.1 m/s^2)") && file.print(",");
headerWriteSuccess &= file.print("Accel z (+/- 0.1 m/s^2)") && file.print(",");
headerWriteSuccess &= file.print("Gyro x (+/- 0.2 rad/s)") && file.print(",");
headerWriteSuccess &= file.print("Gyro y (+/- 0.2 rad/s)") && file.print(",");
headerWriteSuccess &= file.print("Gyro z (+/- 0.2 rad/s)") && file.print(",");
headerWriteSuccess &= file.print("Mag x (uT)") && file.print(",");
headerWriteSuccess &= file.print("Mag y (uT)") && file.print(",");
headerWriteSuccess &= file.print("Mag z (uT)") && file.print(",");
headerWriteSuccess &= file.print("Temp (C)") && file.print(",");
headerWriteSuccess &= file.println("Flight Phase");

if (!headerWriteSuccess) {
Serial.println("Failed to write CSV header");

// Test file with a small write first
if (file.println("CSV Header Test")) {
Serial.println("Test write successful");
} else {
Serial.println("ERROR: Test write failed");
file.close();
return false;
}

// Create header string first to avoid multiple writes
String header = "Time (ms),Accel x (+/- 0.1 m/s^2),Accel y (+/- 0.1 m/s^2),Accel z (+/- 0.1 m/s^2),";
header += "Gyro x (+/- 0.2 rad/s),Gyro y (+/- 0.2 rad/s),Gyro z (+/- 0.2 rad/s),";
header += "Mag x (uT),Mag y (uT),Mag z (uT),Temp (C),Flight Phase";

// Log header length
Serial.print("Header length: ");
Serial.print(header.length());
Serial.println(" bytes");

// Try to write the entire header at once
size_t bytesWritten = file.println(header);

if (bytesWritten == 0) {
Serial.println("Failed to write CSV header (0 bytes written)");
// Try to diagnose the issue
Serial.print("File size: ");
Serial.print(file.size());
Serial.println(" bytes");
Serial.print("File position: ");
Serial.print(file.position());
Serial.println(" bytes");
Serial.print("SPIFFS free space: ");
Serial.print(SPIFFS.totalBytes() - SPIFFS.usedBytes());
Serial.println(" bytes");

file.close();
return false;
}

Serial.print("Successfully wrote header (");
Serial.print(bytesWritten);
Serial.println(" bytes)");

file.close();
file = SPIFFS.open("/data.csv", FILE_APPEND);
Expand All @@ -142,6 +172,18 @@ bool initialize_csv() {
return false;
}

// Verify file is valid and writable
if (!file.size()) {
Serial.println("Warning: File size is 0 after reopening");
}

// Test append operation
if (!file.println("# Test append line")) {
Serial.println("Warning: Test append write failed");
file.close();
return false;
}

Serial.println("Opened file for writing");
return true;
}
Expand Down
15 changes: 15 additions & 0 deletions Avionics/Firmware/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,24 @@ void setup() {
if (!SPIFFS.begin()) {
Serial.println("Failed to mount SPIFFS. Formatting...");

// Add diagnostic info
Serial.print("Total SPIFFS space: ");
Serial.print(SPIFFS.totalBytes());
Serial.println(" bytes");
Serial.print("Used SPIFFS space: ");
Serial.print(SPIFFS.usedBytes());
Serial.println(" bytes");

// Format SPIFFS
if (SPIFFS.format()) {
Serial.println("SPIFFS formatted successfully.");
// Print space info after formatting
Serial.print("Post-format total space: ");
Serial.print(SPIFFS.totalBytes());
Serial.println(" bytes");
Serial.print("Post-format used space: ");
Serial.print(SPIFFS.usedBytes());
Serial.println(" bytes");
} else {
Serial.println("Failed to format SPIFFS. Check partition configuration.");
return; // Exit setup if formatting fails
Expand Down