fix: replace memcpy in PIDInit with explicit field assignment
The old approach used memcpy to copy PID_Init_Config_s into PIDInstance, relying on identical struct layout at the beginning. This is fragile and confusing. Now assigns each field explicitly.
This commit is contained in:
@@ -127,13 +127,20 @@ static void f_PID_ErrorHandle(PIDInstance *pid)
|
|||||||
*/
|
*/
|
||||||
void PIDInit(PIDInstance *pid, PID_Init_Config_s *config)
|
void PIDInit(PIDInstance *pid, PID_Init_Config_s *config)
|
||||||
{
|
{
|
||||||
// config的数据和pid的部分数据是连续且相同的的,所以可以直接用memcpy
|
|
||||||
// @todo: 不建议这样做,可扩展性差,不知道的开发者可能会误以为pid和config是同一个结构体
|
|
||||||
// 后续修改为逐个赋值
|
|
||||||
memset(pid, 0, sizeof(PIDInstance));
|
memset(pid, 0, sizeof(PIDInstance));
|
||||||
// utilize the quality of struct that its memeory is continuous
|
|
||||||
memcpy(pid, config, sizeof(PID_Init_Config_s));
|
pid->Kp = config->Kp;
|
||||||
// set rest of memory to 0
|
pid->Ki = config->Ki;
|
||||||
|
pid->Kd = config->Kd;
|
||||||
|
pid->MaxOut = config->MaxOut;
|
||||||
|
pid->DeadBand = config->DeadBand;
|
||||||
|
pid->Improve = config->Improve;
|
||||||
|
pid->IntegralLimit = config->IntegralLimit;
|
||||||
|
pid->CoefA = config->CoefA;
|
||||||
|
pid->CoefB = config->CoefB;
|
||||||
|
pid->Output_LPF_RC = config->Output_LPF_RC;
|
||||||
|
pid->Derivative_LPF_RC = config->Derivative_LPF_RC;
|
||||||
|
|
||||||
DWT_GetDeltaT(&pid->DWT_CNT);
|
DWT_GetDeltaT(&pid->DWT_CNT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user