训练营PLSR题目
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

348 行
9.8 KiB

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "dma.h"
  22. #include "tim.h"
  23. #include "usart.h"
  24. #include "gpio.h"
  25. /* Private includes ----------------------------------------------------------*/
  26. /* USER CODE BEGIN Includes */
  27. #include "PLSR.h"
  28. #include "includes.h"
  29. /* USER CODE END Includes */
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */
  32. uint8_t ArrFlag = 0; /* 加速度Flag */
  33. int32_t Acc; /* 加速度 */
  34. /* USER CODE END PTD */
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40. /* USER CODE END PM */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* USER CODE BEGIN PV */
  43. /* USER CODE END PV */
  44. /* Private function prototypes -----------------------------------------------*/
  45. void SystemClock_Config(void);
  46. /* USER CODE BEGIN PFP */
  47. /* USER CODE END PFP */
  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */
  50. /* 任务优先级 */
  51. #define TASK_Y1Direction 4
  52. /* 任务堆栈大小 */
  53. #define Task_Y1Direction_SIZE 256
  54. /* 任务控制块 */
  55. OS_TCB Y1Direction_Tsk;
  56. /* 任务栈 */
  57. CPU_STK Task_Y1Direction_STK[Task_Y1Direction_SIZE];
  58. /* 任务函数 */
  59. void Y1Direction(void *p_arg);
  60. /* USER CODE END 0 */
  61. /**
  62. * @brief The application entry point.
  63. * @retval int
  64. */
  65. int main(void)
  66. {
  67. /* USER CODE BEGIN 1 */
  68. OS_ERR err;
  69. /* USER CODE END 1 */
  70. /* MCU Configuration--------------------------------------------------------*/
  71. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  72. HAL_Init();
  73. /* USER CODE BEGIN Init */
  74. /* USER CODE END Init */
  75. /* Configure the system clock */
  76. SystemClock_Config();
  77. /* USER CODE BEGIN SysInit */
  78. /* USER CODE END SysInit */
  79. /* Initialize all configured peripherals */
  80. MX_GPIO_Init();
  81. MX_DMA_Init();
  82. MX_USART1_UART_Init();
  83. MX_TIM10_Init();
  84. MX_TIM9_Init();
  85. /* USER CODE BEGIN 2 */
  86. // PulseInit(0, 0, 0);//设置为第0段脉冲开始,相对模式, 最大脉冲为0
  87. // Base.AccCount = 50;
  88. SetPulse(0, 1, 5, 0, 1);
  89. // SetPulse(1, 100, 200, 0, -1);
  90. __HAL_TIM_SET_AUTORELOAD(&htim9,5-1); //设置要输出的PWM脉冲数 5个
  91. HAL_TIM_Base_Start_IT(&htim9); //启动从定时器
  92. // PulseStart(); //开始产生脉冲
  93. // HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1); //启动主定时器PWM输出
  94. BSP_Init();
  95. OSInit(&err);
  96. OSTaskCreate((OS_TCB * )&Y1Direction_Tsk, /* 任务控制块 */
  97. (CPU_CHAR* )"Y1Direction", /* 任务名字 */
  98. (OS_TASK_PTR)Y1Direction, /* 任务函数 */
  99. (void * )0, /* 传递给任务函数的参数 */
  100. (OS_PRIO )TASK_Y1Direction, /* 任务优先级 */
  101. (CPU_STK * )&Task_Y1Direction_STK[0], /* 任务堆栈基地址 */
  102. (CPU_STK_SIZE)Task_Y1Direction_SIZE/10, /* 任务堆栈深度限位 */
  103. (CPU_STK_SIZE)Task_Y1Direction_SIZE, /* 任务堆栈大小 */
  104. (OS_MSG_QTY)0, /* 任务内部消息队列能够接收的最大消息数目,为0时禁止接收消息 */
  105. (OS_TICK )0, /* 当使能时间片轮转时的时间片长度,为0时为默认长度 */
  106. (void * )0, /* 用户补充的存储区 */
  107. (OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR, /* 任务选项 */
  108. (OS_ERR * )&err); /* 存放该函数错误时的返回值 */
  109. OSStart(&err);
  110. /* USER CODE END 2 */
  111. /* Infinite loop */
  112. /* USER CODE BEGIN WHILE */
  113. while (1)
  114. {
  115. /* USER CODE END WHILE */
  116. /* USER CODE BEGIN 3 */
  117. }
  118. /* USER CODE END 3 */
  119. }
  120. /**
  121. * @brief System Clock Configuration
  122. * @retval None
  123. */
  124. void SystemClock_Config(void)
  125. {
  126. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  127. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  128. /** Configure the main internal regulator output voltage
  129. */
  130. __HAL_RCC_PWR_CLK_ENABLE();
  131. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  132. /** Initializes the RCC Oscillators according to the specified parameters
  133. * in the RCC_OscInitTypeDef structure.
  134. */
  135. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  136. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  137. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  138. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  139. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  140. RCC_OscInitStruct.PLL.PLLM = 8;
  141. RCC_OscInitStruct.PLL.PLLN = 72;
  142. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  143. RCC_OscInitStruct.PLL.PLLQ = 4;
  144. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  145. {
  146. Error_Handler();
  147. }
  148. /** Initializes the CPU, AHB and APB buses clocks
  149. */
  150. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  151. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  152. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  153. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  154. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  155. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  156. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  157. {
  158. Error_Handler();
  159. }
  160. }
  161. /* USER CODE BEGIN 4 */
  162. /**
  163. * @brief 任务1,Y1的方向
  164. * @return 无
  165. */
  166. void Y1Direction(void *p_arg)
  167. {
  168. p_arg = p_arg;
  169. while(1)
  170. {
  171. if (PulseOutput[Base.NowPulse].Direction == 1)
  172. {
  173. HAL_GPIO_WritePin(GPIOI, GPIO_PIN_8, GPIO_PIN_SET);
  174. }
  175. else
  176. {
  177. HAL_GPIO_WritePin(GPIOI, GPIO_PIN_8, GPIO_PIN_RESET);
  178. }
  179. }
  180. }
  181. /**
  182. * @brief 定时器中断回调(PWM计数)
  183. * @return 无
  184. */
  185. void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  186. {
  187. if (htim->Instance == TIM10)
  188. {
  189. if (1 == Base.PulseMod) /* 如果是绝对模式 */
  190. {
  191. if (AllPulseCount == Base.MAX_Pulse) /* 达到了最大脉冲数 */
  192. {
  193. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  194. HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_RESET);
  195. }
  196. else if(PulseCount == PulseOutput[Base.NowPulse].PulseCount) /* 如果当前段的计数等于要求的计数 */
  197. {
  198. PulseCount = 0;
  199. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  200. HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_RESET);
  201. if(PulseOutput[Base.NowPulse].NextPulse != -1)
  202. {
  203. Base.PrePulse = Base.NowPulse; /* 保存之前的脉冲段 */
  204. Base.NowPulse = PulseOutput[Base.NowPulse].NextPulse; /* 进入下一段脉冲 */
  205. Acc = GetAcc(PulseOutput[Base.PrePulse].Frequency,
  206. PulseOutput[Base.NowPulse].Frequency, Base.AccCount); /* 计算Acc */
  207. ArrFlag = 1;
  208. PulseStart(); //开始产生脉冲
  209. }
  210. }
  211. }
  212. else if (0 == Base.PulseMod) /* 如果是相对模式 */
  213. {
  214. if (PulseCount == PulseOutput[Base.NowPulse].PulseCount) /* 如果当前段的计数等于要求的计数 */
  215. {
  216. PulseCount = 0;
  217. HAL_TIM_PWM_Stop_IT(&htim10,TIM_CHANNEL_1); /* 停止PWM输出 */
  218. HAL_GPIO_WritePin(GPIOF, GPIO_PIN_6, GPIO_PIN_RESET);
  219. if(PulseOutput[Base.NowPulse].NextPulse != -1)
  220. {
  221. Base.PrePulse = Base.NowPulse; /* 保存之前的脉冲段 */
  222. Base.NowPulse = PulseOutput[Base.NowPulse].NextPulse; /* 进入下一段脉冲 */
  223. Acc = GetAcc(PulseOutput[Base.PrePulse].Frequency,
  224. PulseOutput[Base.NowPulse].Frequency, Base.AccCount); /* 计算Acc */
  225. ArrFlag = 1;
  226. PulseStart(); //开始产生脉冲
  227. }
  228. }
  229. else
  230. {
  231. PulseCount++;
  232. AllPulseCount++;
  233. }
  234. }
  235. if (ArrFlag == 1)
  236. {
  237. if(PulseCount < Base.AccCount)
  238. {
  239. SetFrequency(0, PulseOutput[Base.PrePulse].Frequency + PulseCount * Acc);
  240. }
  241. else
  242. {
  243. SetFrequency(0, PulseOutput[Base.NowPulse].Frequency);
  244. ArrFlag = 0;
  245. }
  246. }
  247. }
  248. }
  249. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  250. {
  251. if(htim == &htim9)
  252. {
  253. if(__HAL_TIM_GET_FLAG(&htim9, TIM_FLAG_CC2) != RESET) //判断是否触发中断
  254. {
  255. __HAL_TIM_CLEAR_FLAG(&htim9, TIM_FLAG_CC2); //清除中断标志
  256. HAL_GPIO_TogglePin(GPIOI, GPIO_PIN_8);
  257. }
  258. }
  259. }
  260. /* USER CODE END 4 */
  261. /**
  262. * @brief This function is executed in case of error occurrence.
  263. * @retval None
  264. */
  265. void Error_Handler(void)
  266. {
  267. /* USER CODE BEGIN Error_Handler_Debug */
  268. /* User can add his own implementation to report the HAL error return state */
  269. __disable_irq();
  270. while (1)
  271. {
  272. }
  273. /* USER CODE END Error_Handler_Debug */
  274. }
  275. #ifdef USE_FULL_ASSERT
  276. /**
  277. * @brief Reports the name of the source file and the source line number
  278. * where the assert_param error has occurred.
  279. * @param file: pointer to the source file name
  280. * @param line: assert_param error line source number
  281. * @retval None
  282. */
  283. void assert_failed(uint8_t *file, uint32_t line)
  284. {
  285. /* USER CODE BEGIN 6 */
  286. /* User can add his own implementation to report the file name and line number,
  287. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  288. /* USER CODE END 6 */
  289. }
  290. #endif /* USE_FULL_ASSERT */