Certainly! Here's the code you've provided, with the Chinese comments translated into English:
c Copy code
/**
****************************(C) COPYRIGHT 2019 DJI****************************
* @file pid.c/h
* @brief PID implementation functions, including initialization and PID calculation functions.
* @note
* @history
* Version Date Author Modification
* V1.0.0 Dec-26-2018 RM 1. Completed
*
@verbatim
==============================================================================
==============================================================================
@endverbatim
****************************(C) COPYRIGHT 2019 DJI****************************
*/
#include "pid.h"
#include "main.h"
#define LimitMax(input, max) \
{ \
if (input > max) \
{ \
input = max; \
} \
else if (input < -max) \
{ \
input = -max; \
} \
}
/**
* @brief PID structure data initialization
* @param[out] pid: Pointer to the PID structure data
* @param[in] mode: PID_POSITION for normal PID, PID_DELTA for delta PID
* @param[in] PID: Array of PID parameters: PID[0] = Kp, PID[1] = Ki, PID[2] = Kd
* @param[in] max_out: Maximum output of the PID controller
* @param[in] max_iout: Maximum integral output
* @retval None
*/
void PID_init(pid_type_def *pid, uint8_t mode, const fp32 PID[3], fp32 max_out, fp32 max_iout)
{
if (pid == NULL || PID == NULL)
{
return;
}
pid->mode = mode;
pid->Kp = PID[0];
pid->Ki = PID[1];
pid->Kd = PID[2];
pid->max_out = max_out;
pid->max_iout = max_iout;
pid->Dbuf[0] = pid->Dbuf[1] = pid->Dbuf[2] = 0.0f;
pid->error[0] = pid->error[1] = pid->error[2] = pid->Pout = pid->Iout = pid->Dout = pid->out = 0.0f;
}
void PID_init_oldmethod(pid_type_def *pid, uint8_t mode, const fp32 PID[3], fp32 max_out, fp32 max_iout, fp32 max_dout, fp32 dT)
{
if (pid == NULL || PID == NULL)
{
return;
}
pid->mode = PID_POSITION;
pid->Kp = PID[0];
pid->Ki = PID[1];
pid->Kd = PID[2];
pid->max_out = max_out;
pid->max_iout = max_iout;
pid->max_dout = max_dout;
pid->dT = dT;
pid->Dbuf[0] = pid->Dbuf[1] = pid->Dbuf[2] = 0.0f;
pid->error[0] = pid->error[1] = pid->error[2] = pid->Pout = pid->Iout = pid->Dout = pid->out = 0.0f;
}
/**
* @brief PID calculation
* @param[out] pid: Pointer to the PID structure data
* @param[in] ref: Feedback data
* @param[in] set: Set point
* @retval PID output
*/
fp32 PID_calc(pid_type_def *pid, fp32 ref, fp32 set)
{
if (pid == NULL)
{
return 0.0f;
}
pid->error[2] = pid->error[1];
pid->error[1] = pid->error[0];
pid->set = set;
pid->fdb = ref;
pid->error[0] = set - ref;
if (pid->mode == PID_POSITION)
{
pid->Pout = pid->Kp * pid->error[0];
pid->Iout += pid->Ki * pid->error[0];
pid->Dbuf[2] = pid->Dbuf[1];
pid->Dbuf[1] = pid->Dbuf[0];
pid->Dbuf[0] = (pid->error[0] - pid->error[1]);
pid->Dout = pid->Kd * pid->Dbuf[0];
LimitMax(pid->Iout, pid->max_iout);
pid->out = pid->Pout + pid->Iout + pid->Dout;
LimitMax(pid->out, pid->max_out);
}
else if (pid->mode == PID_DELTA)
{
pid->Pout = pid->Kp * (pid->error[0] - pid->error[1]);
pid->Iout = pid->Ki * pid->error[0];
pid->Dbuf[2] = pid->Dbuf[1];
pid->Dbuf[1] = pid->Dbuf[0];
pid->Dbuf[0] = (pid->error[0] - 2.0f * pid->error[1] + pid->error[2]);
pid->Dout = pid->Kd * pid->Dbuf[0];
pid->out += pid->Pout + pid->Iout + pid->Dout;
LimitMax(pid->out, pid->max_out);
}
return pid->out;
}
fp32 PID_calc_oldmethod(pid_type_def *pid, fp32 input, fp32 target) {
if (pid == NULL)
{
return 0.0f;
}
pid->error[2] = pid->error[1];
pid->error[1] = pid->error[0];
pid->set = target;
pid->fdb = input;
pid->error[0] = target - input;
pid->Pout = pid->Kp * pid->error[0];
pid->Iout += pid->Ki * pid->error[0] * pid->dT;
pid->Dbuf[2] = pid->Dbuf[1];
pid->Dbuf[1] = pid->Dbuf[0];
pid->Dbuf[0] = (pid->error[0] - pid->error[1]);
pid->Dout = pid->Kd * pid->Dbuf[0] / pid->dT;
LimitMax(pid->Iout, pid->max_iout);
if (((pid->error[1] > 0) && (pid->error[0] < 0)) || ((pid->error[1] < 0) && (pid->error[0] > 0)))
{
LimitMax(pid->Iout, pid->max_iout / 1000);
}
LimitMax(pid->Dout, pid->max_dout);
pid->out = pid->Pout + pid->Iout + pid->Dout;
LimitMax(pid->out, pid->max_out);
return pid->out;
}
/**
* @brief Clear PID output
* @param[out] pid: Pointer to the PID structure data
* @retval None
*/
void PID_clear(pid_type_def *pid)
{
if (pid == NULL)
{
return;
}
pid->error[0] = pid->error[1] = pid->error[2] = 0.0f;
pid->Dbuf[0] = pid->Dbuf[1] = pid->Dbuf[2] = 0.0f;
pid->out = pid->Pout = pid->Iout = pid->Dout = 0.0f;
pid->fdb = pid->set = 0.0f;
}
Explanation:
The code you've provided is an implementation of a PID (Proportional-Integral-Derivative) controller in C language. The PID controller is a control loop feedback mechanism widely used in industrial control systems.
Here’s a breakdown of the key components and functions:
Macros:
LimitMax
c Copy code
#define LimitMax(input, max) \
{ \
if (input > max) \
{ \
input = max; \
} \
else if (input < -max) \
{ \
input = -max; \
} \
}
This macro limits the value of input to be within the range [-max, max]. It's used to prevent integral windup and limit the maximum output.
Functions:
PID_init
c Copy code
void PID_init(pid_type_def *pid, uint8_t mode, const fp32 PID[3], fp32 max_out, fp32 max_iout)
Initializes the PID controller with the provided parameters.
pid: Pointer to the PID structure.
mode: PID control mode (PID_POSITION or PID_DELTA).
PID: Array containing the PID constants: Kp, Ki, Kd.
max_out: Maximum output value.
max_iout: Maximum integral term output.
PID_init_oldmethod
c Copy code
void PID_init_oldmethod(pid_type_def *pid, uint8_t mode, const fp32 PID[3], fp32 max_out, fp32 max_iout, fp32 max_dout, fp32 dT)
An alternative initialization function, possibly from an older version, including additional parameters:
max_dout: Maximum derivative term output.
dT: Time interval between updates.
PID_calc
c Copy code
fp32 PID_calc(pid_type_def *pid, fp32 ref, fp32 set)
Calculates the PID output.
ref: Feedback value (e.g., the current system output).
set: Set point or desired value.
Supports two modes:
Position Mode (PID_POSITION)
Calculates the output based on the current error.
Delta Mode (PID_DELTA)
Calculates the change in output based on the change in error.
The output is the sum of the proportional (Pout), integral (Iout), and derivative (Dout) terms.
PID_calc_oldmethod
Structures:
Key Concepts in the Code:
Usage Tips:
Final Notes:
The code is designed for use in embedded systems, possibly for controlling motors or actuators in robotics applications.
The presence of #include "main.h" suggests it's part of a larger project where main.h includes necessary definitions and configurations.
Make sure to define the pid_type_def structure and any necessary typedefs like fp32 (likely a float) in your project for this code to compile.
If you have any questions about specific parts of the code or need further assistance with PID controllers, feel free to ask!