diff --git a/DAQ_FW/platformio.ini b/DAQ_FW/platformio.ini index 68b7132..6569c5d 100644 --- a/DAQ_FW/platformio.ini +++ b/DAQ_FW/platformio.ini @@ -18,4 +18,4 @@ lib_deps = SPI adafruit/Adafruit LSM6DS@^4.7.3 adafruit/Adafruit Unified Sensor@^1.1.14 -build_flags = -I include +build_flags = -I include \ No newline at end of file diff --git a/DAQ_FW/src/display_coolant.cpp b/DAQ_FW/src/display_coolant.cpp new file mode 100644 index 0000000..4adbd27 --- /dev/null +++ b/DAQ_FW/src/display_coolant.cpp @@ -0,0 +1,14 @@ +// #include +// #include +// #include + +// #define SD_CS_PIN 5 + +// void setup() { +// // Initialize Serial +// Serial.begin(9600); +// while (!Serial) { // Wait for Serial to be ready +// delay(100); +// } +// } + diff --git a/DAQ_FW/src/main.cpp b/DAQ_FW/src/main.cpp index e460409..29ab447 100644 --- a/DAQ_FW/src/main.cpp +++ b/DAQ_FW/src/main.cpp @@ -3,12 +3,20 @@ #include "wheelSpeed.h" #include #include + +// #include +#include #include "stdlib.h" -// MAX_TEMP = 4 write in define -// MIN_TEMP = 2 -// MIN_PRESSURE = 1.3 -// MAX_PRESSURE = 1.7 +// NTP server and timezone +const char *ntpServer = "pool.ntp.org"; +const long gmtOffset_sec = -28800; // GMT-8 (Pacific Standard Time) +const int daylightOffset_sec = 3600; // Adjust for daylight savings (1h right now) + +#define MAX_TEMP 4 +#define MIN_TEMP 2 +#define MIN_PRESSURE 1.3 +#define MAX_PRESSURE 1.7 #define FAULT_MSG_ID 0x100 #define WHEEL_MSG_ID 0x200 @@ -17,10 +25,19 @@ #define CAN_TX GPIO_NUM_21 #define CAN_RX GPIO_NUM_22 +// ADJUST LATER for actual SD card pin +#define SD_CS_PIN 5 + +bool faultDetected = false; + +// Default values for testing +float pressure = 1.5; +float temp = 3.0; + // object declarations can't be done in setup() // params: sensorname, sensorID, adcAddress -ChildExample SensorTest("TestSensor", 1, ADCAddress::U1); +//ChildExample SensorTest("TestSensor", 1, ADCAddress::U1); // cooloant pressure sensor object decleration //CoolantPressureSensor CoolantPressure("CoolantPressureSensor", 2, ADCAddress::U1); @@ -28,24 +45,31 @@ ChildExample SensorTest("TestSensor", 1, ADCAddress::U1); // coolant tempature sensor object decleration //CoolantTemperatureSensor CoolantTemperature("CoolantTemperature Sensor", 2, ADCAddress::U1); -bool faultDetected = false; - - -void sendFaultSignal(bool faultDetected) { - twai_message_t faultMessage; - faultMessage.identifier = FAULT_MSG_ID; - faultMessage.extd = 0; // Standard ID - faultMessage.data_length_code = 1; // 1 byte of data - faultMessage.data[0] = faultDetected ? 1 : 0; - if (twai_transmit(&faultMessage, pdMS_TO_TICKS(1000)) == ESP_OK) { - Logger::Notice(faultDetected ? "Fault signal sent successfully" : "No fault detected, signal sent"); - } else { - Logger::Error("Failed to send fault signal"); - } +void initCAN() { + // Initialize configuration structures + twai_general_config_t g_config = TWAI_GENERAL_CONFIG_DEFAULT(CAN_TX, CAN_RX, TWAI_MODE_NORMAL); + twai_timing_config_t t_config = TWAI_TIMING_CONFIG_500KBITS(); // Adjust to your CAN bus speed + twai_filter_config_t f_config = TWAI_FILTER_CONFIG_ACCEPT_ALL(); + + // Install TWAI driver + if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) { + Logger::Notice("CAN driver installed successfully"); + } else { + Logger::Error("Failed to install CAN driver"); + return; + } + + // Start TWAI driver + if (twai_start() == ESP_OK) { + Logger::Notice("CAN driver started successfully"); + } else { + Logger::Error("Failed to start CAN driver"); + } } void sendWheelSpeeds(float frontLeft, float frontRight, float rearLeft, float rearRight) { + Logger::Notice("Sending wheel speeds - FL: %.2f, FR: %.2f, RL: %.2f, RR: %.2f", frontLeft, frontRight, rearLeft, rearRight); twai_message_t wheelMessage; wheelMessage.identifier = WHEEL_MSG_ID; wheelMessage.extd = 0; // 11-bit ID @@ -76,14 +100,43 @@ void sendWheelSpeeds(float frontLeft, float frontRight, float rearLeft, float re void setup() { + Serial.begin(115200); Logger::Start(); Logger::Notice("Setup"); //WheelSpeedSetup(); // CanDriver::CanInit(); - SensorTest.Initialize(); + // Initilize CAN + initCAN(); + + // SensorTest.Initialize(); //CoolantPressure.Initialize(); //CoolantTemperature.Initialize(); + + // Initilize CD card + //if (!SD.begin(SD_CS_PIN)) { + // Logger::Error("Failed to initialize SD card"); + // while (1); + + // Logger::Notice("SD card initialized"); + + // Initialize time +// configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); +// Logger::Notice("Waiting for time sync..."); +// while (!time(nullptr)) { +// delay(1000); +// } +// Logger::Notice("Time synchronized"); +// } +} + +String getTimestamp() { + time_t now = time(nullptr); + struct tm *timeInfo = localtime(&now); + + char buffer[30]; + strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeInfo); + return String(buffer); } // Main @@ -91,20 +144,44 @@ void loop() { Logger::Notice("Hello World"); - SensorTest.GetData(); + // SensorTest.GetData(); //float pressure = CoolantPressure.GetData(); //float temp = CoolantTemperature.GetData(); + + // String timestamp = getTimestamp(); // if (pressure < MIN_PRESSURE || pressure > MAX_PRESSURE || temp < MIN_TEMP || temp > MAX_TEMP){ // Logger::Error("Fault detected! Sending fault signal."); // faultDetected = true; + // } + + // Writing to CD Card + // File pressureFile = SD.open("/pressure_data.txt", FILE_APPEND); + // if (pressureFile) { + // pressureFile.println(timestamp + ", " + String(pressure)); + // pressureFile.close(); + // Logger::Notice("Pressure logged: " + String(pressure)); + // } else { + // Logger::Error("Failed to open pressure file for writing"); + // } + + // File temperatureFile = SD.open("/temperature_data.txt", FILE_APPEND); + // if (temperatureFile) { + // temperatureFile.println(timestamp + ", " + String(temp)); + // temperatureFile.close(); + // Logger::Notice("Temperature logged: " + String(temp)); // } else { - // faultDetected = false; - // } + // Logger::Error("Failed to open temperature file for writing"); + // } - // sendFaultSignal(faultDetected); - // sendWheelSpeeds(fl, fr, rl, rr); + // if (pressure < MIN_PRESSURE || pressure > MAX_PRESSURE || temp < MIN_TEMP || temp > MAX_TEMP){ + // Logger::Error("Fault detected! Sending fault signal."); + // faultDetected = true; - //WheelSpeedReset(); + sendWheelSpeeds(1, 2, 3, 4); + Logger::Notice("Sent wheel speeds: "); + + // WheelSpeedReset(); + delay(1000); -} \ No newline at end of file +}