fix(firmware): return gps transport status

This commit is contained in:
wb-eugenia
2026-03-13 19:41:45 +01:00
parent 7510e31c20
commit a4b8a144ed
3 changed files with 83 additions and 68 deletions
@@ -1,6 +1,7 @@
// gps_handler.cpp // gps_handler.cpp
#include "gps_handler.h" #include "gps_handler.h"
#include <cmath> #include <cmath>
#include <cstdio>
// UART handle for communication with GUI // UART handle for communication with GUI
static UART_HandleTypeDef* gui_huart = NULL; static UART_HandleTypeDef* gui_huart = NULL;
@@ -14,40 +15,46 @@ void GPS_Init(UART_HandleTypeDef* huart)
memset(&current_gps, 0, sizeof(GPS_Data_t)); memset(&current_gps, 0, sizeof(GPS_Data_t));
} }
uint8_t GPS_IsDataValid(GPS_Data_t* gps_data) uint8_t GPS_IsDataValid(GPS_Data_t* gps_data)
{ {
// Check if GPS data is within valid ranges if (gps_data == NULL) {
if (gps_data->latitude < -90.0 || gps_data->latitude > 90.0) return 0;
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) if (gps_data->longitude < -180.0 || gps_data->longitude > 180.0)
return 0; return 0;
if (gps_data->altitude < -1000.0 || gps_data->altitude > 10000.0) // -1km to 10km if (gps_data->altitude < -1000.0 || gps_data->altitude > 10000.0) // -1km to 10km
return 0; return 0;
return 1; return 1;
} }
void GPS_ProcessData(GPS_Data_t* gps_data) bool GPS_ProcessData(GPS_Data_t* gps_data)
{ {
// Validate GPS data // Validate GPS data
if (!GPS_IsDataValid(gps_data)) { if (!GPS_IsDataValid(gps_data)) {
return; return false;
} }
// Update current GPS data // Update current GPS data
memcpy(&current_gps, gps_data, sizeof(GPS_Data_t)); memcpy(&current_gps, gps_data, sizeof(GPS_Data_t));
current_gps.timestamp = HAL_GetTick(); current_gps.timestamp = HAL_GetTick();
// Send to GUI // Send to GUI
GPS_SendToGUI(&current_gps); return GPS_SendToGUI(&current_gps);
} }
void GPS_SendToGUI(GPS_Data_t* gps_data) bool GPS_SendToGUI(GPS_Data_t* gps_data)
{ {
if (gui_huart == NULL) return; if (gui_huart == NULL || gps_data == NULL) {
return false;
// Create packet: "GPS:lat,lon,alt\r\n" }
char buffer[64];
// Create packet: "GPS:lat,lon,alt\r\n"
char buffer[64];
// Convert double and float to string with high precision // Convert double and float to string with high precision
int len = snprintf(buffer, sizeof(buffer), "GPS:%.8f,%.8f,%.2f\r\n", 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->longitude,
gps_data->altitude); gps_data->altitude);
if (len > 0 && len < (int)sizeof(buffer)) { if (len <= 0 || len >= (int)sizeof(buffer)) {
// Send via UART3 to GUI return false;
HAL_UART_Transmit(gui_huart, (uint8_t*)buffer, len, 1000); }
}
} // Send via UART3 to GUI
return HAL_UART_Transmit(gui_huart, (uint8_t*)buffer, len, 1000) == HAL_OK;
// Alternative binary protocol version (more efficient) }
void GPS_SendBinaryToGUI(GPS_Data_t* gps_data)
{ // Alternative binary protocol version (more efficient)
if (gui_huart == NULL) return; bool GPS_SendBinaryToGUI(GPS_Data_t* gps_data)
{
// Binary packet structure: if (gui_huart == NULL || gps_data == NULL) {
// [Header 4 bytes][Latitude 8 bytes][Longitude 8 bytes][Altitude 4 bytes][Pitch 4 bytes][CRC 2 bytes] 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 uint8_t packet[30]; // 4 + 8 + 8 + 4 + 4 + 2 = 30 bytes
uint16_t crc = 0; 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; packet[20 + i] = (alt_bits >> (24 - i*8)) & 0xFF;
} }
// Convert float altitude to bytes (big-endian) // Convert float pitch to bytes (big-endian)
uint32_t pitch_bits; uint32_t pitch_bits;
memcpy(&pitch_bits, &gps_data->pitch, sizeof(float)); memcpy(&pitch_bits, &gps_data->pitch, sizeof(float));
for(int i = 0; i < 4; i++) { for(int i = 0; i < 4; i++) {
packet[24 + i] = (pitch_bits >> (24 - i*8)) & 0xFF; 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[28] = (crc >> 8) & 0xFF;
packet[29] = crc & 0xFF; packet[29] = crc & 0xFF;
// Send binary packet // Send binary packet
CDC_Transmit_FS(packet, sizeof(packet)); return CDC_Transmit_FS(packet, sizeof(packet)) == USBD_OK;
} }
@@ -3,10 +3,11 @@
#define GPS_HANDLER_H #define GPS_HANDLER_H
#include "main.h" #include "main.h"
#include "usb_device.h" #include "usb_device.h"
#include "usbd_cdc_if.h" #include "usbd_cdc_if.h"
#include <stdint.h> #include <stdbool.h>
#include <string.h> #include <stdint.h>
#include <string.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -20,11 +21,11 @@ typedef struct {
uint32_t timestamp; uint32_t timestamp;
} GPS_Data_t; } GPS_Data_t;
void GPS_Init(UART_HandleTypeDef* huart); void GPS_Init(UART_HandleTypeDef* huart);
void GPS_ProcessData(GPS_Data_t* gps_data); bool GPS_ProcessData(GPS_Data_t* gps_data);
void GPS_SendToGUI(GPS_Data_t* gps_data); bool GPS_SendToGUI(GPS_Data_t* gps_data);
void GPS_SendBinaryToGUI(GPS_Data_t* gps_data); bool GPS_SendBinaryToGUI(GPS_Data_t* gps_data);
uint8_t GPS_IsDataValid(GPS_Data_t* gps_data); uint8_t GPS_IsDataValid(GPS_Data_t* gps_data);
#ifdef __cplusplus #ifdef __cplusplus
} }
@@ -1524,11 +1524,14 @@ int main(void)
/**********wait for GUI start flag and Send Lat/Long/alt********/ /**********wait for GUI start flag and Send Lat/Long/alt********/
/***************************************************************/ /***************************************************************/
GPS_Data_t gps_data; GPS_Data_t gps_data;
// Binary packet structure: // Binary packet structure:
// [Header 4 bytes][Latitude 8 bytes][Longitude 8 bytes][Altitude 4 bytes][CRC 2 bytes] // [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()}; gps_data = {RADAR_Latitude, RADAR_Longitude, RADAR_Altitude, Pitch_Sensor, HAL_GetTick()};
GPS_SendBinaryToGUI(&gps_data); 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 // Check if start flag was received and settings are ready
do{ do{