Jump to content

    
jenya7

PID регулятор.

Recommended Posts

1 hour ago, Alex-lab said:

Вы определите, что по вашему значит отбросить. Для меня это заменить нулем. Это сделать нельзя.

вот код ребят от ST. сам ПИД.

//This is called from TIM3 update interrupt. defined in input.c
void updatePid()
{
    uint32_t abs_position_error;
    int32_t position_delta;
    int32_t position_delta_delta;
    static uint8_t prevdir;
    
    //getEncoderCount(); this is dangerous to call from interrupt
    if (!motor_running)
    {
	pid_integrated_error=0;
	return;
    }

    position_error = encoder_count - pid_requested_position;

    position_delta= pid_requested_position - pid_last_requested_position;
	position_delta_delta = position_delta - pid_last_requested_position_delta;

	pid_last_requested_position = pid_requested_position;
	pid_last_requested_position_delta = position_delta;

	if(abs(position_error)> max_error)
			max_error = abs(position_error);

	abs_position_error = abs(position_error);

	if (abs_position_error > s.max_error)
	{
	      pwm_motorStop();
	      ERROR_LED_ON;
	      return;

	}
//P
	int32_t output = position_error * s.pid_Kp;

//I
	pid_integrated_error += position_error * s.pid_Ki;
	if (pid_integrated_error > 400000)
		pid_integrated_error = 400000;
	if (pid_integrated_error < -400000)
		pid_integrated_error = -400000;

	output += pid_integrated_error;
//D
	output += (position_error - pid_prev_position_error) * s.pid_Kd;
	pid_prev_position_error = position_error;


//FF1
	output += position_delta * s.pid_FF1;
//FF2
	output += position_delta_delta * s.pid_FF2;

	output /= 100; //provide larger dynamic range for pid. (without this, having pid_Ki = 1 was enough for oscillation.

    //limit output power
	if (output > MAX_DUTY)
		output = MAX_DUTY;
	if (output < -MAX_DUTY)
		output = -MAX_DUTY;
	if(output>0)
	{
		dir=0;

	}
	else
	{
		dir=1;

	}

	duty = abs(output);
}

таймер TIM3 настроен на вызов ПИД.

void TIM3_IRQHandler(void) {
  if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  {
	if(s.inputMethod==inputMethod_stepDir)
	{
	  updatePid();
	  TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
	}
	else
	{
		TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
		updateCtr++;
		if(updateCtr>50)
		{
			 //pwm_motorStop();
			 //ERROR_LED_ON;

		}

	}

  }

не вижу где dt вступает в игру.

Share this post


Link to post
Share on other sites
1 minute ago, jenya7 said:

не вижу где dt вступает в игру.

Там где идет инициализация таймера. Задается значение регистра CCR или ARR.

Share this post


Link to post
Share on other sites
1 hour ago, Alex-lab said:

Там где идет инициализация таймера. Задается значение регистра CCR или ARR.

void initPid()
{

	pid_requested_position=encoder_count;
	pid_integrated_error = 0;
	pid_prev_position_error =0;

	//TIM3 used for pid loop timing.
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_TimeBaseStructure.TIM_Prescaler = 72; //1MHz counter
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_Period = 250; //4kHz update interval
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
	TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
	TIM_Cmd(TIM3, ENABLE);

	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x02;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);

}

как? в выходной PWM добавляется dt?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.