Skip to content

Commit f6b067a

Browse files
committed
Refactor and modularize code: separate configuration, data logging, and sensor handling into distinct header and source files; update documentation and improve readability.
1 parent 08ee203 commit f6b067a

File tree

6 files changed

+491
-361
lines changed

6 files changed

+491
-361
lines changed

include/config.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#ifndef CONFIG_H
2+
#define CONFIG_H
3+
4+
#include <DFRobot_WT61PC.h>
5+
6+
// ============================================================================
7+
// DEBUG CONFIGURATION
8+
// ============================================================================
9+
10+
constexpr bool DEBUG_ENABLED = false; // Set to true for serial debugging
11+
12+
// ============================================================================
13+
// PIN DEFINITIONS
14+
// ============================================================================
15+
16+
namespace Pins {
17+
constexpr int SD_CHIP_SELECT = 17; // SD card CS pin
18+
constexpr int IMU_RX = 10; // IMU RXD connection
19+
constexpr int IMU_TX = 11; // IMU TXD connection
20+
constexpr int TEMPERATURE = A3; // LM35DT temperature sensor
21+
constexpr int PRESSURE = A2; // MPX4115A pressure sensor
22+
constexpr int BUZZER = 13; // Landing detection buzzer
23+
} // namespace Pins
24+
25+
// ============================================================================
26+
// SYSTEM CONFIGURATION
27+
// ============================================================================
28+
29+
namespace Config {
30+
constexpr unsigned long BUZZER_ARM_TIME = 15000; // Time to arm buzzer (ms)
31+
constexpr unsigned long LOOP_DELAY = 200; // Main loop delay (ms)
32+
constexpr unsigned long LANDING_CONFIRM_TIME =
33+
2000; // Landing confirmation time (ms)
34+
constexpr unsigned long IMU_TIMEOUT = 3000; // IMU initialization timeout (ms)
35+
constexpr char DATA_FILENAME[] = "flight_data.txt"; // SD card data file
36+
constexpr float LANDING_ACC_MIN =
37+
9.0; // Minimum acceleration for landing (m/s²)
38+
constexpr float LANDING_ACC_MAX =
39+
12.0; // Maximum acceleration for landing (m/s²)
40+
constexpr int IMU_FREQUENCY = FREQUENCY_5HZ; // Data output frequency
41+
} // namespace Config
42+
43+
// ============================================================================
44+
// SENSOR CALIBRATION CONSTANTS
45+
// ============================================================================
46+
47+
namespace Calibration {
48+
constexpr float TEMP_VOLTAGE_REF =
49+
5.0; // Reference voltage for temperature sensor
50+
constexpr float TEMP_SCALE_FACTOR =
51+
100.0; // LM35DT conversion factor: 100°C per volt (from 10mV/°C)
52+
constexpr float PRESSURE_OFFSET = 0.095; // MPX4115A offset
53+
constexpr float PRESSURE_SCALE = 0.000009; // MPX4115A scale factor
54+
constexpr float PRESSURE_MIN = 15000.0; // MPX4115A minimum valid pressure (Pa)
55+
constexpr float PRESSURE_MAX =
56+
115000.0; // MPX4115A maximum valid pressure (Pa)
57+
constexpr float ALTITUDE_EXPONENT = 1.0 / 5.257; // Barometric formula exponent
58+
constexpr float REFERENCE_TEMP_K =
59+
15.0 + 273.15; // Reference temperature in Kelvin
60+
} // namespace Calibration
61+
62+
#endif // CONFIG_H

include/data_logger.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef DATA_LOGGER_H
2+
#define DATA_LOGGER_H
3+
4+
#include <SD.h>
5+
6+
// ============================================================================
7+
// GLOBAL FILE OBJECT
8+
// ============================================================================
9+
10+
extern File dataFile; // SD card file handle for data logging
11+
12+
// ============================================================================
13+
// FUNCTION DECLARATIONS
14+
// ============================================================================
15+
16+
/**
17+
* @brief Initialize SD card and create data file with headers
18+
* @return true if successful, false otherwise
19+
*/
20+
bool initializeDataLogging();
21+
22+
/**
23+
* @brief Write sensor data to SD card in CSV format
24+
*/
25+
void logDataToSD();
26+
27+
/**
28+
* @brief Print sensor data to serial monitor for debugging
29+
*/
30+
void printDebugData();
31+
32+
#endif // DATA_LOGGER_H

