完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
//motor.c
#define m0_xc_max 1050 //250*5 #define m1_xc_max 375 //150*5/2 #define m2_xc_max 0 #define m3_xc_max 500 //100*5//可能需要修改 #define m4_xc_max 1650 //330*5 #define m0_xc_min 0 #define m1_xc_min -375 //-(150*5/2) #define m2_xc_min -1000 //-200*5 #define m3_xc_min 0 #define m4_xc_min 0 //电机初始状态 #define m0_re_state 0 #define m1_re_state 0 #define m2_re_state 0 #define m3_re_state 0 #define m4_re_state 0 #define m5_re_state 0 //电机驱动的宏定义 #define motor00_stop HAL_GPIO_WritePin(M0__GPIO_Port,M0__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (MO__GPIO_Port ,MO__Pin,GPIO_PIN_RESET) #define motor00_zhengzhuan HAL_GPIO_WritePin (M0__GPIO_Port ,M0__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (MO__GPIO_Port ,MO__Pin ,GPIO_PIN_RESET) #define motor00_fanzhuan HAL_GPIO_WritePin (M0__GPIO_Port ,M0__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (MO__GPIO_Port ,MO__Pin ,GPIO_PIN_SET) #define motor01_stop HAL_GPIO_WritePin (M1__GPIO_Port ,M1__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M1_F5_GPIO_Port ,M1_F5_Pin ,GPIO_PIN_RESET) #define motor01_zhengzhuan HAL_GPIO_WritePin (M1__GPIO_Port ,M1__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M1_F5_GPIO_Port ,M1_F5_Pin ,GPIO_PIN_RESET) #define motor01_fanzhuan HAL_GPIO_WritePin (M1__GPIO_Port ,M1__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M1_F5_GPIO_Port ,M1_F5_Pin ,GPIO_PIN_SET) #define motor02_stop HAL_GPIO_WritePin (M2__GPIO_Port ,M2__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M2_B10_GPIO_Port ,M2_B10_Pin ,GPIO_PIN_RESET) #define motor02_zhengzhuan HAL_GPIO_WritePin (M2__GPIO_Port ,M2__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M2_B10_GPIO_Port ,M2_B10_Pin ,GPIO_PIN_SET) #define motor02_fanzhuan HAL_GPIO_WritePin (M2__GPIO_Port ,M2__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M2_B10_GPIO_Port ,M2_B10_Pin ,GPIO_PIN_RESET) #define motor03_stop HAL_GPIO_WritePin(M3__GPIO_Port ,M3__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M3_C9_GPIO_Port,M3_C9_Pin,GPIO_PIN_RESET) #define motor03_zhengzhuan HAL_GPIO_WritePin(M3__GPIO_Port ,M3__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M3_C9_GPIO_Port ,M3_C9_Pin ,GPIO_PIN_RESET) #define motor03_fanzhuan HAL_GPIO_WritePin (M3__GPIO_Port ,M3__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M3_C9_GPIO_Port ,M3_C9_Pin ,GPIO_PIN_SET) #define motor04_stop HAL_GPIO_WritePin (M4__GPIO_Port ,M4__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M4_C14_GPIO_Port ,M4_C14_Pin ,GPIO_PIN_RESET) #define motor04_zhengzhuan HAL_GPIO_WritePin (M4__GPIO_Port ,M4__Pin ,GPIO_PIN_SET);HAL_GPIO_WritePin (M4_C14_GPIO_Port ,M4_C14_Pin ,GPIO_PIN_RESET) #define motor04_fanzhuan HAL_GPIO_WritePin (M4__GPIO_Port ,M4__Pin ,GPIO_PIN_RESET);HAL_GPIO_WritePin (M4_C14_GPIO_Port ,M4_C14_Pin ,GPIO_PIN_SET) //motor0_x:电机名称,position:电机的位置,mode:电机模式,0停止,1电机正转,2电机反转 void motor_control(int motor0_x,int *position,int mode) { int xc_max,xc_min; switch(motor0_x) { case 0: { xc_max=m0_xc_max; xc_min=m0_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor00_stop; } else if(*positionxc_min) { motor00_fanzhuan; *position -=1; } }break; default : { motor00_stop; } } }break; case 1: { xc_max=m1_xc_max; xc_min=m1_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor01_stop; } else if(*positionxc_min) { motor01_fanzhuan; *position -=1; } }break; default : { motor01_stop; } } }break; case 2: { xc_max=m2_xc_max; xc_min=m2_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor02_stop; } else if(*positionxc_min) { motor02_fanzhuan; *position -=1; } }break; default : { motor02_stop; } } }break; case 3: { xc_max=m3_xc_max; xc_min=m3_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor03_stop; } else if(*positionxc_min) { motor03_fanzhuan; *position -=1; } }break; default : { motor03_stop; } } }break; case 4: { xc_max=m4_xc_max; xc_min=m4_xc_min; switch(mode) { case 1: { if(*position==xc_max) { motor04_stop; } else if(*positionxc_min) { motor04_fanzhuan; *position -=1; } }break; default : { motor04_stop; } } }break; default :; } }//main.c //IO定义 static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); __HAL_RCC_GPIOF_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, M4__Pin|M4_C14_Pin|M5__Pin|M3__Pin |M3_C9_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOA, M0__Pin|MO__Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOF, M1__Pin|M1_F5_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, M2__Pin|M2_B10_Pin, GPIO_PIN_RESET); /*Configure GPIO pins : M4__Pin M4_C14_Pin M5__Pin */ GPIO_InitStruct.Pin = M4__Pin|M4_C14_Pin|M5__Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, GPIO_InitStruct); /*Configure GPIO pins : M0__Pin MO__Pin */ GPIO_InitStruct.Pin = M0__Pin|MO__Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOA, GPIO_InitStruct); /*Configure GPIO pins : M1__Pin M1_F5_Pin */ GPIO_InitStruct.Pin = M1__Pin|M1_F5_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOF, GPIO_InitStruct); /*Configure GPIO pins : M2__Pin M2_B10_Pin */ GPIO_InitStruct.Pin = M2__Pin|M2_B10_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOB, GPIO_InitStruct); /*Configure GPIO pins : M3__Pin M3_C9_Pin */ GPIO_InitStruct.Pin = M3__Pin|M3_C9_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(GPIOC, GPIO_InitStruct); } /* USART1 init function */ static void MX_USART1_UART_Init(void) { huart1.Instance = USART1; huart1.Init.BaudRate = 115200; huart1.Init.WordLength = UART_WORDLENGTH_8B; huart1.Init.StopBits = UART_STOPBITS_1; huart1.Init.Parity = UART_PARITY_NONE; huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE; huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT; if (HAL_UART_Init( huart1) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); } } //将接受过来的信息进行处理,其中case11,12复位 void xinxichuli(uint8_t x) { switch(x) { case 1: { motor_control (0, m0_position,1); }break; case 2: { motor_control(0, m0_position,2); }break; case 3: { motor_control(1, m1_position,1); }break; case 4: { motor_control(1, m1_position,2); }break; case 5: { motor_control(2, m2_position,2); }break; case 6: { motor_control(2, m2_position,1); }break; case 7://需要修改 { while(1) { if(m1_position ==m1_re_state ) { do { motor_control(1, m1_position,1); }while(m1_position !=m1_xc_max); } else if(m1_position ==m1_xc_max ) { do { motor_control(M1, m1_position,2); }while(m1_position !=m1_re_state ); } else if (m1_position ==m1_xc_min ) { do { motor_control(M1, m1_position,1); }while(m1_position !=m1_re_state ); motor_control(M1, m1_position,0); } for(int w=0;wm1_re_state ) { for(int qqe=m1_position ;qqe>m1_re_state ;qqe--) { motor_control(1, m1_position,2); } if(m1_position==m1_re_state) { motor_control(M1, m1_position,0); } } else if(m1_position m1_re_state ;qqew--) { // motor1 (2); motor_control (M1, m1_position ,2); } if(m1_position==m1_re_state) { motor_control(M1, m1_position,0); } } else if(m0_position >m0_re_state ) { while(m0_position>m0_re_state ) { motor_control(M0, m0_position,2); } motor_control(M0, m0_position,0); } else if(m0_position m2_re_state ) { while(m2_position>m2_re_state ) { motor_control(M2, m2_position,2); } motor_control(M2, m2_position,0); } else if(m2_position m1_re_state) { for (int qqe = m1_position; qqe>m1_re_state; qqe--) { motor_control(M1, m1_position,2); } if (m1_position == m1_re_state) { motor_control(M1, m1_position,0); } } else if (m1_position m1_re_state; qqew--) { motor_control(M1, m1_position,2); } if (m1_position == m1_re_state) { motor_control(M1, m1_position,0); } } else if (m0_position >m0_re_state) { while (m0_position>m0_re_state) { motor_control(M0, m0_position,2); } motor_control(M0, m0_position,0); } else if (m0_position m2_re_state) { while (m2_position>m2_re_state) { motor_control(M2, m2_position,2); } motor_control(M2, m2_position,0); } else if (m2_position m1_re_state) { do { motor_control(M1, m1_position,2); } while (m1_position != m1_re_state); if (m1_position == m1_re_state) { motor_control(M1, m1_position,0); } } else if (m1_position < m1_re_state) { do { motor_control(M1, m1_position,1); } while (m1_position != m1_re_state); if (m1_position == m1_re_state) { motor_control(M1, m1_position,0); } } } else if (m2_position != m2_re_state) { if (m2_position > m2_re_state) { do { motor_control(M2, m2_position,2); } while (m2_position != m2_re_state); if (m2_position == m2_re_state) { motor_control(M2, m2_position,0); } } else if (m2_position < m2_re_state) { do { motor_control(M2, m2_position,1); } while (m2_position != m2_re_state); if (m2_position == m2_re_state) { motor_control(M2, m2_position,0); } } if (m2_position == m2_re_state) { motor_control(M2, m2_position,0); } } else if (m0_position != m0_re_state) { if (m0_position > m0_re_state) { do { motor_control(M0, m0_position,2); } while (m0_position != m0_re_state); } else if (m0_position < m0_re_state) { do { motor_control(M0, m0_position,1); } while (m0_position != m0_re_state); } else if (m0_position != m0_re_state) { motor_control(M0, m0_position,0); } } } }break; case 31: { //motor0 (0); motor_control(M0, m0_position,0); }break; case 32: { // motor0 (0); motor_control(M0, m0_position,0); }break; case 33: { motor_control(M1, m1_position,0); }break; case 34: { motor_control(M1, m1_position,0); }break; case 35: { motor_control(M2, m2_position,0); }break; case 36: { motor_control(M2, m2_position,0); }break; } //int main()中的WHILE while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ do { R_stauts =HAL_UART_Receive_IT( huart1,rec,1); }while(R_stauts != HAL_OK );//接受遥控发送过来的数据 //根据主板状态的不同,处理遥控的数据 xinxichuli (rec[0]); } 从上位机接受到信息1~6电机会进行相应的动作,在电机运行的时候电机的位置也在进行相应的累加或者累减, 在电机M0正转后 (即时接受到信息1),我发送复位信息11,或者12进行复位。在下位机接收到11的时候电机的复位没有动作,但是电机位置信息值变动成初始值,而接受到复位信息12的时候电机M0有反转,但是达到最小行程的电机的继电器还在耦合没有释放,而电机位置信息值同样变动成初始值。求大佬告知原因 |
|
相关推荐
1个回答
|
|
在STM32F030R8电机驱动函数中,电机位置数值跳变的原因可能有以下几点:
1. 采样频率不足:如果采样频率较低,可能导致电机位置的测量不准确,从而产生跳变。提高采样频率可以提高测量精度。 2. 滤波器设置不当:如果滤波器的参数设置不合适,可能会导致电机位置的跳变。调整滤波器参数,如滤波器类型、截止频率等,可以减少跳变。 3. 编码器精度问题:如果编码器的精度不高,可能会导致测量到的电机位置数值出现跳变。提高编码器的精度可以减少跳变。 4. 电机驱动器性能问题:电机驱动器的性能不佳可能导致电机位置的跳变。检查电机驱动器的性能,如电流、电压等,确保其工作在合适的范围内。 5. 机械结构问题:电机与负载之间的连接可能存在间隙或摩擦,导致电机位置的跳变。检查机械结构,确保连接紧密且无间隙。 6. 软件算法问题:如果软件算法存在问题,可能会导致电机位置的跳变。检查算法,确保其正确处理电机位置数据。 7. 干扰问题:电机驱动系统可能受到外部电磁干扰,导致电机位置的跳变。检查系统周围的电磁环境,采取措施减少干扰。 8. 电源问题:不稳定的电源可能导致电机驱动器工作不稳定,从而产生电机位置的跳变。确保电源稳定,可以使用稳压器或滤波器来提高电源质量。 针对您提供的代码片段,我注意到有一些可能的问题: - 您定义了一些电机的最大和最小位置值,但是没有看到实际的电机驱动函数。请确保在驱动函数中正确使用了这些值。 - 代码中的`#define m1_xc_min -375`后面有一个空格,这可能是一个打字错误。请检查代码中的其他可能的打字错误。 建议您检查以上几点,找出导致电机位置数值跳变的原因,并进行相应的调整。 |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1804 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1629 浏览 1 评论
1097 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
736 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1686 浏览 2 评论
1944浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
748浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
582浏览 3评论
604浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
565浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-28 20:45 , Processed in 2.192816 second(s), Total 79, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号