From a4b8a144ed966ca0bbe1cdecbba749c28a3ccbe0 Mon Sep 17 00:00:00 2001 From: wb-eugenia Date: Fri, 13 Mar 2026 19:41:45 +0100 Subject: [PATCH] fix(firmware): return gps transport status --- .../9_1_1_C_Cpp_Libraries/gps_handler.cpp | 119 ++++++++++-------- .../9_1_1_C_Cpp_Libraries/gps_handler.h | 19 +-- .../9_1_3_C_Cpp_Code/main.cpp | 13 +- 3 files changed, 83 insertions(+), 68 deletions(-) diff --git a/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/gps_handler.cpp b/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/gps_handler.cpp index 2b15cc2..11e0b0c 100644 --- a/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/gps_handler.cpp +++ b/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/gps_handler.cpp @@ -1,6 +1,7 @@ -// gps_handler.cpp -#include "gps_handler.h" -#include +// gps_handler.cpp +#include "gps_handler.h" +#include +#include // UART handle for communication with GUI static UART_HandleTypeDef* gui_huart = NULL; @@ -14,40 +15,46 @@ void GPS_Init(UART_HandleTypeDef* huart) memset(¤t_gps, 0, sizeof(GPS_Data_t)); } -uint8_t GPS_IsDataValid(GPS_Data_t* gps_data) -{ - // Check if GPS data is within valid ranges - if (gps_data->latitude < -90.0 || gps_data->latitude > 90.0) - return 0; +uint8_t GPS_IsDataValid(GPS_Data_t* gps_data) +{ + if (gps_data == NULL) { + return 0; + } + + // Check if GPS data is within valid ranges + if (gps_data->latitude < -90.0 || gps_data->latitude > 90.0) + return 0; if (gps_data->longitude < -180.0 || gps_data->longitude > 180.0) return 0; if (gps_data->altitude < -1000.0 || gps_data->altitude > 10000.0) // -1km to 10km return 0; - return 1; -} - -void GPS_ProcessData(GPS_Data_t* gps_data) -{ - // Validate GPS data - if (!GPS_IsDataValid(gps_data)) { - return; - } - - // Update current GPS data - memcpy(¤t_gps, gps_data, sizeof(GPS_Data_t)); - current_gps.timestamp = HAL_GetTick(); - - // Send to GUI - GPS_SendToGUI(¤t_gps); -} - -void GPS_SendToGUI(GPS_Data_t* gps_data) -{ - if (gui_huart == NULL) return; - - // Create packet: "GPS:lat,lon,alt\r\n" - char buffer[64]; + return 1; +} + +bool GPS_ProcessData(GPS_Data_t* gps_data) +{ + // Validate GPS data + if (!GPS_IsDataValid(gps_data)) { + return false; + } + + // Update current GPS data + memcpy(¤t_gps, gps_data, sizeof(GPS_Data_t)); + current_gps.timestamp = HAL_GetTick(); + + // Send to GUI + return GPS_SendToGUI(¤t_gps); +} + +bool GPS_SendToGUI(GPS_Data_t* gps_data) +{ + if (gui_huart == NULL || gps_data == NULL) { + return false; + } + + // Create packet: "GPS:lat,lon,alt\r\n" + char buffer[64]; // Convert double and float to string with high precision int len = snprintf(buffer, sizeof(buffer), "GPS:%.8f,%.8f,%.2f\r\n", @@ -55,19 +62,23 @@ void GPS_SendToGUI(GPS_Data_t* gps_data) gps_data->longitude, gps_data->altitude); - if (len > 0 && len < (int)sizeof(buffer)) { - // Send via UART3 to GUI - HAL_UART_Transmit(gui_huart, (uint8_t*)buffer, len, 1000); - } -} - -// Alternative binary protocol version (more efficient) -void GPS_SendBinaryToGUI(GPS_Data_t* gps_data) -{ - if (gui_huart == NULL) return; - - // Binary packet structure: - // [Header 4 bytes][Latitude 8 bytes][Longitude 8 bytes][Altitude 4 bytes][Pitch 4 bytes][CRC 2 bytes] + if (len <= 0 || len >= (int)sizeof(buffer)) { + return false; + } + + // Send via UART3 to GUI + return HAL_UART_Transmit(gui_huart, (uint8_t*)buffer, len, 1000) == HAL_OK; +} + +// Alternative binary protocol version (more efficient) +bool GPS_SendBinaryToGUI(GPS_Data_t* gps_data) +{ + if (gui_huart == NULL || gps_data == NULL) { + return false; + } + + // Binary packet structure: + // [Header 4 bytes][Latitude 8 bytes][Longitude 8 bytes][Altitude 4 bytes][Pitch 4 bytes][CRC 2 bytes] uint8_t packet[30]; // 4 + 8 + 8 + 4 + 4 + 2 = 30 bytes uint16_t crc = 0; @@ -99,10 +110,10 @@ void GPS_SendBinaryToGUI(GPS_Data_t* gps_data) packet[20 + i] = (alt_bits >> (24 - i*8)) & 0xFF; } - // Convert float altitude to bytes (big-endian) - uint32_t pitch_bits; - memcpy(&pitch_bits, &gps_data->pitch, sizeof(float)); - for(int i = 0; i < 4; i++) { + // Convert float pitch to bytes (big-endian) + uint32_t pitch_bits; + memcpy(&pitch_bits, &gps_data->pitch, sizeof(float)); + for(int i = 0; i < 4; i++) { packet[24 + i] = (pitch_bits >> (24 - i*8)) & 0xFF; } @@ -112,8 +123,8 @@ void GPS_SendBinaryToGUI(GPS_Data_t* gps_data) } packet[28] = (crc >> 8) & 0xFF; - packet[29] = crc & 0xFF; - - // Send binary packet - CDC_Transmit_FS(packet, sizeof(packet)); -} + packet[29] = crc & 0xFF; + + // Send binary packet + return CDC_Transmit_FS(packet, sizeof(packet)) == USBD_OK; +} diff --git a/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/gps_handler.h b/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/gps_handler.h index 0425698..73f7b02 100644 --- a/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/gps_handler.h +++ b/9_Firmware/9_1_Microcontroller/9_1_1_C_Cpp_Libraries/gps_handler.h @@ -3,10 +3,11 @@ #define GPS_HANDLER_H #include "main.h" -#include "usb_device.h" -#include "usbd_cdc_if.h" -#include -#include +#include "usb_device.h" +#include "usbd_cdc_if.h" +#include +#include +#include #ifdef __cplusplus extern "C" { @@ -20,11 +21,11 @@ typedef struct { uint32_t timestamp; } GPS_Data_t; -void GPS_Init(UART_HandleTypeDef* huart); -void GPS_ProcessData(GPS_Data_t* gps_data); -void GPS_SendToGUI(GPS_Data_t* gps_data); -void GPS_SendBinaryToGUI(GPS_Data_t* gps_data); -uint8_t GPS_IsDataValid(GPS_Data_t* gps_data); +void GPS_Init(UART_HandleTypeDef* huart); +bool GPS_ProcessData(GPS_Data_t* gps_data); +bool GPS_SendToGUI(GPS_Data_t* gps_data); +bool GPS_SendBinaryToGUI(GPS_Data_t* gps_data); +uint8_t GPS_IsDataValid(GPS_Data_t* gps_data); #ifdef __cplusplus } diff --git a/9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp b/9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp index 0daa07d..975f061 100644 --- a/9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp +++ b/9_Firmware/9_1_Microcontroller/9_1_3_C_Cpp_Code/main.cpp @@ -1524,11 +1524,14 @@ int main(void) /**********wait for GUI start flag and Send Lat/Long/alt********/ /***************************************************************/ - GPS_Data_t gps_data; - // Binary packet structure: - // [Header 4 bytes][Latitude 8 bytes][Longitude 8 bytes][Altitude 4 bytes][CRC 2 bytes] - gps_data = {RADAR_Latitude, RADAR_Longitude, RADAR_Altitude, Pitch_Sensor, HAL_GetTick()}; - GPS_SendBinaryToGUI(&gps_data); + GPS_Data_t gps_data; + // Binary packet structure: + // [Header 4 bytes][Latitude 8 bytes][Longitude 8 bytes][Altitude 4 bytes][Pitch 4 bytes][CRC 2 bytes] + gps_data = {RADAR_Latitude, RADAR_Longitude, RADAR_Altitude, Pitch_Sensor, HAL_GetTick()}; + if (!GPS_SendBinaryToGUI(&gps_data)) { + const uint8_t gps_send_error[] = "GPS binary send failed\r\n"; + HAL_UART_Transmit(&huart3, (uint8_t*)gps_send_error, sizeof(gps_send_error) - 1, 1000); + } // Check if start flag was received and settings are ready do{