else if(I1out_Value4[Iout_channel-1]>0.001;I1out_Value3[Iout_channel-1]<=0.1;)
时间: 2024-04-07 19:29:37 浏览: 18
这段代码中有两个错误:
1. 在if语句的条件表达式中,应该使用逻辑运算符而不是分号,应该是`else if(I1out_Value4[Iout_channel-1]>0.001 && I1out_Value3[Iout_channel-1]<=0.1)`
2. 在if语句中使用了两个分号,应该只有一个分号,应该是`else if(I1out_Value4[Iout_channel-1]>0.001 && I1out_Value3[Iout_channel-1]<=0.1)`
相关问题
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; }如何在主函数中调用以上代码
如果你想在主函数中调用以上代码,需要先定义一个 pid_type_def 结构体类型的变量,然后将其作为参数传递给 PID_init 函数。例如:
```c
#include <stdio.h>
typedef struct {
uint8_t mode;
fp32 Kp, Ki, Kd;
fp32 max_out, max_iout;
fp32 Dbuf[3], error[3];
fp32 Pout, Iout, Dout, out;
} pid_type_def;
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;
}
int main() {
pid_type_def pid;
fp32 PID[3] = {1.0f, 0.5f, 0.2f};
fp32 max_out = 100.0f, max_iout = 50.0f;
uint8_t mode = 1;
PID_init(&pid, mode, PID, max_out, max_iout);
printf("PID initialized:\nKp = %f\nKi = %f\nKd = %f\nmax_out = %f\nmax_iout = %f\n", pid.Kp, pid.Ki, pid.Kd, pid.max_out, pid.max_iout);
return 0;
}
```
在上面的代码中,我们首先定义了一个 pid_type_def 结构体类型的变量 pid,并声明了一些参数。在 main 函数中,我们通过调用 PID_init 函数来初始化 pid 变量,最后输出 pid 的一些属性值。需要注意的是,我们需要使用取地址符 & 来传递 pid 变量的地址给 PID_init 函数,以便函数能够修改 pid 变量的值。
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 { pid->Pout = pid->Kp * pid->error[0]; //Kp*偏差 pid->Iout += pid->Ki * pid->error[0]; //Ki*偏差和 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]; //Kd*(偏差-上偏差) 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 { 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 0; }如何在主函数里面调用以上程序
可以按照以下步骤在主函数中调用以上程序:
1. 定义一个pid_type_def类型的结构体变量pid。
2. 初始化pid结构体变量的各个属性,包括Kp、Ki、Kd、max_iout、max_out、mode等。
3. 在主函数中循环调用PID_calc函数,传入pid结构体变量、参考值ref和设定值set作为参数。
4. 根据需要使用PID计算结果,即pid结构体变量中的out属性。
下面是一个简单的示例代码:
```c
int main(void)
{
pid_type_def pid;
pid.Kp = 1.0f;
pid.Ki = 0.1f;
pid.Kd = 0.01f;
pid.max_iout = 100.0f;
pid.max_out = 1000.0f;
pid.mode = PID_POSITION;
pid.Iout = 0.0f;
pid.Pout = 0.0f;
pid.Dout = 0.0f;
pid.fdb = 0.0f;
pid.set = 0.0f;
pid.error[0] = 0.0f;
pid.error[1] = 0.0f;
pid.error[2] = 0.0f;
pid.Dbuf[0] = 0.0f;
pid.Dbuf[1] = 0.0f;
pid.Dbuf[2] = 0.0f;
fp32 ref = 10.0f;
fp32 set = 20.0f;
while (1)
{
PID_calc(&pid, ref, set);
fp32 output = pid.out;
// 根据需要使用PID计算结果
// ...
}
return 0;
}
```
相关推荐
![PDF](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)