cancel
Showing results for 
Search instead for 
Did you mean: 

PID controller

Hajer_GAMMOUDI
Associate II

Hello ST Community,

I am generating a PWM signal with sample frequency of 10KHz on STM32F4 nucleo board with a clock frequency of 82 MHz.

At the same time, I am programming a PI controller; to make sure that my PI regulator works also with 10KHz frequency; I chose to work with FreeRTOS and create threads to each task on my code.
2 threads to read the ADC values and 1 thread to count error and generate the duty cycle of my PWM signal.

Please help me to sit priorities to these tasks to make sure that my controller still working with 10KHz and to choose a dt to calculate the integral value.

Here is my code:

void StartADC1(void *argument)
{
/* USER CODE BEGIN 5 */
/* Infinite loop */
for(;;)
{
HAL_ADC_Start(&hadc3);
HAL_ADC_PollForConversion(&hadc3, HAL_MAX_DELAY);
Iref=3.3*HAL_ADC_GetValue(&hadc3)/4095;
osDelay(1);
}
//osThreadTerminate(NULL);
/* USER CODE END 5 */
}

void StartPWM(void *argument)
{
/* USER CODE BEGIN StartPWM */
/* Infinite loop */
for(;;)
{
err=Iref-Ich;
Ki=K*R/L;
integral += Ki *dt* (err + err_prev);
err_prev = err;
commande = err * K + integral;
alpha_out_new = commande/Vdc;
TIM8->CCR2=alpha_out_new;
//delayMicroseconds(100);
osDelay(1);
}
//osThreadTerminate(NULL);
/* USER CODE END StartPWM */
}

 

0 REPLIES 0