本帖最后由 pmw_56 于 2018-4-14 16:47 编辑
以下是马达的PID控制函数, 误差怎么和比例相除,还能放大误差?有知道的朋友回答下
// Proportional term
Control_i = Controller_output_G + (Error / PID_PROPORTIONAL);
// PID parameters
#define PID_PROPORTIONAL (5)
#define PID_INTEGRAL (50)
#define PID_DIFFERENTIAL (50)
void PID_MOTOR_Control_Motor(void)
{
int Error;
int Control_i;
// Get the current speed value (0-255)
Speed_measured_G = PID_MOTOR_Read_Current_Speed();
// Get the desired speed value (0-255)
Speed_required_G =
PID_MOTOR_Get_Required_Speed();
if (++Ticks == 100)
{
Speed_required_G = 200;
}
// Difference between required and actual speed (0-255)
Error = Speed_required_G - Speed_measured_G;
// Proportional term
Control_i = Controller_output_G + (Error / PID_PROPORTIONAL);
// Integral term [SET TO 0 IF NOT REQUIRED]
if (PID_INTEGRAL)
{
Sum_G += Error;
Control_i += (Sum_G / (1 + PID_INTEGRAL));
}
// Differential term [SET TO 0 IF NOT REQUIRED]
if (PID_DIFFERENTIAL)
{
Control_i += (Error - Old_error_G) / (1 + PID_DIFFERENTIAL);
// Store error value
Old_error_G = Error;
}
// Adjust to 8-bit range
if (Control_i > 255)
{
Control_i = 255;
Sum_G -= Error; // Windup protection
}
if (Control_i < 0)
{
Control_i = 0;
Sum_G -= Error; // Windup protection
}
// Convert to required 8-bit format
Controller_output_G = (tByte) Control_i;
// Update the PWM setting
PID_MOTOR_Set_New_PWM_Output(Controller_output_G);
// Update display
PID_MOTOR_data_G[4] = CHAR_MAP_G[Speed_measured_G / 100];
PID_MOTOR_data_G[5] = CHAR_MAP_G[(Speed_measured_G % 100) / 10];
PID_MOTOR_data_G[6] = CHAR_MAP_G[Speed_measured_G % 10];
PID_MOTOR_data_G[12] = CHAR_MAP_G[Speed_required_G / 100];
PID_MOTOR_data_G[13] = CHAR_MAP_G[(Speed_required_G % 100) / 10];
PID_MOTOR_data_G[14] = CHAR_MAP_G[Speed_required_G % 10];
PID_MOTOR_data_G[20] = CHAR_MAP_G[Controller_output_G / 100];
PID_MOTOR_data_G[21] = CHAR_MAP_G[(Controller_output_G % 100) / 10];
PID_MOTOR_data_G[22] = CHAR_MAP_G[Controller_output_G % 10];
PC_LINK_O_Write_String_To_Buffer(PID_MOTOR_data_G);
} |