#include "pi_controller.h"

float pi_control(PI_ctrl_t *pi, float error){
	float output = 0.0f;
	/* 积分抗饱和处理 */
	if (pi->output > pi->max_output) {
		if (error < 0) {
			pi->i_errors += error;
		}
	}else if (pi->output < pi->min_output) {
		if (error > 0) {
			pi->i_errors += error;
		}
	}else {
		pi->i_errors += error;
	}
	output = pi->output = pi->Kp_gain * error + pi->Ki_gain * pi->i_errors;
	if (output > pi->max_output) {
		output = pi->max_output;
	}else if (output < pi->min_output) {
		output = pi->min_output;
	}
	return output;
}

void pi_clear(PI_ctrl_t *pi) {
	pi->i_errors = 0.0f;
	pi->output = 0.0f;
}
