diff --git a/Core/Inc/gps.h b/Core/Inc/gps.h index 8c88ef8..1d7d77b 100644 --- a/Core/Inc/gps.h +++ b/Core/Inc/gps.h @@ -10,30 +10,31 @@ #include "i2c.h" /* Macros -------------------------------------------------------------------*/ -#define M9N_ADDR 0x42 -#define M9N_MSB_REG 0xFD -#define M9N_LSB_REG 0xFE -#define M9N_DATA_REG 0xFF - -#define UBX_PREABLE1 0xB5 -#define UBX_PREABLE2 0x62 - -#define UBX_ACK_CLASS 0x05 -#define UBX_ACK_ID 0x01 - -#define UBX_CLASS_Pos 0x02 -#define UBX_ID_Pos 0x03 -#define UBX_LEN_Pos 0x04 -#define UBX_PAYLOAD_Pos 0x06 - -#define UBX_PVT_CLASS 0x01 -#define UBX_PVT_ID 0x07 -#define UBX_PVT_LEN 0x92 +#define M9N_ADDR (0x42) +#define M9N_MSB_REG (0xFD) +#define M9N_LSB_REG (0xFE) +#define M9N_DATA_REG (0xFF) + +#define UBX_PREABLE1 (0xB5) +#define UBX_PREABLE2 (0x62) + +#define UBX_ACK_CLASS (0x05) +#define UBX_ACK_ID (0x01) + +#define UBX_CLASS_Pos (0x02) +#define UBX_ID_Pos (0x03) +#define UBX_LEN_Pos (0x04) +#define UBX_PAYLOAD_Pos (0x06) + +#define UBX_PVT_CLASS (0x01) +#define UBX_PVT_ID (0x07) +#define UBX_PVT_LEN (0x92) +#define UBX_PVT_FIX_Pos (0x14 + UBX_PAYLOAD_Pos) #define UBX_PVT_LON_Pos (0x18 + UBX_PAYLOAD_Pos) #define UBX_PVT_LAT_Pos (0x1C + UBX_PAYLOAD_Pos) #define UBX_PVT_SPD_Pos (0x3C + UBX_PAYLOAD_Pos) -#define RETRY_COUNT 25 +#define RETRY_COUNT (25) /* Structs and Enums --------------------------------------------------------*/ typedef struct { @@ -53,8 +54,9 @@ typedef struct { } UBX_Parser; typedef enum { - GPS_OK = 0, - GPS_ERROR = 1 + GPS_OK, + GPS_ERROR, + GPS_NO_FIX, } GPS_Status; typedef struct { @@ -74,7 +76,7 @@ typedef struct { GPS_Status GPS_Init(); /** - * @brief Get GPS Position Data + * @brief Checks for GPS lock and gets the current position and speed * * @param data [GPS_Data*] Pointer to GPS Data Struct * @return GPS_Status diff --git a/Core/Src/gps.c b/Core/Src/gps.c index d81e5b8..4bceac4 100644 --- a/Core/Src/gps.c +++ b/Core/Src/gps.c @@ -110,6 +110,7 @@ GPS_Status GPS_Init() { GPS_Status Get_Position(GPS_Data* data) { volatile uint16_t len = 0; uint8_t count = 0; + GPS_Status ret_val = GPS_ERROR; uint8_t poll_nav_pvt[] = { UBX_PREABLE1, UBX_PREABLE2, // Sync Chars 0x01, 0x07, // Class (NAV), ID (PVT) @@ -130,21 +131,28 @@ GPS_Status Get_Position(GPS_Data* data) { I2C_Read(I2C1, M9N_ADDR, M9N_DATA_REG, buffer, len); - // Data is sent in signed little-endian 32-bit integer, two's complement - data->latitude = buffer[UBX_PVT_LAT_Pos + 3] << 24 | - buffer[UBX_PVT_LAT_Pos + 2] << 16 | - buffer[UBX_PVT_LAT_Pos + 1] << 8 | - buffer[UBX_PVT_LAT_Pos]; - data->longitude = buffer[UBX_PVT_LON_Pos + 3] << 24 | - buffer[UBX_PVT_LON_Pos + 2] << 16 | - buffer[UBX_PVT_LON_Pos + 1] << 8 | - buffer[UBX_PVT_LON_Pos]; - data->speed = buffer[UBX_PVT_SPD_Pos + 3] << 24 | - buffer[UBX_PVT_SPD_Pos + 2] << 16 | - buffer[UBX_PVT_SPD_Pos + 1] << 8 | - buffer[UBX_PVT_SPD_Pos]; + if (buffer[UBX_PVT_FIX_Pos] != 0x00) { + // Data is sent in signed little-endian 32-bit integer, two's complement + data->latitude = buffer[UBX_PVT_LAT_Pos + 3] << 24 | + buffer[UBX_PVT_LAT_Pos + 2] << 16 | + buffer[UBX_PVT_LAT_Pos + 1] << 8 | + buffer[UBX_PVT_LAT_Pos]; + data->longitude = buffer[UBX_PVT_LON_Pos + 3] << 24 | + buffer[UBX_PVT_LON_Pos + 2] << 16 | + buffer[UBX_PVT_LON_Pos + 1] << 8 | + buffer[UBX_PVT_LON_Pos]; + data->speed = buffer[UBX_PVT_SPD_Pos + 3] << 24 | + buffer[UBX_PVT_SPD_Pos + 2] << 16 | + buffer[UBX_PVT_SPD_Pos + 1] << 8 | + buffer[UBX_PVT_SPD_Pos]; + ret_val = GPS_OK; + } + else { + ret_val = GPS_NO_FIX; + } + - return GPS_OK; + return ret_val; } GPS_Status I2C_Send_UBX_CFG(I2C_TypeDef* I2C, uint8_t dev, uint8_t* msg, size_t msg_len) {