stm32hal库串级pid代码
时间: 2023-10-09 07:16:00 浏览: 78
串级pid代码,适用于stm32系列
以下是使用STM32 HAL库实现串级PID控制的示例代码。这个例子是使用STM32F4 Discovery板和Keil MDK-ARM开发环境编写的。
首先,需要在代码中包含必要的头文件和定义一些变量和常量:
```c
#include "stm32f4xx_hal.h"
#include "math.h"
#define PWM_FREQ 10000 // PWM频率,单位为Hz
#define PWM_PERIOD ((SystemCoreClock / PWM_FREQ) - 1) // PWM周期
#define KP1 2.0 // 一级PID控制器的比例系数
#define KI1 0.1 // 一级PID控制器的积分系数
#define KD1 0.01 // 一级PID控制器的微分系数
#define KP2 1.0 // 二级PID控制器的比例系数
#define KI2 0.05 // 二级PID控制器的积分系数
#define KD2 0.005 // 二级PID控制器的微分系数
static TIM_HandleTypeDef htim2; // 定义TIM2句柄
static TIM_HandleTypeDef htim3; // 定义TIM3句柄
static uint32_t adc_value; // ADC采样值
static float setpoint = 0.0; // 设定值
static float error1 = 0.0; // 一级PID控制器误差
static float error2 = 0.0; // 二级PID控制器误差
static float last_error1 = 0.0; // 上一次一级PID控制器误差
static float last_error2 = 0.0; // 上一次二级PID控制器误差
static float integral1 = 0.0; // 一级PID控制器积分项
static float integral2 = 0.0; // 二级PID控制器积分项
static float derivative1 = 0.0; // 一级PID控制器微分项
static float derivative2 = 0.0; // 二级PID控制器微分项
static float output1 = 0.0; // 一级PID控制器输出值
static float output2 = 0.0; // 二级PID控制器输出值
static float last_output2 = 0.0; // 上一次二级PID控制器输出值
```
然后,需要初始化TIM2和TIM3定时器,以及ADC采样:
```c
void SystemClock_Config(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
// 初始化TIM2
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = PWM_PERIOD;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_PWM_Init(&htim2);
// 初始化TIM3
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 4095;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim3);
HAL_TIM_Base_Start_IT(&htim3);
// 初始化ADC
ADC_ChannelConfTypeDef sConfig;
ADC_HandleTypeDef hadc1;
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_ADC1_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = ENABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
HAL_ADC_Init(&hadc1);
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = 1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
HAL_ADC_ConfigChannel(&hadc1, &sConfig);
// 启动PWM输出
TIM_OC_InitTypeDef sConfigOC;
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;
HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
while (1)
{
// 读取ADC采样值
HAL_ADC_Start(&hadc1);
if (HAL_ADC_PollForConversion(&hadc1, 1000000) == HAL_OK)
{
adc_value = HAL_ADC_GetValue(&hadc1);
}
HAL_ADC_Stop(&hadc1);
// 一级PID控制器
error1 = setpoint - adc_value;
integral1 += error1;
derivative1 = error1 - last_error1;
output1 = KP1 * error1 + KI1 * integral1 + KD1 * derivative1;
last_error1 = error1;
// 二级PID控制器
error2 = output1 - last_output2;
integral2 += error2;
derivative2 = error2 - last_error2;
output2 = KP2 * error2 + KI2 * integral2 + KD2 * derivative2;
last_error2 = error2;
last_output2 = output2;
// 更新PWM输出
__HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, (uint32_t)(output2 * PWM_PERIOD / 3.3));
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
```
在主循环中,首先读取ADC采样值,然后使用一级PID控制器计算输出值,再使用二级PID控制器对一级PID控制器的输出进行控制,最后更新PWM输出。
需要注意的是,此示例代码中的一级PID控制器和二级PID控制器的参数是根据实际情况进行设置的,需要根据具体应用进行调整。
希望这能帮助你实现串级PID控制。
阅读全文