/* shoot Task */ /* Includes ----------------------------------------------------------------- */ #include "task/user_task.h" /* USER INCLUDE BEGIN */ #include "module/shoot.h" #include "module/config.h" #include "device/ai.h" /* USER INCLUDE END */ /* Private typedef ---------------------------------------------------------- */ /* Private define ----------------------------------------------------------- */ /* Private macro ------------------------------------------------------------ */ /* Private variables -------------------------------------------------------- */ /* USER STRUCT BEGIN */ Shoot_t shoot; Shoot_CMD_t shoot_cmd; AI_result_t shoot_ai_result_cmd; PackageMCU_t shoot_ai_mcu_package; /* USER STRUCT END */ /* Private function --------------------------------------------------------- */ /* Exported functions ------------------------------------------------------- */ void Task_shoot(void *argument) { (void)argument; /* 未使用argument,消除警告 */ /* 计算任务运行到指定频率需要等待的tick数 */ const uint32_t delay_tick = osKernelGetTickFreq() / SHOOT_FREQ; osDelay(SHOOT_INIT_DELAY); /* 延时一段时间再开启任务 */ uint32_t tick = osKernelGetTickCount(); /* 控制任务运行频率的计时 */ /* USER CODE INIT BEGIN */ Shoot_Init(&shoot,&Config_GetRobotParam()->shoot,SHOOT_FREQ); Shoot_SetMode(&shoot,SHOOT_MODE_SINGLE); shoot_ai_mcu_package.data.bullet_count = 0; static bool last_fire_state = false; bool current_fire_state = false; // 当前是否需要发射 static uint32_t ai_last_fire_tick = 0; static const uint32_t ai_single_interval_ms = 220; static const uint32_t ai_burst_interval_ms = 110; static const uint32_t ai_continue_interval_ms = 0; /* USER CODE INIT END */ while (1) { tick += delay_tick; /* 计算下一个唤醒时刻 */ /* USER CODE BEGIN */ osMessageQueueGet(task_runtime.msgq.shoot.cmd, &shoot_cmd, NULL, 0); osMessageQueueGet(task_runtime.msgq.shoot.ai.s_cmd, &shoot_ai_result_cmd, NULL, 0); if(shoot_cmd.control_mode==SHOOT_REMOTE) { //do nothing,使用遥控器的指令 current_fire_state = shoot_cmd.firecmd; } else if(shoot_cmd.control_mode==SHOOT_AI) { uint32_t now_ms = osKernelGetTickCount(); uint32_t interval_ms = ai_single_interval_ms; shoot_cmd.ready = true; if (shoot_ai_result_cmd.mode == 2) { switch (shoot_cmd.mode) { case SHOOT_MODE_CONTINUE: interval_ms = ai_continue_interval_ms; break; case SHOOT_MODE_BURST: interval_ms = ai_burst_interval_ms; break; case SHOOT_MODE_SINGLE: default: interval_ms = ai_single_interval_ms; break; } if (interval_ms == 0) { shoot_cmd.firecmd = true; } else if ((now_ms - ai_last_fire_tick) >= interval_ms) { shoot_cmd.firecmd = true; ai_last_fire_tick = now_ms; } else { shoot_cmd.firecmd = false; } } else { shoot_cmd.firecmd = false; ai_last_fire_tick = now_ms; } current_fire_state = shoot_cmd.firecmd; } if(current_fire_state == true && last_fire_state == false) { shoot_ai_mcu_package.data.bullet_count++; /* 每次射击时增加射击数量 */ } last_fire_state = current_fire_state; Shoot_UpdateFeedback(&shoot); Shoot_SetMode(&shoot,shoot_cmd.mode); Shoot_Control(&shoot,&shoot_cmd); osMessageQueueReset(task_runtime.msgq.shoot.ai.s_cmd_ai_bool_count); osMessageQueuePut(task_runtime.msgq.shoot.ai.s_cmd_ai_bool_count, &shoot_ai_mcu_package, 0, 0); /* USER CODE END */ osDelayUntil(tick); /* 运行结束,等待下一次唤醒 */ } }