小学生
最后登录1970-1-1
在线时间 小时
注册时间2021-9-6
|
开发板:野火骄阳电机开发板;使用定时器1控制两个直流无刷电机正反差速转。
通道2、3、4配置一样;
/*Cubemx生成的代码*/
- void MX_TIM1_Init(void)
- {
- TIM_ClockConfigTypeDef sClockSourceConfig = {0};
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
- htim1.Instance = TIM1;
- htim1.Init.Prescaler = 1;
- htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim1.Init.Period = 5599;
- htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim1.Init.RepetitionCounter = 0;
- htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
- if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
- if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 0;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
- sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
- sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
- {
- Error_Handler();
- }
- sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
- sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
- sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
- sBreakDeadTimeConfig.DeadTime = 0;
- sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
- sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
- sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
- if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
- {
- Error_Handler();
- }
- HAL_TIM_MspPostInit(&htim1);
- }
复制代码 定时器相关的初始化已在main函数里调用;
控制部分逻辑:
- if(DirControl==0xd0) //向前
- {
- TIM1->CCR1 = Robot.leftSpeedOut;
- TIM1->CCR2 = 0;
- TIM1->CCR3 = Robot.rightSpeedOut;
- TIM1->CCR4 = 0;
- }
- if(DirControl==0xb0) //向后
- {
- TIM1->CCR1 = 0;
- TIM1->CCR2 = Robot.leftSpeedOut;
- TIM1->CCR3 = 0;
- TIM1->CCR4 = Robot.rightSpeedOut;
- }
- if(DirControl==0x0d) //原地向右
- {
- TIM1->CCR1 = Robot.leftSpeedOut;
- TIM1->CCR2 = 0;
- TIM1->CCR3 = 0;
- TIM1->CCR4 = Robot.rightSpeedOut;
- }
- if(DirControl==0x0b) //原地向左
- {
- TIM1->CCR1 = 0;
- TIM1->CCR2 = Robot.leftSpeedOut;
- TIM1->CCR3 = Robot.rightSpeedOut;
- TIM1->CCR4 = 0;
- }
- if(DirControl==0xdd) //向前向右
- {
- TIM1->CCR1 = Robot.leftSpeedOut;
- TIM1->CCR2 = 0;
- TIM1->CCR3 = Robot.rightSpeedOut/5;
- TIM1->CCR4 = 0;
- }
- if(DirControl==0xdb) //向前向左
- {
- TIM1->CCR1 = Robot.leftSpeedOut/5;
- TIM1->CCR2 = 0;
- TIM1->CCR3 = Robot.rightSpeedOut;
- TIM1->CCR4 = 0;
- }
- if(DirControl==0xbd) //向后向右
- {
- TIM1->CCR1 = 0;
- TIM1->CCR2 = Robot.leftSpeedOut;
- TIM1->CCR3 = 0;
- TIM1->CCR4 = Robot.rightSpeedOut/5;
- }
- if(DirControl==0xbb) //向后向左
- {
- TIM1->CCR1 = 0;
- TIM1->CCR2 = Robot.leftSpeedOut/5;
- TIM1->CCR3 = 0;
- TIM1->CCR4 = Robot.rightSpeedOut;
- }
复制代码 控制中,向前、向后、原地向左、原地向右都是正常的,当CCR的值一样时都没问题
问题就来了,差速的时候,也就是向前向左、右,后退向左、右,此时就一个电机转,也就是只有一路PWM波输出,但是CCR的值都写进去了,一个5500,一个1100时,只有5500的那个电机转了,1100的无输出;
然后将代码移植到STM32F103RCT6中,全部控制指令都正常;但是在STM32F407IGT6骄阳开发板中,就出现以上问题。
|
|