신동해

(add) main_arm.c, main_cart.c

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 Ultimate Liberty license
13 + * SLA0044, the "License"; You may not use this file except in compliance with
14 + * the License. You may obtain a copy of the License at:
15 + * www.st.com/SLA0044
16 + *
17 + ******************************************************************************
18 + */
19 +/* USER CODE END Header */
20 +
21 +/* Includes ------------------------------------------------------------------*/
22 +#include "main.h"
23 +#include "i2c.h"
24 +#include "i2s.h"
25 +#include "spi.h"
26 +#include "tim.h"
27 +#include "usart.h"
28 +#include "usb_host.h"
29 +#include "gpio.h"
30 +
31 +/* Private includes ----------------------------------------------------------*/
32 +/* USER CODE BEGIN Includes */
33 +#include <stdbool.h>
34 +/* USER CODE END Includes */
35 +
36 +/* Private typedef -----------------------------------------------------------*/
37 +/* USER CODE BEGIN PTD */
38 +
39 +/* USER CODE END PTD */
40 +
41 +/* Private define ------------------------------------------------------------*/
42 +/* USER CODE BEGIN PD */
43 +/* USER CODE END PD */
44 +
45 +/* Private macro -------------------------------------------------------------*/
46 +/* USER CODE BEGIN PM */
47 +
48 +/* USER CODE END PM */
49 +
50 +/* Private variables ---------------------------------------------------------*/
51 +
52 +/* USER CODE BEGIN PV */
53 +bool distance_flag = false;
54 +int grab_mode = 0;
55 +
56 +uint8_t rx3_data = 0;
57 +
58 +
59 +uint8_t tmp_stop = 1;
60 +uint8_t tmp_speed1 = 2;
61 +uint8_t tmp_speed2 = 3;
62 +uint8_t tmp_speed3 = 4;
63 +
64 +int MOTOR_PWM[5];
65 +int MOTOR_PWM_MEAN[5];
66 +int MOTOR_PWM_MAX[5];
67 +int MOTOR_PWM_MIN[5];
68 +int mode[5]={1,1,1,1,1};
69 +char direction;
70 +char response;
71 +
72 +volatile uint32_t distance;
73 +#define Delay_ms HAL_Delay
74 +#define millis() HAL_GetTick()
75 +#define SYS_CLOCK 168
76 +#define SYSTICK_LOAD 167999
77 +__IO uint32_t uwTick=0;
78 +extern __IO uint32_t uwTick;
79 +/* USER CODE END PV */
80 +
81 +/* Private function prototypes -----------------------------------------------*/
82 +void SystemClock_Config(void);
83 +static void MX_NVIC_Init(void);
84 +void MX_USB_HOST_Process(void);
85 +
86 +/* USER CODE BEGIN PFP */
87 +
88 +/* USER CODE END PFP */
89 +
90 +/* Private user code ---------------------------------------------------------*/
91 +/* USER CODE BEGIN 0 */
92 +uint32_t micros() {
93 + return (uwTick&0x3FFFFF)*1000 + (SYSTICK_LOAD-SysTick->VAL)/SYS_CLOCK;
94 +}
95 +
96 +void Delay_us(uint32_t us) {
97 + uint32_t temp = micros();
98 + uint32_t comp = temp + us;
99 + uint8_t flag = 0;
100 + while(comp > temp){
101 + if(((uwTick&0x3FFFFF)==0)&&(flag==0)){
102 + flag = 1;
103 + }
104 + if(flag) temp = micros() + 0x400000UL * 1000;
105 + else temp = micros();
106 + }
107 +}
108 +
109 +void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) //External interrupt for Sonar
110 +{
111 + static uint32_t ss=0;
112 + uint32_t temp = GPIOC->IDR & 0x0002;//PC1이니까 2^(1)=2
113 + switch (temp) {
114 + case 0x0002:
115 + ss = micros();
116 + break;
117 +
118 + case 0x0000 :
119 + distance = (micros() - ss) / 58;
120 + if(distance <= 2){
121 + distance_flag = true;
122 + }
123 + break;
124 + }
125 +}
126 +
127 +
128 +/* USER CODE END 0 */
129 +
130 +/**
131 + * @brief The application entry point.
132 + * @retval int
133 + */
134 +int main(void)
135 +{
136 + /* USER CODE BEGIN 1 */
137 +
138 +
139 + /* USER CODE END 1 */
140 +
141 + /* MCU Configuration--------------------------------------------------------*/
142 +
143 + /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
144 + HAL_Init();
145 +
146 + /* USER CODE BEGIN Init */
147 +
148 + /* USER CODE END Init */
149 +
150 + /* Configure the system clock */
151 + SystemClock_Config();
152 +
153 + /* USER CODE BEGIN SysInit */
154 +
155 + /* USER CODE END SysInit */
156 +
157 + /* Initialize all configured peripherals */
158 + MX_GPIO_Init();
159 + MX_I2C1_Init();
160 + MX_I2S3_Init();
161 + MX_SPI1_Init();
162 + MX_TIM3_Init();
163 + MX_TIM12_Init();
164 + MX_USB_HOST_Init();
165 + MX_TIM1_Init();
166 + MX_USART2_UART_Init();
167 + MX_USART3_UART_Init();
168 +
169 + /* Initialize interrupts */
170 + MX_NVIC_Init();
171 + /* USER CODE BEGIN 2 */
172 +
173 + //raspberryPi to robotArm
174 + HAL_UART_Receive_IT(&huart3,&rx3_data,1);
175 +
176 + //Robot Arm
177 + HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
178 + HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
179 + HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);
180 + HAL_TIM_PWM_Start(&htim12,TIM_CHANNEL_1);
181 + HAL_TIM_PWM_Start(&htim12,TIM_CHANNEL_2);
182 +
183 + //SONAR
184 + HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1);
185 +
186 +
187 + /* USER CODE END 2 */
188 +
189 + /* Infinite loop */
190 + /* USER CODE BEGIN WHILE */
191 +
192 + while (1)
193 + {
194 +
195 + TIM3->CCR3=300; // 1 top
196 + TIM12->CCR1=350; //2
197 + TIM12->CCR2=300; // 3
198 + TIM3->CCR2= 300; //4
199 + TIM3->CCR1 = 500; // 5
200 +
201 + if(rx3_data == 1){ // 전방 차량과의 거리가 70cm 미만일 때 stm_cart에 신호값 1 전달
202 + HAL_UART_Transmit(&huart2, &tmp_stop, 1, 100);
203 + }
204 + else if(rx3_data == 2){ // 전방 차량과의 거리가 70cm 이상 100cm 미만일 때 stm_cart에 신호값 2 전달
205 + HAL_UART_Transmit(&huart2, &tmp_speed1, 1, 100);
206 + }
207 + else if(rx3_data == 3){ // 전방 차량과의 거리가 100cm 이상 150cm 미만일 stm_cart에 신호값 3 전달
208 + HAL_UART_Transmit(&huart2, &tmp_speed2, 1, 100);
209 + }
210 + else (rx3_data == 4){ // 전방에 차량이 없거나 거리가 150cm 이상일 때 stm_cart에 신호값 4 전달
211 + HAL_UART_Transmit(&huart2, &tmp_speed3, 1, 100);
212 + }
213 +
214 + distance_flag = false;
215 + rx3_data = 0;
216 +
217 +
218 + /* USER CODE END WHILE */
219 + MX_USB_HOST_Process();
220 +
221 + /* USER CODE BEGIN 3 */
222 +
223 +
224 +
225 + }
226 + /* USER CODE END 3 */
227 +}
228 +
229 +/**
230 + * @brief System Clock Configuration
231 + * @retval None
232 + */
233 +void SystemClock_Config(void)
234 +{
235 + RCC_OscInitTypeDef RCC_OscInitStruct = {0};
236 + RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
237 + RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
238 +
239 + /** Configure the main internal regulator output voltage
240 + */
241 + __HAL_RCC_PWR_CLK_ENABLE();
242 + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
243 + /** Initializes the CPU, AHB and APB busses clocks
244 + */
245 + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
246 + RCC_OscInitStruct.HSEState = RCC_HSE_ON;
247 + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
248 + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
249 + RCC_OscInitStruct.PLL.PLLM = 8;
250 + RCC_OscInitStruct.PLL.PLLN = 336;
251 + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
252 + RCC_OscInitStruct.PLL.PLLQ = 7;
253 + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
254 + {
255 + Error_Handler();
256 + }
257 + /** Initializes the CPU, AHB and APB busses clocks
258 + */
259 + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
260 + |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
261 + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
262 + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
263 + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
264 + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
265 +
266 + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
267 + {
268 + Error_Handler();
269 + }
270 + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2S;
271 + PeriphClkInitStruct.PLLI2S.PLLI2SN = 192;
272 + PeriphClkInitStruct.PLLI2S.PLLI2SR = 2;
273 + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
274 + {
275 + Error_Handler();
276 + }
277 +}
278 +
279 +/**
280 + * @brief NVIC Configuration.
281 + * @retval None
282 + */
283 +static void MX_NVIC_Init(void)
284 +{
285 + /* EXTI1_IRQn interrupt configuration */
286 + HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
287 + HAL_NVIC_EnableIRQ(EXTI1_IRQn);
288 + /* USART3_IRQn interrupt configuration */
289 + HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
290 + HAL_NVIC_EnableIRQ(USART3_IRQn);
291 +}
292 +
293 +/* USER CODE BEGIN 4 */
294 +void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
295 +{
296 + if(huart->Instance==USART3){
297 + HAL_UART_Receive_IT(&huart3,&rx3_data,1);
298 + }
299 +}
300 +/* USER CODE END 4 */
301 +
302 +/**
303 + * @brief This function is executed in case of error occurrence.
304 + * @retval None
305 + */
306 +void Error_Handler(void)
307 +{
308 + /* USER CODE BEGIN Error_Handler_Debug */
309 + /* User can add his own implementation to report the HAL error return state */
310 +
311 + /* USER CODE END Error_Handler_Debug */
312 +}
313 +
314 +#ifdef USE_FULL_ASSERT
315 +/**
316 + * @brief Reports the name of the source file and the source line number
317 + * where the assert_param error has occurred.
318 + * @param file: pointer to the source file name
319 + * @param line: assert_param error line source number
320 + * @retval None
321 + */
322 +void assert_failed(uint8_t *file, uint32_t line)
323 +{
324 + /* USER CODE BEGIN 6 */
325 + /* User can add his own implementation to report the file name and line number,
326 + tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
327 + /* USER CODE END 6 */
328 +}
329 +#endif /* USE_FULL_ASSERT */
330 +
331 +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
This diff is collapsed. Click to expand it.