이민규

add code

1 +/* USER CODE BEGIN Header */
2 +/**
3 + ******************************************************************************
4 + * @file : main.c
5 + * @brief : Main program body
6 + ******************************************************************************
7 + * @attention
8 + *
9 + * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
10 + * All rights reserved.</center></h2>
11 + *
12 + * This software component is licensed by ST under BSD 3-Clause license,
13 + * the "License"; You may not use this file except in compliance with the
14 + * License. You may obtain a copy of the License at:
15 + * opensource.org/licenses/BSD-3-Clause
16 + *
17 + ******************************************************************************
18 + */
19 +/* USER CODE END Header */
20 +/* Includes ------------------------------------------------------------------*/
21 +#include "main.h"
22 +#include "tim.h"
23 +#include "usart.h"
24 +#include "gpio.h"
25 +
26 +/* Private includes ----------------------------------------------------------*/
27 +/* USER CODE BEGIN Includes */
28 +#include <math.h>
29 +#include <stdbool.h>
30 +/* USER CODE END Includes */
31 +
32 +/* Private typedef -----------------------------------------------------------*/
33 +/* USER CODE BEGIN PTD */
34 +
35 +/* USER CODE END PTD */
36 +
37 +/* Private define ------------------------------------------------------------*/
38 +/* USER CODE BEGIN PD */
39 +/* USER CODE END PD */
40 +
41 +/* Private macro -------------------------------------------------------------*/
42 +/* USER CODE BEGIN PM */
43 +
44 +/* USER CODE END PM */
45 +
46 +/* Private variables ---------------------------------------------------------*/
47 +
48 +/* USER CODE BEGIN PV */
49 +uint8_t rx2_data = 0;
50 +
51 +uint32_t desired_speed = 3000;
52 +uint32_t MOTER_PWM[4] = {0};
53 +uint32_t Kp = 1;
54 +uint32_t encoder_cnt[4] = {0};
55 +uint32_t encoder_speed[4] = {0};
56 +int32_t error_speed[4] = {0};
57 +int32_t PID_speed[4] = {0};
58 +uint32_t old_PID_speed[4] = {3000, 3000, 3000, 3000};
59 +
60 +/***********lidar************/
61 +//response data
62 +bool scan_start = false;
63 +uint8_t rx3_start[7] = {0};
64 +uint8_t rx3_data[5] = {0};
65 +uint8_t Q = 0;
66 +bool S = false;
67 +uint16_t angle;
68 +uint16_t d;
69 +uint16_t distance[360] = {0};
70 +int16_t avg_DIFF = 0;
71 +
72 +//protocal
73 +uint8_t scan_command[2] = {0xA5,0x20};
74 +uint8_t stop_command[2] = {0xA5,0x25};
75 +uint8_t soft_reboot[2] = {0xA5,0x40};
76 +uint8_t scan_express[2] = {0xA5,0x82};
77 +uint8_t scan_force[2] = {0xA5,0x21};
78 +uint8_t device_info[2] = {0xA5,0x50};
79 +uint8_t health_status[2] = {0xA5,0x52};
80 +uint8_t sample_rate[2] = {0xA5,0x59};
81 +uint8_t scan_response[7] = {0xa5, 0x5a, 0x5, 0x0, 0x0, 0x40, 0x81};
82 +/* USER CODE END PV */
83 +
84 +/* Private function prototypes -----------------------------------------------*/
85 +void SystemClock_Config(void);
86 +static void MX_NVIC_Init(void);
87 +/* USER CODE BEGIN PFP */
88 +
89 +/* USER CODE END PFP */
90 +
91 +/* Private user code ---------------------------------------------------------*/
92 +/* USER CODE BEGIN 0 */
93 +bool array_element_of_index_equal(uint8_t a[], uint8_t b[], uint8_t size) {
94 + uint8_t i;
95 + for(i=0; i<size; i++){
96 + if( a[i] != b[i] )
97 + return false;
98 + }
99 + return true;
100 +}
101 +int16_t array_avg_compare(uint16_t distance[]){
102 +
103 + uint32_t sum_R = 0;
104 + uint32_t sum_L = 0;
105 + uint8_t len_L = 0;
106 + uint8_t len_R = 0;
107 + uint16_t avg_R = 0;
108 + uint16_t avg_L = 0;
109 + int16_t avg_diff = 0;
110 +
111 + for(int i=0; i<90; i++){
112 + sum_R += distance[i];
113 + if(distance[i]!=0){
114 + len_R++;
115 + }
116 + }
117 + avg_R = sum_R/len_R;
118 +
119 + for(int i=270; i<360; i++){
120 + sum_L += distance[i];
121 + if(distance[i]!=0){
122 + len_L++;
123 + }
124 + }
125 + avg_L = sum_L/len_L;
126 +
127 + avg_diff = avg_R - avg_L;
128 +
129 + return avg_diff;
130 +}
131 +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) //Timer interrupt every 20ms
132 +{
133 + if(htim->Instance == TIM6){
134 +
135 + //HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13,GPIO_PIN_SET);
136 + encoder_cnt[0] = TIM2->CNT;
137 + TIM2->CNT=0;
138 +
139 + encoder_cnt[1] = TIM3->CNT;
140 + TIM3->CNT=0;
141 +
142 + encoder_cnt[2] = TIM4->CNT;
143 + TIM4->CNT=0;
144 +
145 + encoder_cnt[3] = TIM5->CNT;
146 + TIM5->CNT=0;
147 +
148 + encoder_speed[0] = 164.18 * exp(0.0112*encoder_cnt[0]);
149 + encoder_speed[1] = 164.18 * exp(0.0112*encoder_cnt[1]);
150 + encoder_speed[2] = 164.18 * exp(0.0112*encoder_cnt[2]);
151 + encoder_speed[3] = 164.18 * exp(0.0112*encoder_cnt[3]);
152 +
153 + error_speed[0] = desired_speed - encoder_speed[0];
154 + error_speed[1] = desired_speed - encoder_speed[1];
155 + error_speed[2] = desired_speed - encoder_speed[2];
156 + error_speed[3] = desired_speed - encoder_speed[3];
157 +
158 + PID_speed[0] = old_PID_speed[0] + Kp*error_speed[0];
159 + PID_speed[1] = old_PID_speed[1] + Kp*error_speed[1];
160 + PID_speed[2] = old_PID_speed[2] + Kp*error_speed[2];
161 + PID_speed[3] = old_PID_speed[3] + Kp*error_speed[3];
162 +
163 + old_PID_speed[0] = PID_speed[0];
164 + old_PID_speed[1] = PID_speed[1];
165 + old_PID_speed[2] = PID_speed[2];
166 + old_PID_speed[3] = PID_speed[3];
167 + }
168 +}
169 +
170 +/* USER CODE END 0 */
171 +
172 +/**
173 + * @brief The application entry point.
174 + * @retval int
175 + */
176 +int main(void)
177 +{
178 + /* USER CODE BEGIN 1 */
179 +
180 + /* USER CODE END 1 */
181 +
182 + /* MCU Configuration--------------------------------------------------------*/
183 +
184 + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
185 + HAL_Init();
186 +
187 + /* USER CODE BEGIN Init */
188 +
189 + /* USER CODE END Init */
190 +
191 + /* Configure the system clock */
192 + SystemClock_Config();
193 +
194 + /* USER CODE BEGIN SysInit */
195 +
196 + /* USER CODE END SysInit */
197 +
198 + /* Initialize all configured peripherals */
199 + MX_GPIO_Init();
200 + MX_TIM1_Init();
201 + MX_TIM2_Init();
202 + MX_TIM3_Init();
203 + MX_TIM4_Init();
204 + MX_TIM6_Init();
205 + MX_USART2_UART_Init();
206 + MX_USART3_UART_Init();
207 + MX_TIM5_Init();
208 +
209 + /* Initialize interrupts */
210 + MX_NVIC_Init();
211 + /* USER CODE BEGIN 2 */
212 +
213 + HAL_Delay(3000);
214 +
215 + //Initialize for motor PWM
216 + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
217 + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
218 + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
219 + HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_4);
220 +
221 + //Initialize for motor direction
222 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, SET);
223 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, RESET);
224 +
225 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, SET);
226 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, RESET);
227 +
228 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, SET);
229 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, RESET);
230 +
231 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, SET);
232 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, RESET);
233 +
234 + //Initialize for encoder count value
235 + TIM2->CNT = 0;
236 + TIM3->CNT = 0;
237 + TIM4->CNT = 0;
238 + TIM5->CNT = 0;
239 +
240 + //Initialize for Encoder
241 + HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
242 + HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
243 + HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_ALL);
244 + HAL_TIM_Encoder_Start(&htim5, TIM_CHANNEL_ALL);
245 +
246 + //Initialize for timer interrupt initialization for Encoder (50ms)
247 + HAL_TIM_Base_Start_IT(&htim6);
248 +
249 + //LIDAR_scan_start
250 + //HAL_UART_Transmit(&huart3, &scan_command, 2, 100);
251 +
252 + //robotArm to Cart
253 + HAL_UART_Receive_IT(&huart2,&rx2_data,1);
254 +
255 +
256 + /* USER CODE END 2 */
257 +
258 + /* Infinite loop */
259 + /* USER CODE BEGIN WHILE */
260 +
261 + while(1){
262 +
263 + if(rx2_data == 1){
264 + HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_RESET);
265 + HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, RESET);
266 + HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, SET);
267 + //go left
268 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, SET);
269 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, RESET);
270 +
271 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, RESET);
272 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, SET);
273 +
274 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, RESET);
275 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, SET);
276 +
277 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, SET);
278 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, RESET);
279 + /*
280 + TIM1->CCR1 = 6000;
281 + TIM1->CCR2 = 4500;
282 + TIM1->CCR3 = 8000;
283 + TIM1->CCR4 = 9999;
284 + */
285 + TIM1->CCR1 = 8000;
286 + TIM1->CCR2 = 9999;
287 + TIM1->CCR3 = 6000;
288 + TIM1->CCR4 = 4500;
289 +
290 + HAL_Delay(100);
291 +
292 + rx2_data = 5;
293 + //HAL_Delay(2000);
294 +
295 +
296 +
297 +
298 + }else if(rx2_data == 2){
299 + HAL_GPIO_WritePin(GPIOC,GPIO_PIN_13, GPIO_PIN_SET);
300 + HAL_GPIO_WritePin(GPIOE, GPIO_PIN_4, SET);
301 + HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, RESET);
302 + //go right
303 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, RESET);
304 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, SET);
305 +
306 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, SET);
307 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, RESET);
308 +
309 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, SET);
310 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, RESET);
311 +
312 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, RESET);
313 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, SET);
314 +
315 + TIM1->CCR1 = 8000;
316 + TIM1->CCR2 = 9999;
317 + TIM1->CCR3 = 6000;
318 + TIM1->CCR4 = 4500;
319 +
320 + HAL_Delay(100);
321 +
322 + rx2_data = 5;
323 + //HAL_Delay(2000);
324 +
325 + }else if(rx2_data == 5){
326 + //stop
327 +
328 + TIM1->CCR1 = 0;
329 + TIM1->CCR2 = 0;
330 + TIM1->CCR3 = 0;
331 + TIM1->CCR4 = 0;
332 +
333 + }else if(rx2_data == 3){
334 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, SET);
335 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, RESET);
336 +
337 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, SET);
338 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, RESET);
339 +
340 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, SET);
341 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, RESET);
342 +
343 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, SET);
344 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, RESET);
345 + TIM1->CCR1 = 3000;
346 + TIM1->CCR2 = 3000;
347 + TIM1->CCR3 = 3000;
348 + TIM1->CCR4 = 3000;
349 +
350 +
351 +
352 + }else if(rx2_data == 6){
353 +
354 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, SET);
355 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, RESET);
356 +
357 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, SET);
358 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, RESET);
359 +
360 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, SET);
361 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, RESET);
362 +
363 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, SET);
364 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, RESET);
365 +
366 + TIM1->CCR1 = 4000;
367 + TIM1->CCR2 = 4000;
368 + TIM1->CCR3 = 4000;
369 + TIM1->CCR4 = 4000;
370 +
371 + HAL_Delay(2000);
372 +
373 + rx2_data = 5;
374 +
375 + }
376 +
377 +/*
378 + }else if(rx2_data == 11){
379 +
380 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, RESET);
381 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_13, SET);
382 +
383 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_14, SET);
384 + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_15, RESET);
385 +
386 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, SET);
387 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, RESET);
388 +
389 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, RESET);
390 + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_11, SET);
391 +
392 + TIM1->CCR1 = 3000;
393 + TIM1->CCR2 = 9999;
394 + TIM1->CCR3 = 8000;
395 + TIM1->CCR4 = 3000;
396 +
397 + HAL_Delay(100);
398 +
399 + rx2_data = 5;
400 + //HAL_Delay(2000);
401 +
402 + }*/
403 + // rx2_data = 0;
404 + }
405 +
406 +
407 + /* USER CODE END WHILE */
408 +
409 + /* USER CODE BEGIN 3 */
410 +
411 + /* USER CODE END 3 */
412 +}
413 +
414 +/**
415 + * @brief System Clock Configuration
416 + * @retval None
417 + */
418 +void SystemClock_Config(void)
419 +{
420 + RCC_OscInitTypeDef RCC_OscInitStruct = {0};
421 + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
422 +
423 + /** Configure the main internal regulator output voltage
424 + */
425 + __HAL_RCC_PWR_CLK_ENABLE();
426 + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
427 + /** Initializes the RCC Oscillators according to the specified parameters
428 + * in the RCC_OscInitTypeDef structure.
429 + */
430 + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
431 + RCC_OscInitStruct.HSEState = RCC_HSE_ON;
432 + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
433 + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
434 + RCC_OscInitStruct.PLL.PLLM = 4;
435 + RCC_OscInitStruct.PLL.PLLN = 168;
436 + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
437 + RCC_OscInitStruct.PLL.PLLQ = 7;
438 + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
439 + {
440 + Error_Handler();
441 + }
442 + /** Initializes the CPU, AHB and APB buses clocks
443 + */
444 + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
445 + |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
446 + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
447 + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
448 + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
449 + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
450 +
451 + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
452 + {
453 + Error_Handler();
454 + }
455 +}
456 +
457 +/**
458 + * @brief NVIC Configuration.
459 + * @retval None
460 + */
461 +static void MX_NVIC_Init(void)
462 +{
463 + /* USART2_IRQn interrupt configuration */
464 + HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
465 + HAL_NVIC_EnableIRQ(USART2_IRQn);
466 + /* TIM6_DAC_IRQn interrupt configuration */
467 + HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 0, 0);
468 + HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
469 +}
470 +
471 +/* USER CODE BEGIN 4 */
472 +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
473 +{
474 + if(huart->Instance==USART2){
475 + HAL_UART_Receive_IT(&huart2,&rx2_data,1);
476 + }
477 +}
478 +/* USER CODE END 4 */
479 +
480 +/**
481 + * @brief This function is executed in case of error occurrence.
482 + * @retval None
483 + */
484 +void Error_Handler(void)
485 +{
486 + /* USER CODE BEGIN Error_Handler_Debug */
487 + /* User can add his own implementation to report the HAL error return state */
488 +
489 + /* USER CODE END Error_Handler_Debug */
490 +}
491 +
492 +#ifdef USE_FULL_ASSERT
493 +/**
494 + * @brief Reports the name of the source file and the source line number
495 + * where the assert_param error has occurred.
496 + * @param file: pointer to the source file name
497 + * @param line: assert_param error line source number
498 + * @retval None
499 + */
500 +void assert_failed(uint8_t *file, uint32_t line)
501 +{
502 + /* USER CODE BEGIN 6 */
503 + /* User can add his own implementation to report the file name and line number,
504 + tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
505 + /* USER CODE END 6 */
506 +}
507 +#endif /* USE_FULL_ASSERT */
508 +
509 +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/