Skip to content

Commit

Permalink
Rename PID class (#218)
Browse files Browse the repository at this point in the history
Signed-off-by: Javier Balloffet <[email protected]>
  • Loading branch information
jballoffet authored Feb 14, 2024
1 parent d3b8315 commit efa766b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 26 deletions.
4 changes: 2 additions & 2 deletions andino_firmware/src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ InterruptInArduino App::right_encoder_channel_b_interrupt_in_(Hw::kRightEncoderC
Encoder App::right_encoder_(&right_encoder_channel_a_interrupt_in_,
&right_encoder_channel_b_interrupt_in_);

PID App::left_pid_controller_(Constants::kPidKp, Constants::kPidKd, Constants::kPidKi,
Pid App::left_pid_controller_(Constants::kPidKp, Constants::kPidKd, Constants::kPidKi,
Constants::kPidKo, -Constants::kPwmMax, Constants::kPwmMax);
PID App::right_pid_controller_(Constants::kPidKp, Constants::kPidKd, Constants::kPidKi,
Pid App::right_pid_controller_(Constants::kPidKp, Constants::kPidKd, Constants::kPidKi,
Constants::kPidKo, -Constants::kPwmMax, Constants::kPwmMax);

unsigned long App::last_pid_computation_{0};
Expand Down
4 changes: 2 additions & 2 deletions andino_firmware/src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ class App {
static Encoder right_encoder_;

/// PID controllers (one per wheel).
static PID left_pid_controller_;
static PID right_pid_controller_;
static Pid left_pid_controller_;
static Pid right_pid_controller_;

/// Tracks the last time the PID computation was made.
static unsigned long last_pid_computation_;
Expand Down
14 changes: 7 additions & 7 deletions andino_firmware/src/pid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace andino {
// - http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-derivative-kick/
// - http://brettbeauregard.com/blog/2011/04/improving-the-beginner%E2%80%99s-pid-tuning-changes/

void PID::reset(int encoder_count) {
void Pid::reset(int encoder_count) {
// Since we can assume that the PID is only turned on when going from stop to moving, we can init
// everything on zero.
setpoint_ = 0;
Expand All @@ -82,15 +82,15 @@ void PID::reset(int encoder_count) {
}

/// @brief Enable PID
void PID::enable() { enabled_ = true; }
void Pid::enable() { enabled_ = true; }

/// @brief Is the PID controller enabled?
bool PID::enabled() { return enabled_; }
bool Pid::enabled() { return enabled_; }

/// @brief Disable PID
void PID::disable() { enabled_ = false; }
void Pid::disable() { enabled_ = false; }

void PID::compute(int encoder_count, int& computed_output) {
void Pid::compute(int encoder_count, int& computed_output) {
if (!enabled_) {
// Reset PID once to prevent startup spikes.
if (last_input_ != 0) {
Expand Down Expand Up @@ -123,9 +123,9 @@ void PID::compute(int encoder_count, int& computed_output) {
last_output_ = output;
}

void PID::set_setpoint(int setpoint) { setpoint_ = setpoint; }
void Pid::set_setpoint(int setpoint) { setpoint_ = setpoint; }

void PID::set_tunings(int kp, int kd, int ki, int ko) {
void Pid::set_tunings(int kp, int kd, int ki, int ko) {
kp_ = kp;
kd_ = kd;
ki_ = ki;
Expand Down
11 changes: 3 additions & 8 deletions andino_firmware/src/pid.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
namespace andino {

/// @brief This class provides a simple PID controller implementation.
class PID {
class Pid {
public:
/// @brief Constructs a new PID object.
///
Expand All @@ -42,13 +42,8 @@ class PID {
/// @param ko Tuning output gain.
/// @param output_min Output minimum limit.
/// @param output_max Output maximum limit.
PID(int kp, int kd, int ki, int ko, int output_min, int output_max)
: kp_(kp),
kd_(kd),
ki_(ki),
ko_(ko),
output_min_(output_min),
output_max_(output_max) {}
Pid(int kp, int kd, int ki, int ko, int output_min, int output_max)
: kp_(kp), kd_(kd), ki_(ki), ko_(ko), output_min_(output_min), output_max_(output_max) {}

/// @brief Resets the PID controller.
///
Expand Down
14 changes: 7 additions & 7 deletions andino_firmware/test/desktop/test_pid/pid_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace test {
namespace {

TEST(PidTest, Initialize) {
andino::PID pid_controller(1, 0, 0, 1, -100, 100);
andino::Pid pid_controller(1, 0, 0, 1, -100, 100);
int output = 0;

// Controller is disabled by default, so output variable should remain unchanged.
Expand All @@ -45,7 +45,7 @@ TEST(PidTest, Initialize) {
}

TEST(PidTest, ComputeOutputProportionalGain) {
andino::PID pid_controller(3, 0, 0, 1, -100, 100);
andino::Pid pid_controller(3, 0, 0, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();
Expand All @@ -58,7 +58,7 @@ TEST(PidTest, ComputeOutputProportionalGain) {
}

TEST(PidTest, ComputeOutputProportionalAndDerivativeGain) {
andino::PID pid_controller(3, 2, 0, 1, -100, 100);
andino::Pid pid_controller(3, 2, 0, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();
Expand All @@ -71,7 +71,7 @@ TEST(PidTest, ComputeOutputProportionalAndDerivativeGain) {
}

TEST(PidTest, ComputeOutputProportionalAndIntegralGain) {
andino::PID pid_controller(3, 0, 1, 1, -100, 100);
andino::Pid pid_controller(3, 0, 1, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();
Expand All @@ -84,7 +84,7 @@ TEST(PidTest, ComputeOutputProportionalAndIntegralGain) {
}

TEST(PidTest, ComputeOutputProportionalDerivativeAndIntegralGain) {
andino::PID pid_controller(3, 2, 1, 1, -100, 100);
andino::Pid pid_controller(3, 2, 1, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();
Expand All @@ -97,7 +97,7 @@ TEST(PidTest, ComputeOutputProportionalDerivativeAndIntegralGain) {
}

TEST(PidTest, Reset) {
andino::PID pid_controller(3, 0, 0, 1, -100, 100);
andino::Pid pid_controller(3, 0, 0, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();
Expand All @@ -113,7 +113,7 @@ TEST(PidTest, Reset) {
}

TEST(PidTest, SetTunings) {
andino::PID pid_controller(3, 0, 0, 1, -100, 100);
andino::Pid pid_controller(3, 0, 0, 1, -100, 100);
int output = 0;
pid_controller.set_setpoint(15);
pid_controller.enable();
Expand Down

0 comments on commit efa766b

Please sign in to comment.