From 0116e8246d30217f6b6991e030676ed12bbd4a78 Mon Sep 17 00:00:00 2001 From: EN Date: Mon, 15 Jul 2024 20:10:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E5=B9=B6=E8=93=9D=E7=89=99=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=EF=BC=88=E5=BE=85=E6=B5=8B=E8=AF=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 6 +- CAR.ioc | 13 +- Core/Inc/stm32f1xx_hal_conf.h | 2 +- Core/Src/main.c | 60 ++--- Core/Src/usart.c | 6 +- MDK-ARM/CAR.uvoptx | 371 +++++++++++++-------------- MDK-ARM/CAR.uvprojx | 163 ++++-------- {User => Peripheral}/Inc/bluetooth.h | 0 Peripheral/Inc/control.h | 21 ++ {User => Peripheral}/Inc/hcsr04.h | 4 +- {User => Peripheral}/Inc/led.h | 0 {User => Peripheral}/Inc/line_seek.h | 2 + {User => Peripheral}/Inc/motor.h | 0 {User => Peripheral}/Src/bluetooth.c | 56 ++-- Peripheral/Src/control.c | 189 ++++++++++++++ {User => Peripheral}/Src/hcsr04.c | 32 +-- {User => Peripheral}/Src/led.c | 0 {User => Peripheral}/Src/line_seek.c | 3 - {User => Peripheral}/Src/motor.c | 47 ++-- User/Inc/control.h | 10 - User/Src/control.c | 88 ------- 21 files changed, 571 insertions(+), 502 deletions(-) rename {User => Peripheral}/Inc/bluetooth.h (100%) create mode 100644 Peripheral/Inc/control.h rename {User => Peripheral}/Inc/hcsr04.h (65%) rename {User => Peripheral}/Inc/led.h (100%) rename {User => Peripheral}/Inc/line_seek.h (88%) rename {User => Peripheral}/Inc/motor.h (100%) rename {User => Peripheral}/Src/bluetooth.c (56%) create mode 100644 Peripheral/Src/control.c rename {User => Peripheral}/Src/hcsr04.c (69%) rename {User => Peripheral}/Src/led.c (100%) rename {User => Peripheral}/Src/line_seek.c (96%) rename {User => Peripheral}/Src/motor.c (71%) delete mode 100644 User/Inc/control.h delete mode 100644 User/Src/control.c diff --git a/.gitignore b/.gitignore index 757e1ef..37951ed 100644 --- a/.gitignore +++ b/.gitignore @@ -5,10 +5,6 @@ MDK-ARM/DebugConfig MDK-ARM/CAR.uvguix.* MDK-ARM/startup_stm32f103xb.lst MDK-ARM/RTE -MDK-ARM/CAR.uvguix.Admin -MDK-ARM/car.uvguix.Administrator -MDK-ARM/CAR.uvguix.lenovo build/ -cmake/ - +cmake/ \ No newline at end of file diff --git a/CAR.ioc b/CAR.ioc index d149822..4f0a985 100644 --- a/CAR.ioc +++ b/CAR.ioc @@ -69,7 +69,7 @@ Mcu.UserName=STM32F103C8Tx MxCube.Version=6.12.0 MxDb.Version=DB.6.0.120 NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false -NVIC.DMA1_Channel6_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true +NVIC.DMA1_Channel6_IRQn=true\:0\:0\:true\:false\:true\:true\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.EXTI15_10_IRQn=true\:1\:0\:true\:false\:true\:true\:true\:true NVIC.ForceEnableDMAVector=false @@ -79,9 +79,10 @@ NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.PriorityGroup=NVIC_PRIORITYGROUP_2 NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false -NVIC.SysTick_IRQn=true\:3\:0\:true\:false\:true\:false\:true\:false +NVIC.SysTick_IRQn=true\:2\:0\:true\:false\:true\:false\:true\:false NVIC.TIM2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true -NVIC.USART2_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true +NVIC.USART1_IRQn=true\:3\:1\:true\:false\:true\:true\:true\:true +NVIC.USART2_IRQn=true\:3\:0\:true\:false\:true\:true\:true\:true NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false PA0-WKUP.GPIOParameters=PinState,GPIO_Label PA0-WKUP.GPIO_Label=FM_K2_POWERC @@ -200,7 +201,7 @@ ProjectManager.FreePins=false ProjectManager.HalAssertFull=false ProjectManager.HeapSize=0x200 ProjectManager.KeepUserCode=true -ProjectManager.LastFirmware=false +ProjectManager.LastFirmware=true ProjectManager.LibraryCopy=1 ProjectManager.MainLocation=Core/Src ProjectManager.NoMain=false @@ -209,7 +210,7 @@ ProjectManager.ProjectBuild=false ProjectManager.ProjectFileName=CAR.ioc ProjectManager.ProjectName=CAR ProjectManager.ProjectStructure= -ProjectManager.RegisterCallBack=TIM,USART +ProjectManager.RegisterCallBack=TIM ProjectManager.StackSize=0x400 ProjectManager.TargetToolchain=Makefile ProjectManager.ToolChainLocation= @@ -267,7 +268,7 @@ TIM3.Pulse-PWM\ Generation4\ CH4=255 TIM4.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1 TIM4.Channel-PWM\ Generation2\ CH2=TIM_CHANNEL_2 TIM4.IPParameters=Channel-PWM Generation1 CH1,Channel-PWM Generation2 CH2,Prescaler,Period,Pulse-PWM Generation1 CH1,Pulse-PWM Generation2 CH2 -TIM4.Period=100 +TIM4.Period=0 TIM4.Prescaler=720 - 1 TIM4.Pulse-PWM\ Generation1\ CH1=100 TIM4.Pulse-PWM\ Generation2\ CH2=100 diff --git a/Core/Inc/stm32f1xx_hal_conf.h b/Core/Inc/stm32f1xx_hal_conf.h index 8dc8f19..6250279 100644 --- a/Core/Inc/stm32f1xx_hal_conf.h +++ b/Core/Inc/stm32f1xx_hal_conf.h @@ -129,7 +129,7 @@ * @brief This is the HAL system configuration section */ #define VDD_VALUE 3300U /*!< Value of VDD in mv */ -#define TICK_INT_PRIORITY 3U /*!< tick interrupt priority (lowest by default) */ +#define TICK_INT_PRIORITY 2U /*!< tick interrupt priority (lowest by default) */ #define USE_RTOS 0U #define PREFETCH_ENABLE 1U diff --git a/Core/Src/main.c b/Core/Src/main.c index c01329c..c867412 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -18,9 +18,7 @@ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" - #include "dma.h" -#include "gpio.h" #include "i2c.h" #include "tim.h" #include "usart.h" @@ -29,8 +27,6 @@ /* USER CODE BEGIN Includes */ #include "bluetooth.h" #include "hcsr04.h" -#include "led.h" -#include "motor.h" #include "syscalls.h" /* USER CODE END Includes */ @@ -95,39 +91,35 @@ int main(void) /* USER CODE END SysInit */ - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_DMA_Init(); - MX_I2C2_Init(); - MX_TIM2_Init(); - MX_TIM3_Init(); - MX_TIM4_Init(); - MX_USART2_UART_Init(); - MX_USART1_UART_Init(); - /* USER CODE BEGIN 2 */ - HC_SR04_Init(); - BLUETOOTH_Init(); + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_DMA_Init(); + MX_I2C2_Init(); + MX_TIM2_Init(); + MX_TIM3_Init(); + MX_TIM4_Init(); + // MX_USART1_UART_Init(); + MX_USART2_UART_Init(); + /* USER CODE BEGIN 2 */ + BLUETOOTH_Init(); + // HC_SR04_Init(); + /* USER CODE END 2 */ - /* USER CODE BEGIN 2 */ + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + while (1) + { + // if (sonor_state == 0) + // { + // sonar_mm(); + // } + // HAL_Delay(300); // 延时300毫秒 - /* USER CODE END 2 */ + /* USER CODE END WHILE */ - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - while (1) - { - my_printf(HUART1, "test huart1 \r\n"); - my_printf(HUART2, "test huart2\r\n"); - - // App_LineSeek(); - - // HAL_Delay(300); // 延时300毫秒 - - /* USER CODE END WHILE */ - - /* USER CODE BEGIN 3 */ - } - /* USER CODE END 3 */ + /* USER CODE BEGIN 3 */ + } + /* USER CODE END 3 */ } /** diff --git a/Core/Src/usart.c b/Core/Src/usart.c index 24060a9..897a412 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -114,6 +114,9 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + /* USART1 interrupt Init */ + HAL_NVIC_SetPriority(USART1_IRQn, 3, 1); + HAL_NVIC_EnableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspInit 1 */ /* USER CODE END USART1_MspInit 1 */ @@ -159,7 +162,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx); /* USART2 interrupt Init */ - HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); + HAL_NVIC_SetPriority(USART2_IRQn, 3, 0); HAL_NVIC_EnableIRQ(USART2_IRQn); /* USER CODE BEGIN USART2_MspInit 1 */ @@ -215,4 +218,5 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) /* USER CODE BEGIN 1 */ + /* USER CODE END 1 */ diff --git a/MDK-ARM/CAR.uvoptx b/MDK-ARM/CAR.uvoptx index 2c48a98..6733efa 100644 --- a/MDK-ARM/CAR.uvoptx +++ b/MDK-ARM/CAR.uvoptx @@ -135,7 +135,7 @@ 0 CMSIS_AGDI - -X"" -O206 -S8 -C0 -P00 -N00("ARM CoreSight SW-DP") -D00(1BA01477) -L00(0) -TO18 -TC10000000 -TP20 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103C8$Flash\STM32F10x_128.FLM) + -X"" -O206 -S8 -C0 -P00000000 -N00("ARM CoreSight SW-DP") -D00(1BA01477) -L00(0) -TO65554 -TC10000000 -TT10000000 -TP20 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC1000 -FN1 -FF0STM32F10x_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32F103C8$Flash\STM32F10x_128.FLM) 0 @@ -157,70 +157,54 @@ 0 0 - 39 + 34 1 -
134225148
+
134226582
0 0 0 0 0 1 - D:\OneDrive\OneDrive - my.swjtu.edu.cn\Data\program code\Projects\Car\Core\Src\hcsr04.c + ..\Peripheral\Src\bluetooth.c - \\car\../Core/Src/hcsr04.c\39 + \\CAR\../Peripheral/Src/bluetooth.c\34
1 0 - 98 + 32 1 -
134229854
+
134221102
0 0 0 0 0 1 - ../Core/Src/main.c + ..\Peripheral\Src\control.c - \\car\../Core/Src/main.c\98 + \\CAR\../Peripheral/Src/control.c\32
2 0 - 45 + 24 1 -
134220846
+
0
0 0 0 0 0 - 1 - D:\OneDrive\OneDrive - my.swjtu.edu.cn\Data\program code\Projects\Car\Core\Src\hcsr04.c + 0 + ..\Peripheral\Src\bluetooth.c - \\car\../Core/Src/hcsr04.c\45 +
3 0 - 32 - 1 -
134229764
- 0 - 0 - 0 - 0 - 0 - 1 - D:\OneDrive\OneDrive - my.swjtu.edu.cn\Data\program code\Projects\Car\Core\Src\hcsr04.c - - \\car\../Core/Src/hcsr04.c\32 -
- - 4 - 0 99 1
0
@@ -235,7 +219,7 @@
- 5 + 4 0 97 1 @@ -255,22 +239,25 @@ 0 1 - time - - - 1 - 1 - distance + num,0x0A 1 - 0 - &rxSize + 8 + rxBuffer 0 + + + 2 + 0 + num + 0 + + 0 @@ -315,7 +302,7 @@ 1 - 0 + 1 0 2 10000000 @@ -344,8 +331,8 @@ - Core - 1 + Application/User/App + 0 0 0 0 @@ -356,14 +343,34 @@ 0 0 0 + ..\App\Src\app_line_seek.c + app_line_seek.c + 0 + 0 + + + + + Application/User/Core + 1 + 0 + 0 + 0 + + 3 + 3 + 1 + 0 + 0 + 0 ../Core/Src/main.c main.c 0 0 - 2 - 3 + 3 + 4 1 0 0 @@ -374,8 +381,8 @@ 0 - 2 - 4 + 3 + 5 1 0 0 @@ -386,8 +393,8 @@ 0 - 2 - 5 + 3 + 6 1 0 0 @@ -398,8 +405,8 @@ 0 - 2 - 6 + 3 + 7 1 0 0 @@ -410,8 +417,8 @@ 0 - 2 - 7 + 3 + 8 1 0 0 @@ -422,8 +429,20 @@ 0 - 2 - 8 + 3 + 9 + 1 + 0 + 0 + 0 + ..\Core\Src\syscalls.c + syscalls.c + 0 + 0 + + + 3 + 10 1 0 0 @@ -434,8 +453,8 @@ 0 - 2 - 9 + 3 + 11 1 0 0 @@ -445,15 +464,83 @@ 0 0 + + + + Application/User/Peripheral + 1 + 0 + 0 + 0 - 2 - 10 + 4 + 12 1 0 0 0 - ..\Core\Src\syscalls.c - syscalls.c + ..\Peripheral\Src\bluetooth.c + bluetooth.c + 0 + 0 + + + 4 + 13 + 1 + 0 + 0 + 0 + ..\Peripheral\Src\control.c + control.c + 0 + 0 + + + 4 + 14 + 1 + 0 + 0 + 0 + ..\Peripheral\Src\hcsr04.c + hcsr04.c + 0 + 0 + + + 4 + 15 + 1 + 0 + 0 + 0 + ..\Peripheral\Src\led.c + led.c + 0 + 0 + + + 4 + 16 + 1 + 0 + 0 + 0 + ..\Peripheral\Src\line_seek.c + line_seek.c + 0 + 0 + + + 4 + 17 + 1 + 0 + 0 + 0 + ..\Peripheral\Src\motor.c + motor.c 0 0 @@ -466,8 +553,8 @@ 0 0 - 3 - 11 + 5 + 18 1 0 0 @@ -478,8 +565,8 @@ 0 - 3 - 12 + 5 + 19 1 0 0 @@ -490,8 +577,8 @@ 0 - 3 - 13 + 5 + 20 1 0 0 @@ -502,8 +589,8 @@ 0 - 3 - 14 + 5 + 21 1 0 0 @@ -514,8 +601,8 @@ 0 - 3 - 15 + 5 + 22 1 0 0 @@ -526,8 +613,8 @@ 0 - 3 - 16 + 5 + 23 1 0 0 @@ -538,8 +625,8 @@ 0 - 3 - 17 + 5 + 24 1 0 0 @@ -550,8 +637,8 @@ 0 - 3 - 18 + 5 + 25 1 0 0 @@ -562,8 +649,8 @@ 0 - 3 - 19 + 5 + 26 1 0 0 @@ -574,8 +661,8 @@ 0 - 3 - 20 + 5 + 27 1 0 0 @@ -586,8 +673,8 @@ 0 - 3 - 21 + 5 + 28 1 0 0 @@ -598,8 +685,8 @@ 0 - 3 - 22 + 5 + 29 1 0 0 @@ -610,8 +697,8 @@ 0 - 3 - 23 + 5 + 30 1 0 0 @@ -622,8 +709,8 @@ 0 - 3 - 24 + 5 + 31 1 0 0 @@ -634,8 +721,8 @@ 0 - 3 - 25 + 5 + 32 1 0 0 @@ -653,106 +740,6 @@ 0 0 0 - - 4 - 26 - 1 - 0 - 0 - 0 - ../Core/Src/system_stm32f1xx.c - system_stm32f1xx.c - 0 - 0 - - - - - User - 1 - 0 - 0 - 0 - - 5 - 27 - 1 - 0 - 0 - 0 - ..\User\Src\bluetooth.c - bluetooth.c - 0 - 0 - - - 5 - 28 - 1 - 0 - 0 - 0 - ..\User\Src\control.c - control.c - 0 - 0 - - - 5 - 29 - 1 - 0 - 0 - 0 - ..\User\Src\hcsr04.c - hcsr04.c - 0 - 0 - - - 5 - 30 - 1 - 0 - 0 - 0 - ..\User\Src\led.c - led.c - 0 - 0 - - - 5 - 31 - 1 - 0 - 0 - 0 - ..\User\Src\motor.c - motor.c - 0 - 0 - - - 5 - 32 - 1 - 0 - 0 - 0 - ..\User\Src\line_seek.c - line_seek.c - 0 - 0 - - - - - App - 0 - 0 - 0 - 0 6 33 @@ -760,8 +747,8 @@ 0 0 0 - ..\App\Src\app_line_seek.c - app_line_seek.c + ../Core/Src/system_stm32f1xx.c + system_stm32f1xx.c 0 0 diff --git a/MDK-ARM/CAR.uvprojx b/MDK-ARM/CAR.uvprojx index 2bbd6d4..025ef48 100644 --- a/MDK-ARM/CAR.uvprojx +++ b/MDK-ARM/CAR.uvprojx @@ -16,9 +16,9 @@ STM32F103C8 STMicroelectronics - Keil.STM32F1xx_DFP.2.2.0 - http://www.keil.com/pack/ - IRAM(0x20000000-0x20004FFF) IROM(0x8000000-0x800FFFF) CLOCK(8000000) CPUTYPE("Cortex-M3") TZ + Keil.STM32F1xx_DFP.2.4.1 + https://www.keil.com/pack/ + IRAM(0x20000000-0x20004FFF) IROM(0x8000000-0x800FFFF) CLOCK(8000000) CPUTYPE("Cortex-M3") TZ @@ -48,8 +48,8 @@ 0 1 - car\ - car + CAR\ + CAR 1 0 1 @@ -81,7 +81,7 @@ 0 - 1 + 0 0 @@ -339,7 +339,7 @@ USE_HAL_DRIVER,STM32F103xB - ../Core/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F1xx/Include;../Drivers/CMSIS/Include;../User/Inc;../App/Inc + ../App/Inc;../Core/Inc;../Peripheral/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F1xx/Include;../Drivers/CMSIS/Include @@ -352,7 +352,7 @@ 0 0 0 - 4 + 1 @@ -391,7 +391,17 @@ - Core + Application/User/App + + + app_line_seek.c + 1 + ..\App\Src\app_line_seek.c + + + + + Application/User/Core main.c @@ -407,57 +417,6 @@ dma.c 1 ../Core/Src/dma.c - - - 2 - 0 - 0 - 0 - 0 - 1 - 2 - 2 - 2 - 2 - 11 - - - 1 - - - - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 2 - 0 - 2 - 2 - 2 - 2 - 2 - 0 - 0 - 2 - 2 - 2 - 2 - 2 - - - - - - - - - i2c.c @@ -474,6 +433,11 @@ 1 ../Core/Src/usart.c + + syscalls.c + 1 + ..\Core\Src\syscalls.c + stm32f1xx_it.c 1 @@ -484,10 +448,40 @@ 1 ../Core/Src/stm32f1xx_hal_msp.c + + + + Application/User/Peripheral + - syscalls.c + bluetooth.c 1 - ..\Core\Src\syscalls.c + ..\Peripheral\Src\bluetooth.c + + + control.c + 1 + ..\Peripheral\Src\control.c + + + hcsr04.c + 1 + ..\Peripheral\Src\hcsr04.c + + + led.c + 1 + ..\Peripheral\Src\led.c + + + line_seek.c + 1 + ..\Peripheral\Src\line_seek.c + + + motor.c + 1 + ..\Peripheral\Src\motor.c @@ -581,51 +575,6 @@ - - User - - - bluetooth.c - 1 - ..\User\Src\bluetooth.c - - - control.c - 1 - ..\User\Src\control.c - - - hcsr04.c - 1 - ..\User\Src\hcsr04.c - - - led.c - 1 - ..\User\Src\led.c - - - motor.c - 1 - ..\User\Src\motor.c - - - line_seek.c - 1 - ..\User\Src\line_seek.c - - - - - App - - - app_line_seek.c - 1 - ..\App\Src\app_line_seek.c - - - ::CMSIS diff --git a/User/Inc/bluetooth.h b/Peripheral/Inc/bluetooth.h similarity index 100% rename from User/Inc/bluetooth.h rename to Peripheral/Inc/bluetooth.h diff --git a/Peripheral/Inc/control.h b/Peripheral/Inc/control.h new file mode 100644 index 0000000..24c2799 --- /dev/null +++ b/Peripheral/Inc/control.h @@ -0,0 +1,21 @@ +#ifndef __CONTROL_H +#define __CONTROL_H + +#include "main.h" + +#define MODE_STOP 0 +// 遥控模式 +#define MODE_REMOTE 1 +// 巡线模式 +#define MODE_PATROL 2 +// 避障模式 +#define MODE_AVOID 3 + +// 小车运行模式 +extern uint8_t mode; + +void CONTROL_CommonCmd(uint8_t type, uint8_t state); +void CONTROL_RGB(uint8_t r, uint8_t g, uint8_t b); +void CONTROL_Mode(uint8_t new_mode, uint8_t state); + +#endif diff --git a/User/Inc/hcsr04.h b/Peripheral/Inc/hcsr04.h similarity index 65% rename from User/Inc/hcsr04.h rename to Peripheral/Inc/hcsr04.h index 4ebc588..fc23f64 100644 --- a/User/Inc/hcsr04.h +++ b/Peripheral/Inc/hcsr04.h @@ -4,12 +4,12 @@ #include "main.h" extern uint32_t sonor_distance; // 测量出的距离,单位mm -extern uint8_t sonor_state; // 测距状态变量,0:未测量,1:正在测量,2:已测量 +extern uint8_t sonor_state; // 测距状态变量,0:未测量,1:正在测量,2:已测量 void HC_SR04_Init(void); uint32_t sonar_mm(void); // 测距完毕回调,执行完自定义任务后将sonor_state置0 - void Sonar_CP_Callback(void); +void Sonar_CP_Callback(void); #endif diff --git a/User/Inc/led.h b/Peripheral/Inc/led.h similarity index 100% rename from User/Inc/led.h rename to Peripheral/Inc/led.h diff --git a/User/Inc/line_seek.h b/Peripheral/Inc/line_seek.h similarity index 88% rename from User/Inc/line_seek.h rename to Peripheral/Inc/line_seek.h index 1ff2bdb..7873655 100644 --- a/User/Inc/line_seek.h +++ b/Peripheral/Inc/line_seek.h @@ -1,6 +1,8 @@ #ifndef __LINE_SEEK_H__ #define __LINE_SEEK_H__ +#include "main.h" + // 获取巡线状态 void LineSeek_Get(int *p_iL1, int *p_iL2, int *p_iR1, int *p_iR2); diff --git a/User/Inc/motor.h b/Peripheral/Inc/motor.h similarity index 100% rename from User/Inc/motor.h rename to Peripheral/Inc/motor.h diff --git a/User/Src/bluetooth.c b/Peripheral/Src/bluetooth.c similarity index 56% rename from User/Src/bluetooth.c rename to Peripheral/Src/bluetooth.c index 7b7a470..931e1d4 100644 --- a/User/Src/bluetooth.c +++ b/Peripheral/Src/bluetooth.c @@ -1,29 +1,41 @@ #include "bluetooth.h" - +#include "usart.h" +#include "control.h" #include #include - -#include "control.h" -#include "usart.h" +#include "syscalls.h" #define BUFFER_SIZE 100 #define MESSAGE_SIZE 19 #define CMD_NUM 9 -static int8_t rxBuffer[BUFFER_SIZE], txBuffer[BUFFER_SIZE]; +static int8_t rxBuffer[BUFFER_SIZE], txBuffer[10000]; +static uint16_t txLen; static uint8_t cmdIndex; void BLUETOOTH_Init(void) { HAL_UARTEx_ReceiveToIdle_DMA(&huart2, rxBuffer, 100); + // HAL_UART_Receive_IT(&huart2, txBuffer + txLen, 1); } +// void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) +// { +// if (huart == &huart2) +// { +// ++txLen; +// if (txLen == 10000) +// txLen = 0; +// } +// } + void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { if (huart == &huart2 && rxBuffer[0] == '$' && rxBuffer[Size - 1] == '#') { + rxBuffer[Size] = '\0'; // 通用协议 - if (rxBuffer[2] == ',') + if (mode == MODE_REMOTE && rxBuffer[2] == ',') { cmdIndex = 0; for (uint8_t i = 1; i < MESSAGE_SIZE; i += 2) @@ -33,13 +45,21 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) break; if (cmdIndex == CMD_NUM) cmdIndex = 0; - CONTROL_CommonCmd(cmdIndex, rxBuffer[cmdIndex * 2 + 1]); + CONTROL_CommonCmd(cmdIndex, rxBuffer[cmdIndex * 2 + 1] - '0'); } else { static uint16_t num[6]; - switch (rxBuffer[5]) + + // 模式选择 + if (rxBuffer[5] == 'M') { + sscanf(rxBuffer + 5, "%*4s%c%c", num, num + 1); + CONTROL_Mode(num[0] - '0', num[1] - '0'); + } + else if (mode == MODE_REMOTE) + switch (rxBuffer[5]) + { // 机械手 case 'S': break; @@ -51,17 +71,17 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) sscanf(rxBuffer + 5, "%*3s%hu,%*3s%hu,%*3s%hu", num, num + 1, num + 2); CONTROL_RGB(num[0], num[1], num[2]); break; - - // 模式选择 - case 'M': - sscanf(rxBuffer + 5, "%*4s%c%c", num, num + 1); - CONTROL_Mode(num[0] - '0', num[1] - '0'); - break; - } + } } - memcpy(txBuffer, rxBuffer, Size); - printf("%s", txBuffer); - HAL_UART_Transmit_IT(&huart2, txBuffer, Size); + // my_printf(&huart2, rxBuffer); + // strcpy(txBuffer + txLen, rxBuffer); + // if (txLen + Size >= 9999) + // txLen = 0; + // else + // txLen += Size; + // txBuffer[txLen] = '\0'; + // if (strcmp(rxBuffer, "$send#") == 0) + // my_printf(&huart2, txBuffer); BLUETOOTH_Init(); } } diff --git a/Peripheral/Src/control.c b/Peripheral/Src/control.c new file mode 100644 index 0000000..9765e99 --- /dev/null +++ b/Peripheral/Src/control.c @@ -0,0 +1,189 @@ +#include "control.h" +#include "hcsr04.h" +#include "led.h" +#include "motor.h" + +#define CMD_MOVE 0 +#define CMD_ROTATE 1 +#define CMD_BUZZER 2 +#define CMD_SPEED 3 +// #define CMD_SHAKE 4 +// #define CMD_SING 5 +#define CMD_LED 6 +// #define CMD_FIRE 7 +// #define CMD_SERVO 8 + +// 小车默认速度 +static int8_t speed = 20; +uint8_t mode = MODE_REMOTE; + +void carMove(uint8_t state); +void carSpin(uint8_t state); +void callBuzzer(uint8_t state); +void setSpeed(uint8_t state); +// void shake(uint8_t state); +// void sing(uint8_t state); +void setLed(uint8_t state); +// void fire(uint8_t state); +// void servo(uint8_t state); + +void CONTROL_CommonCmd(uint8_t type, uint8_t state) +{ + switch (type) + { + case CMD_MOVE: + carMove(state); + break; + case CMD_ROTATE: + carSpin(state); + break; + case CMD_BUZZER: + callBuzzer(state); + break; + case CMD_SPEED: + setSpeed(state); + break; + // case CMD_SHAKE: + // break; + // case CMD_SING: + // break; + case CMD_LED: + setLed(state); + break; + // case CMD_FIRE: + // break; + // case CMD_SERVO: + // break; + } +} + +void CONTROL_RGB(uint8_t r, uint8_t g, uint8_t b) +{ + LED_Start(LED_ALL); + LED_SetDuty(r, g, b); +} + +void CONTROL_Mode(uint8_t new_mode, uint8_t state) +{ + if (state == 0) + { + mode = MODE_STOP; + MOTOR_Stop(); + } + else + { + mode = new_mode; + switch (new_mode) + { + case MODE_REMOTE: + speed = 20; + MOTOR_SetDuty(speed, speed); + MOTOR_Start(); + break; + case MODE_PATROL: + break; + case MODE_AVOID: + break; + default: + mode = MODE_STOP; + MOTOR_Stop(); + break; + } + } +} + +void carMove(uint8_t state) +{ + if (state == 0) + MOTOR_Stop(); + else + { + MOTOR_Start(); + switch (state) + { + // 前进 + case 1: + MOTOR_SetDuty(speed, speed); + break; + // 后退 + case 2: + MOTOR_SetDuty(-speed, -speed); + break; + // 左转 + case 3: + MOTOR_SetDuty(0, speed); + break; + // 右转 + case 4: + MOTOR_SetDuty(speed, 0); + break; + } + } +} + +void carSpin(uint8_t state) +{ + if (state == 1) + MOTOR_SetDuty(speed, -speed); + else if (state == 2) + MOTOR_SetDuty(-speed, speed); +} + +void callBuzzer(uint8_t state) +{ + HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_RESET); + HAL_Delay(200); + HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_SET); +} + +void setSpeed(uint8_t state) +{ + if (state == 1) + speed += 10; + else if (state == 2) + speed -= 10; + if (speed > 100) + speed = 100; + else if (speed < 20) + speed = 20; +} + +void setLed(uint8_t state) +{ + LED_Start(LED_ALL); + switch (state) + { + // 白 + case 1: + LED_SetDuty(255, 255, 255); + break; + // 红 + case 2: + LED_SetDuty(255, 0, 0); + break; + // 绿 + case 3: + LED_SetDuty(0, 255, 0); + break; + // 蓝 + case 4: + LED_SetDuty(0, 0, 255); + break; + // 青 + case 5: + LED_SetDuty(0, 255, 255); + break; + // 品红 + case 6: + LED_SetDuty(255, 0, 255); + break; + // 黄 + case 7: + LED_SetDuty(255, 255, 0); + break; + // 关 + default: + LED_SetDuty(0, 0, 0); + break; + } +} diff --git a/User/Src/hcsr04.c b/Peripheral/Src/hcsr04.c similarity index 69% rename from User/Src/hcsr04.c rename to Peripheral/Src/hcsr04.c index 0c6610b..20fba15 100644 --- a/User/Src/hcsr04.c +++ b/Peripheral/Src/hcsr04.c @@ -1,14 +1,13 @@ #include "hcsr04.h" - -#include "stm32f1xx_hal_tim.h" #include "tim.h" +#include "syscalls.h" -uint64_t time; // 声明变量,用来计时 -uint64_t time_end; // 声明变量,存储回波信号时间 -uint8_t sonor_state = 1; // 测距状态变量,0:未测量,1:正在测量,2:已测量 -uint32_t sonor_distance; // 测量出的距离,单位mm +static uint64_t time_start; // 声明变量,用来计时 +static uint64_t time_end; // 声明变量,存储回波信号时间 +uint8_t sonor_state; // 测距状态变量,0:未测量,1:正在测量,2:已测量 +uint32_t sonor_distance; // 测量出的距离,单位mm -void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim); +void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim); // 初始化超声波测距 void HC_SR04_Init(void) @@ -36,11 +35,12 @@ uint32_t sonar_mm(void) return sonor_distance; } -void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) +void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if (htim == &htim2) - { // 每10us自增一 - time++; + { + // 每10us自增一 + ++time_start; } } @@ -50,22 +50,22 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if (HAL_GPIO_ReadPin(Echo_GPIO_Port, Echo_Pin)) { + time_start = 0; HAL_TIM_Base_Start_IT(&htim2); - time = 0; } else { - time_end = time; + time_end = time_start; HAL_TIM_Base_Stop_IT(&htim2); sonor_state = 2; Sonar_CP_Callback(); - sonor_state = 0; } } } -// 测距完毕回调,执行完自定义任务后将sonor_state置0void -__attribute__((weak)) void Sonar_CP_Callback(void) +// 测距完毕回调,执行完自定义任务后将sonor_state置0 +__attribute__((weak)) void Sonar_CP_Callback() { - printf("distance = %d (mm)\n", sonor_distance); + my_printf(HUART2, "distance = %d (mm)\n", sonor_distance); + sonor_state = 0; } diff --git a/User/Src/led.c b/Peripheral/Src/led.c similarity index 100% rename from User/Src/led.c rename to Peripheral/Src/led.c diff --git a/User/Src/line_seek.c b/Peripheral/Src/line_seek.c similarity index 96% rename from User/Src/line_seek.c rename to Peripheral/Src/line_seek.c index 4c16608..446215a 100644 --- a/User/Src/line_seek.c +++ b/Peripheral/Src/line_seek.c @@ -1,8 +1,5 @@ - #include "line_seek.h" -#include "main.h" - /** * @brief 获取巡线状态 * @param[in] int *p_iL1, int *p_iL2, int *p_iR1, int *p_iR2 四路巡线位指针 diff --git a/User/Src/motor.c b/Peripheral/Src/motor.c similarity index 71% rename from User/Src/motor.c rename to Peripheral/Src/motor.c index 33d20f5..cf1860b 100644 --- a/User/Src/motor.c +++ b/Peripheral/Src/motor.c @@ -3,6 +3,7 @@ #include "tim.h" #define MAX_SPEED 100 +#define MIN_SPEED 15 #define MOTOR_L 1 #define MOTOR_R 2 #define MOTOR_TIM (&htim4) @@ -37,30 +38,21 @@ void MOTOR_Stop(void) /// @brief 控制电机占空比 -/// @param leftDuty 绝对值 20-100, 支持正负 -/// @param rightDuty 绝对值 20-100,支持正负 +/// @param leftDuty 绝对值 0或20-100, 支持正负 +/// @param rightDuty 绝对值 0或20-100,支持正负 void MOTOR_SetDuty(int8_t leftDuty, int8_t rightDuty) { leftDuty = preProcess(leftDuty, MOTOR_L); rightDuty = preProcess(rightDuty, MOTOR_R); - uint16_t leftSpeed = (uint32_t)MAX_SPEED * leftDuty / 100; - __HAL_TIM_SetCompare(MOTOR_TIM, MOTOR_L_CHAN, leftSpeed); - __HAL_TIM_SetCompare(MOTOR_TIM, MOTOR_R_CHAN, leftSpeed); - - uint16_t rightSpeed = (uint32_t)MAX_SPEED * rightDuty / 100; - __HAL_TIM_SetCompare(MOTOR_TIM, MOTOR_L_CHAN, rightSpeed); - __HAL_TIM_SetCompare(MOTOR_TIM, MOTOR_R_CHAN, rightSpeed); + __HAL_TIM_SetCompare(MOTOR_TIM, MOTOR_L_CHAN, leftDuty); + __HAL_TIM_SetCompare(MOTOR_TIM, MOTOR_R_CHAN, rightDuty); } int8_t preProcess(int8_t duty, uint8_t motor) { - if (duty >= 0) + if (duty > 0) { - if (duty > 100) - duty = 100; - else if (duty < 20) - duty = 20; if (motor & MOTOR_L) { HAL_GPIO_WritePin(L_FORWARD_GPIO_Port, L_FORWARD_Pin, GPIO_PIN_SET); @@ -71,13 +63,11 @@ int8_t preProcess(int8_t duty, uint8_t motor) HAL_GPIO_WritePin(R_FORWARD_GPIO_Port, R_FORWARD_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(R_BACKWARD_GPIO_Port, R_BACKWARD_Pin, GPIO_PIN_RESET); } + } - else + else if (duty < 0) { - if (duty < -100) - duty = -100; - else if (duty > -20) - duty = -20; + duty = -duty; if (motor & MOTOR_L) { HAL_GPIO_WritePin(L_FORWARD_GPIO_Port, L_FORWARD_Pin, GPIO_PIN_RESET); @@ -89,5 +79,24 @@ int8_t preProcess(int8_t duty, uint8_t motor) HAL_GPIO_WritePin(R_BACKWARD_GPIO_Port, R_BACKWARD_Pin, GPIO_PIN_SET); } } + else + { + duty = MAX_SPEED; + if (motor & MOTOR_L) + { + HAL_GPIO_WritePin(L_FORWARD_GPIO_Port, L_FORWARD_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(L_BACKWARD_GPIO_Port, L_BACKWARD_Pin, GPIO_PIN_RESET); + } + if (motor & MOTOR_R) + { + HAL_GPIO_WritePin(R_FORWARD_GPIO_Port, R_FORWARD_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(R_BACKWARD_GPIO_Port, R_BACKWARD_Pin, GPIO_PIN_RESET); + } + } + + if (duty > MAX_SPEED) + duty = MAX_SPEED; + if (duty < MIN_SPEED) + duty = MIN_SPEED; return duty; } diff --git a/User/Inc/control.h b/User/Inc/control.h deleted file mode 100644 index 0a439e5..0000000 --- a/User/Inc/control.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef __CONTROL_H -#define __CONTROL_H - -#include "main.h" - -void CONTROL_CommonCmd(uint8_t type, uint8_t state); -void CONTROL_RGB(uint8_t r, uint8_t g, uint8_t b); -void CONTROL_Mode(uint8_t mode, uint8_t state); - -#endif diff --git a/User/Src/control.c b/User/Src/control.c deleted file mode 100644 index 81ab58e..0000000 --- a/User/Src/control.c +++ /dev/null @@ -1,88 +0,0 @@ -#include "control.h" -#include "hcsr04.h" -#include "led.h" -#include "motor.h" - -#define CMD_MOVE 0 -#define CMD_ROTATE 1 -#define CMD_BUZZER 2 -#define CMD_SPEED 3 -// #define CMD_SHAKE 4 -// #define CMD_SING 5 -#define CMD_LED 6 -// #define CMD_FIRE 7 -// #define CMD_SERVO 8 - -void move(uint8_t state); -void rotate(uint8_t state); -void buzzer(uint8_t state); -void speed(uint8_t state); -// void shake(uint8_t state); -// void sing(uint8_t state); -void led(uint8_t state); -// void fire(uint8_t state); -// void servo(uint8_t state); - -void CONTROL_CommonCmd(uint8_t type, uint8_t state) -{ - switch (type) - { - case CMD_MOVE: - move(state); - break; - case CMD_ROTATE: - rotate(state); - break; - case CMD_BUZZER: - buzzer(state); - break; - case CMD_SPEED: - speed(state); - break; - // case CMD_SHAKE: - // break; - // case CMD_SING: - // break; - case CMD_LED: - led(state); - break; - // case CMD_FIRE: - // break; - // case CMD_SERVO: - // break; - } -} - -void CONTROL_RGB(uint8_t r, uint8_t g, uint8_t b) -{ - LED_Start(LED_ALL); - LED_SetDuty(r, g, b); -} - -void CONTROL_Mode(uint8_t mode, uint8_t state) -{ -} - -void move(uint8_t state) -{ -} - -void rotate(uint8_t state) -{ -} - -void buzzer(uint8_t state) -{ - if (state) - HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_RESET); - else - HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_SET); -} - -void speed(uint8_t state) -{ -} - -void led(uint8_t state) -{ -}