//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:电机名称,posi
tion:电机的位置,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有反转,但是达到最小行程的电机的继电器还在耦合没有释放,而电机位置信息值同样变动成初始值。求大佬告知原因