-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
49 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,82 @@ | ||
#include "pid.h" | ||
|
||
#ifdef PID_ERRORS_VECTOR | ||
void pid_init(PidController_t pid_controller, | ||
void pid_init(PidController_t *pid_controller, | ||
float kp, | ||
float ki, | ||
float kd, | ||
float sample_time, | ||
float anti_windUp, | ||
float *prev_errors, | ||
uint8_t n_prev_errors) { | ||
pid_controller.kp = kp; | ||
pid_controller.ki = ki; | ||
pid_controller.kd = kd; | ||
pid_controller.integrator = 0.0; | ||
pid_controller.n_prev_errors = n_prev_errors; | ||
pid_controller.prev_error_index = pid_controller.n_prev_errors - 1; | ||
for (int i = 0; i < pid_controller.n_prev_errors; ++i) { | ||
pid_controller.prev_errors[i] = 0.0; | ||
pid_controller->kp = kp; | ||
pid_controller->ki = ki; | ||
pid_controller->kd = kd; | ||
pid_controller->integrator = 0.0; | ||
pid_controller->n_prev_errors = n_prev_errors; | ||
pid_controller->prev_error_index = pid_controller->n_prev_errors - 1; | ||
for (int i = 0; i < pid_controller->n_prev_errors; ++i) { | ||
pid_controller->prev_errors[i] = 0.0; | ||
} | ||
pid_controller.error = 0.0; | ||
pid_controller.set_point = 0.0; | ||
pid_controller.sample_time = sample_time; | ||
pid_controller.anti_windUp = anti_windUp; | ||
pid_controller->error = 0.0; | ||
pid_controller->set_point = 0.0; | ||
pid_controller->sample_time = sample_time; | ||
pid_controller->anti_windUp = anti_windUp; | ||
} | ||
#else | ||
void pid_init(PidController_t pid_controller, | ||
void pid_init(PidController_t *pid_controller, | ||
float kp, | ||
float ki, | ||
float kd, | ||
float sample_time, | ||
float anti_windUp) { | ||
pid_controller.kp = kp; | ||
pid_controller.ki = ki; | ||
pid_controller.kd = kd; | ||
pid_controller.integrator = 0.0; | ||
pid_controller.prev_error = 0.0; | ||
pid_controller.error = 0.0; | ||
pid_controller.set_point = 0.0; | ||
pid_controller.sample_time = sample_time; | ||
pid_controller.anti_windUp = anti_windUp; | ||
pid_controller->kp = kp; | ||
pid_controller->ki = ki; | ||
pid_controller->kd = kd; | ||
pid_controller->integrator = 0.0; | ||
pid_controller->prev_error = 0.0; | ||
pid_controller->error = 0.0; | ||
pid_controller->set_point = 0.0; | ||
pid_controller->sample_time = sample_time; | ||
pid_controller->anti_windUp = anti_windUp; | ||
} | ||
#endif | ||
|
||
void pid_update(PidController_t pid_controller, float status) { | ||
void pid_update(PidController_t *pid_controller, float status) { | ||
#ifdef PID_ERRORS_VECTOR | ||
pid_controller.prev_error_index = (pid_controller.prev_error_index + 1) % pid_controller.n_prev_errors; | ||
pid_controller.prev_errors[pid_controller.prev_error_index] = pid_controller.error; | ||
pid_controller->prev_error_index = (pid_controller->prev_error_index + 1) % pid_controller->n_prev_errors; | ||
pid_controller->prev_errors[pid_controller->prev_error_index] = pid_controller->error; | ||
#else | ||
pid_controller.prev_error = pid_controller.prev_error; | ||
pid_controller->prev_error = pid_controller->prev_error; | ||
#endif | ||
pid_controller.error = pid_controller.set_point - status; | ||
pid_controller.integrator += pid_controller.error * pid_controller.sample_time; | ||
pid_controller->error = pid_controller->set_point - status; | ||
pid_controller->integrator += pid_controller->error * pid_controller->sample_time; | ||
} | ||
|
||
float pid_compute(PidController_t pid_controller) { | ||
float pid_compute(PidController_t *pid_controller) { | ||
#ifdef PID_ERRORS_VECTOR | ||
uint8_t index = (pid_controller.prev_error_index + 1) % pid_controller.n_prev_errors; | ||
float derivative = (pid_controller.error - pid_controller.prev_errors[index]) / (pid_controller.sample_time * pid_controller.n_prev_errors); | ||
uint8_t index = (pid_controller->prev_error_index + 1) % pid_controller->n_prev_errors; | ||
float derivative = (pid_controller->error - pid_controller->prev_errors[index]) / (pid_controller->sample_time * pid_controller->n_prev_errors); | ||
#else | ||
float derivative = (pid_controller.error - pid_controller.prev_error) / pid_controller.sample_time; | ||
float derivative = (pid_controller->error - pid_controller->prev_error) / pid_controller->sample_time; | ||
#endif | ||
float integral = pid_controller.ki * pid_controller.integrator; | ||
if (integral > pid_controller.anti_windUp) { | ||
integral = pid_controller.anti_windUp; | ||
} else if (integral < -pid_controller.anti_windUp) { | ||
integral = -pid_controller.anti_windUp; | ||
float integral = pid_controller->ki * pid_controller->integrator; | ||
if (integral > pid_controller->anti_windUp) { | ||
integral = pid_controller->anti_windUp; | ||
} else if (integral < -pid_controller->anti_windUp) { | ||
integral = -pid_controller->anti_windUp; | ||
} | ||
float value = pid_controller.kp * pid_controller.error + integral + pid_controller.kd * derivative; | ||
float value = pid_controller->kp * pid_controller->error + integral + pid_controller->kd * derivative; | ||
return value; | ||
} | ||
|
||
void pid_reset(PidController_t pid_controller) { | ||
pid_controller.integrator = 0.0; | ||
void pid_reset(PidController_t *pid_controller) { | ||
pid_controller->integrator = 0.0; | ||
#ifdef PID_ERRORS_VECTOR | ||
for (int i = 0; i < pid_controller.n_prev_errors; ++i) { | ||
pid_controller.prev_errors[i] = 0.0; | ||
for (int i = 0; i < pid_controller->n_prev_errors; ++i) { | ||
pid_controller->prev_errors[i] = 0.0; | ||
} | ||
#else | ||
pid_controller.prev_error = 0.0; | ||
pid_controller->prev_error = 0.0; | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters