diff --git a/.mxproject b/.mxproject index 2ee0082..c4723fa 100644 --- a/.mxproject +++ b/.mxproject @@ -7,7 +7,7 @@ HeaderPath=..\Drivers\STM32F1xx_HAL_Driver\Inc;..\Drivers\STM32F1xx_HAL_Driver\I CDefines=USE_HAL_DRIVER;STM32F103xB;USE_HAL_DRIVER;USE_HAL_DRIVER; [PreviousUsedMakefileFiles] -SourceFiles=Core\Src\main.c;Core\Src\gpio.c;Core\Src\dma.c;Core\Src\i2c.c;Core\Src\tim.c;Core\Src\usart.c;Core\Src\stm32f1xx_it.c;Core\Src\stm32f1xx_hal_msp.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_i2c.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;Core\Src\system_stm32f1xx.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_i2c.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;Core\Src\system_stm32f1xx.c;;; +SourceFiles=Core\Src\main.c;Core\Src\gpio.c;Core\Src\dma.c;Core\Src\tim.c;Core\Src\usart.c;Core\Src\stm32f1xx_it.c;Core\Src\stm32f1xx_hal_msp.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;Core\Src\system_stm32f1xx.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_tim_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_rcc_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_gpio.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_dma.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_cortex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_pwr.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_flash_ex.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_exti.c;Drivers\STM32F1xx_HAL_Driver\Src\stm32f1xx_hal_uart.c;Drivers\CMSIS\Device\ST\STM32F1xx\Source\Templates\system_stm32f1xx.c;Core\Src\system_stm32f1xx.c;;; HeaderPath=Drivers\STM32F1xx_HAL_Driver\Inc;Drivers\STM32F1xx_HAL_Driver\Inc\Legacy;Drivers\CMSIS\Device\ST\STM32F1xx\Include;Drivers\CMSIS\Include;Core\Inc; CDefines=USE_HAL_DRIVER;STM32F103xB;USE_HAL_DRIVER;USE_HAL_DRIVER; diff --git a/App/Inc/app_bus.h b/App/Inc/app_bus.h new file mode 100644 index 0000000..bdcb3e5 --- /dev/null +++ b/App/Inc/app_bus.h @@ -0,0 +1,8 @@ +#ifndef __APP_BUS_H +#define __APP_BUS_H + +void BlockDetect_Init(); +void App_Bus(void); + +#endif + diff --git a/App/Inc/app_line_seek.h b/App/Inc/app_line_seek.h index 3c94856..fcc8650 100644 --- a/App/Inc/app_line_seek.h +++ b/App/Inc/app_line_seek.h @@ -3,8 +3,13 @@ #include "tim.h" +extern int beep_time; + void LineSeek_Init(void); +void LineSeek_Start(void); +void LineSeek_Stop(void); void App_LineSeek(void); void Hanlde_Crossroad(void); -#endif \ No newline at end of file +#endif + diff --git a/App/Src/app_bus.c b/App/Src/app_bus.c new file mode 100644 index 0000000..1a5faeb --- /dev/null +++ b/App/Src/app_bus.c @@ -0,0 +1,98 @@ +#include "app_bus.h" + +#include "app_line_seek.h" +#include "block_detect.h" +#include "buzzer.h" +#include "led.h" +#include "line_seek.h" +#include "motor.h" +#include "path_plan.h" +#include "syn6288.h" + +typedef enum +{ + RUNNING, // 按路线运营 + READY_TO_PARK, // 准备进站停车(或者准备左转) + PARKING, // 进站停车 + WAIT_PASSENGER, // 等候乘客 + STOP // 停车 +} BusState; + +static BusState state; + +static int current_station; +char* stations[] = {"太平园站", "西南交大站", "新业北街站", "终点站"}; + +void App_Bus(void) +{ + // printf("bus_state:%d\n", state); + + LineSeek_GetStatusStr(); + + switch (state) + { + case RUNNING: // 按路线运营 + { + LineSeek_Start(); + if (Has_Block(FRONT)) // 前方有障碍物,停车 + { + state = STOP; + } + else if (Has_Block(RIGHT_FRONT)) // 右前方有障碍物,准备停车或者左转避障 + { + state = READY_TO_PARK; + // state = WAIT_PASSENGER; + } + else if (Has_Block(LEFT_FRONT)) // 左前方有障碍物,右转避障 + { + Direction nextDirs[] = {RIGHT, RIGHT}; + ReplacePathStep(nextDirs, 2); + } + break; + } + case READY_TO_PARK: // 准备进站停车,或者准备左转避障 + { + if (LineSeek_Equals("0xxx")) // 左方有路线,左转 + { + Direction nextDirs[] = {LEFT, LEFT}; + ReplacePathStep(nextDirs, 2); + state = RUNNING; + } + else if (LineSeek_Equals("xxx0")) // 右方有路线,右转进站 + { + Direction nextDirs[] = {RIGHT, RIGHT}; + ReplacePathStep(nextDirs, 2); + state = PARKING; + } + break; + } + case PARKING: // 进站停车 + { + if (Has_Block(RIGHT_FRONT)) // 右前方有障碍物,停车 + { + state = WAIT_PASSENGER; + } + break; + } + case WAIT_PASSENGER: // 等候乘客 + { + // TODO: 添加乘客等待逻辑 + LineSeek_Stop(); + SYN_FrameInfo(0, stations[current_station]); + BUZZER_StartNTimes(3, 500, 500); + MOTOR_Stop(); + HAL_Delay(5000); + current_station++; + state = RUNNING; + break; + } + case STOP: // 停车 + { + LineSeek_Stop(); + break; + } + default: + break; + } + App_LineSeek(); +} \ No newline at end of file diff --git a/App/Src/app_line_seek.c b/App/Src/app_line_seek.c index 5e1e5ae..703b772 100644 --- a/App/Src/app_line_seek.c +++ b/App/Src/app_line_seek.c @@ -1,6 +1,9 @@ #include "app_line_seek.h" +#include "bluetooth.h" +#include "buzzer.h" +#include "led.h" #include "line_seek.h" #include "main.h" #include "motor.h" @@ -8,16 +11,15 @@ #include "stm32f1xx_hal_tim.h" #include "syscalls.h" #include "tim.h" +#include "timer.h" #define LOW_TUNE_SPEED -5 #define LOW_SPEED 35 #define MID_SPEED 40 #define HIGH_SPEED 45 -#define MIN_TURN_TIME 300 // 鏈灏忚浆鐩磋鏃堕棿 -#define MIN_CROSSING_TIME 200 // 鏈灏忚蛋鍗佸瓧璺彛鏃堕棿 -#define MIN_CROSSING_STRAIGHT_TIME 50 // 鏈灏忚蛋鍗佸瓧璺彛鏃堕棿 -#define MIN_TUNE_TIME 40 // 鏈灏忓井璋冩椂闂 -#define MAX_TURN_TIME 4000 // 鏈澶ц浆寮椂闂 +#define MIN_TURN_TIME 200 // 鏈灏忚浆鐩磋鏃堕棿 +#define MIN_CROSSING_TIME 300 // 鏈灏忚蛋鍗佸瓧璺彛鏃堕棿 +#define MIN_TUNE_TIME 30 // 鏈灏忓井璋冩椂闂 typedef enum { @@ -27,41 +29,53 @@ typedef enum TUNE_LEFT, // 宸﹀井璋 TUNE_RIGHT, // 鍙冲井璋 TURN_LEFT_BIG, - TURN_RIGHT_BIG + TURN_RIGHT_BIG, + MOVE_STOP } MoveState; -MoveState state, preState; // 杩愬姩鐘舵 -int turn_time; // 杞集鏃堕棿 -int beep_time; // 铚傞福鍣ㄩ福鍙椂闂 -int is_crossing; // 鏄惁姝e湪璧板崄瀛楄矾鍙 +static MoveState state, preState; // 杩愬姩鐘舵 +int turn_time; // 杞集鏃堕棿 +int is_crossing; // 鏄惁姝e湪璧板崄瀛楄矾鍙 +static timer_event_id = -1; -void LINESEEK_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef* htim) +// 姣1ms涓娆″洖璋 +void LINESEEK_TURN_TIME() { if (turn_time < INT16_MAX) { - turn_time++; // 姣1ms鑷涓 - } - - if (beep_time++ > 100) - { - HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_SET); + turn_time++; } } +// 宸$嚎鍔熻兘鍒濆鍖 void LineSeek_Init(void) { - // 娉ㄥ唽瀹氭椂鍣ㄤ腑鏂孩鍑哄洖璋 - HAL_TIM_RegisterCallback(&htim4, HAL_TIM_PERIOD_ELAPSED_CB_ID, LINESEEK_HAL_TIM_PeriodElapsedCallback); - HAL_TIM_Base_Start_IT(&htim4); + // 娉ㄥ唽瀹氭椂鍣ㄤ簨浠 + timer_event_id = TIMER_AddInfiniteLoopEvent(timer_event_id, LINESEEK_TURN_TIME, 1); +} + +void LineSeek_Start(void) +{ + if (state == MOVE_STOP) + { + state = MOVE_STRAIGHT; + } +} + +void LineSeek_Stop(void) +{ + state = MOVE_STOP; } void App_LineSeek(void) { - int LineL1 = 1, LineL2 = 1, LineR1 = 1, LineR2 = 1; + // LineSeek_GetStatusStr(); - // LineSeek_GetStatus(&LineL1, &LineL2, &LineR1, &LineR2); // 鑾峰彇榛戠嚎妫娴嬬姸鎬 - - LineSeek_GetStatusStr(); + // if (state != MOVE_STOP) + // { + // BLUETOOTH_Send("line_seek state:%d, line_seek: %s\n", state, LineSeek_Status); + // // BLUETOOTH_Send("line_seek: %s \n", LineSeek_Status); + // } // printf("%s %d \n", LineSeek_Status, turn_time); // return; @@ -77,15 +91,19 @@ void App_LineSeek(void) LineSeek_Status[3] = '1'; } + if (LineSeek_Equals("0000") && !is_crossing && state != MOVE_STOP) // 鍗佸瓧璺彛 + { + Hanlde_Crossroad(); + return; + } + switch (state) { case MOVE_STRAIGHT: // 鐩磋 + LED_Stop(LED_ALL); + LED_Start(LED_R); MOTOR_SetDuty(HIGH_SPEED, HIGH_SPEED); - if (LineSeek_Equals("0000")) // 鍗佸瓧璺彛 - { - Hanlde_Crossroad(); - } - else if (LineSeek_Equals("1011")) // 宸﹀井璋 + if (LineSeek_Equals("1011")) // 宸﹀井璋 { state = TUNE_LEFT; } @@ -117,18 +135,24 @@ void App_LineSeek(void) } break; case TURN_LEFT: // 宸︾洿瑙 + + LED_Stop(LED_ALL); + LED_Start(LED_G); MOTOR_SetDuty(-HIGH_SPEED, HIGH_SPEED); - if (LineSeek_Equals("0000")) // 鍗佸瓧璺彛 - { - Hanlde_Crossroad(); - } - else if (LineSeek_Equals("1001") && - ((preState != TUNE_LEFT && turn_time >= MIN_TURN_TIME - 1) || - (preState == TUNE_LEFT && turn_time >= MIN_TUNE_TIME))) // 鐩磋 + if (LineSeek_Equals("1001") && + ((preState != TUNE_LEFT && turn_time >= MIN_TURN_TIME - 1) || + (preState == TUNE_LEFT && turn_time >= MIN_TUNE_TIME))) // 鐩磋 { state = MOVE_STRAIGHT; preState = TURN_LEFT; is_crossing = 0; + LED_Stop(LED_ALL); + LED_Start(LED_B); + } + else if (LineSeek_Equals("xxx0") && turn_time < MIN_TURN_TIME && !is_crossing) // 渚т竵瀛楄矾鍙 + { + preState = TUNE_RIGHT; + Hanlde_Crossroad(); } else if (LineSeek_Equals("xxx0") && !is_crossing) // 杞繃澶翠簡 { @@ -139,18 +163,19 @@ void App_LineSeek(void) break; case TURN_RIGHT: // 鍙崇洿瑙 MOTOR_SetDuty(HIGH_SPEED, -HIGH_SPEED); - if (LineSeek_Equals("0000")) // 鍗佸瓧璺彛 - { - Hanlde_Crossroad(); - } - else if (LineSeek_Equals("1001") && - ((preState != TUNE_RIGHT && turn_time >= MIN_TURN_TIME - 1) || - (preState == TUNE_RIGHT && turn_time >= MIN_TUNE_TIME))) // 鐩磋 + if (LineSeek_Equals("1001") && + ((preState != TUNE_RIGHT && turn_time >= MIN_TURN_TIME - 1) || + (preState == TUNE_RIGHT && turn_time >= MIN_TUNE_TIME))) // 鐩磋 { state = MOVE_STRAIGHT; preState = TURN_RIGHT; is_crossing = 0; } + else if (LineSeek_Equals("0xxx") && turn_time < MIN_TURN_TIME && !is_crossing) // 渚т竵瀛楄矾鍙 + { + preState = TUNE_LEFT; + Hanlde_Crossroad(); + } else if (LineSeek_Equals("0xxx") && !is_crossing) // 杞繃澶翠簡 { turn_time = MIN_TURN_TIME; @@ -160,11 +185,7 @@ void App_LineSeek(void) break; case TURN_LEFT_BIG: // 宸﹂攼瑙 MOTOR_SetDuty(-MID_SPEED, MID_SPEED); - if (LineSeek_Equals("0000")) // 鍗佸瓧璺彛 - { - Hanlde_Crossroad(); - } - else if (LineSeek_Equals("0001")) // 宸︾洿瑙 + if (LineSeek_Equals("0001")) // 宸︾洿瑙 { turn_time = 0; state = TURN_LEFT; @@ -178,11 +199,7 @@ void App_LineSeek(void) break; case TURN_RIGHT_BIG: // 鍙抽攼瑙 MOTOR_SetDuty(MID_SPEED, -MID_SPEED); - if (LineSeek_Equals("0000")) // 鍗佸瓧璺彛 - { - Hanlde_Crossroad(); - } - else if (LineSeek_Equals("1000")) // 鍙崇洿瑙 + if (LineSeek_Equals("1000")) // 鍙崇洿瑙 { turn_time = 0; state = TURN_RIGHT; @@ -200,13 +217,13 @@ void App_LineSeek(void) { state = MOVE_STRAIGHT; } - else if (LineSeek_Equals("0xx1") && GetCurrentDirection() != STRAIGHT) // 宸﹁浆 + else if (LineSeek_Equals("0xx1")) // 宸﹁浆 { turn_time = 0; state = TURN_LEFT; preState = TUNE_LEFT; } - else if (LineSeek_Equals("1xx0") && GetCurrentDirection() != STRAIGHT) // 鍙宠浆 + else if (LineSeek_Equals("1xx0")) // 鍙宠浆 { turn_time = 0; state = TURN_RIGHT; @@ -219,47 +236,35 @@ void App_LineSeek(void) { state = MOVE_STRAIGHT; } - else if (LineSeek_Equals("0xx1") && GetCurrentDirection() != STRAIGHT) // 宸﹁浆 + else if (LineSeek_Equals("0xx1")) // 宸﹁浆 { turn_time = 0; state = TURN_LEFT; preState = TUNE_RIGHT; } - else if (LineSeek_Equals("1xx0") && GetCurrentDirection() != STRAIGHT) // 鍙宠浆 + else if (LineSeek_Equals("1xx0")) // 鍙宠浆 { turn_time = 0; state = TURN_RIGHT; preState = TUNE_RIGHT; } break; + case MOVE_STOP: + MOTOR_Stop(); + break; default: state = MOVE_STRAIGHT; MOTOR_SetDuty(MID_SPEED, MID_SPEED); break; } - // if (turn_time >= MAX_TURN_TIME) - // { - // // state = MOVE_STRAIGHT; - // MOTOR_SetDuty(0, 0); - // } - // printf("curr state: %d \n", state); } void Hanlde_Crossroad(void) { - if (is_crossing) - { - return; - } - - beep_time = 0; - HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_RESET); - // state = MOVE_STRAIGHT; - // buzzer(1); - // HAL_Delay(50); - // buzzer(0); + // 寮鍚渹楦e櫒 + BUZZER_StartTimed(1); // 鏍规嵁瑙勫垝鐨勮矾寰勮繘琛屽喅鏂 is_crossing = 1; @@ -281,4 +286,4 @@ void Hanlde_Crossroad(void) break; } // printf("state: %d, dir: %d \n", state, direction); -} \ No newline at end of file +} diff --git a/App/Src/app_line_seek_v1.c b/App/Src/app_line_seek_v1.c deleted file mode 100644 index 09e17bc..0000000 --- a/App/Src/app_line_seek_v1.c +++ /dev/null @@ -1,88 +0,0 @@ - -#include "app_line_seek.h" - -#include "line_seek.h" -#include "main.h" -#include "motor.h" -#include "path_plan.h" -#include "syscalls.h" - -// #define LOW_SPEED 30 -// #define MID_SPEED 35 -// #define HIGH_SPEED 40 - -#define LOW_SPEED 25 -#define MID_SPEED 30 -#define HIGH_SPEED 35 - - -void App_LineSeek(void) -{ - int LineL1 = 1, LineL2 = 1, LineR1 = 1, LineR2 = 1; - - LineSeek_GetStatus(&LineL1, &LineL2, &LineR1, &LineR2); // 鑾峰彇榛戠嚎妫娴嬬姸鎬 - - LineSeek_GetStatusStr(); - printf("%s\n", LineSeek_Status); - - // return; - - if (LineSeek_Equals("0000")) // 涓佸瓧锛堝崄瀛楋級璺彛 - { - buzzer(1); - - // 鏍规嵁瑙勫垝鐨勮矾寰勮繘琛屽喅鏂 - switch (GetNextDirection()) - { - case STRAIGHT: - MOTOR_SetDuty(HIGH_SPEED, HIGH_SPEED); // 鐩磋蛋 - break; - case LEFT: - MOTOR_SetDuty(HIGH_SPEED, -HIGH_SPEED); // 宸﹁浆 - break; - case RIGHT: - MOTOR_SetDuty(-HIGH_SPEED, HIGH_SPEED); // 鍙宠浆 - break; - default: - MOTOR_Stop(); - break; - } - } - else - { - buzzer(0); - } - - if (LineSeek_Equals("1000")) // 鍙崇洿瑙 - { - MOTOR_SetDuty(HIGH_SPEED, -HIGH_SPEED); // 鍙虫棆 - // HAL_Delay(80); - } - else if (LineSeek_Equals("0001")) // 宸︾洿瑙 - { - MOTOR_SetDuty(-HIGH_SPEED, HIGH_SPEED); // 宸︽棆; - // HAL_Delay(80); - } - else if (LineSeek_Equals("0xx1")) // 宸︽渶澶栦晶妫娴(閿愯) - { - MOTOR_SetDuty(-HIGH_SPEED, MID_SPEED); // 宸︽棆 - // HAL_Delay(80); - } - else if (LineSeek_Equals("1xx0")) // 鍙虫渶澶栦晶妫娴(閿愯) - { - MOTOR_SetDuty(MID_SPEED, -HIGH_SPEED); // 鍙虫棆 - // HAL_Delay(80); - } - else if (LineSeek_Equals("x01x")) // 涓棿榛戠嚎涓婄殑浼犳劅鍣ㄥ井璋冭溅宸﹁浆 - { - MOTOR_SetDuty(0, LOW_SPEED); // 宸﹁浆 - } - else if (LineSeek_Equals("x10x")) // 涓棿榛戠嚎涓婄殑浼犳劅鍣ㄥ井璋冭溅鍙宠浆 - { - MOTOR_SetDuty(LOW_SPEED, 0); // 鍙宠浆 - } - else if (LineSeek_Equals("x00x")) // 閮芥槸榛戣壊, 鍔犻熷墠杩 - { - MOTOR_SetDuty(MID_SPEED, MID_SPEED); // 鐩磋蛋 - } -} diff --git a/App/Src/app_ultrasonic.c b/App/Src/app_ultrasonic.c index 2a2e5c2..798a4c7 100644 --- a/App/Src/app_ultrasonic.c +++ b/App/Src/app_ultrasonic.c @@ -35,7 +35,8 @@ int APP_Find_Direction(void) else if(left_block < BLOCK_DISTANCE && right_block < BLOCK_DISTANCE && straight_block < BLOCK_DISTANCE)// 鍓嶅乏鍙冲潎鏈夐殰纰嶇墿锛屽仠姝㈠苟楦g瑳 { LED_SetDuty(255,0,0); - return STOP; + // return STOP; + return -1; } else if(straight_block < BLOCK_DISTANCE && left_block < BLOCK_DISTANCE)// 鍓嶅乏鏈夐殰纰嶇墿锛屽彸杞 { diff --git a/CAR.ioc b/CAR.ioc index 4e36b4b..879ab35 100644 --- a/CAR.ioc +++ b/CAR.ioc @@ -4,7 +4,17 @@ CAD.pinconfig= CAD.provider= Dma.Request0=USART2_RX Dma.Request1=USART2_TX -Dma.RequestsNb=2 +Dma.Request2=USART1_TX +Dma.RequestsNb=3 +Dma.USART1_TX.2.Direction=DMA_MEMORY_TO_PERIPH +Dma.USART1_TX.2.Instance=DMA1_Channel4 +Dma.USART1_TX.2.MemDataAlignment=DMA_MDATAALIGN_BYTE +Dma.USART1_TX.2.MemInc=DMA_MINC_ENABLE +Dma.USART1_TX.2.Mode=DMA_NORMAL +Dma.USART1_TX.2.PeriphDataAlignment=DMA_PDATAALIGN_BYTE +Dma.USART1_TX.2.PeriphInc=DMA_PINC_DISABLE +Dma.USART1_TX.2.Priority=DMA_PRIORITY_LOW +Dma.USART1_TX.2.RequestParameters=Instance,Direction,PeriphInc,MemInc,PeriphDataAlignment,MemDataAlignment,Mode,Priority Dma.USART2_RX.0.Direction=DMA_PERIPH_TO_MEMORY Dma.USART2_RX.0.Instance=DMA1_Channel6 Dma.USART2_RX.0.MemDataAlignment=DMA_MDATAALIGN_BYTE @@ -82,7 +92,6 @@ 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\:3\:0\:true\:false\:true\:true\:true\:true -NVIC.DMA1_Channel7_IRQn=true\:0\:0\:false\:false\:true\:true\:true\:true NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false\:false NVIC.EXTI15_10_IRQn=true\:2\:0\:true\:false\:true\:true\:true\:true NVIC.ForceEnableDMAVector=false @@ -228,7 +237,7 @@ ProjectManager.ProjectName=CAR ProjectManager.ProjectStructure= ProjectManager.RegisterCallBack=TIM ProjectManager.StackSize=0x400 -ProjectManager.TargetToolchain=MDK-ARM V5.32 +ProjectManager.TargetToolchain=Makefile ProjectManager.ToolChainLocation= ProjectManager.UAScriptAfterPath= ProjectManager.UAScriptBeforePath= @@ -296,7 +305,7 @@ TIM4.Period=100 - 1 TIM4.Prescaler=720 - 1 TIM4.Pulse-PWM\ Generation1\ CH1=0 TIM4.Pulse-PWM\ Generation2\ CH2=0 -USART1.BaudRate=115200 +USART1.BaudRate=9600 USART1.IPParameters=VirtualMode,BaudRate,Parity,WordLength USART1.Parity=PARITY_NONE USART1.VirtualMode=VM_ASYNC diff --git a/Core/Inc/delay.h b/Core/Inc/delay.h new file mode 100644 index 0000000..3c66a53 --- /dev/null +++ b/Core/Inc/delay.h @@ -0,0 +1,10 @@ +#ifndef __DELAY_H +#define __DELAY_H + +#include "main.h" + +extern void delay_init(void); +extern void delay_us(uint32_t nus); +extern void delay_ms(uint32_t nms); + +#endif diff --git a/Core/Inc/syscalls.h b/Core/Inc/syscalls.h index 744a463..b62cfd4 100644 --- a/Core/Inc/syscalls.h +++ b/Core/Inc/syscalls.h @@ -8,11 +8,19 @@ #include #include #include + #include "usart.h" +#define DEFAULT_HUART 1 #define HUART1 (&huart1) #define HUART2 (&huart2) +#if DEFAULT_HUART == 1 +#define HUART HUART1 +#else +#define HUART HUART2 +#endif + // 鏉′欢缂栬瘧锛岄傞厤涓嶅悓骞冲彴 (GNU, GCC) #ifdef __GNUC__ #define PUTCHAR_PROTOTYPE int __io_putchar(int ch) @@ -25,10 +33,7 @@ int _read(int file, char *ptr, int len); int _write(int file, char *ptr, int len); -PUTCHAR_PROTOTYPE; -GETCHAR_PROTOTYPE; - -void delay_us(uint16_t us); +int myprintf(const char *format, ...); void my_printf(UART_HandleTypeDef *huart, const char *buf, ...); #endif diff --git a/Core/Src/delay.c b/Core/Src/delay.c new file mode 100644 index 0000000..c5b8f5a --- /dev/null +++ b/Core/Src/delay.c @@ -0,0 +1,127 @@ +#include "main.h" + +#define USE_HAL_LEGACY +#include "stm32_hal_legacy.h" + +#define Timebase_Source_is_SysTick 1 //褰揟imebase Source涓篠ysTick鏃舵敼涓1 +//#define Timebase_Source_is_SysTick 0 //褰撲娇鐢‵reeRTOS锛孴imebase Source涓哄叾浠栧畾鏃跺櫒鏃舵敼涓0 + +#if (!Timebase_Source_is_SysTick) + extern TIM_HandleTypeDef htimx; //褰撲娇鐢‵reeRTOS锛孴imebase Source涓哄叾浠栧畾鏃跺櫒鏃讹紝淇敼涓哄搴旂殑瀹氭椂鍣 + #define Timebase_htim htimx + + #define Delay_GetCounter() __HAL_TIM_GetCounter(&Timebase_htim) + #define Delay_GetAutoreload() __HAL_TIM_GetAutoreload(&Timebase_htim) +#else + #define Delay_GetCounter() (SysTick->VAL) + #define Delay_GetAutoreload() (SysTick->LOAD) +#endif + +static uint16_t fac_us = 0; +static uint32_t fac_ms = 0; + +/*鍒濆鍖*/ +void delay_init(void) +{ +#if (!Timebase_Source_is_SysTick) + fac_ms = 1000000; //浣滀负鏃跺熀鐨勮鏁板櫒鏃堕挓棰戠巼鍦℉AL_InitTick()涓璁句负浜1MHz + fac_us = fac_ms / 1000; +#else + fac_ms = SystemCoreClock / 1000; + fac_us = fac_ms / 1000; +#endif +} + +/*寰绾у欢鏃*/ +void delay_us(uint32_t nus) +{ + uint32_t ticks = 0; + uint32_t told = 0; + uint32_t tnow = 0; + uint32_t tcnt = 0; + uint32_t reload = 0; + + reload = Delay_GetAutoreload(); + + ticks = nus * fac_us; + + told = Delay_GetCounter(); + + while (1) + { + tnow = Delay_GetCounter(); + + if (tnow != told) + { + if (tnow < told) + { + tcnt += told - tnow; + } + else + { + tcnt += reload - tnow + told; + } + told = tnow; + if (tcnt >= ticks) + { + break; + } + } + } +} + +/*姣绾у欢鏃*/ +void delay_ms(uint32_t nms) +{ + uint32_t ticks = 0; + uint32_t told = 0; + uint32_t tnow = 0; + uint32_t tcnt = 0; + uint32_t reload = 0; + + reload = Delay_GetAutoreload(); + + ticks = nms * fac_ms; + + told = Delay_GetCounter(); + + while (1) + { + tnow = Delay_GetCounter(); + + if (tnow != told) + { + if (tnow < told) + { + tcnt += told - tnow; + } + else + { + tcnt += reload - tnow + told; + } + told = tnow; + if (tcnt >= ticks) + { + break; + } + } + } +} + +/*閲嶅啓HAL_Delay*/ +void HAL_Delay(uint32_t Delay) +{ + uint32_t tickstart = HAL_GetTick(); + uint32_t wait = Delay; + + /*涓嶅お鏄庣櫧瀹樻柟婧愮爜涓哄暐杩欎箞鍐欙紝浼氬寤舵椂1ms锛屾敞閲婃帀鍚庢洿鍑*/ +// /* Add a freq to guarantee minimum wait */ +// if (wait < HAL_MAX_DELAY) +// { +// wait += (uint32_t)(uwTickFreq); +// } + + while ((HAL_GetTick() - tickstart) < wait) + { + } +} diff --git a/Core/Src/main.c b/Core/Src/main.c index dfa645b..2bf2224 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -18,20 +18,25 @@ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" + #include "dma.h" +#include "gpio.h" #include "tim.h" #include "usart.h" -#include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ +#include "app_bus.h" #include "app_line_seek.h" +#include "block_detect.h" #include "bluetooth.h" #include "buzzer.h" +#include "delay.h" #include "hcsr04.h" #include "led.h" #include "motor.h" #include "path_plan.h" +#include "servo.h" #include "syscalls.h" #include "timer.h" /* USER CODE END Includes */ @@ -54,7 +59,11 @@ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ - +int printf_event_id = -1; +void Print(uint16_t time) +{ + printf("timed print: %d\n", time); +} /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ @@ -77,74 +86,92 @@ void SystemClock_Config(void); */ int main(void) { + /* USER CODE BEGIN 1 */ + int i = 0; + char pData[] = "hello world"; + /* USER CODE END 1 */ - /* USER CODE BEGIN 1 */ + /* MCU Configuration--------------------------------------------------------*/ - uint8_t pData[10] = {1, 2, 3}; - /* USER CODE END 1 */ + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ + HAL_Init(); - /* MCU Configuration--------------------------------------------------------*/ + /* USER CODE BEGIN Init */ - /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ - HAL_Init(); + /* USER CODE END Init */ - /* USER CODE BEGIN Init */ + /* Configure the system clock */ + SystemClock_Config(); - /* USER CODE END Init */ + /* USER CODE BEGIN SysInit */ - /* Configure the system clock */ - SystemClock_Config(); + /* USER CODE END SysInit */ - /* USER CODE BEGIN SysInit */ + /* Initialize all configured peripherals */ + MX_GPIO_Init(); + MX_DMA_Init(); + MX_TIM1_Init(); + MX_TIM2_Init(); + MX_TIM3_Init(); + MX_TIM4_Init(); + // MX_USART1_UART_Init(); + MX_USART2_UART_Init(); - /* USER CODE END SysInit */ + /* USER CODE BEGIN 2 */ + HAL_Delay(500); - /* Initialize all configured peripherals */ - MX_GPIO_Init(); - MX_DMA_Init(); - MX_TIM1_Init(); - MX_TIM2_Init(); - MX_TIM3_Init(); - MX_TIM4_Init(); - // MX_USART1_UART_Init(); - // MX_USART2_UART_Init(); - /* USER CODE BEGIN 2 */ - // HC_SR04_Init(); - // BLUETOOTH_Init(); - // MOTOR_Init(); - // PathPlanner_Init(); - // LineSeek_Init(); - TIMER_Init(); - /* USER CODE END 2 */ + delay_init(); + TIMER_Init(); + HC_SR04_Init(); + BLUETOOTH_Init(); + MOTOR_Init(); + PathPlanner_Init(); + LineSeek_Init(); + BlockDetect_Init(); + SERVO_Init(); + SERVO_Rotate(90); - /* Infinite loop */ - /* USER CODE BEGIN WHILE */ - while (1) - { + HAL_Delay(2000); - BUZZER_StartTimed(3); - HAL_Delay(1000); - // my_printf(HUART1, "%s\r\n", *pData); + // BUZZER_StartNTimes(20, 500, 500); + /* USER CODE END 2 */ - // if (HAL_OK == HAL_UART_Receive(&huart2, (uint8_t *)pData, 2, 1000)) - // { - // HAL_UART_Transmit(&huart2, (uint8_t *)pData, sizeof(pData), 1000); - // HAL_Delay(500); - // } + /* Infinite loop */ + /* USER CODE BEGIN WHILE */ + while (1) + { + // printf("printf: %s \r\n", pData); + // myprintf("myprintf: %s \r\n", pData); + // BUZZER_StartTimed(5); + // HAL_Delay(1000); + // my_printf(HUART1, "HUART1: %s \r\n", pData); + // my_printf(HUART2, "HUART2: %s \r\n", pData); - // HAL_Delay(2000); + // if (HAL_OK == HAL_UART_Receive(&huart2, (uint8_t *)pData, 2, 1000)) + // { + // HAL_UART_Transmit(&huart1, (uint8_t *)pData, sizeof(pData), 1000); + // HAL_Delay(500); + // } - // App_LineSeek(); + // HAL_Delay(2000); + // printf("printf: %s \r\n", pData); + App_Bus(); - // MOTOR_SetDuty(-20,20); + // BUZZER_StartTimed(1000); + // HAL_Delay(3000); + // printf("ruunning\n"); + // BUZZER_StartNTimes(5, 500, 500); + // HAL_Delay(5000); - // HAL_Delay(300); // ??300?? + // App_LineSeek(); - /* USER CODE END WHILE */ + // MOTOR_SetDuty(-20,20); - /* USER CODE BEGIN 3 */ - } - /* USER CODE END 3 */ + /* USER CODE END WHILE */ + + /* USER CODE BEGIN 3 */ + } + /* USER CODE END 3 */ } /** @@ -153,36 +180,36 @@ int main(void) */ void SystemClock_Config(void) { - RCC_OscInitTypeDef RCC_OscInitStruct = {0}; - RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; + RCC_OscInitTypeDef RCC_OscInitStruct = {0}; + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; - /** Initializes the RCC Oscillators according to the specified parameters - * in the RCC_OscInitTypeDef structure. - */ - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; - RCC_OscInitStruct.HSEState = RCC_HSE_ON; - RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; - RCC_OscInitStruct.HSIState = RCC_HSI_ON; - RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; - RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; - if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) - { - Error_Handler(); - } + /** Initializes the RCC Oscillators according to the specified parameters + * in the RCC_OscInitTypeDef structure. + */ + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) + { + Error_Handler(); + } - /** Initializes the CPU, AHB and APB buses clocks - */ - RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; - RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; - RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; - RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; - RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; + /** Initializes the CPU, AHB and APB buses clocks + */ + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; - if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) - { - Error_Handler(); - } + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) + { + Error_Handler(); + } } /* USER CODE BEGIN 4 */ @@ -195,13 +222,13 @@ void SystemClock_Config(void) */ void Error_Handler(void) { - /* USER CODE BEGIN Error_Handler_Debug */ - /* User can add his own implementation to report the HAL error return state */ - __disable_irq(); - while (1) - { - } - /* USER CODE END Error_Handler_Debug */ + /* USER CODE BEGIN Error_Handler_Debug */ + /* User can add his own implementation to report the HAL error return state */ + __disable_irq(); + while (1) + { + } + /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT @@ -214,9 +241,9 @@ void Error_Handler(void) */ void assert_failed(uint8_t *file, uint32_t line) { - /* USER CODE BEGIN 6 */ - /* User can add his own implementation to report the file name and line number, - ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ - /* USER CODE END 6 */ + /* USER CODE BEGIN 6 */ + /* User can add his own implementation to report the file name and line number, + ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ + /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ diff --git a/Core/Src/syscalls.c b/Core/Src/syscalls.c index 9fdcefd..5ceb9be 100644 --- a/Core/Src/syscalls.c +++ b/Core/Src/syscalls.c @@ -1,7 +1,7 @@ #include "syscalls.h" -extern UART_HandleTypeDef huart1; +volatile uint8_t usart_dma_tx_over = 1; // 鏉′欢缂栬瘧锛岄傞厤涓嶅悓骞冲彴 (GNU, GCC) #ifdef __GNUC__ @@ -42,7 +42,7 @@ __attribute__((weak)) int _write(int file, char *ptr, int len) */ PUTCHAR_PROTOTYPE { - HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); // 闃诲寮忔棤闄愮瓑寰 + HAL_UART_Transmit(HUART, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } @@ -58,20 +58,47 @@ PUTCHAR_PROTOTYPE GETCHAR_PROTOTYPE { uint8_t ch = 0; - HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, HAL_MAX_DELAY); + HAL_UART_Receive(HUART, (uint8_t *)&ch, 1, HAL_MAX_DELAY); return ch; } -void delay_us(uint16_t us) +// 涓插彛鍙戦佸畬姣曞洖璋冨嚱鏁帮紝璁剧疆usart_dma_tx_over鏍囧織涓1 +void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { - volatile uint32_t delay = (HAL_RCC_GetHCLKFreq() / 4000000 * us); - while (delay--); +#if DEFAULT_HUART == 1 + if (huart->Instance == USART1) +#else + if (huart->Instance == USART2) +#endif + { + usart_dma_tx_over = 1; + } } /** - * @brief 鑷畾涔塸rintf - * @param huart, buf - * @return void + * @brief 鑷畾涔塸rintf锛屼覆鍙d娇鐢ㄥご鏂囦欢瀹氫箟鐨凞EFAULT_HUART锛孌MA涓插彛涓柇鏂瑰紡鍙戦 + * + * @usage my_printf(HUART1, "USART1_Target:\r\n"); + */ +int myprintf(const char *format, ...) +{ + va_list arg; + static char SendBuff[256] = {0}; + int rv; + while (!usart_dma_tx_over); // 绛夊緟鍓嶄竴娆MA鍙戦佸畬鎴 + + va_start(arg, format); + rv = vsnprintf((char *)SendBuff, sizeof(SendBuff) + 1, (char *)format, arg); + va_end(arg); + + HAL_UART_Transmit_DMA(HUART, (uint8_t *)SendBuff, rv); + usart_dma_tx_over = 0; // 娓0鍏ㄥ眬鏍囧織锛屽彂閫佸畬鎴愬悗閲嶆柊缃1 + + return rv; +} + +/** + * @brief 鑷畾涔塸rintf, 闇鎸囧畾鍙戦佷覆鍙 * * @usage my_printf(HUART1, "USART1_Target:\r\n"); */ diff --git a/Core/Src/usart.c b/Core/Src/usart.c index 051cc8a..c214151 100644 --- a/Core/Src/usart.c +++ b/Core/Src/usart.c @@ -26,6 +26,7 @@ UART_HandleTypeDef huart1; UART_HandleTypeDef huart2; +DMA_HandleTypeDef hdma_usart1_tx; DMA_HandleTypeDef hdma_usart2_rx; DMA_HandleTypeDef hdma_usart2_tx; @@ -42,7 +43,7 @@ void MX_USART1_UART_Init(void) /* USER CODE END USART1_Init 1 */ huart1.Instance = USART1; - huart1.Init.BaudRate = 115200; + huart1.Init.BaudRate = 9600; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; @@ -115,6 +116,23 @@ void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + /* USART1 DMA Init */ + /* USART1_TX Init */ + hdma_usart1_tx.Instance = DMA1_Channel4; + hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; + hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE; + hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE; + hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; + hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; + hdma_usart1_tx.Init.Mode = DMA_NORMAL; + hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW; + if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK) + { + Error_Handler(); + } + + __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart1_tx); + /* USART1 interrupt Init */ HAL_NVIC_SetPriority(USART1_IRQn, 3, 1); HAL_NVIC_EnableIRQ(USART1_IRQn); @@ -204,6 +222,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10); + /* USART1 DMA DeInit */ + HAL_DMA_DeInit(uartHandle->hdmatx); + /* USART1 interrupt Deinit */ HAL_NVIC_DisableIRQ(USART1_IRQn); /* USER CODE BEGIN USART1_MspDeInit 1 */ diff --git a/MDK-ARM/CAR.uvoptx b/MDK-ARM/CAR.uvoptx index 30ebff0..9dce777 100644 --- a/MDK-ARM/CAR.uvoptx +++ b/MDK-ARM/CAR.uvoptx @@ -1,4 +1,4 @@ - + 1.0 @@ -45,7 +45,7 @@ 79 66 8 - + 1 @@ -104,16 +104,16 @@ 0 0 3 - - - - - - - - - - + + + + + + + + + + BIN\CMSIS_AGDI.dll @@ -130,7 +130,7 @@ 0 ARMDBGFLAGS - + 0 @@ -140,7 +140,7 @@ 0 DLGUARM - + 0 @@ -152,25 +152,9 @@ 0 0 - 34 - 1 -
134226736
- 0 - 0 - 0 - 0 - 0 - 1 - ..\Peripheral\Src\bluetooth.c - - \\CAR\../Peripheral/Src/bluetooth.c\34 -
- - 1 - 0 32 1 -
134221058
+
134222756
0 0 0 @@ -178,28 +162,60 @@ 0 1 ..\Peripheral\Src\control.c - + \\CAR\../Peripheral/Src/control.c\32
- 2 + 1 0 - 24 + 15 1 -
0
+
134222672
0 0 0 0 0 - 0 - startup_stm32f103xb.s - - + 1 + ..\Peripheral\Src\buzzer.c + + \\CAR\../Peripheral/Src/buzzer.c\15 +
+ + 2 + 0 + 135 + 1 +
134239198
+ 0 + 0 + 0 + 0 + 0 + 1 + ..\Service\Src\timer.c + + \\CAR\../Service/Src/timer.c\135
3 0 + 145 + 1 +
134239330
+ 0 + 0 + 0 + 0 + 0 + 1 + ..\Service\Src\timer.c + + \\CAR\../Service/Src/timer.c\145 +
+ + 4 + 0 97 1
0
@@ -210,11 +226,11 @@ 0 0 startup_stm32f103xb.s - - + +
- 4 + 5 0 99 1 @@ -226,8 +242,8 @@ 0 0 startup_stm32f103xb.s - - + + @@ -282,19 +298,19 @@ 0 0 - - + + 0 0 0 - - - - - - - - + + + + + + + + 1 1 @@ -459,17 +475,29 @@ 0 0 + + 3 + 12 + 1 + 0 + 0 + 0 + ..\Core\Src\delay.c + delay.c + 0 + 0 + Application/User/Service - 0 + 1 0 0 0 4 - 12 + 13 1 0 0 @@ -481,7 +509,7 @@ 4 - 13 + 14 1 0 0 @@ -491,17 +519,29 @@ 0 0 + + 4 + 15 + 1 + 0 + 0 + 0 + ..\Service\Src\block_detect.c + block_detect.c + 0 + 0 + Application/User/Peripheral - 0 + 1 0 0 0 5 - 14 + 16 1 0 0 @@ -513,7 +553,7 @@ 5 - 15 + 17 1 0 0 @@ -525,7 +565,7 @@ 5 - 16 + 18 1 0 0 @@ -537,7 +577,7 @@ 5 - 17 + 19 1 0 0 @@ -549,7 +589,7 @@ 5 - 18 + 20 1 0 0 @@ -561,7 +601,7 @@ 5 - 19 + 21 1 0 0 @@ -573,7 +613,7 @@ 5 - 20 + 22 1 0 0 @@ -585,7 +625,7 @@ 5 - 21 + 23 1 0 0 @@ -597,7 +637,7 @@ 5 - 22 + 24 1 0 0 @@ -609,7 +649,7 @@ 5 - 23 + 25 1 0 0 @@ -629,7 +669,7 @@ 0 6 - 24 + 26 1 0 0 @@ -641,7 +681,7 @@ 6 - 25 + 27 1 0 0 @@ -653,7 +693,7 @@ 6 - 26 + 28 1 0 0 @@ -665,7 +705,7 @@ 6 - 27 + 29 1 0 0 @@ -677,7 +717,7 @@ 6 - 28 + 30 1 0 0 @@ -689,7 +729,7 @@ 6 - 29 + 31 1 0 0 @@ -701,7 +741,7 @@ 6 - 30 + 32 1 0 0 @@ -713,7 +753,7 @@ 6 - 31 + 33 1 0 0 @@ -725,7 +765,7 @@ 6 - 32 + 34 1 0 0 @@ -737,7 +777,7 @@ 6 - 33 + 35 1 0 0 @@ -749,7 +789,7 @@ 6 - 34 + 36 1 0 0 @@ -761,7 +801,7 @@ 6 - 35 + 37 1 0 0 @@ -773,7 +813,7 @@ 6 - 36 + 38 1 0 0 @@ -785,7 +825,7 @@ 6 - 37 + 39 1 0 0 @@ -805,7 +845,7 @@ 0 7 - 38 + 40 1 0 0 diff --git a/MDK-ARM/CAR.uvprojx b/MDK-ARM/CAR.uvprojx index 7664ff0..8e11fbf 100644 --- a/MDK-ARM/CAR.uvprojx +++ b/MDK-ARM/CAR.uvprojx @@ -1,7 +1,10 @@ - - + + + 2.1 +
### uVision Project, (C) Keil Software
+ CAR @@ -13,31 +16,31 @@ STM32F103C8 STMicroelectronics - Keil.STM32F1xx_DFP.2.4.1 - https://www.keil.com/pack/ + Keil.STM32F1xx_DFP.2.2.0 + http://www.keil.com/pack/ IRAM(0x20000000-0x20004FFF) IROM(0x8000000-0x800FFFF) CLOCK(8000000) CPUTYPE("Cortex-M3") TZ - - - + + + 0 - - - - - - - - - - + + + + + + + + + + $$Device:STM32F103C8$SVD\STM32F103xx.svd 0 0 - - - - - + + + + + 0 0 @@ -52,15 +55,15 @@ 1 1 1 - + 1 0 0 0 0 - - + + 0 0 0 @@ -69,8 +72,8 @@ 0 0 - - + + 0 0 0 @@ -79,15 +82,15 @@ 0 1 - - + + 0 0 0 0 1 - + 0 @@ -101,8 +104,8 @@ 0 0 3 - - + + 0 @@ -111,7 +114,7 @@ DCM.DLL -pCM3 SARMCM3.DLL - + TCM.DLL -pCM3 @@ -136,10 +139,10 @@ 1 BIN\UL2CM3.DLL "" () - - - - + + + + 0 @@ -172,7 +175,7 @@ 0 0 "Cortex-M3" - + 0 0 0 @@ -306,11 +309,11 @@ 0x0 - + 1 - 2 + 1 0 0 1 @@ -333,9 +336,9 @@ 0 0 - + USE_HAL_DRIVER,STM32F103xB - + ../App/Inc;../Core/Inc;../Service/Inc;../Peripheral/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc;../Drivers/STM32F1xx_HAL_Driver/Inc/Legacy;../Drivers/CMSIS/Device/ST/STM32F1xx/Include;../Drivers/CMSIS/Include @@ -351,10 +354,10 @@ 0 1 - - - - + + + + @@ -364,15 +367,15 @@ 0 1 0 - - - - - - - - - + + + + + + + + + @@ -445,6 +448,11 @@ 1 ../Core/Src/stm32f1xx_hal_msp.c
+ + delay.c + 1 + ..\Core\Src\delay.c +
@@ -460,6 +468,11 @@ 1 ..\Service\Src\timer.c + + block_detect.c + 1 + ..\Service\Src\block_detect.c + @@ -608,18 +621,20 @@ + - + - + - + - + + @@ -628,5 +643,5 @@ - + diff --git a/Makefile b/Makefile index 3b62267..ae30941 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [4.4.0-B60] date: [Wed Jul 17 21:44:09 CST 2024] +# File automatically-generated by tool: [projectgenerator] version: [4.4.0-B60] date: [Fri Jul 19 12:38:57 CST 2024] ########################################################################################################################## # ------------------------------------------------ @@ -38,15 +38,14 @@ BUILD_DIR = build C_SOURCES = \ Core/Src/main.c \ Core/Src/gpio.c \ -Core/Src/i2c.c \ Core/Src/syscalls.c \ Core/Src/tim.c \ Core/Src/dma.c \ Core/Src/usart.c \ Core/Src/stm32f1xx_it.c \ Core/Src/stm32f1xx_hal_msp.c \ +Core/Src/delay.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \ @@ -68,11 +67,16 @@ Peripheral/Src/led.c \ Peripheral/Src/bluetooth.c \ Peripheral/Src/control.c \ Peripheral/Src/line_seek.c \ -Peripheral/Src/path_plan.c \ Peripheral/Src/servo.c \ Peripheral/Src/infrared.c \ +Peripheral/Src/syn6288.c \ +Peripheral/Src/buzzer.c \ App/Src/app_line_seek.c \ -App/Src/app_ultrasonic.c +App/Src/app_ultrasonic.c \ +App/Src/app_bus.c \ +Service/Src/path_plan.c \ +Service/Src/timer.c \ +Service/Src/block_detect.c # ASM sources @@ -134,6 +138,7 @@ AS_INCLUDES = # C includes C_INCLUDES = \ -ICore/Inc \ +-IService/Inc \ -IPeripheral/Inc \ -IApp/Inc \ -IDrivers/STM32F1xx_HAL_Driver/Inc \ diff --git a/Peripheral/Inc/buzzer.h b/Peripheral/Inc/buzzer.h index 1e5322e..9e8a984 100644 --- a/Peripheral/Inc/buzzer.h +++ b/Peripheral/Inc/buzzer.h @@ -3,11 +3,13 @@ #include "main.h" +extern int timed_beep_event_id; +extern int times_beep_event_id; + void BUZZER_Start(); void BUZZER_Stop(); -/// @brief 铚傞福鍣ㄥ畾鏃堕福鍙 -/// @param time 楦e彨鏃堕棿锛屽崟浣100姣 -void BUZZER_StartTimed(uint8_t time); +void BUZZER_StartTimed(uint16_t time); +void BUZZER_StartNTimes(uint8_t n, uint16_t on_time, uint16_t off_time); #endif diff --git a/Peripheral/Inc/led.h b/Peripheral/Inc/led.h index 76494f7..50b0b9f 100644 --- a/Peripheral/Inc/led.h +++ b/Peripheral/Inc/led.h @@ -11,6 +11,10 @@ void LED_Start(uint8_t led); void LED_Stop(uint8_t led); + +void LED_StartTimed(uint8_t led, uint16_t time); +void LED_StartNTimes(uint8_t led, uint16_t on_time, uint16_t off_time); + // duty in [0, 255] void LED_SetDuty(uint8_t r, uint8_t g, uint8_t b); diff --git a/Peripheral/Src/bluetooth.c b/Peripheral/Src/bluetooth.c index 04913c9..1863d6d 100644 --- a/Peripheral/Src/bluetooth.c +++ b/Peripheral/Src/bluetooth.c @@ -1,14 +1,16 @@ #include "bluetooth.h" -#include "usart.h" -#include "control.h" -#include "line_seek.h" -#include "infrared.h" -#include "hcsr04.h" -#include "timer.h" + +#include +#include #include #include -#include -#include + +#include "control.h" +#include "hcsr04.h" +#include "infrared.h" +#include "line_seek.h" +#include "timer.h" +#include "usart.h" #define BUFFER_SIZE 255 #define MESSAGE_SIZE 19 @@ -17,24 +19,24 @@ static int8_t rxBuffer[BUFFER_SIZE], txBuffer[BUFFER_SIZE]; static uint16_t txLen; static uint8_t cmdIndex; - +static uint8_t timer_event_id = -1; void sendSensor(void) { - static uint8_t line_seek[5]; - strcpy(line_seek, LineSeek_GetStatusStr()); - for (uint8_t i = 0; i < 5; ++i) - line_seek[i] ^= 1; - BLUETOOTH_Send("$4WD,CSB%u,PV-,GS-,LF%s,HW%hhu%hhu,GM-#", - (uint32_t)round((double)sonor_distance / 10), - line_seek, - !INFRARED_GetL(), - !INFRARED_GetR()); + // static uint8_t line_seek[5]; + // strcpy(line_seek, LineSeek_GetStatusStr()); + // for (uint8_t i = 0; i < 5; ++i) + // line_seek[i] ^= 1; + // BLUETOOTH_Send("$4WD,CSB%u,PV-,GS-,LF%s,HW%hhu%hhu,GM-#", + // (uint32_t)round((double)sonor_distance / 10), + // line_seek, + // !INFRARED_GetL(), + // !INFRARED_GetR()); } void BLUETOOTH_Init(void) { HAL_UARTEx_ReceiveToIdle_IT(&huart2, rxBuffer, 100); - TIMER_AddLoopEvent(EVENT_BLUETOOTH, sendSensor, 200); + timer_event_id = TIMER_AddInfiniteLoopEvent(timer_event_id, sendSensor, 1); } void BLUETOOTH_Send(const char *str, ...) @@ -77,19 +79,19 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) else if (mode == MODE_REMOTE) switch (rxBuffer[5]) { - // 鏈烘鎵 - case 'S': - break; - // 鑸垫満浜戝彴 - case 'P': - sscanf(rxBuffer + 5, "%*3s%hu", num); - CONTROL_Servo(180 - num[0]); - break; - // 涓冨僵鐏 - case 'C': - sscanf(rxBuffer + 5, "%*3s%hu,%*3s%hu,%*3s%hu", num, num + 1, num + 2); - CONTROL_RGB(num[0], num[1], num[2]); - break; + // 鏈烘鎵 + case 'S': + break; + // 鑸垫満浜戝彴 + case 'P': + sscanf(rxBuffer + 5, "%*3s%hu", num); + CONTROL_Servo(180 - num[0]); + break; + // 涓冨僵鐏 + case 'C': + sscanf(rxBuffer + 5, "%*3s%hu,%*3s%hu,%*3s%hu", num, num + 1, num + 2); + CONTROL_RGB(num[0], num[1], num[2]); + break; } } BLUETOOTH_Init(); diff --git a/Peripheral/Src/buzzer.c b/Peripheral/Src/buzzer.c index b9bb26a..c06bd19 100644 --- a/Peripheral/Src/buzzer.c +++ b/Peripheral/Src/buzzer.c @@ -1,7 +1,9 @@ #include "buzzer.h" + #include "timer.h" -static uint8_t state; +int timed_beep_event_id = -1; +int times_beep_event_id = -1; inline void BUZZER_Start() { @@ -11,16 +13,20 @@ inline void BUZZER_Start() inline void BUZZER_Stop() { HAL_GPIO_WritePin(FM_K2_POWERC_GPIO_Port, FM_K2_POWERC_Pin, GPIO_PIN_SET); - if (state == 1) - { - state = 0; - TIMER_DelLoopEvent(EVENT_BUZZER); - } } -void BUZZER_StartTimed(uint8_t time) +/// @brief 鍚姩铚傞福鍣ㄩ福鍙 +/// @param time 楦e彨鏃堕棿锛屽崟浣1姣 +void BUZZER_StartTimed(uint16_t time) { - state = 1; BUZZER_Start(); - TIMER_AddLoopEvent(EVENT_BUZZER, BUZZER_Stop, time * 100); + timed_beep_event_id = TIMER_AddDelayEvent(timed_beep_event_id, BUZZER_Stop, time); +} + +/// @brief 鍚姩铚傞福鍣ㄩ福鍙玭娆★紝姣忔鍙玱n_time姣锛屽仠off_time姣 +/// @param on_time 楦e彨鏃堕棿锛屽崟浣1姣 +/// @param off_time 鍋滄楦e彨鏃堕棿锛屽崟浣1姣 +void BUZZER_StartNTimes(uint8_t n, uint16_t on_time, uint16_t off_time) +{ + times_beep_event_id = TIMER_AddFiniteLoopEventWithParam(times_beep_event_id, BUZZER_StartTimed, on_time + off_time, n, on_time); } diff --git a/Peripheral/Src/hcsr04.c b/Peripheral/Src/hcsr04.c index f46e603..12785b0 100644 --- a/Peripheral/Src/hcsr04.c +++ b/Peripheral/Src/hcsr04.c @@ -1,11 +1,13 @@ #include "hcsr04.h" -#include "tim.h" -#include "syscalls.h" -static uint64_t time_start; // 澹版槑鍙橀噺锛岀敤鏉ヨ鏃 -static uint64_t time_end; // 澹版槑鍙橀噺锛屽瓨鍌ㄥ洖娉俊鍙锋椂闂 -uint8_t sonor_state; // 娴嬭窛鐘舵佸彉閲忥紝0锛氭湭娴嬮噺锛1锛氭鍦ㄦ祴閲忥紝2锛氬凡娴嬮噺 -uint32_t sonor_distance; // 娴嬮噺鍑虹殑璺濈锛屽崟浣峬m +#include "syscalls.h" +#include "delay.h" +#include "tim.h" + +static uint64_t time_start; // 澹版槑鍙橀噺锛岀敤鏉ヨ鏃 +static uint64_t time_end; // 澹版槑鍙橀噺锛屽瓨鍌ㄥ洖娉俊鍙锋椂闂 +uint8_t sonor_state; // 娴嬭窛鐘舵佸彉閲忥紝0锛氭湭娴嬮噺锛1锛氭鍦ㄦ祴閲忥紝2锛氬凡娴嬮噺 +uint32_t sonor_distance = INT32_MAX; // 娴嬮噺鍑虹殑璺濈锛屽崟浣峬m void HCSR04_HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim); @@ -18,6 +20,8 @@ void HC_SR04_Init(void) // 娉ㄥ唽瀹氭椂鍣ㄤ腑鏂孩鍑哄洖璋 HAL_TIM_RegisterCallback(&htim2, HAL_TIM_PERIOD_ELAPSED_CB_ID, HCSR04_HAL_TIM_PeriodElapsedCallback); + + sonor_distance = INT32_MAX; } // 鍚姩娴嬭窛锛屽崟浣嶏細mm @@ -27,11 +31,7 @@ uint32_t sonar_mm(void) HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_SET); // 杈撳嚭楂樼數骞 delay_us(15); // 寤舵椂15寰 HAL_GPIO_WritePin(TRIG_GPIO_Port, TRIG_Pin, GPIO_PIN_RESET); // 杈撳嚭浣庣數骞 - if (time_end / 100 < 38) - { // 鍒ゆ柇鏄惁灏忎簬38姣锛屽ぇ浜38姣鐨勫氨鏄秴鏃讹紝鐩存帴璋冨埌涓嬮潰杩斿洖0 - sonor_distance = (time_end * 346) / 2; // 璁$畻璺濈锛25掳C绌烘皵涓殑闊抽熶负346m/s - sonor_distance = sonor_distance / 100; // 鍥犱负涓婇潰鐨則ime_end鐨勫崟浣嶆槸10寰锛屾墍浠ヨ寰楀嚭鍗曚綅涓烘绫崇殑璺濈缁撴灉锛岃繕寰楅櫎浠100 - } + return sonor_distance; } @@ -58,14 +58,15 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) time_end = time_start; HAL_TIM_Base_Stop_IT(&htim2); sonor_state = 2; + sonor_distance = (time_end / 100 < 38) ? (time_end * 346) / 200 : INT32_MAX; Sonar_CP_Callback(); + sonor_state = 0; } } } -// 娴嬭窛瀹屾瘯鍥炶皟锛屾墽琛屽畬鑷畾涔変换鍔″悗灏唖onor_state缃0 +// 娴嬭窛瀹屾瘯鍥炶皟锛屾墽琛屽畬鑷畾涔変换鍔″悗浼氬皢sonor_state缃0 __attribute__((weak)) void Sonar_CP_Callback() { - my_printf(HUART1, "distance = %d (mm)\n", sonor_distance); - sonor_state = 0; + my_printf(HUART2, "distance = %d (mm)\n", sonor_distance); } diff --git a/Peripheral/Src/led.c b/Peripheral/Src/led.c index cdf511d..fe0423c 100644 --- a/Peripheral/Src/led.c +++ b/Peripheral/Src/led.c @@ -1,34 +1,49 @@ #include "led.h" + #include "tim.h" +#include "timer.h" #define LED_TIM (&htim3) #define LED_R_CHAN TIM_CHANNEL_4 #define LED_G_CHAN TIM_CHANNEL_3 #define LED_B_CHAN TIM_CHANNEL_2 +static led_timed_id = -1; +static led_times_id = -1; + void LED_Start(uint8_t led) { - if (led & LED_R) - HAL_TIM_PWM_Start(LED_TIM, LED_R_CHAN); - if (led & LED_G) - HAL_TIM_PWM_Start(LED_TIM, LED_G_CHAN); - if (led & LED_B) - HAL_TIM_PWM_Start(LED_TIM, LED_B_CHAN); + if (led & LED_R) + HAL_TIM_PWM_Start(LED_TIM, LED_R_CHAN); + if (led & LED_G) + HAL_TIM_PWM_Start(LED_TIM, LED_G_CHAN); + if (led & LED_B) + HAL_TIM_PWM_Start(LED_TIM, LED_B_CHAN); } void LED_Stop(uint8_t led) { - if (led & LED_R) - HAL_TIM_PWM_Stop(LED_TIM, LED_R_CHAN); - if (led & LED_G) - HAL_TIM_PWM_Stop(LED_TIM, LED_G_CHAN); - if (led & LED_B) - HAL_TIM_PWM_Stop(LED_TIM, LED_B_CHAN); + if (led & LED_R) + HAL_TIM_PWM_Stop(LED_TIM, LED_R_CHAN); + if (led & LED_G) + HAL_TIM_PWM_Stop(LED_TIM, LED_G_CHAN); + if (led & LED_B) + HAL_TIM_PWM_Stop(LED_TIM, LED_B_CHAN); +} + +void LED_StartTimed(uint8_t led, uint16_t time) +{ + led_timed_id = TIMER_AddDelayEvent(led_timed_id, LED_Stop, time); +} + +void LED_StartNTimes(uint8_t led, uint16_t on_time, uint16_t off_time) +{ + // led_times_id = TIMER_AddFiniteLoopEvent(led_times_id, LED_Stop, time); } void LED_SetDuty(uint8_t r, uint8_t g, uint8_t b) { - __HAL_TIM_SetCompare(LED_TIM, LED_R_CHAN, r); - __HAL_TIM_SetCompare(LED_TIM, LED_G_CHAN, g); - __HAL_TIM_SetCompare(LED_TIM, LED_B_CHAN, b); + __HAL_TIM_SetCompare(LED_TIM, LED_R_CHAN, r); + __HAL_TIM_SetCompare(LED_TIM, LED_G_CHAN, g); + __HAL_TIM_SetCompare(LED_TIM, LED_B_CHAN, b); } diff --git a/STM32Make.make b/STM32Make.make index 059cdd7..22f2e37 100644 --- a/STM32Make.make +++ b/STM32Make.make @@ -36,10 +36,12 @@ BUILD_DIR = build ###################################### # C sources C_SOURCES = \ +App/Src/app_bus.c \ App/Src/app_line_seek.c \ +App/Src/app_ultrasonic.c \ +Core/Src/delay.c \ Core/Src/dma.c \ Core/Src/gpio.c \ -Core/Src/i2c.c \ Core/Src/main.c \ Core/Src/stm32f1xx_hal_msp.c \ Core/Src/stm32f1xx_it.c \ @@ -56,20 +58,25 @@ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_flash_ex.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_gpio_ex.c \ -Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_i2c.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_pwr.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_tim_ex.c \ Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_uart.c \ -User/Src/bluetooth.c \ -User/Src/control.c \ -User/Src/hcsr04.c \ -User/Src/led.c \ -User/Src/line_seek.c \ -User/Src/motor.c \ -User/Src/path_plan.c +Peripheral/Src/bluetooth.c \ +Peripheral/Src/buzzer.c \ +Peripheral/Src/control.c \ +Peripheral/Src/hcsr04.c \ +Peripheral/Src/infrared.c \ +Peripheral/Src/led.c \ +Peripheral/Src/line_seek.c \ +Peripheral/Src/motor.c \ +Peripheral/Src/servo.c \ +Peripheral/Src/syn6288.c \ +Service/Src/block_detect.c \ +Service/Src/path_plan.c \ +Service/Src/timer.c CPP_SOURCES = \ @@ -147,7 +154,8 @@ C_INCLUDES = \ -IDrivers/CMSIS/Include \ -IDrivers/STM32F1xx_HAL_Driver/Inc \ -IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \ --IUser/Inc +-IPeripheral/Inc \ +-IService/Inc diff --git a/Service/Inc/block_detect.h b/Service/Inc/block_detect.h new file mode 100644 index 0000000..044f271 --- /dev/null +++ b/Service/Inc/block_detect.h @@ -0,0 +1,15 @@ +#ifndef __BLOCK_DETECT_H +#define __BLOCK_DETECT_H + +// 瀹氫箟闅滅鏂瑰悜鏋氫妇 +typedef enum +{ + FRONT, + LEFT_FRONT, + RIGHT_FRONT +} BlockDirection; + +void BlockDetect_Init(); +int Has_Block(BlockDirection direction); + +#endif \ No newline at end of file diff --git a/Service/Inc/path_plan.h b/Service/Inc/path_plan.h index 3cb03cd..461bb6b 100644 --- a/Service/Inc/path_plan.h +++ b/Service/Inc/path_plan.h @@ -6,24 +6,13 @@ typedef enum { STRAIGHT, LEFT, - RIGHT, - STOP + RIGHT } Direction; -// 瀹氫箟璺緞瑙勫垝鍣ㄧ粨鏋 -typedef struct -{ - int currentStep; - Direction* path; - int pathLength; -} PathPlanner; - -// 鍏ㄥ眬璺緞瑙勫垝鍣 -extern PathPlanner pathPlanner; - void PathPlanner_Init(); Direction GetCurrentDirection(); Direction GetNextDirection(); void AddPathStep(Direction step); +void ReplacePathStep(Direction* step, int n); #endif diff --git a/Service/Inc/timer.h b/Service/Inc/timer.h index 9e31a2f..78f79e4 100644 --- a/Service/Inc/timer.h +++ b/Service/Inc/timer.h @@ -3,14 +3,13 @@ #include "main.h" -#define EVENT_LED 0 -#define EVENT_BUZZER 1 -#define EVENT_BLUETOOTH 2 -#define EVENT_SERVO 3 -#define EVENT_MAX 4 +#define EVENT_MAX 20 void TIMER_Init(void); -void TIMER_AddLoopEvent(uint8_t type, void (*func)(void), uint16_t loop_time); -void TIMER_DelLoopEvent(uint8_t type); +int8_t TIMER_AddInfiniteLoopEvent(int8_t id, void (*func)(void), uint16_t loop_time); +int8_t TIMER_AddFiniteLoopEvent(int8_t id, void (*func)(void), uint16_t loop_time, uint16_t loop_cnt); +int8_t TIMER_AddFiniteLoopEventWithParam(int8_t event_id, void (*func)(uint16_t param), uint16_t loop_time, uint16_t loop_cnt, uint16_t param); +int8_t TIMER_AddDelayEvent(int8_t id, void (*func)(void), uint16_t delay_time); +void TIMER_DelLoopEvent(int8_t id); #endif diff --git a/Service/Src/block_detect.c b/Service/Src/block_detect.c new file mode 100644 index 0000000..a628c7a --- /dev/null +++ b/Service/Src/block_detect.c @@ -0,0 +1,64 @@ +#include "block_detect.h" + +#include "bluetooth.h" +#include "buzzer.h" +#include "hcsr04.h" +#include "infrared.h" +#include "syscalls.h" +#include "timer.h" + +int has_block_front; +int has_block_left; +int has_block_right; +static uint8_t timer_event_id = -1; +static uint8_t timer_event_id2 = -1; + +void INFRARED_Detect(); +void Sonar_Detect(); + +// 閬块殰妫娴嬪垵濮嬪寲 +void BlockDetect_Init() +{ + timer_event_id = TIMER_AddInfiniteLoopEvent(timer_event_id, INFRARED_Detect, 20); + timer_event_id2 = TIMER_AddInfiniteLoopEvent(timer_event_id2, Sonar_Detect, 150); +} + +// 姣20ms绾㈠妫娴嬩竴娆¢殰纰嶇墿 +void INFRARED_Detect() +{ + has_block_left = INFRARED_GetL() == INFRARED_MEET; + has_block_right = INFRARED_GetR() == INFRARED_MEET; +} + +// 姣100ms瓒呭0娉㈡娴嬩竴娆¢殰纰嶇墿 +void Sonar_Detect() +{ + if (sonor_state == 0) sonar_mm(); +} + +// 瓒呭0娉㈡祴璺濆畬姣曞洖璋 +void Sonar_CP_Callback() +{ + // BLUETOOTH_Send("f: %d, l: %d, r: %d\n sonar distance = %d (mm)\n", has_block_front, has_block_left, has_block_right, sonor_distance); + has_block_front = sonor_distance < 200; +} + +int Has_Block(BlockDirection direction) +{ + switch (direction) + { + case FRONT: + BUZZER_StartTimed(10); + return has_block_front; + break; + case LEFT_FRONT: + BUZZER_StartTimed(2); + return has_block_left; + break; + case RIGHT_FRONT: + BUZZER_StartTimed(2); + return has_block_right; + break; + } + return 0; +} diff --git a/Service/Src/path_plan.c b/Service/Src/path_plan.c index 9240296..aed1d35 100644 --- a/Service/Src/path_plan.c +++ b/Service/Src/path_plan.c @@ -4,8 +4,16 @@ #include "stdio.h" #include "stdlib.h" -// 鍏ㄥ眬璺緞瑙勫垝鍣 -PathPlanner pathPlanner; +// 瀹氫箟璺緞瑙勫垝鍣ㄧ粨鏋 +typedef struct +{ + int currentStep; + Direction* path; + int pathLength; +} PathPlanner; + +// 璺緞瑙勫垝鍣 +static PathPlanner pathPlanner; void SetPath(); @@ -24,9 +32,38 @@ void SetPath() int i; for (i = 0; i < 100; i++) { - AddPathStep(LEFT); - AddPathStep(RIGHT); AddPathStep(STRAIGHT); + AddPathStep(STRAIGHT); + AddPathStep(STRAIGHT); + AddPathStep(RIGHT); + AddPathStep(LEFT); + AddPathStep(STRAIGHT); + AddPathStep(STRAIGHT); + AddPathStep(LEFT); + AddPathStep(STRAIGHT); + AddPathStep(STRAIGHT); + AddPathStep(STRAIGHT); + AddPathStep(LEFT); + AddPathStep(LEFT); + AddPathStep(LEFT); + // AddPathStep(STRAIGHT); + // AddPathStep(STRAIGHT); + // AddPathStep(STRAIGHT); + // AddPathStep(LEFT); + // AddPathStep(LEFT); + // AddPathStep(STRAIGHT); + // AddPathStep(STRAIGHT); + // AddPathStep(STRAIGHT); + // AddPathStep(RIGHT); + // AddPathStep(STRAIGHT); + // AddPathStep(STRAIGHT); + + // AddPathStep(STRAIGHT); + // AddPathStep(STRAIGHT); + // AddPathStep(LEFT); + // AddPathStep(LEFT); + // AddPathStep(STRAIGHT); + // AddPathStep(STRAIGHT); } } @@ -52,3 +89,13 @@ void AddPathStep(Direction step) pathPlanner.path = realloc(pathPlanner.path, ++pathPlanner.pathLength * sizeof(Direction)); pathPlanner.path[pathPlanner.pathLength - 1] = step; } + +// 鏇挎崲鎺ヤ笅鏉ョ殑n涓矾寰勬柟鍚 +void ReplacePathStep(Direction* step, int n) +{ + int i; + for (i = 0; i < n; i++) + { + pathPlanner.path[(pathPlanner.currentStep + 1 + i) % pathPlanner.pathLength] = step[i]; + } +} diff --git a/Service/Src/timer.c b/Service/Src/timer.c index ecec7f7..e9f695d 100644 --- a/Service/Src/timer.c +++ b/Service/Src/timer.c @@ -1,22 +1,35 @@ #include "timer.h" -#include "tim.h" + #include +#include "tim.h" + +typedef enum +{ + INFINITE_LOOP_EVENT, // 鏃犻檺寰幆浜嬩欢 + FINITE_LOOP_EVENT, // 鏈夐檺寰幆浜嬩欢 + FINITE_LOOP_EVENT_WITH_PARAM, + DELAY_EVENT // 寤惰繜浜嬩欢 +} EventType; + typedef struct Event { + uint8_t id; void (*func)(void); + void (*param_func)(uint16_t); uint16_t time; + EventType type; uint8_t enabled; + uint16_t loop_cnt; + uint32_t begin_time; + uint16_t param; } Event; static Event event[EVENT_MAX]; -static uint32_t time_cnt; -static uint32_t time_lcm = 1; +static int event_cnt; +static uint32_t current_time; -uint32_t gcd(uint32_t a, uint32_t b); -uint32_t lcm(uint32_t a, uint32_t b); - -void TIMER_Count(void); +void TIMER_Count(TIM_HandleTypeDef *htim); void TIMER_Init(void) { @@ -24,41 +37,124 @@ void TIMER_Init(void) HAL_TIM_Base_Start_IT(&htim4); } -/// @brief 娉ㄥ唽瀹氭椂鍥炶皟浜嬩欢 +/// @brief 娉ㄥ唽鏃犻檺寰幆瀹氭椂鍥炶皟浜嬩欢 /// @param func 鍥炶皟鍑芥暟 void func(void) /// @param loop_time 瀹氭椂鏃堕棿锛屽崟浣嶆绉 -void TIMER_AddLoopEvent(uint8_t type, void (*func)(void), uint16_t loop_time) +int8_t TIMER_AddInfiniteLoopEvent(int8_t event_id, void (*func)(void), uint16_t loop_time) { - event[type].func = func; - event[type].time = loop_time; - event[type].enabled = 1; - time_lcm = lcm(time_lcm, loop_time); + if (event_id == -1) event_id = event_cnt++; + event[event_id].type = INFINITE_LOOP_EVENT; + event[event_id].id = event_id; + event[event_id].func = func; + event[event_id].time = loop_time; + event[event_id].begin_time = current_time; + event[event_id].enabled = 1; + return event_id; } -void TIMER_DelLoopEvent(uint8_t type) +/// @brief 娉ㄥ唽鏈夐檺寰幆瀹氭椂鍥炶皟浜嬩欢 +/// @param func 鍥炶皟鍑芥暟 void func(void) +/// @param loop_time 瀹氭椂鏃堕棿锛屽崟浣嶆绉 +/// @param loop_cnt 寰幆娆℃暟 +int8_t TIMER_AddFiniteLoopEventWithParam(int8_t event_id, void (*func)(uint16_t param), uint16_t loop_time, uint16_t loop_cnt, uint16_t param) { - event[type].enabled = 0; - time_lcm /= event[type].time; + if (event_id == -1) event_id = event_cnt++; + event[event_id].type = FINITE_LOOP_EVENT_WITH_PARAM; + event[event_id].id = event_id; + event[event_id].param_func = func; + event[event_id].time = loop_time; + event[event_id].begin_time = current_time; + event[event_id].enabled = 1; + event[event_id].param = param; + event[event_id].loop_cnt = loop_cnt; + return event_id; } -void TIMER_Count(void) +int8_t TIMER_AddFiniteLoopEvent(int8_t event_id, void (*func)(void), uint16_t loop_time, uint16_t loop_cnt) { - ++time_cnt; - for (uint8_t i = 0; i < EVENT_MAX; ++i) - if (event[i].enabled && time_cnt % event[i].time == 0) - event[i].func(); - if (time_cnt == time_lcm) - time_cnt = 0; + if (event_id == -1) event_id = event_cnt++; + event[event_id].type = FINITE_LOOP_EVENT; + event[event_id].id = event_id; + event[event_id].func = func; + event[event_id].time = loop_time; + event[event_id].begin_time = current_time; + event[event_id].enabled = 1; + event[event_id].loop_cnt = loop_cnt; + return event_id; } -uint32_t gcd(uint32_t a, uint32_t b) +/// @brief 娉ㄥ唽寤惰繜瀹氭椂鍥炶皟浜嬩欢锛堝崟娆★級 +/// @param func 鍥炶皟鍑芥暟 void func(void) +/// @param loop_time 瀹氭椂鏃堕棿锛屽崟浣嶆绉 +int8_t TIMER_AddDelayEvent(int8_t event_id, void (*func)(void), uint16_t delay_time) { - if (b == 0) - return a; - return gcd(b, a % b); + if (event_id == -1) event_id = event_cnt++; + event[event_id].type = DELAY_EVENT; + event[event_id].id = event_id; + event[event_id].func = func; + event[event_id].time = delay_time; + event[event_id].begin_time = current_time; + event[event_id].enabled = 1; + return event_id; } -uint32_t lcm(uint32_t a, uint32_t b) +void TIMER_DelLoopEvent(int8_t id) { - return a / gcd(a, b) * b; + event[id].enabled = 0; +} + +void TIMER_Count(TIM_HandleTypeDef *htim) +{ + ++current_time; + for (uint8_t i = 0; i < event_cnt; ++i) + { + if (event[i].enabled == 0) + continue; + + switch (event[i].type) + { + case INFINITE_LOOP_EVENT: // 鏃犻檺寰幆浜嬩欢 + if ((current_time - event[i].begin_time) % event[i].time == 0) + { + event[i].func(); + } + break; + case FINITE_LOOP_EVENT: // 鏈夐檺寰幆浜嬩欢 + if ((current_time - event[i].begin_time) % event[i].time == 0) + { + event[i].func(); + if (event[i].loop_cnt-- <= 0) + { + TIMER_DelLoopEvent(event[i].id); + } + } + break; + case FINITE_LOOP_EVENT_WITH_PARAM: + if ((current_time - event[i].begin_time) % event[i].time == 0) + { + printf("FINITE_LOOP_EVENT_WITH_PARAM %d, %d, %d\n", + current_time, event[i].begin_time, event[i].time); + event[i].param_func(event[i].param); + if (--event[i].loop_cnt <= 0) + { + printf("remove FINITE_LOOP_EVENT_WITH_PARAM %d, %d, %d\n", + current_time, event[i].begin_time, event[i].time); + TIMER_DelLoopEvent(event[i].id); + } + } + break; + case DELAY_EVENT: // 寤惰繜浜嬩欢 + if (current_time > event[i].begin_time && + (current_time - event[i].begin_time) % event[i].time == 0) + { + event[i].func(); + printf("DELAY_EVENT %d, %d, %d\n", + current_time, event[i].begin_time, event[i].time); + TIMER_DelLoopEvent(event[i].id); + } + break; + default: + break; + } + } }