Skip to content

Commit

Permalink
fix: function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
conquat committed Jun 12, 2024
1 parent 87c0672 commit 826ffe8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 50 deletions.
88 changes: 44 additions & 44 deletions pid/pid.c
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
}
11 changes: 5 additions & 6 deletions pid/pid.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#ifndef _PID_H_
#define _PID_H_

#include <stdbool.h>
#include <stdint.h>

// #define PID_ERRORS_VECTOR
Expand All @@ -25,7 +24,7 @@ typedef struct pidController_t{
} PidController_t;

#ifdef PID_ERRORS_VECTOR
void pid_init(PidController_t pid_controller,
void pid_init(PidController_t *pid_controller,
float kp,
float ki,
float kd,
Expand All @@ -34,16 +33,16 @@ void pid_init(PidController_t pid_controller,
float *prev_errors,
uint8_t n_prev_errors);
#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);
#endif

void pid_update(PidController_t pid_controller, float status);
float pid_compute(PidController_t pid_controller);
void pid_reset(PidController_t pid_controller);
void pid_update(PidController_t *pid_controller, float status);
float pid_compute(PidController_t *pid_controller);
void pid_reset(PidController_t *pid_controller);

#endif

0 comments on commit 826ffe8

Please sign in to comment.