前言,emm,确实,已经在视觉控制部分卡了很久了 ,查了很多资料,目前,对于专业做机器臂的企业用的大多是用的路径规划,它涉及到了人工智能方向,但在本科生阶段,大多用的是一种叫运动学逆解的思路,它用到的是解三角函数的方式,这是对于低自由度的,对于高自由度的还会用到现代控制理论的知识,,只能说,创作之路略微小小小小的困难.
准备物品:
STM32F407VET6 、STM32F103C8T6、HC05蓝牙模块,两块HC06模块 三块步进电机驱动器, 一台三轴机械臂


-
主体思路:想做的是一个三轴机械臂,能实现蓝牙控制和视觉抓取(目前只完成蓝牙控制) 用手机连接一块HC06蓝牙,当收到手机的指令后,在单片机(c8t6和vet6的控制下,完成简单的三轴运动)。
5步骤:
1.STM32407ZGT6
这里的VET6主要担任了两个任务,一个是接收手机的数据,并发送给c8t6,另一个就是控制三个步进电机,步进电机只要控制,而控制步进电机需要一个驱动器,在驱动器的作用下,我们只要对其输入脉冲电机就能够旋转,输入高低电平,电机便能正转和反转, 具体如下:
-
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "i2c.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ #define USART_REC_LEN 200 uint8_t Res ,S; //数据存放的地方 uint8_t USART2_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.(一般给200,接收数据量大就增加) uint16_t USART2_RX_STA; //接收状态标记 /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ uint8_t send_str0[]="8"; uint8_t send_str1[]="9"; /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); MX_TIM1_Init(); MX_USART1_UART_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_3); HAL_UART_Receive_IT(&huart2, &Res, 1); HAL_UART_Transmit_IT(&huart1, &Res, 1); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { HAL_Delay(100); USART2_RX_BUF[0]=S; /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ //if(USART1_RX_STA & 0x8000)// 1000 0000 0000 0000 0000 判断是否接受到 //{ // // while(huart1.gState != HAL_UART_STATE_READY){}; if( USART2_RX_BUF[0]== '1') // 当收到字符'1'的时候 机械臂向上运动 {__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,100); //定时器1通道1输出一段脉冲 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,0);//屏蔽定时器1通道2 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,0);//屏蔽定时器1通道3 HAL_Delay(1); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_RESET); //F407引脚 输出 低电平 使其向上运动 } if(USART2_RX_BUF[0]== '2') // 当收到字符'2' 的时候 机械臂向下运动 {__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,100);//定时器1通道1输出一段脉冲 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,0);//屏蔽定时器1通道2 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,0); //屏蔽定时器1通道3 HAL_Delay(1); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_4,GPIO_PIN_SET); //F407引脚 输出 高电平 使其向下运动 } if(USART2_RX_BUF[0]== '3') // 当收到字符'3' 的时候 机械臂向左运动 { __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,100);//定时器1通道2输出一段脉冲 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,0);//屏蔽定时器1通道1 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,0);//屏蔽定时器1通道3 HAL_Delay(1); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET);//F407引脚 输出 低电平 使其向左运动 } if(USART2_RX_BUF[0]== '4') // 当收到字符'4'的时候 机械臂向右运动 { __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,100);//定时器1通道2输出一段脉冲 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,0);//屏蔽定时器1通道1 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,0);//屏蔽定时器1通道3 HAL_Delay(1); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET);//F407引脚 输出 高电平 使其向右运动 } if(USART2_RX_BUF[0]== '5') // 当收到字符'5'的时候 机械臂向前运动 { __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,100);//定时器1 通道3输出一段脉冲 HAL_Delay(1); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_RESET);//F407引脚 输出 低电平 使其向前运动 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,0); //屏蔽定时器1通道1 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,0); //屏蔽定时器1通道2 } if(USART2_RX_BUF[0]== '6') // 当收到字符'6'的时候 机械臂向后运动 { __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,100);//定时器1 通道3 输出一段脉冲 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,0);//屏蔽定时器1通道2 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,0);//屏蔽定时器1通道1 HAL_Delay(1); HAL_GPIO_WritePin(GPIOA,GPIO_PIN_6,GPIO_PIN_SET);//F407引脚 输出 高电平 使其向后运动 } if(USART2_RX_BUF[0]== 'A') // 当收到字符'6'的时候 机械臂停止 { __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,0);//定时器1 通道3 输出一段脉冲 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,0);//屏蔽定时器1通道2 __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,0);//屏蔽定时器1通道1 HAL_Delay(1); } USART2_RX_STA=0; } HAL_Delay(10); } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { /* 判断是哪个串口触发的中断 */ if(huart ->Instance == USART2) { S=Res; HAL_UART_Transmit(&huart1,&S,1,1000); //等待下一次接收中断 HAL_UART_Receive_IT(huart,&Res,1); } } /* USER CODE END 3 */ /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; 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_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ 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 */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ 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 */ } #endif /* USE_FULL_ASSERT */这是main.c里面的文件,思路很简单,就是,用 串口2接收来自手机的信号 具体是'1' '2 ' '3' '4' '5' '6' '7' '8' 注意,这里传输的字符, 同时用串口1把这个数据发送给另一块单片机c8t6(为什么还要用一块单片机呢 ,因为抓取的爪子距离 zgt6 太远了,,所以干脆直接在那附近加一块单片机单独控制,)收到1.2 3 时根据不同情况输出一段pwm,控制器机械臂运动
-
这是cubemx 的引
脚图,也是相当明朗,用到了,定时器1的三个通道.还有两个串口 ,IIC是用来显示坐标的 但视觉部分还未加载,所以 ,目前没有用的 ,,同时用三个角PE9 到PE13输出高低电平,因为机械臂的电机时步进电机,输出高低时会控制正反转,当然了,控制步进电机还需要 步进电机驱动器 -
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2023 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ #define USART_REC_LEN 200 uint8_t Res; //数据存放的地方 uint8_t USART1_RX_BUF[USART_REC_LEN]; //接收缓冲,最大USART_REC_LEN个字节.(一般给200,接收数据量大就增加) uint16_t USART1_RX_STA; //接收状态标记 /* USER CODE END PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_TIM3_Init(); MX_USART1_UART_Init(); HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1); HAL_UART_Receive_IT(&huart1, &Res, 1); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ if(USART1_RX_STA & 0x8000)// 1000 0000 0000 0000 0000 判断是否接受到 { while(huart1.gState != HAL_UART_STATE_READY){}; if( USART1_RX_BUF[0]== '8') // 当收到字符'1'的时候 机械臂向上运动 {__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,500); //定时器1通道1输出一段脉冲 HAL_Delay(1); } if( USART1_RX_BUF[0]== '9') {__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_1,0); HAL_Delay(1); } // if(USART1_RX_BUF[0]== '7') // { // __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,0); // HAL_Delay(10); // } // if(USART1_RX_BUF[0]== '8') // { __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,0); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,0); // __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,0); // // } // // else // { // __HAL_TIM_SET_COMPARE(&htim2,TIM_CHANNEL_1,25); // } USART1_RX_STA=0; } HAL_Delay(10); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { /* 判断是哪个串口触发的中断 */ if(huart ->Instance == USART1) { if((USART1_RX_STA&0x8000)==0)//接收未完成 { //读取接收到的数据 if(Res==0x0D) { USART1_RX_STA|=0x8000;//读取到数据 标志位置1 HAL_UART_Receive_IT(&huart1, &Res, 1);//接收的数据存到Res } else { USART1_RX_BUF[USART1_RX_STA&0X3FFF]=Res;// Res的数据 USART1_RX_STA++; if(USART1_RX_STA>(USART_REC_LEN-1)) USART1_RX_STA=0; } } //等待下一次接收中断 HAL_UART_Receive_IT(huart,&Res,1); } } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { 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 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(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ 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 */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ 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 */ } #endif /* USE_FULL_ASSERT */这里是c8t6的代码,就是用来控制爪子的抓取的,收到8抓取,9松开抓子
-

c8t6的cubemx配置很简单,就是打开了串口和一个定时器,其它的引脚暂时没用,
-
最后 就是
这是手机蓝牙端,按up发送'1',dowm发送'2' 送开时发送A 当然也可以自己
最后是视频效果
蓝牙机械臂
总体代码链接:
链接:https://pan.baidu.com/s/124qH9PBN6jMnlYq1KngEoA?pwd=7777
提取码:7777
--来自百度网盘超级会员V3的分享
6457

被折叠的 条评论
为什么被折叠?



