From 2b3e4129e80a61893e3fa46e9795f7c2a1eb921c Mon Sep 17 00:00:00 2001 From: Radzhab Bisultanov Date: Fri, 20 Feb 2026 15:16:59 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B0=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20lidar=5Fupdate(),=20=D0=BE=D0=B1=D0=BD=D0=BE=D0=B2?= =?UTF-8?q?=D0=BB=D1=8F=D1=8E=D1=89=D0=B0=D1=8F=20=D0=B4=D0=B0=D0=BD=D0=BD?= =?UTF-8?q?=D1=8B=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Source/BSP/Inc/lidar.h | 11 ++++++++++- Source/BSP/Src/lidar.c | 30 +++++++++++++++++++----------- 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/Source/BSP/Inc/lidar.h b/Source/BSP/Inc/lidar.h index b7ffb15..40c4e8e 100644 --- a/Source/BSP/Inc/lidar.h +++ b/Source/BSP/Inc/lidar.h @@ -7,6 +7,8 @@ #define USART3_START_BYTE 0x59 #define USART3_FRAME_SIZE 9 +#define LIDAR_MIN_DIST 0.01f +#define LIDAR_MAX_DIST 40.0f typedef struct { @@ -21,7 +23,14 @@ typedef struct uint8_t checksum; } lidar_data_buf; +typedef struct +{ + uint16_t distance; // meters + uint16_t strength; + uint16_t temperature; +} lidar_data; + void lidar_init(); -void lidar_update(); +void lidar_update(lidar_data* lidar); #endif \ No newline at end of file diff --git a/Source/BSP/Src/lidar.c b/Source/BSP/Src/lidar.c index 4a9242a..231cd7c 100644 --- a/Source/BSP/Src/lidar.c +++ b/Source/BSP/Src/lidar.c @@ -75,17 +75,25 @@ void USART3_IRQHandler() if (usart3_index == USART3_FRAME_SIZE) { - for (uint8_t i = 0; i < USART3_FRAME_SIZE; ++i) usart3_checksum += buff_data[i]; - if (buffer.checksum == usart3_checksum) - { - usart3_index = 0; - usart3_frame_ready = 1; - } - else - { - usart3_index = 0; - usart3_frame_ready = 0; - } + usart3_index = 0; + usart3_frame_ready = 1; } } +} + +void lidar_update(lidar_data* lidar) +{ + if (!usart3_frame_ready) + return; + + usart3_frame_ready = 0; + + for (uint8_t i = 0; i < USART3_FRAME_SIZE; ++i) usart3_checksum += buff_data[i]; + + if (buffer.checksum != usart3_checksum) + return; + + lidar->distance = buffer.distance_l | (buffer.distance_h << 8); + lidar->strength = buffer.strength_l | (buffer.strength_h << 8); + lidar->temperature = buffer.temp_l | (buffer.temp_h << 8); } \ No newline at end of file