include/sensors.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#ifndef SENSORS_H
2+
#define SENSORS_H
3+
4+
#include <Arduino.h>
5+
#include <DFRobot_WT61PC.h>
6+
#include <SoftwareSerial.h>
7+
8+
// ============================================================================
9+
// SENSOR DATA STRUCTURES
10+
// ============================================================================
11+
12+
/**
13+
* @brief Structure to hold all sensor readings
14+
*/
15+
struct SensorReadings {
16+
unsigned long timestamp = 0; // System uptime in milliseconds
17+
18+
// IMU Data
19+
float accelX = 0.0; // X-axis acceleration (m/s²)
20+
float accelY = 0.0; // Y-axis acceleration (m/s²)
21+
float accelZ = 0.0; // Z-axis acceleration (m/s²)
22+
float accelMagnitude = 0.0; // Total acceleration magnitude (m/s²)
23+
float gyroX = 0.0; // X-axis angular velocity (°/s)
24+
float gyroY = 0.0; // Y-axis angular velocity (°/s)
25+
float gyroZ = 0.0; // Z-axis angular velocity (°/s)
26+
27+
// Environmental Data
28+
float temperature = 0.0; // Temperature (°C)
29+
float pressure = 0.0; // Atmospheric pressure (Pa)
30+
float altitude = 0.0; // Calculated altitude (m)
31+
};
32+
33+
/**
34+
* @brief Structure to hold system state information
35+
*/
36+
struct SystemState {
37+
float referencePressure =
38+
0.0; // Ground level pressure for altitude calculation (Pa)
39+
bool sdCardReady = false; // SD card initialization status
40+
bool buzzerArmed = false; // Landing detection buzzer status
41+
};
42+
43+
// ============================================================================
44+
// GLOBAL SENSOR OBJECTS
45+
// ============================================================================
46+
47+
extern SoftwareSerial imuSerial; // Serial connection to IMU
48+
extern DFRobot_WT61PC imuSensor; // IMU sensor object
49+
extern SensorReadings sensors; // Current sensor readings
50+
extern SystemState systemState; // Current system state
51+
52+
// ============================================================================
53+
// FUNCTION DECLARATIONS
54+
// ============================================================================
55+
56+
/**
57+
* @brief Read temperature from LM35DT sensor
58+
* @param pin Analog pin connected to temperature sensor
59+
* @return Temperature in Celsius
60+
*/
61+
float readTemperature(int pin);
62+
63+
/**
64+
* @brief Read pressure from MPX4115A sensor
65+
* @param pin Analog pin connected to pressure sensor
66+
* @return Pressure in Pascals
67+
*/
68+
float readPressure(int pin);
69+
70+
/**
71+
* @brief Calculate altitude from pressure differential
72+
* @param currentPressure Current atmospheric pressure (Pa)
73+
* @param referencePressure Ground level pressure (Pa)
74+
* @return Altitude in meters
75+
*/
76+
float calculateAltitude(float currentPressure, float referencePressure);
77+
78+
/**
79+
* @brief Update all sensor readings
80+
*/
81+
void updateSensorReadings();
82+
83+
/**
84+
* @brief Check for landing condition and control buzzer
85+
*/
86+
void checkLandingCondition();
87+
88+
/**
89+
* @brief Initialize IMU sensor
90+
* @return true if successful, false otherwise
91+
*/
92+
bool initializeIMU();
93+
94+
/**
95+
* @brief Initialize reference pressure for altitude calculation
96+
*/
97+
void initializeReferencePressure();
98+
99+
#endif // SENSORS_H

