|
150黑币
定时器2初始化,做编码器读速度用
- void AX_ENCODER_AB_Init(uint16_t cycle)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_ICInitTypeDef TIM_ICInitStructure;
- //GPIO功能时钟使能
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
- GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);
- //配置IO口为复用功能-定时器通道
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //复用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度100MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //TIM时钟使能
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- //Timer configuration in Encoder mode
- TIM_DeInit(TIM2);
- TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
- TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
- TIM_TimeBaseStructure.TIM_Period = cycle;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
- TIM_ICStructInit(&TIM_ICInitStructure);
- TIM_ICInitStructure.TIM_ICFilter = 0;
- TIM_ICInit(TIM2, &TIM_ICInitStructure);
- //Reset counter
- TIM2->CNT = 0;
- TIM_Cmd(TIM2, ENABLE);
- }
- /**
- * @简 述 编码器AB获取计数器数值
- * @参 数 无
- * @返回值 计数器当前值
- */
- uint16_t AX_ENCODER_AB_GetCounter(void)
- {
- int16_t temp;
- temp=TIM_GetCounter(TIM2);
- printf("CNT的值为%d\r\n",temp);
- <font color="#00ffff" style="background-color: red;"> return (TIM_GetCounter(TIM2)); </font>
- }
- /**
- * @简 述 编码器AB设置计数器数值
- * @参 数 count 计数器数值
- * @返回值 无
- */
- void AX_ENCODER_AB_SetCounter(uint16_t count)
- {
- TIM2->CNT = count;
- }
复制代码
PID读取部分,使用了操作系统的高优先级任务
- void PID_task(void*pvParameters)
- {
- printf("进行PID计算\r\n");
- while(1)
- {
- printf("开始进行PID计算\r\n");
- //计算编码器变化值,即获取小车实际速度
- ax_encoder_delta[0] = <font color="#00ffff">(<span style="background-color: red;">AX_ENCODER_AB_GetCounter()</span>-</font>ENCODER_MID_VALUE);
- ax_encoder_delta[1] = (AX_ENCODER_CD_GetCounter()-ENCODER_MID_VALUE);
- ax_encoder_delta[2] = (AX_ENCODER_EF_GetCounter()-ENCODER_MID_VALUE);
- ax_encoder_delta[3] = -(AX_ENCODER_GH_GetCounter()-ENCODER_MID_VALUE);
- //设置编码器初始中间值
- AX_ENCODER_AB_SetCounter(ENCODER_MID_VALUE);
- AX_ENCODER_CD_SetCounter(ENCODER_MID_VALUE);
- AX_ENCODER_EF_SetCounter(ENCODER_MID_VALUE);
- AX_ENCODER_GH_SetCounter(ENCODER_MID_VALUE);
- //运动解析,获取目标转速
- printf("A电机转速 %d \r\n",ax_encoder_delta[0]);
- printf("B电机转速 %d \r\n",ax_encoder_delta[1]);
- printf("C电机转速 %d \r\n",ax_encoder_delta[2]);
- printf("D电机转速 %d \r\n",ax_encoder_delta[3]);
- AX_MOVE_Kinematics(ax_vx,ax_vy,ax_vz);
- printf("A目标转速 %d \r\n",ax_encoder_delta_target[0]);
- printf("B目标转速 %d \r\n",ax_encoder_delta_target[1]);
- printf("C目标转速 %d \r\n",ax_encoder_delta_target[2]);
- printf("D目标转速 %d \r\n",ax_encoder_delta_target[3]);
- //PID控制
- ax_motor_pwm[0] = AX_MID_MotorVelocityPidCtl_A(ax_encoder_delta_target[0], ax_encoder_delta[0]);
- ax_motor_pwm[1] = AX_MID_MotorVelocityPidCtl_B(ax_encoder_delta_target[1], ax_encoder_delta[1]);
- ax_motor_pwm[2] = AX_MID_MotorVelocityPidCtl_C(ax_encoder_delta_target[2], ax_encoder_delta[2]);
- ax_motor_pwm[3] = AX_MID_MotorVelocityPidCtl_D(ax_encoder_delta_target[3], ax_encoder_delta[3]);
- printf("A电机输出 %d \r\n",ax_motor_pwm[0]);
- printf("B电机输出 %d \r\n",ax_motor_pwm[1]);
- printf("C电机输出 %d \r\n",ax_motor_pwm[2]);
- printf("D电机输出 %d \r\n",ax_motor_pwm[3]);
- printf("PID计算完毕\r\n");
- AX_MOTOR_A_SetSpeed(ax_motor_pwm[0]);
- AX_MOTOR_B_SetSpeed(ax_motor_pwm[1]);
- AX_MOTOR_C_SetSpeed(ax_motor_pwm[2]);
- AX_MOTOR_D_SetSpeed(ax_motor_pwm[3]);
- vTaskDelay(300);
-
- }
- }
复制代码
A电机得到的速度反馈只能是1,0,-1,然后又去看TIM2,CNT的值,发现是30000,30000是赋的初值。
BC电机被我屏蔽了
通过示波器,观察D,和A,的编码器AB相,转动轮子,波形一致。也换过电机编码器测试,
综上,问题直指TIM2CNT 的读取部分,
附上全部代码,只求解决问题。
|
|