Commit b897dc99 authored by Oleg Nikulin's avatar Oleg Nikulin

Переделан pid

parent 70afe8dc
......@@ -156,11 +156,14 @@ uint32_t last_beep_time = 0;
uint32_t last_poll_time = 0;
uint32_t lastRpmCheck = 0;
float p[3] = {};
float i[3] = {};
float d[3] = {};
//float p[3] = {};
//float i[3] = {};
//float d[3] = {};
float k_p = 40;
float errorIntegral[3] = {};
float prevError[3] = {};
float k_p = -40;
float k_i = 0.005;
float k_d = 0;
......@@ -325,19 +328,25 @@ void rpm_control() {
for (int group = 0; group < 3; group++) {//управление скоростью 3-х групп вентиляторов
if (controls[group].copy_from < 0 || controls[group].copy_from > 2) {
uint8_t duty_cycle = 0;
float pwmDutyCycle = ON_DUTY_CYCLE;
if (pc_respond){
d[group] = (controls[group].temperature - controls[group].target_temperature - p[group]) / float(millis() - lastRpmCheck);
p[group] = controls[group].temperature - controls[group].target_temperature;
i[group] = i[group] + p[group] * (millis() - lastRpmCheck);
if (fabs(i[group] * k_i) >= i_max) {
i[group] = i_max / k_i;
}
uint32_t deltaTime = millis() - lastRpmCheck;
float error = controls[group].target_temperature - controls[group].temperature;
//чтобы i не накапливалась
if (controls[group].dutyCycle < controls[group].max_duty_cycle && controls[group].dutyCycle < ON_DUTY_CYCLE)
errorIntegral[group] += error * deltaTime;
float errorChangeRate = (error - prevError[group]) / deltaTime;
prevError[group] = error;
float p = k_p * error;
float i = k_i * errorIntegral[group];
float d = k_d * errorChangeRate;
pwmDutyCycle = round(p[group] * k_p + i[group] * k_i + d[group] * k_d);
pwmDutyCycle = p + i + d;
}
set_duty_cycle(controls[group], round(pwmDutyCycle));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment