1. 什么场合会用到结构体数组
举个例子,我控制13个电机,电机参数申明一个结构体。正常结构体类型实例化要实例化13个结构体变量。因为一个结构体变量只能存储一组信息,再挨个赋值很麻烦。由此引出了结构体数组。
2. 结构体数组申明方法
2.1 先定义结构体类型,后定义结构体数组
struct UploadFormat
{
uint8 head[2];
uint16 eco2;
uint8 checksum;
};
struct UploadFormat line[23];
2.2 在定义结构体类型的同时定义结构体数组
struct UploadFormat
{
uint8 head[2];
uint16 eco2;
uint8 checksum;
}line[23];
2.3 直接定义结构体数组
struct
{
uint8 head[2];
uint16 eco2;
uint8 checksum;
}line[23];
3. 传统的实现方法
typedef struct{
__IO uint16_t SetTemp; //设定目标 Desired Value
__IO uint16_t SumError; //误差累计
__IO uint16_t Proportion; //比例常数 Proportional Const
__IO uint16_t Integral; //积分常数 Integral Const
__IO uint16_t Derivative; //微分常数 Derivative Const
__IO float Proportion_Current; //比例常数 Proportional Const
__IO float Integral_Current; //积分常数 Integral Const
__IO float Derivative_Current; //微分常数 Derivative Const
__IO float LastError; //Error[-1]
__IO float PrevError; //Error[-2]
}PID_TypeDef;
3.1 对结构体进行初始化:
PID_TypeDef PidChannel_0 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative = 80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_1 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_2 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_3 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_4 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_5 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_6 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_7 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_8 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_9 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_10 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_11 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_12 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_13 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_14 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current =50,
.Integral_Current = 0,
.Derivative_Current = 1
};
PID_TypeDef PidChannel_15 = {
.SetTemp = 50, //设定目标 Desired Value
.LastError = 0, //Error[-1]
.PrevError = 0, //Error[-2]
.Proportion = 80, //比例常数 Proportional Const
.Integral = 0, //积分常数 Integral Const
.Derivative =80, //微分常数 Derivative Const
.Proportion_Current = 50,
.Integral_Current = 0,
.Derivative_Current = 1
};
3.2 对结构体进行引用
建立一个函数,该函数传入数据的形参就是结构体类型参数。
int32_t PIDCalc_IRQHandler(PID_TypeDef *PidChannel,VirPwmTypeDef *VirPwmDef,float CurrentTemp)
{
float iError,iIncpid;
if(VirPwmDef->Status == 1)
{
iError = PidChannel->SetTemp - CurrentTemp;
}
return(iIncpid);
}
VirPwmChannel_0.DutyCycle = PIDCalc_IRQHandler(&PidChannel_0,&VirPwmChannel_0,*(adctmparray+12));
4. 结构体数组实现方法
利用结构体数组初始化我可以这样写。这样类型申明和初始化的工作就做完了。
typedef struct PID_TypeDef{
__IO uint16_t SetTemp; //设定目标 Desired Value
__IO uint16_t SumError; //误差累计
__IO uint16_t Proportion; //比例常数 Proportional Const
__IO uint16_t Integral; //积分常数 Integral Const
__IO uint16_t Derivative; //微分常数 Derivative Const
__IO float Proportion_Current; //比例常数 Proportional Const
__IO float Integral_Current; //积分常数 Integral Const
__IO float Derivative_Current; //微分常数 Derivative Const
__IO float LastError; //Error[-1]
__IO float PrevError; //Error[-2]
};
struct PID_TypeDef pid_type[13] = { {50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
{50,0,0,80,0,80,50,0,1},
};
结构体数组引用。
int32_t PIDCalc_IRQHandler(PID_TypeDef *PidChannel,VirPwmTypeDef *VirPwmDef,float CurrentTemp)
{
float iError,iIncpid;
if(VirPwmDef->Status == 1)
{
iError = PidChannel->SetTemp - CurrentTemp;
}
return(iIncpid);
}
VirPwmChannel_0.DutyCycle = PIDCalc_IRQHandler(&pid_type[1],&VirPwmChannel[1],*(adctmparray+12));