From 8c825d2cb38a2bdccd78eb6526845fce24b7dc59 Mon Sep 17 00:00:00 2001 From: regg qu <825918830@qq.com> Date: Wed, 17 Jul 2024 17:39:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B6=85=E5=A3=B0=E6=B3=A2=E9=81=BF=E9=9A=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/Inc/app_ultrasonic.h | 6 +++ App/Src/app_ultrasonic.c | 51 +++++++++++++++++++++++++ Core/Inc/main.h | 4 ++ Core/Src/main.c | 4 +- MDK-ARM/CAR.uvoptx | 82 +++++++++++++++++++++++----------------- MDK-ARM/CAR.uvprojx | 5 +++ 6 files changed, 115 insertions(+), 37 deletions(-) create mode 100644 App/Inc/app_ultrasonic.h create mode 100644 App/Src/app_ultrasonic.c diff --git a/App/Inc/app_ultrasonic.h b/App/Inc/app_ultrasonic.h new file mode 100644 index 0000000..2defafd --- /dev/null +++ b/App/Inc/app_ultrasonic.h @@ -0,0 +1,6 @@ +#ifndef __APP_ULTRASONIC_H +#define __APP_ULTRASONIC_H + +int APP_Find_Direction(void); + +#endif \ No newline at end of file diff --git a/App/Src/app_ultrasonic.c b/App/Src/app_ultrasonic.c new file mode 100644 index 0000000..724a251 --- /dev/null +++ b/App/Src/app_ultrasonic.c @@ -0,0 +1,51 @@ +#include "app_ultrasonic.h" +#include "servo.h" +#include "hcsr04.h" +#include "main.h" +#include "led.h" + +#define BLOCK_DISTANCE 150 + +int APP_Find_Direction(void) +{ + uint32_t left_block,right_block,straight_block; + + // 测定左侧障碍物距离 + SERVO_Rotate(50); + HAL_Delay(300); + left_block = sonar_mm(); + + // 测定正前侧障碍物距离 + SERVO_Rotate(90); + HAL_Delay(300); + straight_block = sonar_mm(); + + // 测定右侧障碍物距离 + SERVO_Rotate(150); + HAL_Delay(300); + right_block = sonar_mm(); + + LED_Start(7); + + if(left_block > BLOCK_DISTANCE && right_block > BLOCK_DISTANCE && straight_block > BLOCK_DISTANCE)// 前左右均无障碍物,直行 + { + return STRAIGHT; + } + else if(left_block < BLOCK_DISTANCE && right_block < BLOCK_DISTANCE && straight_block < BLOCK_DISTANCE)// 前左右均有障碍物,停止并鸣笛 + { + LED_SetDuty(255,0,0); + return STOP; + } + else if(straight_block < BLOCK_DISTANCE && left_block < BLOCK_DISTANCE)// 前左有障碍物,右转 + { + return RIGHT; + } + else if (straight_block < BLOCK_DISTANCE && right_block < BLOCK_DISTANCE)// 前右有障碍物,左转 + { + return LEFT; + } + else + { + return STRAIGHT; + } +} \ No newline at end of file diff --git a/Core/Inc/main.h b/Core/Inc/main.h index d01cff8..afa8e32 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -106,6 +106,10 @@ void Error_Handler(void); /* USER CODE BEGIN Private defines */ #define LOW (0) #define HIGH (1) +#define LEFT (0) +#define STRAIGHT (1) +#define RIGHT (2) +#define STOP (3) /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/Core/Src/main.c b/Core/Src/main.c index 6b15d2e..e0a7a4b 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -105,7 +105,7 @@ int main(void) MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ BLUETOOTH_Init(); - // HC_SR04_Init(); + HC_SR04_Init(); /* USER CODE END 2 */ /* Infinite loop */ @@ -113,7 +113,7 @@ int main(void) while (1) { /* USER CODE END WHILE */ - + /* USER CODE BEGIN 3 */ } diff --git a/MDK-ARM/CAR.uvoptx b/MDK-ARM/CAR.uvoptx index b3285ad..2478158 100644 --- a/MDK-ARM/CAR.uvoptx +++ b/MDK-ARM/CAR.uvoptx @@ -332,7 +332,7 @@ Application/User/App - 0 + 1 0 0 0 @@ -348,6 +348,18 @@ 0 0 + + 2 + 3 + 1 + 0 + 0 + 0 + ..\App\Src\app_ultrasonic.c + app_ultrasonic.c + 0 + 0 + @@ -358,7 +370,7 @@ 0 3 - 3 + 4 1 0 0 @@ -370,9 +382,9 @@ 3 - 4 + 5 1 - 0 + 1 0 0 ../Core/Src/main.c @@ -382,7 +394,7 @@ 3 - 5 + 6 1 0 0 @@ -394,7 +406,7 @@ 3 - 6 + 7 1 0 0 @@ -406,7 +418,7 @@ 3 - 7 + 8 1 0 0 @@ -418,7 +430,7 @@ 3 - 8 + 9 1 0 0 @@ -430,7 +442,7 @@ 3 - 9 + 10 1 0 0 @@ -442,7 +454,7 @@ 3 - 10 + 11 1 0 0 @@ -454,7 +466,7 @@ 3 - 11 + 12 1 0 0 @@ -474,7 +486,7 @@ 0 4 - 12 + 13 1 0 0 @@ -486,7 +498,7 @@ 4 - 13 + 14 1 0 0 @@ -498,7 +510,7 @@ 4 - 14 + 15 1 0 0 @@ -510,7 +522,7 @@ 4 - 15 + 16 1 0 0 @@ -522,7 +534,7 @@ 4 - 16 + 17 1 0 0 @@ -534,7 +546,7 @@ 4 - 17 + 18 1 0 0 @@ -546,7 +558,7 @@ 4 - 18 + 19 1 0 0 @@ -558,7 +570,7 @@ 4 - 19 + 20 1 0 0 @@ -578,7 +590,7 @@ 0 5 - 20 + 21 1 0 0 @@ -590,7 +602,7 @@ 5 - 21 + 22 1 0 0 @@ -602,7 +614,7 @@ 5 - 22 + 23 1 0 0 @@ -614,7 +626,7 @@ 5 - 23 + 24 1 0 0 @@ -626,7 +638,7 @@ 5 - 24 + 25 1 0 0 @@ -638,7 +650,7 @@ 5 - 25 + 26 1 0 0 @@ -650,7 +662,7 @@ 5 - 26 + 27 1 0 0 @@ -662,7 +674,7 @@ 5 - 27 + 28 1 0 0 @@ -674,7 +686,7 @@ 5 - 28 + 29 1 0 0 @@ -686,7 +698,7 @@ 5 - 29 + 30 1 0 0 @@ -698,7 +710,7 @@ 5 - 30 + 31 1 0 0 @@ -710,7 +722,7 @@ 5 - 31 + 32 1 0 0 @@ -722,7 +734,7 @@ 5 - 32 + 33 1 0 0 @@ -734,7 +746,7 @@ 5 - 33 + 34 1 0 0 @@ -746,7 +758,7 @@ 5 - 34 + 35 1 0 0 @@ -766,7 +778,7 @@ 0 6 - 35 + 36 1 0 0 diff --git a/MDK-ARM/CAR.uvprojx b/MDK-ARM/CAR.uvprojx index a09f68e..f392583 100644 --- a/MDK-ARM/CAR.uvprojx +++ b/MDK-ARM/CAR.uvprojx @@ -398,6 +398,11 @@ 1 ..\App\Src\app_line_seek.c + + app_ultrasonic.c + 1 + ..\App\Src\app_ultrasonic.c +