src/data_logger.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include "data_logger.h"
2+
3+
#include <SD.h>
4+
5+
#include "config.h"
6+
#include "sensors.h"
7+
8+
// Global file object (actual definition)
9+
File dataFile;
10+
11+
/**
12+
* @brief Initialize SD card and create data file with headers
13+
* @return true if successful, false otherwise
14+
*/
15+
bool initializeDataLogging() {
16+
pinMode(Pins::SD_CHIP_SELECT, OUTPUT);
17+
18+
if (!SD.begin(Pins::SD_CHIP_SELECT)) {
19+
if (DEBUG_ENABLED) {
20+
Serial.println(F("ERROR: SD card initialization failed!"));
21+
}
22+
return false;
23+
}
24+
25+
// Create/open data file and write headers
26+
dataFile = SD.open(Config::DATA_FILENAME, FILE_WRITE);
27+
if (dataFile) {
28+
dataFile.println();
29+
dataFile.println(F("=== ROCKET FLIGHT DATA LOG ==="));
30+
dataFile.print(F("Timestamp(ms),Pressure(Pa),Temperature(C),Altitude(m),"));
31+
dataFile.print(F("AccelX(m/s²),AccelY(m/s²),AccelZ(m/s²),AccelMag(m/s²),"));
32+
dataFile.println(F("GyroX(°/s),GyroY(°/s),GyroZ(°/s)"));
33+
dataFile.close();
34+
35+
if (DEBUG_ENABLED) {
36+
Serial.println(F("✓ Data logging initialized"));
37+
}
38+
return true;
39+
}
40+
41+
return false;
42+
}
43+
44+
/**
45+
* @brief Write sensor data to SD card
46+
*/
47+
void logDataToSD() {
48+
if (!systemState.sdCardReady) return;
49+
50+
dataFile = SD.open(Config::DATA_FILENAME, FILE_WRITE);
51+
if (dataFile) {
52+
// Write data in CSV format
53+
dataFile.print(sensors.timestamp);
54+
dataFile.print(",");
55+
dataFile.print(sensors.pressure, 2);
56+
dataFile.print(",");
57+
dataFile.print(sensors.temperature, 2);
58+
dataFile.print(",");
59+
dataFile.print(sensors.altitude, 2);
60+
dataFile.print(",");
61+
dataFile.print(sensors.accelX, 3);
62+
dataFile.print(",");
63+
dataFile.print(sensors.accelY, 3);
64+
dataFile.print(",");
65+
dataFile.print(sensors.accelZ, 3);
66+
dataFile.print(",");
67+
dataFile.print(sensors.accelMagnitude, 3);
68+
dataFile.print(",");
69+
dataFile.print(sensors.gyroX, 3);
70+
dataFile.print(",");
71+
dataFile.print(sensors.gyroY, 3);
72+
dataFile.print(",");
73+
dataFile.println(sensors.gyroZ, 3);
74+
dataFile.close();
75+
} else if (DEBUG_ENABLED) {
76+
Serial.println(F("ERROR: Failed to write to SD card"));
77+
}
78+
}
79+
80+
/**
81+
* @brief Print sensor data to serial monitor for debugging
82+
*/
83+
void printDebugData() {
84+
if (!DEBUG_ENABLED) return;
85+
86+
Serial.print(F("Time: "));
87+
Serial.print(sensors.timestamp);
88+
Serial.print(F(" | P: "));
89+
Serial.print(sensors.pressure, 1);
90+
Serial.print(F(" | T: "));
91+
Serial.print(sensors.temperature, 1);
92+
Serial.print(F(" | Alt: "));
93+
Serial.print(sensors.altitude, 1);
94+
Serial.print(F(" | Acc: ["));
95+
Serial.print(sensors.accelX, 2);
96+
Serial.print(F(", "));
97+
Serial.print(sensors.accelY, 2);
98+
Serial.print(F(", "));
99+
Serial.print(sensors.accelZ, 2);
100+
Serial.print(F("] |Mag: "));
101+
Serial.print(sensors.accelMagnitude, 2);
102+
Serial.print(F(" | Gyro: ["));
103+
Serial.print(sensors.gyroX, 1);
104+
Serial.print(F(", "));
105+
Serial.print(sensors.gyroY, 1);
106+
Serial.print(F(", "));
107+
Serial.print(sensors.gyroZ, 1);
108+
Serial.println(F("]"));
109+
}

0 commit comments

Comments
 (0)